breakdown(phone) → {Object}
- Since:
- v0.1.0
- Kind:
- function
- Source:
- Category:
- Function
- Signature:
String -> String -> Object
Takes a provided phone string and breaks it down into an object of codes only works loosely for NANP numbers. The gatcha here is that NANP numbers take the form of NXX NXX XXXX where N is a digit from 2-9 and X is a digit from 0-9, but in order to support placeholders we use a [_0-9]{3} check
Parameters
Name | Type | Description |
---|---|---|
phone | String | The phone number to breakdown |
Returns
Type | Description |
---|---|
Object | Returns an object of the broken down phone number |
breakdown('111-222-3333');
// => { areaCode: '111', localCode: '222', lineNumber: '3333', extension: '' }
breakdown('5554441111');
// => { areaCode: '555', localCode: '444', lineNumber: '1111', extension: '' }
breakdown('555-444-3333 x 8989');
// => { areaCode: '555', localCode: '444', lineNumber: '3333', extension: '8989' }
// Works with placeholder syntax
breakdown('555-___-____')
// => { areaCode: '555', localCode: '___', lineNumber: '____', extension: '' }
breakdownWithFormat(format, phone) → {Object}
- Since:
- v4.1.0
- Kind:
- function
- Source:
- Category:
- Function
- Signature:
String -> String -> Object
Breaks down a phone number based on a custom format provided and returns an object with the parts of the phone number C - Country Code A- Area Code L - Local Code N - Line Number X - Extension Does NOT work with placeholders
Parameters
Name | Type | Description |
---|---|---|
format | String | The format to validate against |
phone | String | The phone number to breakdown |
Returns
Type | Description |
---|---|
Object | Returns an object with the parts of the phone number |
import { breakdownWithFormat } from 'phone-fns'
breakdownWithFormat('+C (AAA) LLL-NNNN xXXX', '+1 (555) 444-3333 x123') // => { countryCode: '1', areaCode: '555', localCode: '444', lineNumber: '3333', extension: '123' }
breakdownWithFormat('AAA-LLL-NNNN', '010-XYZ-1234') // => Error: The phone number provided does not match the format provided or is an invalid phone number
// it's also curried
const fn = breakdownWithFormat('+C (AAA) LLL-NNNN xXXX')
fn('+1 (555) 444-3333 x123') // => { countryCode: '1', areaCode: '123', localCode: '456', lineNumber: '7890', extension: '123' }
findSeparators(phone) → {Array}
- Since:
- v4.1.0
- Kind:
- function
- Source:
- Category:
- Function
- Signature:
String -> Array
Finds a list of separators in a phone number string
Parameters
Name | Type | Description |
---|---|---|
phone | String | The phone number to breakdown |
Returns
Type | Description |
---|---|
Array | Returns an array of separators found in the phone number |
import { findSeparators } from 'phone-fns'
findSeparators('123-456-7890') // => ['-']
findSeparators('123.456.7890') // => ['.']
findSeparators('123 456 7890') // => [' ']
findSeparators('1234567890') // => []
format(layout, phone) → {String}
- Since:
- v0.1.0
- Kind:
- function
- Source:
- Category:
- Function
- Signature:
String -> String -> String
Allows you to format phone numbers however you desire using N as number placeholders and C as country code placeholders these placeholders are case insensitive
Parameters
Name | Type | Description |
---|---|---|
layout | String | The desired layout of the phone number |
phone | String | The phone number to breakdown |
Returns
Type | Description |
---|---|
String | Returns a string which is the formatted phone number |
format('(NNN) NNN.NNNN', '444-555-6666') // => '(444) 555.6666'
format('C + (NNN) NNN.NNNN', '1444-555-6666') // => '1 + (444) 555.6666'
format('CC + NNN.NNN.NNNN', '163334445555') // => '16 + 333.444.5555'
format('(NNN) NNN.NNNN x NNNN', '44455566668989') // => '(444) 555.6666 x 8989'
// Format is case insensitive
format('(NNN) nnn-NNnn', '4445556666') // => (444) 555-6666
// Format is also curried
const fn = format('NNN.NNN.NNNN')
fn('4445556666') // => '444.555.6666'
fn('(333) 444-5555') // => '333.444.5555'
isValid(phone) → {Boolean}
- Since:
- v0.1.0
- Kind:
- function
- Deprecated:
Use isValidWithFormat instead
- Source:
- Category:
- Function
- Signature:
String -> Boolean
Validates the base number, does not take the country code or extension into consideration for this validation. Focuses more on NANP numbers and their format
Parameters
Name | Type | Description |
---|---|---|
phone | String | The phone number to breakdown |
Returns
Type | Description |
---|---|
Boolean | Returns a boolean if the number provided is valid or not |
isValid('555-444-3333') // => true
isValid('5555') // => false
isValidWithFormat(format, phone) → {Boolean}
- Since:
- v4.1.0
- Kind:
- function
- Source:
- Category:
- Function
- Signature:
String -> String -> Boolean
Validates a phone number based on a custom format provided
Parameters
Name | Type | Description |
---|---|---|
format | String | The format to validate against |
phone | String | The phone number to validate |
Returns
Type | Description |
---|---|
Boolean | Returns a boolean if the number provided is valid or not |
import { isValidWithFormat } from 'phone-fns'
isValidWithFormat('NNN-NNN-NNNN', '123-456-7890') // => true
isValidWithFormat('NNN-NNN-NNNN', '010-XYZ-1234') // => false
isValidWithFormat('NNN-NNN-NNNN', '1234567890') // => false
// It's also curried
const fn = isValidWithFormat('NNN-NNN-NNNN')
fn('123-456-7890') // => true
fn('010-XYZ-1234') // => false
fn('1234567890') // => false
normalize(phone) → {String}
- Since:
- v4.1.0
- Kind:
- function
- Source:
- Category:
- Function
- Signature:
String -> String
Strips all of the special characters from the given string but leaves extension and country code characters in place
Parameters
Name | Type | Description |
---|---|---|
phone | String | The phone number to trim and strip down |
Returns
Type | Description |
---|---|
String | Returns the newly created phone number string |
import { normalize } from 'phone-fns'
normalize('555-444-3333') // => '5554443333'
normalize('5554443333') // => '5554443333'
normalize('555.444.3333 x 123') // => '5554443333x123'
uglify(phone) → {String}
- Since:
- v0.1.0
- Kind:
- function
- Source:
- Category:
- Function
- Signature:
String -> String
Strips all of the special characters from the given string
Parameters
Name | Type | Description |
---|---|---|
phone | String | The phone number to trim and strip down |
Returns
Type | Description |
---|---|
String | Returns the newly created phone number string |
uglify('555-444-3333') // => '5554443333'
uglify('5554443333') // => '5554443333'
validate(phone) → {Boolean}
- Since:
- v4.1.0
- Kind:
- function
- Source:
- Category:
- Function
- Signature:
String -> Boolean
Validates the base number, strips out special characters and spaces upon validation, can handle country code and extension in the phone number
Parameters
Name | Type | Description |
---|---|---|
phone | String | The phone number we want to validate |
Returns
Type | Description |
---|---|
Boolean | Returns a boolean if the number provided is valid or not |
import { validate } from 'phone-fns'
validate('555-444-3333') // => true
validate('5555') // => false
validate('5554443333') // => true
validate('5554443333 x 123') // => true