├── .npmignore ├── .travis.yml ├── .gitignore ├── index.js ├── package.json ├── LICENSE-MIT ├── README.md └── test └── test.js /.npmignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '6' 4 | sudo: false 5 | notifications: 6 | email: false 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.tmproj 2 | *~ 3 | .DS_Store 4 | node_modules/ 5 | node_modules/* 6 | /npm-debug.log 7 | 8 | node_modules/.bin/mocha 9 | 10 | node_modules/.bin/_mocha 11 | 12 | node_modules/mocha/node_modules/.bin/jade 13 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | function normalize(phoneNumber) { 2 | return phoneNumber.replace( 3 | /^[\+\d{1,3}\-\s]*\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/, 4 | "$1$2$3" 5 | ); 6 | } 7 | 8 | function format(phoneNumber, formatString, options) { 9 | // Normalize the phone number first unless not asked to do so in the options 10 | if (!options || !options.normalize) { 11 | phoneNumber = normalize(phoneNumber) 12 | }; 13 | for ( var i = 0, l = phoneNumber.length; i < l; i++ ) { 14 | formatString = formatString.replace("N", phoneNumber[i]); 15 | } 16 | 17 | return formatString; 18 | } 19 | 20 | module.exports = { 21 | normalize: normalize, 22 | format: format 23 | }; 24 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "phone-formatter", 3 | "description": "Parse and format telephone numbers.", 4 | "version": "0.0.2", 5 | "homepage": "https://github.com/stevekinney/node-phone-formatter", 6 | "author": { 7 | "name": "Steve Kinney", 8 | "email": "hello@stevekinney.net", 9 | "url": "http://stevekinney.net" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git://github.com/stevekinney/node-phone-formatter.git" 14 | }, 15 | "bugs": { 16 | "url": "https://github.com/stevekinney/node-phone-formatter/issues" 17 | }, 18 | "licenses": [ 19 | { 20 | "type": "MIT", 21 | "url": "https://github.com/stevekinney/phone-formatter/blob/master/LICENSE-MIT" 22 | } 23 | ], 24 | "main": "index.js", 25 | "engines": { 26 | "node": ">= 0.12" 27 | }, 28 | "scripts": { 29 | "test": "./node_modules/mocha/bin/mocha" 30 | }, 31 | "devDependencies": { 32 | "mocha": "^3.1.2" 33 | }, 34 | "keywords": [] 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Steve Kinney 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Phone Number Formatter 2 | 3 | Parsing and formatting phone numbers so you don't have to. 4 | 5 | ## Getting Started 6 | Install the module with: `npm install phone-formatter` 7 | 8 | Phone-formatter is pretty straight-forward. First, it can normalize pretty much any format you can throw at it. If it can't, then send a pull request with a failing test and it will (shortly thereafter). 9 | 10 | ```javascript 11 | var phoneFormatter = require('phone-formatter'); 12 | 13 | phoneFormatter.normalize('212.555.1212'); 14 | // returns "2125551212" 15 | 16 | phoneFormatter.normalize('+1 (212) 555-1212'); 17 | // returns "2125551212" 18 | ``` 19 | 20 | You get the idea. It can also format a series of ten digits into almost any format your heart desires. Use the letter "N" as a place holder. 21 | 22 | ```javascript 23 | phoneFormatter.format("2125551212", "(NNN) NNN-NNNN"); 24 | // returns "(212) 555-1212" 25 | ``` 26 | 27 | ### But I want to do both at the same time! 28 | 29 | That's cool. Do it. 30 | 31 | ```javascript 32 | phoneFormatter.format("(212) 555-1212", "NNN.NNN.NNNN") 33 | // returns "212.555.1212" 34 | ``` 35 | 36 | If for some reason, this is not what you want: you can turn it off. 37 | 38 | ```javascript 39 | phoneFormatter.format("(212) 555-1212", "NNN.NNN.NNNN", {normalize: false}) 40 | // Will probably crash and burn hideously. What are you even doing? 41 | ``` 42 | 43 | ## Documentation 44 | 45 | As it stands, there are only two methods, `normalize` and `format`. They are pretty much fleshed out above. That said, I'm reserving this second for future greatness. 46 | 47 | I can confirm that Phone Formatter can normalize the following formats. 48 | 49 | * (212) 555 1212 50 | * (212) 555.1212 51 | * (212) 555-1212 52 | * (212) 5551212 53 | * (212)5551212 54 | * 212 555 1212 55 | * 212.555.1212 56 | * 212-555-1212 57 | * 1-212-555-1212 58 | * +1 (212) 555-1212 59 | * 12125551212 60 | * +45 (212) 555-1212 61 | * 2125551212 62 | 63 | It may handle ever more, but I haven't tested it. 64 | 65 | ## Contributing 66 | 67 | Pull requests are welcome as long as they are accompanied by tests. 68 | 69 | Right now, this library is incredibly American-centric and that kind of stinks, but my use case consisted exclusively of American phone numbers. If you'd like to tweak Phone Formatter to better suit your situation, please do! 70 | 71 | **Brief Style Guide**: Two spaces, no space before function parentheses, semi-colons everywhere. 72 | 73 | ## Release History 74 | 75 | * 0.0.2: Normalize phone numbers by default. 76 | * 0.0.1: Just two methods and some tests. 77 | 78 | ## License 79 | Copyright (c) 2013 Steve Kinney 80 | Licensed under the MIT license. 81 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var PhoneFormatter = require('../') 3 | 4 | describe('Normalize', function() { 5 | 6 | var testFormats = [ 7 | "(212) 555 1212", 8 | "(212) 555.1212", 9 | "(212) 555-1212", 10 | "(212) 5551212", 11 | "(212)5551212", 12 | "212 555 1212", 13 | "212.555.1212", 14 | "212-555-1212", 15 | "1-212-555-1212", 16 | "+1 (212) 555-1212", 17 | "12125551212", 18 | "+45 (212) 555-1212", 19 | "2125551212" 20 | ] 21 | 22 | testFormats.forEach(function(number) { 23 | it('should be able to parse ' + number, function() { 24 | assert.deepEqual( 25 | PhoneFormatter.normalize(number), 26 | "2125551212" 27 | ); 28 | }); 29 | }); 30 | 31 | }); 32 | 33 | describe('Format', function() { 34 | 35 | var phoneNumber = "2125551212" 36 | 37 | describe('strings', function () { 38 | 39 | it('should be able to format numbers with (NNN) NNN NNNN', function() { 40 | assert.deepEqual( 41 | PhoneFormatter.format(phoneNumber, "(NNN) NNN NNNN"), 42 | "(212) 555 1212" 43 | ); 44 | }); 45 | 46 | it('should be able to format numbers with (NNN) NNN.NNNN', function() { 47 | assert.deepEqual( 48 | PhoneFormatter.format(phoneNumber, "(NNN) NNN.NNNN"), 49 | "(212) 555.1212" 50 | ); 51 | }); 52 | 53 | it('should be able to format numbers with (NNN) NNN-NNNN', function() { 54 | assert.deepEqual( 55 | PhoneFormatter.format(phoneNumber, "(NNN) NNN-NNNN"), 56 | "(212) 555-1212" 57 | ); 58 | }); 59 | 60 | it('should be able to format numbers with (NNN) NNNNNNN', function() { 61 | assert.deepEqual( 62 | PhoneFormatter.format(phoneNumber, "(NNN) NNNNNNN"), 63 | "(212) 5551212" 64 | ); 65 | }); 66 | 67 | it('should be able to format numbers with (NNN)NNNNNNN', function() { 68 | assert.deepEqual( 69 | PhoneFormatter.format(phoneNumber, "(NNN)NNNNNNN"), 70 | "(212)5551212" 71 | ); 72 | }); 73 | 74 | it('should be able to format numbers with NNN NNN NNNN', function() { 75 | assert.deepEqual( 76 | PhoneFormatter.format(phoneNumber, "NNN NNN NNNN"), 77 | "212 555 1212" 78 | ); 79 | }); 80 | 81 | it('should be able to format numbers with NNN.NNN.NNNN', function() { 82 | assert.deepEqual( 83 | PhoneFormatter.format(phoneNumber, "NNN.NNN.NNNN"), 84 | "212.555.1212" 85 | ); 86 | }); 87 | 88 | it('should be able to format numbers with NNN-NNN-NNNN', function() { 89 | assert.deepEqual( 90 | PhoneFormatter.format(phoneNumber, "NNN-NNN-NNNN"), 91 | "212-555-1212" 92 | ); 93 | }); 94 | 95 | it('should be able to format numbers with 1-NNN-NNN-NNNN', function() { 96 | assert.deepEqual( 97 | PhoneFormatter.format(phoneNumber, "1-NNN-NNN-NNNN"), 98 | "1-212-555-1212" 99 | ); 100 | }); 101 | 102 | it('should be able to format numbers with +1 (NNN) NNN-NNNN', function() { 103 | assert.deepEqual( 104 | PhoneFormatter.format(phoneNumber, "+1 (NNN) NNN-NNNN"), 105 | "+1 (212) 555-1212" 106 | ); 107 | }); 108 | 109 | it('should be able to format numbers with 1NNNNNNNNNN', function() { 110 | assert.deepEqual( 111 | PhoneFormatter.format(phoneNumber, "1NNNNNNNNNN"), 112 | "12125551212" 113 | ); 114 | }); 115 | 116 | }); 117 | 118 | describe('options', function () { 119 | 120 | it('should be able to normalize a phone number before parsing it', function() { 121 | assert.deepEqual( 122 | PhoneFormatter.format("(212) 555-1212", "NNN.NNN.NNNN"), 123 | "212.555.1212" 124 | ); 125 | }); 126 | 127 | }); 128 | 129 | }); 130 | --------------------------------------------------------------------------------