Skip to content

Instantly share code, notes, and snippets.

@powellmichael
Forked from Pierre-RA/phone.pipe.ts
Created February 5, 2021 15:28
Show Gist options
  • Save powellmichael/1570bb80c19bebfaaf220f67ef0e6234 to your computer and use it in GitHub Desktop.
Save powellmichael/1570bb80c19bebfaaf220f67ef0e6234 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