phone-prettify v1.5.1
phone-prettify
A simple and light weight phone formatting utility that can handle most (US) formats, basic extension and long distance style phone numbers.
How-To
Standard module system
import {uglify, dashed, dotted, normalize, longDistance, extensionNumber} from 'phone-prettify';
// Now you can use each as a function
dashed(phone);
dotted(phone2);
Common JS
pPretty = require('phone-prettify');
pPretty.dashed(phone);
Through the browser
<script src="path/to/location/dist/phone-prettify.umd.js"></script>
phonePrettify.dashed(phone);
Formats
Each format can convert from each other. for example you can send dotted
a dashed
format number and get the dotted
format back.
uglify
- Returns string of entered phone with no formattinggroupTwo
- Groups 8 or 10 character numbers to aXX XX XX XX
orXX XX XX XX XX
formatgroupFour
- Groups 8 or 10 character numbers to aXXXX XXXX
orXX XXXX XXXX
formatnormalize
- Returns phone number with(xxx)xxx-xxxx
formatdashed
- Returns phone number withxxx-xxx-xxxx
formatdotted
- Returns phone number withxxx.xxx.xxxx
formatlongDistance
- Returns phone number withx+xxx-xxx-xxxx
format take an optional param to format with different stylesextensionNumber
- Returns phone number withXXX-XXX-XXXX x XXXX
format takes an optional param to format with different styles
Return
Each method returns the formatted version of the string passed in. If an improper phone number or a falsey value are passed in, the method will simply return back that value
import { normalize } from 'phone-prettyify';
console.log(normalize('88551'));
// Output: '88551'
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-prettify';
console.log(uglify('555-444-1111'));
// Output: 5554441111
groupTwo(phone)
Groups the number into four or five groups of two
Arguments
phone
-String
: the desired phone number to run against
Usage
import {groupTwo} from 'phone-prettify';
console.log(groupTwo('44332211'));
// Output: 44 33 22 11
// Also supports 10 character numbers
console.log(groupTwo('5544332211'));
// Output: 55 44 33 22 11
groupFour(phone)
Groups the number into two groups of four, or into one group of two and two groups of four
Arguments
phone
-String
: the desired phone number to run against
Usage
import {groupFour} from 'phone-prettify';
console.log(groupFour('44332211'));
// Output: 4433 2211
// Also supports 10 character numbers
console.log(groupFour('5544332211'));
// Output: 55 4433 2211
normalize(phone)
normalizes
the phone number format (see Formats)
Arguments
phone
-String
: the desired phone number to run against
Usage
import {normalize} from 'phone-prettify';
console.log(normalize('5554441111'));
// Output: (555) 444-1111
dashed(phone)
convert the phone number to dashed
format (see Formats)
Arguments
phone
-String
: the desired phone number to run against
Usage
import {dashed} from 'phone-prettify';
console.log(dashed('5554441111'));
// Output: 555-444-1111
dotted(phone)
convert the phone number to dotted
format (see Formats)
Arguments
phone
-String
: the desired phone number to run against
Usage
import {dotted} from 'phone-prettify';
console.log(dotted('5554441111'));
// Output: 555.444.1111
longDistance(phone, format)
convert the phone number to longDistance
format (see Formats)
Arguments
phone
-String
: the desired phone number to run againstformat
-String
: the format you'd like the outcome to be inDefault: dashed
Usage
import {longDistance} from 'phone-prettify';
console.log(longDistance('15554441111', 'dotted'));
// Output: 1+555.444.1111
extensionNumber(phone, format)
convert the phone number to extensionNumber
format (see Formats)
Arguments
phone
-String
: the desired phone number to run againstformat
-String
: the format you'd like the outcome to be inDefault: dashed
Usage
import {extensionNumber} from 'phone-prettify';
console.log(extensionNumber('55544411118989', 'dotted'));
// Output: 555.444.1111 x 8989