@azify/cpf-cnpj
v0.1.1
Published
CPF/CNPJ validation, formatting and comparison helpers (including alphanumeric CNPJ).
Keywords
Readme
@azify/cpf-cnpj
Validation, formatting, and comparison of CPF/CNPJ, including support for alphanumeric CNPJ (effective Jul/2026).
Installation
npm install @azify/cpf-cnpjUsage
import { cpf, cnpj, taxpayer } from '@azify/cpf-cnpj'
cpf.isValid('529.982.247-25') // true
cpf.isValid('111.111.111-11') // false (invalid check digit)
cpf.format('52998224725') // '529.982.247-25'
cpf.format('123') // null (doesn't have 11 digits)
cpf.compare(a, b) // true | false | null (null if a or b is invalid)
cpf.clean('529.982.247-25') // '52998224725'
cnpj.isValid('11.444.777/0001-61') // true
cnpj.isValid('AB123CD4501E66') // true (alphanumeric CNPJ)
cnpj.isValid('11.111.111/1111-11') // false (invalid check digit)
cnpj.format('11444777000161') // '11.444.777/0001-61'
cnpj.format('AB123CD4501E66') // 'AB.123.CD4/501E-66'
cnpj.format('123') // null (doesn't have 14 positions)
cnpj.compare(a, b) // true | false | null (null if a or b is invalid)
cnpj.clean('11.444.777/0001-61') // '11444777000161'
taxpayer.type('52998224725') // 'cpf' | 'cnpj' | null (null if it's not a valid CPF or CNPJ)
taxpayer.isValid('invalid') // false
taxpayer.format('invalid') // null
taxpayer.compare(a, b) // true | false | null (null if a or b is invalid)
taxpayer.clean(value) // normalizes CPF or CNPJ (alphanumeric)About null returns
formatreturnsnullwhen the value doesn't have the expected length (CPF: 11 digits; CNPJ: 14 positions). It does not check the verification digit — a well-formed CPF/CNPJ with an invalid checksum is still formatted (e.g.,cpf.format('00000000000')returns'000.000.000-00'). UseisValidwhen you need to guarantee the document is actually valid.comparereturnsnull(notfalse) when either of the two values is invalid — this lets you distinguish "different documents" from "cannot compare".taxpayer.typereturnsnullwhen the value is neither a valid CPF nor a valid CNPJ (it doesn't distinguish "not a CPF" from "not a CNPJ").
