carvalhoviniciusluiz / cpf-cnpj-validator

Valida e formata strings de CPF ou CNPJ.
MIT License
206 stars 27 forks source link

Unformat function #24

Closed IncognitaDev closed 3 years ago

IncognitaDev commented 3 years ago

I think this module can contain a unformat function to remove dots and others non number characters.

carvalhoviniciusluiz commented 3 years ago

already exists,

import { cpf } from 'cpf-cnpj-validator'; 

const num = cpf.generate();
// #=> 25634428777

cpf.format(num); // <----- here!! 
// #=> 256.344.287-77
IncognitaDev commented 3 years ago

this function aplly format, I sugested a function that removes format

carvalhoviniciusluiz commented 3 years ago

I understand, this is an accessory feature. A very simple regex can be used to test.

const removeMask = (value) => {
  return value && value.replace(/[^\w\s]/gi, '')
}

removeMask('725.428.702-72')
// #=> "72542870272"

removeMask('33.057.042/0001-00')
// #=> "33057042000100"

but in this case we are removing all special characters and it could also be any string.

Should we leave the treatment open for any string or, treat only number of valid documents to have a more specific regex?

carvalhoviniciusluiz commented 3 years ago

I found the idea interesting, I'll check the best way to add it...

I'm ending this problem for now