immutable-cpf
v1.4.1
Published
A tiny library to handle CPF (Cadastro de Pessoa Física), a brazilian identification document, in an immutable flavour.
Downloads
14
Maintainers
Readme
Immutable CPF
A tiny library to handle CPF in an immutable flavour.
The CPF (Cadastro de Pessoas Físicas, [sepeˈɛfi]; portuguese for "Natural Persons Register") is the Brazilian individual taxpayer registry identification. This number is attributed by the Brazilian Federal Revenue to Brazilians and resident aliens who, directly or indirectly, pay taxes in Brazil.
🇧🇷
Installation
Use the npm package manager to install Immutable CPF.
npm i immutable-cpf
Usage
The library provides a the CPF
class to create immutable instances
representing CPF documents. You can create instances with any iterable of digits
and format or validate them. See the example:
import { CPF } from 'immutable-cpf';
const cpf = new CPF([3, 1, 6, 7, 5, 7, 4, 5, 5, 0, 1]);
cpf.equals(cpf); // true
cpf.checkValidity(); // true
cpf.format(); // '316.757.455-01'
You can also create instances from strings using the CPF.from
method.
import { CPF } from 'immutable-cpf';
const cpfA = new CPF([3, 1, 6, 7, 5, 7, 4, 5, 5, 0, 1]);
const cpfB = CPF.from('316.757.455-01');
const cpfC = CPF.from('3 1 6 7 5 7 4 5 5 0 1 ');
cpfA.equals(cpfB); // true
cpfA.equals(cpfC); // true
The
CPF
class implements theEvaluable
interface and it's suitable to be used along ImmutableJS data structures.
The method CPF.prototype.getValidity
returns the validity
state of the instance. If you only want to check if the instance is valid or
not, see the CPF.prototype.checkValidity
method.
import { CPF } from 'immutable-cpf';
const empty = new CPF([]);
empty.checkValidity(); // false, it's empty
const semi = new CPF([3, 1, 6, 7]);
semi.checkValidity(); // false, it's not complete
const invalid = new CPF([3, 1, 6, 7, 5, 7, 4, 5, 5, 1, 2]);
semi.checkValidity(); // false, its check digits fails
const valid = new CPF([3, 1, 6, 7, 5, 7, 4, 5, 5, 0, 1]);
valid.checkValidity(); // true
The library also provides the method CPF.create
to generate
valid instances with pseudo-random numbers.
import { CPF } from 'immutable-cpf';
const cpf = CPF.create();
cpf.checkValidity(); // true
The default JSON serialization a CPF
instance is a string. You can also access
it directly calling the CPF.prototype.toJSON
.
import { CPF } from 'immutable-cpf';
const user = {s
name: 'José Silva',
cpf: new CPF([3, 1, 6, 7, 5, 7, 4, 5, 5, 0, 1]),
};
JSON.stringify(user); // '{"name": "José Silva", "cpf": "31675745501"}'
user.cpf.toJSON(); // '31675745501'
API
See the complete API on the Wiki's page.
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.