I've searched here and there, and I am not able to find something specific about formatting a phone number.
Currently, I am retrieving phone numbers form a JSON in the following format:
25565115
However, I want to achieve this result:
02-55-65-115
For that, I believe that I need to use a custom pipe and I don't think that there is a built-in one that does it automatically.
Can you please give me some guidance on how to do so?
You may use something like that in a custom Angular2 pipe:
Check this AngularJS out for more info, but as I said you need to convert it to Angular2:
http://jsfiddle.net/jorgecas99/S7aSj/
Building on "user5975786", here is the same code for Angular2
PLUNKER
pipe
implementation in TS would look like thisUsage:
There are many things that can be improved, I just made it work for this particular case.
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:
Then:
Finally:
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. =]
When formatting phone numbers from a JSON data service, this was the simplest solution I could think of.
This will format "25565115" into "02-55-65-115"
Hope this helps someone!