Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chenglou/phone-parser
Parse the phone input into an output format of your choice. Good for formatting a user input.
https://github.com/chenglou/phone-parser
Last synced: 2 months ago
JSON representation
Parse the phone input into an output format of your choice. Good for formatting a user input.
- Host: GitHub
- URL: https://github.com/chenglou/phone-parser
- Owner: chenglou
- Created: 2013-06-06T03:38:47.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-06-08T02:17:26.000Z (over 11 years ago)
- Last Synced: 2024-10-08T00:06:01.171Z (3 months ago)
- Language: JavaScript
- Homepage:
- Size: 117 KB
- Stars: 6
- Watchers: 4
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: Readme.md
- Changelog: History.md
Awesome Lists containing this project
README
# Phone Parser
Parse the phone input into an output format of your choice. Good for formatting a user input.## Installation
Through [component](https://github.com/component/component):```bash
$ component install chenglou/phone-parser
```Through [npm](https://npmjs.org):
```bash
$ npm install phone-parser
```Or directly [download the script](https://raw.github.com/chenglou/phone-parser/master/index.js) for browsers. Rename it however you want.
## API
`'x'` represents a digit placeholder.```js
// This is the component/npm format. Browser format below.
var phoneParser = require('phone-parser');phoneParser('2341234447', '(xxx) xxx-xxxx'); // Returns '(234) 123-4447'.
phoneParser('+86 455 6988', 'xxxxxxxxx'); // Returns '864556988'.
phoneParser(' 123 4567 ', '+1 xxx xxxx'); // Returns '+1 123 4567'.
phoneParser('123 4567890', '+1 xxx xxxx'); // Error: phone format cannot be parsed.
```Remember, only `'x'` is a placeholder. The rest is treated as part of the formatting.
**For browsers** (real-life example of formatting an input field):
```html
var phoneField = document.getElementById('phone');
phoneField.addEventListener('change', function() {
try{
phoneField.value = phoneParser(phoneField.value, '(xxx) xxx-xxxx');
} catch(e) {}
});```
## Testing
Using npm:If you haven't installed jasmine-node and CoffeeScript:
```bash
$ npm install
```Then:
```bash
$ npm test
```## License
MIT.