Skip to content

Commit

Permalink
Initial commit FTW
Browse files Browse the repository at this point in the history
  • Loading branch information
stevekinney committed Mar 4, 2013
0 parents commit 59dfa92
Show file tree
Hide file tree
Showing 201 changed files with 41,607 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .npmignore
@@ -0,0 +1 @@
/node_modules/
22 changes: 22 additions & 0 deletions LICENSE-MIT
@@ -0,0 +1,22 @@
Copyright (c) 2013 Steve Kinney

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
63 changes: 63 additions & 0 deletions README.md
@@ -0,0 +1,63 @@
# Phone Number Formatter

Parsing and formatting phone numbers so you don't have to.

## Getting Started
Install the module with: `npm install phone-formatter`

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).

```javascript
var phoneFormatter = require('phone-formatter');

phoneFormatter.normalize('212.555.1212');
// returns "2125551212"

phoneFormatter.normalize('+1 (212) 555-1212');
// returns "2125551212"
```

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.

```javascript
phoneFormatter.format("2125551212", "(NNN) NNN-NNNN");
// returns "(212) 555-1212"
```

## Documentation

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.

I can confirm that Phone Formatter can normalize the following formats.

* (212) 555 1212
* (212) 555.1212
* (212) 555-1212
* (212) 5551212
* (212)5551212
* 212 555 1212
* 212.555.1212
* 212-555-1212
* 1-212-555-1212
* +1 (212) 555-1212
* 12125551212
* +45 (212) 555-1212
* 212555121

It may handle ever more, but I haven't tested it.

## Contributing

Pull requests are welcome as long as they are accompanied by tests.

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!

**Brief Style Guide**: Two spaces, no space before function parentheses, semi-colons everywhere.

## Release History

* 0.0.1: Just two methods and some tests.

## License
Copyright (c) 2013 Steve Kinney
Licensed under the MIT license.
26 changes: 26 additions & 0 deletions index.js
@@ -0,0 +1,26 @@
module.exports = {

normalize: function extract(phoneNumber) {

return phoneNumber.replace(
/^[\+\d{1,3}\-\s]*\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/,
"$1$2$3"
);

},

format: function format(phoneNumber, formatString, options) {

if (options && options.normalize === true) {
phoneNumber = this.normalize(phoneNumber)
};

for ( var i = 0, l = phoneNumber.length; i < l; i++ ) {
formatString = formatString.replace("N", phoneNumber[i]);
}

return formatString;

},

};
1 change: 1 addition & 0 deletions node_modules/.bin/_mocha

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/mocha

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions node_modules/mocha/.npmignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions node_modules/mocha/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 59dfa92

Please sign in to comment.