Phone Fns
A small modern, and functional phone number library which gathers inspiration from the fun date-fns library
[!NOTE]
Currently Phone-Fns is focused on US based phone numbers, I am happy to welcome contributions to internationalize the library!
How-To
npm i phone-fns
Standard module system
import * as phoneFns from 'phone-fns'
phoneFns.uglify('555-444-3333')
Common JS
const phoneFns = require('phone-fns')
phoneFns.uglify('555-444-3333')
Using Unpkg or jsdelivr CDN (As of v4.0.0+)
<script src="https://cdn.jsdelivr.net/npm/phone-fns@4.0.0/dist/phone-fns.iife.min.js"></script>
<script>
phoneFns.uglify('555-444-3333')
</script>
Through the browser
<script src="path/to/location/dist/phone-fns.iife.min.js"></script>
<script>
phoneFns.uglify('555-444-3333')
</script>
Usage
import * as phoneFns from 'phone-fns'
phoneFns.breakdown('4443332222')
phoneFns.format('(NNN) NNN-NNNN', '4443332222')
You can also destructure to only use the functions you want
import { breakdown, format } from 'phone-fns'
breakdown('4443332222')
format('(NNN) NNN-NNNN', '4443332222')
Placeholder Support
Phone-Fns as of v3.2.0 now supports placeholder syntax using Underscores _
const fn = format('NNN-NNN-NNNN')
fn('__________')
fn('444_______')
fn('444555____')
fn('4445556666')
format('NNN-NNN-NNNN x NNNN', '5554443333____')
This will only work with underscores or other characters not picked up by the Regex \W
type.
Just call format as the phone number updates in order to get back the newly updated string. Useful for using it with a input mask setup