From 8c79e9adb9d44b2342c78c910d32fc5bafd9faf9 Mon Sep 17 00:00:00 2001 From: jbsathler Date: Sun, 26 Feb 2017 19:44:30 -0300 Subject: [PATCH 1/3] Added support to phone mask with country code --- src/br/phone/br-phone.js | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/br/phone/br-phone.js b/src/br/phone/br-phone.js index d37b4f7f..9aee94ef 100644 --- a/src/br/phone/br-phone.js +++ b/src/br/phone/br-phone.js @@ -8,19 +8,22 @@ var maskFactory = require('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) 0-0000-0000'), // with country code + areaCode : new StringMask('(00) 0-0000-0000'), // with area code + simple : new StringMask('0-0000-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; @@ -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]$/, ''); @@ -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; } } }); From 7522fb6ddec873b43549cb72f763b3ace6d985a2 Mon Sep 17 00:00:00 2001 From: Igor Rafael Date: Fri, 15 Sep 2017 19:36:21 -0300 Subject: [PATCH 2/3] style: apply eslint fix --- src/br/phone/br-phone.js | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/br/phone/br-phone.js b/src/br/phone/br-phone.js index fbd5523a..f3770b9b 100644 --- a/src/br/phone/br-phone.js +++ b/src/br/phone/br-phone.js @@ -8,21 +8,21 @@ var maskFactory = require('../../libs/mask-factory'); * see http://portal.embratel.com.br/embratel/9-digito/ */ var phoneMask8D = { - 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 = { - countryCode : new StringMask('+00 (00) 0-0000-0000'), // with country code - areaCode : new StringMask('(00) 0-0000-0000'), // with area code - simple : new StringMask('0-0000-0000') // without area code -}, phoneMask0800 = { - countryCode : null, // N/A - areaCode : null, // N/A - simple : new StringMask('0000-000-0000') // N/A, so it's "simple" -}; + 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 = { + countryCode : new StringMask('+00 (00) 0-0000-0000'), //with country code + areaCode : new StringMask('(00) 0-0000-0000'), //with area code + simple : new StringMask('0-0000-0000') //without area code + }, phoneMask0800 = { + 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) { + clearValue: function(rawValue) { return rawValue.toString().replace(/[^0-9]/g, '').slice(0, 13); }, format: function(cleanValue) { @@ -54,12 +54,12 @@ module.exports = maskFactory({ brPhoneNumber: function(value) { var valueLength = value && value.toString().length; - // 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 + //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; } } From 0638c9475a6655c936b6fae6549a910f4df364c3 Mon Sep 17 00:00:00 2001 From: Igor Rafael Date: Fri, 15 Sep 2017 19:58:15 -0300 Subject: [PATCH 3/3] test: test uiBrPhoneNumber with country code --- src/br/phone/br-phone.js | 6 +++--- src/br/phone/br-phone.spec.js | 6 +++++- src/br/phone/br-phone.test.js | 13 ++++++++++++- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/br/phone/br-phone.js b/src/br/phone/br-phone.js index f3770b9b..549cc68e 100644 --- a/src/br/phone/br-phone.js +++ b/src/br/phone/br-phone.js @@ -12,9 +12,9 @@ var phoneMask8D = { areaCode : new StringMask('(00) 0000-0000'), //with area code simple : new StringMask('0000-0000') //without area code }, phoneMask9D = { - countryCode : new StringMask('+00 (00) 0-0000-0000'), //with country code - areaCode : new StringMask('(00) 0-0000-0000'), //with area code - simple : new StringMask('0-0000-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 = { countryCode : null, //N/A areaCode : null, //N/A diff --git a/src/br/phone/br-phone.spec.js b/src/br/phone/br-phone.spec.js index 8c1195b9..c3048132 100644 --- a/src/br/phone/br-phone.spec.js +++ b/src/br/phone/br-phone.spec.js @@ -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'}, diff --git a/src/br/phone/br-phone.test.js b/src/br/phone/br-phone.test.js index bd5f76f2..8b2291b2 100644 --- a/src/br/phone/br-phone.test.js +++ b/src/br/phone/br-phone.test.js @@ -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(''); + 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(''); @@ -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();