Created
August 10, 2017 23:29
-
-
Save rogeriomq/6d4234c450a8655808ee8ea1b3c69e0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* By Rogério M. de Queiroz([email protected]) | |
* 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