Skip to content

Commit

Permalink
feat(ui-br-boleto-bancario): define a component to parse and format b…
Browse files Browse the repository at this point in the history
…razilian "boleto bancário"
  • Loading branch information
assisrafael committed Jun 7, 2015
1 parent 757e320 commit b658245
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/br/boleto-bancario/boleto-bancario.js
@@ -0,0 +1,21 @@
var StringMask = require('string-mask');
var maskFactory = require('mask-factory');

var boletoBancarioMask = new StringMask('00000.00000 00000.000000 00000.000000 0 00000000000000');

module.exports = maskFactory({
validationErrorKey: 'brBoletoBancario',
clearValue: function(rawValue) {
return rawValue.replace(/[^0-9]/g, '').slice(0, 47);
},
format: function(cleanValue) {
if (cleanValue.length === 0) {
return cleanValue;
}

return boletoBancarioMask.apply(cleanValue).replace(/[^0-9]$/, '');
},
validate: function(value) {
return value.length === 47;
}
});
45 changes: 45 additions & 0 deletions src/br/boleto-bancario/boleto-bancario.test.js
@@ -0,0 +1,45 @@
require('../br-masks');

describe('ui-br-boleto-bancario-mask', function() {
beforeEach(angular.mock.module('ui.utils.masks.br'));

it('should format initial model values', function() {
var input = TestUtil.compile('<input ng-model="model" ui-br-boleto-bancario-mask>', {
model: '34958723405162304548623240917012593348590495345'
});

var model = input.controller('ngModel');
expect(model.$viewValue).toBe('34958.72340 51623.045486 23240.917012 5 93348590495345');
});

it('should ignore non digits', function() {
var input = TestUtil.compile('<input ng-model="model" ui-br-boleto-bancario-mask>');
var model = input.controller('ngModel');

var tests = [
{value:'@', viewValue:'', modelValue:''},
{value:'2-', viewValue:'2', modelValue:'2'},
{value:'23a', viewValue:'23', modelValue:'23'},
{value:'23_346', viewValue:'23346', modelValue:'23346'},
{value:'23346!324', viewValue:'23346.324', modelValue:'23346324'},
{value:'23346!324sdfg9870', viewValue:'23346.32498 70', modelValue:'233463249870'},
];

tests.forEach(function(test) {
input.val(test.value).triggerHandler('input');
expect(model.$viewValue).toBe(test.viewValue);
expect(model.$modelValue).toBe(test.modelValue);
});
});

it('should validate a "boleto bancário"', function() {
var input = TestUtil.compile('<input ng-model="model" ui-br-boleto-bancario-mask>', {
model: '104914433855119000002000000001413252'
});

var model = input.controller('ngModel');
expect(model.$error.brBoletoBancario).toBe(true);
input.val('10491443385511900000200000000141325230000093423').triggerHandler('input');
expect(model.$error.brBoletoBancario).toBe(false);
});
});
1 change: 1 addition & 0 deletions src/br/br-masks.js
Expand Up @@ -2,6 +2,7 @@ var m = angular.module('ui.utils.masks.br', [
require('../helpers'),
require('./cpf-cnpj/cpf-cnpj')
])
.directive('uiBrBoletoBancarioMask', require('./boleto-bancario/boleto-bancario'))
.directive('uiBrCepMask', require('./cep/cep'))
.directive('uiBrIeMask', require('./inscricao-estadual/ie'))
.directive('uiNfeAccessKeyMask', require('./nfe/nfe'))
Expand Down

0 comments on commit b658245

Please sign in to comment.