https://github.com/dhershman1/phone-fns
A small modern, and functional phone number library
https://github.com/dhershman1/phone-fns
fns formatting functional phone-fns phone-number
Last synced: about 1 year ago
JSON representation
A small modern, and functional phone number library
- Host: GitHub
- URL: https://github.com/dhershman1/phone-fns
- Owner: dhershman1
- License: mit
- Created: 2017-10-31T00:13:08.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-10-25T00:49:40.000Z (over 1 year ago)
- Last Synced: 2025-04-15T14:51:15.816Z (over 1 year ago)
- Topics: fns, formatting, functional, phone-fns, phone-number
- Language: JavaScript
- Homepage: https://phone-fns.dusty.codes/
- Size: 1.09 MB
- Stars: 16
- Watchers: 1
- Forks: 1
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: changelog.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Phone Fns
A small modern, and functional phone number library which gathers inspiration from the fun [date-fns](https://github.com/date-fns/date-fns) library
- [Documentation](https://phone-fns.dusty.codes/)
## How-To
```cli
npm i phone-fns
```
Standard module system
```js
import * as phoneFns from 'phone-fns'
phoneFns.uglify('555-444-3333') // => '5554443333'
```
Common JS
```js
const phoneFns = require('phone-fns')
phoneFns.uglify('555-444-3333') // => '5554443333'
```
Using Unpkg or jsdelivr CDN (As of v4.0.0+)
```html
phoneFns.uglify('555-444-3333') // => '5554443333'
```
Through the browser
```html
phoneFns.uglify('555-444-3333') // => '5554443333'
```
## Usage
```javascript
import * as 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
```javascript
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 `_`
```js
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