Skip to content

Instantly share code, notes, and snippets.

@wilsaantos
Created August 12, 2021 17:06
Show Gist options
  • Save wilsaantos/e5d83c53d015f577ee10beafc9298721 to your computer and use it in GitHub Desktop.
Save wilsaantos/e5d83c53d015f577ee10beafc9298721 to your computer and use it in GitHub Desktop.
Angular Pipe CPF ou CNPJ
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