-
-
Save powellmichael/1570bb80c19bebfaaf220f67ef0e6234 to your computer and use it in GitHub Desktop.
Angular 2/4 Pretty Phone number pipe
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'; | |
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