@fnando/cpf

中文文档
1.0.2发布于 2020/07/22MITTypeScriptESM + CJS零依赖近 30 天下载 13.5万

Validate, generate and format CPF numbers

原始使用文档
文档目录3 节
  1. Installation
  2. Usage
  3. Strict Validation

CPF

Build Status NPM package version License: MIT Minified size Minified+Gzip size

This package does some CPF magic. It allows you to create, validate and format CPF documents.

HINT: Check out the CNPJ counter part available at https://github.com/fnando/cnpj.

Installation

This lib is available as a NPM package. To install it, use the following command:

npm install @fnando/cpf --save

If you're using Yarn (and you should):

yarn add @fnando/cpf

Usage

// Node.js-specific
const cpf = require("@fnando/cpf/commonjs");

// @import
import * as cpf from "@fnando/cpf"; // import the whole library
import { isValid as isValidCpf } from "@fnando/cpf"; // import just one function

// import via <script>; the lib will available as window.cpf
// <script src="cpf.js"></script>

cpf.isValid("532.820.857-96");
//=> true

cpf.isValid("53282085796");
//=> true

cpf.strip("532.820.857-96");
//=> 53282085796

cpf.format("53282085796");
//=> 532.820.857-96

cpf.generate(true); // generate formatted number
//=> 838.684.734-40

cpf.generate(); // generate unformatted number
//=> 72777632898

On the web, without transformation, just use web/cpf.min.js.

Strict Validation

By default, validations will strip any characters you provide. This means that the following is valid, because only numbers will be considered:

cpf.isValid("101#688!!!!!!542......36");
//=> true

cpf.strip("101#688!!!!!!542......36");
//=> 10168854236

If you want to strict validate strings, use the following signature:

cpf.isValid(number, strict);

The same example would now return false:

cpf.isValid("101#688!!!!!!542......36", true);
//=> false

版本与发布记录

@fnando/cpf 共发布 5 个版本,首次发布于 2018/04/04;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.2latest2020/07/226 年前
1.0.12020/02/206 年前
1.0.02020/02/206 年前
0.1.12018/10/278 年前
0.1.02018/04/048 年前

关于 @fnando/cpf 的常见问题

@fnando/cpf 是什么?

Validate, generate and format CPF numbers

如何安装 @fnando/cpf?

在项目目录运行 npm install @fnando/cpf,或使用 pnpm add / yarn add / bun add。

@fnando/cpf 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fnando/cpf 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@fnando/cpf 可以商用吗?

@fnando/cpf 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fnando/cpf 的下载量有多少?

根据 npm 官方下载统计,@fnando/cpf 在 2026/10/15 – 2026/11/13 这 30 天内被下载 135,241 次,日均约 4,508 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fnando/cpf 还在维护吗?

最新版本 1.0.2 发布于 2020/07/22(6 年前);至今共发布 5 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/04/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。