I just bumped myself on this article showing how to do that using a Google's lib called libphonenumber. It seems they use this lib in many different languages and has a very wide support (the link is just to the JS package version). Here's how I've implemented it to portuguese / brazilian phone numbers:
First:
npm i libphonenumber-js
Then:
# if you're using Ionic
ionic generate pipe Phone
# if you're just using Angular
ng generate pipe Phone
Finally:
import { Pipe, PipeTransform } from '@angular/core';
import { parsePhoneNumber } from 'libphonenumber-js';
@Pipe({
name: 'phone'
})
export class PhonePipe implements PipeTransform {
transform(phoneValue: number | string): string {
const stringPhone = phoneValue + '';
const phoneNumber = parsePhoneNumber(stringPhone, 'BR');
const formatted = phoneNumber.formatNational();
return formatted;
}
}
You can implement many different ways with this lib. There's many, many handy methods on it, you can just go reading and see how it suits you.
Thumbs up if you've liked. =]
PhonePipe
, don't you have to import it first like this?import {PhonePipe} from './pipe'
– Selfdefense