Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui-br-boleto-bancario): define a component to parse and format b…
…razilian "boleto bancário"
- Loading branch information
1 parent
757e320
commit b658245
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters