Skip to content

Instantly share code, notes, and snippets.

@Pierre-RA
Last active February 20, 2021 11:13
Show Gist options
  • Save Pierre-RA/04b37b7ae1a839f51b7b97e928c9a942 to your computer and use it in GitHub Desktop.
Save Pierre-RA/04b37b7ae1a839f51b7b97e928c9a942 to your computer and use it in GitHub Desktop.
Angular 2/4 Pretty Phone number pipe
import { Pipe, PipeTransform } from '@angular/core';
import { parsePhoneNumber, CountryCode } from 'libphonenumber-js/min';
@Pipe({
name: 'phone'
})
export class PhonePipe implements PipeTransform {
transform(phoneValue: number | string, country: string): any {
try {
const phoneNumber = parsePhoneNumber(phoneValue + '', country as CountryCode);
return phoneNumber.formatNational();
} catch (error) {
return phoneValue;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment