Skip to content

Commit

Permalink
feat(br-phone-number): allow numbers without DDD
Browse files Browse the repository at this point in the history
* allow typing br-phone-number without DDD, rewriting tests

* rewriting related test
  • Loading branch information
richardaum authored and assisrafael committed Oct 14, 2016
1 parent 28b9337 commit 8a5c5cd
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 103 deletions.
44 changes: 30 additions & 14 deletions src/br/phone/br-phone.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,51 @@ var maskFactory = require('mask-factory');
* FIXME: all numbers will have 9 digits after 2016.
* see http://portal.embratel.com.br/embratel/9-digito/
*/
var phoneMask8D = new StringMask('(00) 0000-0000'),
phoneMask9D = new StringMask('(00) 00000-0000'),
phoneMask0800 = new StringMask('0000-000-0000');
var phoneMask8D = {
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
}, phoneMask0800 = {
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, 11);
},
format: function(cleanValue) {
var formatedValue;
format: function (cleanValue) {
var formattedValue;

if (cleanValue.indexOf('0800') === 0) {
formatedValue = phoneMask0800.apply(cleanValue);
formattedValue = phoneMask0800.simple.apply(cleanValue);
} else if (cleanValue.length < 9) {
formattedValue = phoneMask8D.simple.apply(cleanValue) || '';
} else if (cleanValue.length < 10) {
formattedValue = phoneMask9D.simple.apply(cleanValue);
} else if (cleanValue.length < 11) {
formatedValue = phoneMask8D.apply(cleanValue) || '';
formattedValue = phoneMask8D.areaCode.apply(cleanValue);
} else {
formatedValue = phoneMask9D.apply(cleanValue);
formattedValue = phoneMask9D.areaCode.apply(cleanValue);
}

return formatedValue.trim().replace(/[^0-9]$/, '');
return formattedValue.trim().replace(/[^0-9]$/, '');
},
getModelValue: function(formattedValue, originalModelType) {
getModelValue: function (formattedValue, originalModelType) {
var cleanValue = this.clearValue(formattedValue);

return originalModelType === 'number' ? parseInt(cleanValue) : cleanValue;
},
validations: {
brPhoneNumber: function(value) {
brPhoneNumber: function (value) {
var valueLength = value && value.toString().length;
return valueLength === 10 || valueLength === 11;

// 8- 8D without DD
// 9- 9D without DD
// 10- 9D with DD
// 11- 8D with DD and 0800
return valueLength >= 8 && valueLength <= 11;
}
}
});
132 changes: 66 additions & 66 deletions src/br/phone/br-phone.spec.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
'use strict';

describe('ui.utils.masks.number', function() {
it('should load the demo page', function() {
describe('ui.utils.masks.number', function () {
it('should load the demo page', function () {
browser.get('/src/br/phone/br-phone.html');
expect(browser.getTitle()).toEqual('BR Phone Number Spec');
});

describe('ui-br-phone-number:', function() {
it('should apply a 0800 phone number mask while the user is typping:', function() {
describe('ui-br-phone-number:', function () {
it('should apply a 0800 phone number mask while the user is typping:', function () {
var BS = protractor.Key.BACK_SPACE;

var tests = [
{key:'0', viewValue:'(0', modelValue:'0'},
{key:'8', viewValue:'(08', modelValue:'08'},
{key:'0', viewValue:'(08) 0', modelValue:'080'},
{key:'0', viewValue:'0800', modelValue:'0800'},
{key:'1', viewValue:'0800-1', modelValue:'08001'},
{key:'2', viewValue:'0800-12', modelValue:'080012'},
{key:'3', viewValue:'0800-123', modelValue:'0800123'},
{key:'1', viewValue:'0800-123-1', modelValue:'08001231'},
{key:'2', viewValue:'0800-123-12', modelValue:'080012312'},
{key:'3', viewValue:'0800-123-123', modelValue:'0800123123'},
{key:'4', viewValue:'0800-123-1234', modelValue:'08001231234'},
{key:'5', viewValue:'0800-123-1234', modelValue:'08001231234'},
{key:BS, viewValue:'0800-123-123', modelValue:'0800123123'},
{key:BS, viewValue:'0800-123-12', modelValue:'080012312'},
{key:BS, viewValue:'0800-123-1', modelValue:'08001231'},
{key:BS, viewValue:'0800-123', modelValue:'0800123'},
{key:BS, viewValue:'0800-12', modelValue:'080012'},
{key:BS, viewValue:'0800-1', modelValue:'08001'},
{key:BS, viewValue:'0800', modelValue:'0800'},
{key:BS, viewValue:'(08) 0', modelValue:'080'},
{key:BS, viewValue:'(08', modelValue:'08'},
{key:BS, viewValue:'(0', modelValue:'0'},
{key:BS, viewValue:'', modelValue:''},
{key: '0', viewValue: '0', modelValue: '0'},
{key: '8', viewValue: '08', modelValue: '08'},
{key: '0', viewValue: '080', modelValue: '080'},
{key: '0', viewValue: '0800', modelValue: '0800'},
{key: '1', viewValue: '0800-1', modelValue: '08001'},
{key: '2', viewValue: '0800-12', modelValue: '080012'},
{key: '3', viewValue: '0800-123', modelValue: '0800123'},
{key: '1', viewValue: '0800-123-1', modelValue: '08001231'},
{key: '2', viewValue: '0800-123-12', modelValue: '080012312'},
{key: '3', viewValue: '0800-123-123', modelValue: '0800123123'},
{key: '4', viewValue: '0800-123-1234', modelValue: '08001231234'},
{key: '5', viewValue: '0800-123-1234', modelValue: '08001231234'},
{key: BS, viewValue: '0800-123-123', modelValue: '0800123123'},
{key: BS, viewValue: '0800-123-12', modelValue: '080012312'},
{key: BS, viewValue: '0800-123-1', modelValue: '08001231'},
{key: BS, viewValue: '0800-123', modelValue: '0800123'},
{key: BS, viewValue: '0800-12', modelValue: '080012'},
{key: BS, viewValue: '0800-1', modelValue: '08001'},
{key: BS, viewValue: '0800', modelValue: '0800'},
{key: BS, viewValue: '080', modelValue: '080'},
{key: BS, viewValue: '08', modelValue: '08'},
{key: BS, viewValue: '0', modelValue: '0'},
{key: BS, viewValue: '', modelValue: ''}
];

var input = element(by.model('freeLinePhoneNumber')),
Expand All @@ -46,33 +46,33 @@ describe('ui.utils.masks.number', function() {
}
});

it('should apply a phone number mask while the user is typping:', function() {
it('should apply a phone number mask while the user is typping:', function () {
var BS = protractor.Key.BACK_SPACE;

var tests = [
{key:'1', viewValue:'(1', modelValue:'1'},
{key:'2', viewValue:'(12', modelValue:'12'},
{key:'3', viewValue:'(12) 3', modelValue:'123'},
{key:'4', viewValue:'(12) 34', modelValue:'1234'},
{key:'5', viewValue:'(12) 345', modelValue:'12345'},
{key:'6', viewValue:'(12) 3456', modelValue:'123456'},
{key:'7', viewValue:'(12) 3456-7', modelValue:'1234567'},
{key:'8', viewValue:'(12) 3456-78', modelValue:'12345678'},
{key:'9', viewValue:'(12) 3456-789', 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:BS, viewValue:'(12) 3456-7890', modelValue:'1234567890'},
{key:BS, viewValue:'(12) 3456-789', modelValue:'123456789'},
{key:BS, viewValue:'(12) 3456-78', modelValue:'12345678'},
{key:BS, viewValue:'(12) 3456-7', modelValue:'1234567'},
{key:BS, viewValue:'(12) 3456', modelValue:'123456'},
{key:BS, viewValue:'(12) 345', modelValue:'12345'},
{key:BS, viewValue:'(12) 34', modelValue:'1234'},
{key:BS, viewValue:'(12) 3', modelValue:'123'},
{key:BS, viewValue:'(12', modelValue:'12'},
{key:BS, viewValue:'(1', modelValue:'1'},
{key:BS, viewValue:'', modelValue:''},
{key: '1', viewValue: '1', modelValue: '1'},
{key: '2', viewValue: '12', modelValue: '12'},
{key: '3', viewValue: '123', modelValue: '123'},
{key: '4', viewValue: '1234', modelValue: '1234'},
{key: '5', viewValue: '1234-5', modelValue: '12345'},
{key: '6', viewValue: '1234-56', modelValue: '123456'},
{key: '7', viewValue: '1234-567', modelValue: '1234567'},
{key: '8', viewValue: '1234-5678', modelValue: '12345678'},
{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: BS, viewValue: '(12) 3456-7890', modelValue: '1234567890'},
{key: BS, viewValue: '12345-6789', modelValue: '123456789'},
{key: BS, viewValue: '1234-5678', modelValue: '12345678'},
{key: BS, viewValue: '1234-567', modelValue: '1234567'},
{key: BS, viewValue: '1234-56', modelValue: '123456'},
{key: BS, viewValue: '1234-5', modelValue: '12345'},
{key: BS, viewValue: '1234', modelValue: '1234'},
{key: BS, viewValue: '123', modelValue: '123'},
{key: BS, viewValue: '12', modelValue: '12'},
{key: BS, viewValue: '1', modelValue: '1'},
{key: BS, viewValue: '', modelValue: ''}
];

var input = element(by.model('phoneNumber')),
Expand All @@ -85,24 +85,24 @@ describe('ui.utils.masks.number', function() {
}
});

it('should apply a phone number mask in a model with default value:', function() {
it('should apply a phone number mask in a model with default value:', function () {
var BS = protractor.Key.BACK_SPACE;

var tests = [
{key:'1', viewValue:'(1', modelValue:'1'},
{key:'2', viewValue:'(12', modelValue:'12'},
{key:'3', viewValue:'(12) 3', modelValue:'123'},
{key:'4', viewValue:'(12) 34', modelValue:'1234'},
{key:'5', viewValue:'(12) 345', modelValue:'12345'},
{key:'6', viewValue:'(12) 3456', modelValue:'123456'},
{key:'7', viewValue:'(12) 3456-7', modelValue:'1234567'},
{key:BS, viewValue:'(12) 3456', modelValue:'123456'},
{key:BS, viewValue:'(12) 345', modelValue:'12345'},
{key:BS, viewValue:'(12) 34', modelValue:'1234'},
{key:BS, viewValue:'(12) 3', modelValue:'123'},
{key:BS, viewValue:'(12', modelValue:'12'},
{key:BS, viewValue:'(1', modelValue:'1'},
{key:BS, viewValue:'', modelValue:''},
{key: '1', viewValue: '1', modelValue: '1'},
{key: '2', viewValue: '12', modelValue: '12'},
{key: '3', viewValue: '123', modelValue: '123'},
{key: '4', viewValue: '1234', modelValue: '1234'},
{key: '5', viewValue: '1234-5', modelValue: '12345'},
{key: '6', viewValue: '1234-56', modelValue: '123456'},
{key: '7', viewValue: '1234-567', modelValue: '1234567'},
{key: BS, viewValue: '1234-56', modelValue: '123456'},
{key: BS, viewValue: '1234-5', modelValue: '12345'},
{key: BS, viewValue: '1234', modelValue: '1234'},
{key: BS, viewValue: '123', modelValue: '123'},
{key: BS, viewValue: '12', modelValue: '12'},
{key: BS, viewValue: '1', modelValue: '1'},
{key: BS, viewValue: '', modelValue: ''}
];

var input = element(by.model('initializedPhoneNumber')),
Expand Down
Loading

0 comments on commit 8a5c5cd

Please sign in to comment.