Skip to content
Merged
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
35 changes: 22 additions & 13 deletions src/br/phone/br-phone.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ var maskFactory = require('../../libs/mask-factory');
* see http://portal.embratel.com.br/embratel/9-digito/
*/
var phoneMask8D = {
areaCode: new StringMask('(00) 0000-0000'), //with area code
simple: new StringMask('0000-0000') //without area code
countryCode : new StringMask('+00 (00) 0000-0000'), //with country code
areaCode : new StringMask('(00) 0000-0000'), //with area code
simple : new StringMask('0000-0000') //without area code
}, phoneMask9D = {
areaCode: new StringMask('(00) 00000-0000'), //with area code
simple: new StringMask('00000-0000') //without area code
countryCode : new StringMask('+00 (00) 00000-0000'), //with country code
areaCode : new StringMask('(00) 00000-0000'), //with area code
simple : new StringMask('00000-0000') //without area code
}, phoneMask0800 = {
areaCode: null, //N/A
simple: new StringMask('0000-000-0000') //N/A, so it's "simple"
countryCode : null, //N/A
areaCode : null, //N/A
simple : new StringMask('0000-000-0000') //N/A, so it's "simple"
};

module.exports = maskFactory({
clearValue: function(rawValue) {
return rawValue.toString().replace(/[^0-9]/g, '').slice(0, 11);
return rawValue.toString().replace(/[^0-9]/g, '').slice(0, 13);
},
format: function(cleanValue) {
var formattedValue;
Expand All @@ -33,8 +36,12 @@ module.exports = maskFactory({
formattedValue = phoneMask9D.simple.apply(cleanValue);
} else if (cleanValue.length < 11) {
formattedValue = phoneMask8D.areaCode.apply(cleanValue);
} else {
} else if (cleanValue.length < 12) {
formattedValue = phoneMask9D.areaCode.apply(cleanValue);
} else if (cleanValue.length < 13) {
formattedValue = phoneMask8D.countryCode.apply(cleanValue);
} else {
formattedValue = phoneMask9D.countryCode.apply(cleanValue);
}

return formattedValue.trim().replace(/[^0-9]$/, '');
Expand All @@ -47,11 +54,13 @@ module.exports = maskFactory({
brPhoneNumber: function(value) {
var valueLength = value && value.toString().length;

//8- 8D without DD
//9- 9D without DD
//10- 9D with DD
//11- 8D with DD and 0800
return valueLength >= 8 && valueLength <= 11;
//8- 8D without AC
//9- 9D without AC
//10- 8D with AC
//11- 9D with AC and 0800
//12- 8D with AC plus CC
//13- 9D with AC plus CC
return valueLength >= 8 && valueLength <= 13;
}
}
});
6 changes: 5 additions & 1 deletion src/br/phone/br-phone.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ describe('ui.utils.masks.number', function() {
{key: '9', viewValue: '12345-6789', modelValue: '123456789'},
{key: '0', viewValue: '(12) 3456-7890', modelValue: '1234567890'},
{key: '1', viewValue: '(12) 34567-8901', modelValue: '12345678901'},
{key: '2', viewValue: '(12) 34567-8901', modelValue: '12345678901'},
{key: '2', viewValue: '+12 (34) 5678-9012', modelValue: '123456789012'},
{key: '3', viewValue: '+12 (34) 56789-0123', modelValue: '1234567890123'},
{key: '4', viewValue: '+12 (34) 56789-0123', modelValue: '1234567890123'},
{key: BS, viewValue: '+12 (34) 5678-9012', modelValue: '123456789012'},
{key: BS, viewValue: '(12) 34567-8901', modelValue: '12345678901'},
{key: BS, viewValue: '(12) 3456-7890', modelValue: '1234567890'},
{key: BS, viewValue: '12345-6789', modelValue: '123456789'},
{key: BS, viewValue: '1234-5678', modelValue: '12345678'},
Expand Down
13 changes: 12 additions & 1 deletion src/br/phone/br-phone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ describe('ui-br-phone-number', function() {
expect(model.$viewValue).toBe('12345-6789');
});

it('should format model values with country code', function() {
var input = TestUtil.compile('<input ng-model="model" ui-br-phone-number>');
var model = input.controller('ngModel');

input.val('123456789012').triggerHandler('input');
expect(model.$viewValue).toBe('+12 (34) 5678-9012');
input.val('1234567890123').triggerHandler('input');
expect(model.$viewValue).toBe('+12 (34) 56789-0123');
});

it('should ignore non digits', function() {
var input = TestUtil.compile('<input ng-model="model" ng-model-options="{allowInvalid:true}"' +
' ui-br-phone-number>');
Expand Down Expand Up @@ -105,7 +115,8 @@ describe('ui-br-phone-number', function() {
[
'12345678', '12345678901',
'1234567890', '123456789',
'12345678'
'12345678', '123456789012',
'1234567890123'
].forEach(function(number) {
input.val(number).triggerHandler('input');
expect(model.$error.brPhoneNumber).toBeUndefined();
Expand Down