Skip to content

Additional phone number support/cleanup #413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions lib/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,23 @@ helpers.addCommas = function(num) {
* @api public
*/

helpers.phoneNumber = function(num) {
num = num.toString();
helpers.phoneNumber = function(value) {
var num = value.toString().replace(/\D+/g, '');
var prefix = '';

// format only the numbers you can format.
// If it's not going to work, don't do it.
if (num === '' || !(num.length == 10 || num.length == 11)) {
return value;
}
else if(num.length == 11 && num.substr(0,1) == '1'){
prefix = '1 ';
num = num.substr(1);
}

return '(' + num.substr(0, 3) + ') '
+ num.substr(3, 3) + '-'
+ num.substr(6, 4);
return prefix + '(' + num.substr(0,3) + ') '
+ num.substr(3,3) + '-'
+ num.substr(6,4);
};

/**
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"Stephen Way (http://stephenway.net)",
"Thomas Jaggi (http://responsive.ch)",
"Tim Douglas (https://github.com/timdouglas)",
"Will Berger (https://github.com/willzzzzzzzz)",
"(https://github.com/homersimpsons)"
],
"repository": "helpers/handlebars-helpers",
Expand Down
13 changes: 10 additions & 3 deletions test/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ describe('number', function() {
});

describe('phoneNumber', function() {
it('Format a phone number.', function() {
var fn = hbs.compile('{{phoneNumber value}}');
assert.equal(fn({value: '8005551212'}), '(800) 555-1212');
it('should return formatted number if 10-11 numeric digits', function () {
assert.equal(hbs.compile('{{phoneNumber value}}')({ value: '8005551212' }), '(800) 555-1212');
assert.equal(hbs.compile('{{phoneNumber value}}')({ value: '800.555.1212' }), '(800) 555-1212');
assert.equal(hbs.compile('{{phoneNumber value}}')({ value: '18005551212' }), '1 (800) 555-1212');
assert.equal(hbs.compile('{{phoneNumber value}}')({ value: '1-800-555-1212' }), '1 (800) 555-1212');
assert.equal(hbs.compile('{{phoneNumber value}}')({ value: '+18005551212' }), '1 (800) 555-1212');
});
it('should return original value if not 10-11 numeric digits', function () {
assert.equal(hbs.compile('{{phoneNumber value}}')({ value: '800555121' }), '800555121');
assert.equal(hbs.compile('{{phoneNumber value}}')({ value: 'foo' }), 'foo');
});
});

Expand Down