Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fnando/cpf
🇧🇷 Validate, generate and format CPF numbers
https://github.com/fnando/cpf
cpf javascript npm
Last synced: about 15 hours ago
JSON representation
🇧🇷 Validate, generate and format CPF numbers
- Host: GitHub
- URL: https://github.com/fnando/cpf
- Owner: fnando
- License: mit
- Created: 2018-04-04T13:14:46.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-04-03T01:57:18.000Z (over 2 years ago)
- Last Synced: 2024-12-15T08:05:53.861Z (8 days ago)
- Topics: cpf, javascript, npm
- Language: TypeScript
- Homepage:
- Size: 462 KB
- Stars: 166
- Watchers: 2
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# CPF
[![Build Status](https://travis-ci.org/fnando/cpf.svg?branch=master)](https://travis-ci.org/fnando/cpf)
[![NPM package version](https://img.shields.io/npm/v/@fnando/cpf.svg)](https://www.npmjs.com/package/@fnando/cpf)
![License: MIT](https://img.shields.io/npm/l/@fnando/cpf.svg)
![Minified size](http://img.badgesize.io/fnando/cpf/master/web/cpf.min.js.svg?label=cpf+min+size)
![Minified+Gzip size](http://img.badgesize.io/fnando/cpf/master/web/cpf.min.js.svg?compression=gzip&label=cpf+min%2Bgzip+size)This package does some
[CPF](http://en.wikipedia.org/wiki/Cadastro_de_Pessoas_F%C3%ADsicas) magic. It
allows you to create, validate and format CPF documents.**HINT:** Check out the CNPJ counter part available at
.## 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
```js
// 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 ; the lib will available as window.CPF
// <script src="cpf.js">cpf.isValid("532.820.857-96");
//=> truecpf.isValid("53282085796");
//=> truecpf.strip("532.820.857-96");
//=> 53282085796cpf.format("53282085796");
//=> 532.820.857-96cpf.generate(true); // generate formatted number
//=> 838.684.734-40cpf.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:```js
cpf.isValid("101#688!!!!!!542......36");
//=> truecpf.strip("101#688!!!!!!542......36");
//=> 10168854236
```If you want to strict validate strings, use the following signature:
```js
cpf.isValid(number, strict);
```The same example would now return `false`:
```js
cpf.isValid("101#688!!!!!!542......36", true);
//=> false
```