4lessandrodev / type-ddd

This package provide utils files and interfaces to assistant build a complex application with domain driving design.
https://www.npmjs.com/package/@type-ddd/core
MIT License
256 stars 14 forks source link

Implement CPFValueObject as util Value Object #36

Closed 4lessandrodev closed 3 years ago

4lessandrodev commented 3 years ago

CPF stands for Cadastro de Pessoas Físicas (Natural Persons Register). Native Brazilians can request a CPF at any time in their lives - parents will often apply on behalf of their children when they are newborns, as CPFs are vital to living in Brazil and it’s best to obtain one sooner rather than later.

A CPF is 11 digits long, comprised of nine base digits, and two digits at the end that are the result of an arithmetic operation on the first nine numbers, meaning any typing mistakes will lead to an invalid number. Here is an example of a CPF: 231.002.999-00.

4lessandrodev commented 3 years ago

Validate digits

Reference Doc

Possible Province

Example CPF nº 000.000.006-00

0 - Rio Grande do Sul

1 - Distrito Federal, Goiás, Mato Grosso do Sul e Tocantins

2 - Pará, Amazonas, Acre, Amapá, Rondônia e Roraima

3 - Ceará, Maranhão e Piauí

4 - Pernambuco, Rio Grande do Norte, Paraíba e Alagoas

5 - Bahia e Sergipe

6 - Minas Gerais

7 - Rio de Janeiro e Espírito Santo

8 - São Paulo

9 - Paraná e Santa Catarina

4lessandrodev commented 3 years ago

Regex to validate CPF

/^([0-9]{3})[\.]((?!\1)[0-9]{3})[\.]([0-9]{3})[-]([0-9]{2})$|^[0-9]{11}$/
4lessandrodev commented 3 years ago

Remove special chars

cpf.replace(/[\.]|[-]/g,'');