diff --git a/package-lock.json b/package-lock.json index 8ff31b4..7659fdf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2656,4 +2656,4 @@ } } } -} +} \ No newline at end of file diff --git a/src/core/masks/brazilian-masks.ts b/src/core/masks/brazilian-masks.ts new file mode 100644 index 0000000..0457b8a --- /dev/null +++ b/src/core/masks/brazilian-masks.ts @@ -0,0 +1,25 @@ +export function numberMask(value: string): string { + return value.replace(/\D/g, ""); +} + +export function cpfTaxIdMask(value: string): string { + return value + .replace(/(\d{3})(\d)/, "$1.$2") + .replace(/(\d{3})(\d)/, "$1.$2") + .replace(/(\d{3})(\d{1,2})$/, "$1-$2"); +} + +export function cnpjTaxIdMask(value: string): string { + return value + .replace(/(\d{2})(\d)/, "$1.$2") + .replace(/(\d{3})(\d)/, "$1.$2") + .replace(/(\d{3})(\d)/, "$1/$2") + .replace(/(\d{4})(\d{1,2})$/, "$1-$2"); +} + +export function taxIdMask(value: string): string { + const valueWithoutSpecialCharacters = numberMask(value); + return valueWithoutSpecialCharacters.length > 11 + ? cnpjTaxIdMask(valueWithoutSpecialCharacters) + : cpfTaxIdMask(valueWithoutSpecialCharacters); +}