Created
August 12, 2021 17:06
-
-
Save wilsaantos/e5d83c53d015f577ee10beafc9298721 to your computer and use it in GitHub Desktop.
Angular Pipe CPF ou CNPJ
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
import { Pipe, PipeTransform } from '@angular/core'; | |
@Pipe({ name: 'CPFCNPJ' }) | |
export class CPFCNPJPipe implements PipeTransform { | |
transform(value) { | |
if (value.toString().length === 11) { | |
return value.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/g, '\$1.\$2.\$3\-\$4'); | |
} | |
if (value.toString().length === 14) { | |
return value.replace(/(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/g, "\$1.\$2.\$3\/\$4\-\$5"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment