Skip to content

Instantly share code, notes, and snippets.

@rogeriomq
Created August 10, 2017 23:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rogeriomq/6d4234c450a8655808ee8ea1b3c69e0b to your computer and use it in GitHub Desktop.
Save rogeriomq/6d4234c450a8655808ee8ea1b3c69e0b to your computer and use it in GitHub Desktop.
/**
* By Rogério M. de Queiroz(rogerio.mq@gmail.com)
* https://github.com/rogeriomq
* Funções de filtros diversos.
* Código inspirado nos .js de igorcosta/ng-filters-br:
* https://github.com/igorcosta/ng-filters-br/tree/master/src/brasil/filters
*/
const cpfFormatter = (input) => {
let str = input + ''
str = str.replace(/\D/g, '')
str = str.replace(/(\d{3})(\d)/, '$1.$2')
str = str.replace(/(\d{3})(\d)/, '$1.$2')
str = str.replace(/(\d{3})(\d{1,2})$/, '$1-$2')
return str
}
const cnpjFormatter = (input) => {
let str = input + ''
str = str.replace(/\D/g, '')
str = str.replace(/^(\d{2})(\d)/, '$1.$2')
str = str.replace(/^(\d{2})\.(\d{3})(\d)/, '$1.$2.$3')
str = str.replace(/\.(\d{3})(\d)/, '.$1/$2')
str = str.replace(/(\d{4})(\d)/, '$1-$2')
return str
}
const cepFormatter = (input) => {
let str = input + ''
str = str.replace(/\D/g, '')
str = str.replace(/^(\d{2})(\d{3})(\d)/, '$1.$2-$3')
return str
}
const foneFormatter = (input) => {
let str = input + ''
str = str.replace(/\D/g, '')
if (str.length === 11) str = str.replace(/^(\d{2})(\d{5})(\d{4})/, '($1) $2-$3')
else str = str.replace(/^(\d{2})(\d{4})(\d{4})/, '($1) $2-$3')
return str
}
export {
cpfFormatter,
cnpjFormatter,
cepFormatter,
foneFormatter
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment