phone-fns
A small modern, and functional phone number library which gather inspiration from the fun date-fns library
How-To
Standard module system
import { format } from 'phone-fns';
format(phone, layout);
Common JS
const { format } = require('phone-fns');
format(phone, layout);
Through the browser
<script src="path/to/location/dist/phone-fns.umd.js"></script>
phoneFns.format(phone, layout);
Methods
uglify(phone)
uglifies
the phone number down to just the number string
Arguments
phone
- String
: the desired phone number to run against
Usage
import {uglify} from 'phone-fns';
console.log(uglify('555-444-1111'));
format(phone, format, isLD)
Customized formatting function allowing you to create your own custom formats
Arguments
phone
- String
: The desired phone number to run againstformat
- String
: The desired format to set the number into, see aboveisLD
- Boolean
: Tell the function to run the long distance rule
Formatting
A
- Area Code NumberL
- Local Code Number (Usually the first three digits)N
- Line Number (Usually the last four digits)E
- Extension Number (Usually an additional set of digits at the end)C
- Country Code Number (Usually the set of digits that go ahead of a number)
Usage
import { format } from 'phone-fns';
format('4443332222', '(AAA) LLL-NNNN');
format('1124443332222', 'CCC + (AAA)-LLL.NNNN', true);
format('44433322228989', '(AAA).LLL.NNNN x EEEE');
find(phone, code)
Find a piece of the phone number and return it
Arguments
phone
- String
: the desired phone number to run againstcode
- String
: the piece of the phone number to return can be areaCode
, localCode
, lineNumber
, countryCode
, or extension
Usage
import { find } from 'phone-fns';
console.log(find('555-444-3333', 'areaCode'));
breakdown(phone, isLD)
Takes the provided phone string and breaks it down into an object like so:
{
countryCode: '',
areaCode: '',
localCode: '',
lineNumber: '',
extension: ''
}
Arguments
phone
- String
: the desired phone number to run againstisLD
- Boolean
: tell the function if the phone is using a long distance style or not
Usage
import { breakdown } from 'phone-fns';
console.log(breakdown('555-444-3333'));
console.log(breakdown('112555-444-3333', true));
console.log(breakdown('555-444-33338989'));
isValid(phone)
Validates if the phone number is valid or not
Arguments
phone
- String
: the desired phone number to run against
Usage
import { isValid } from 'phone-fns';
console.log(isValid('555-444-3333'));
console.log(isValid('8896'));
identical(x, y)
Validates if the phone number is valid or not
Arguments
x
- String
: the desired phone number to run against y
y
- String
: the desired phone number to run against x
Usage
import { identical } from 'phone-fns';
console.log(identical('555-444-3333', '555-444-3333'));
console.log(identical('555-444-3333', '555-333-4444'));