phone-fns

A small, modern, and functional phone library for javascript

Usage no npm install needed!

<script type="module">
  import phoneFns from 'https://cdn.skypack.dev/phone-fns';
</script>

README

npm CircleCI codecov

Phone Fns

A small modern, and functional phone number library which gathers inspiration from the fun date-fns library

How-To

npm i phone-fns

Standard module system

import phoneFns from 'phone-fns'

Common JS

const phoneFns = require('phone-fns')

Through the browser

<script src="path/to/location/dist/phone-fns.min.js"></script>

Usage

import phoneFns from 'phone-fns'

phoneFns.breakdown('4443332222')
// => { areaCode: '444', localCode: '333', lineNumber: '2222', extension: '' }

phoneFns.format('(NNN) NNN-NNNN', '4443332222')
// => '(444) 333-2222'

You can also destructure to only use the functions you want

import { breakdown, format } from 'phone-fns'

breakdown('4443332222')
// => { areaCode: '444', localCode: '333', lineNumber: '2222', extension: '' }

format('(NNN) NNN-NNNN', '4443332222')
// => '(444) 333-2222'

Placeholder Support

Phone-Fns as of v3.2.0 now supports placeholder syntax using Underscores _

const fn = format('NNN-NNN-NNNN')

fn('__________') // => '___-___-____'
fn('444_______') // => '444-___-____'
fn('444555____') // => '444-555-____'
fn('4445556666') // => '444-555-6666'
format('NNN-NNN-NNNN x NNNN', '5554443333____') // => '555-444-3333 x ____'

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