Skip to content

Commit

Permalink
Accept passing money mask as input and template
Browse files Browse the repository at this point in the history
  • Loading branch information
franklinjavier committed Mar 17, 2016
1 parent fc06ab0 commit b7fdc9d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
12 changes: 8 additions & 4 deletions src/jquery.maskx.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
var plugin = function (settings) {
var $input, mask,
_execmascara = function () {
$input.value = mask($input.value);
$input.value = mask($input.value, 'input');
},
_mascara = function (o, f) {
$input = o;
Expand Down Expand Up @@ -41,8 +41,8 @@
});
};

$.fn.maskx = plugin;
$.maskx = plugin;
$.fn.maskx = plugin;
$.maskx = plugin;

plugin.defaults = {
maskx: '',
Expand Down Expand Up @@ -93,8 +93,12 @@
v = v.replace(/(\d{2})(\d)/, "$1h$2");
return v;
};
plugin.money = function (v) {
plugin.money = function (v, input) {
if (!input && /^\d+$/.test(v)) {
v = parseInt(v, 10) * 100;
}
v = String(v || '');
v = v.replace(/(\d)\.(\d{1}$)/, "$1.$20");
v = v.replace(/\D/g, "");
v = v.replace(/(\d)(\d{8})$/, "$1.$2");
v = v.replace(/(\d)(\d{5})$/, "$1.$2");
Expand Down
25 changes: 15 additions & 10 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,22 @@


describe("maskx money", function() {
it("should be equal", function() {
expect(maskx.money('1234')).toEqual('12,34');
});
it("should be equal", function() {
expect(maskx.money('12345')).toEqual('123,45');
it("should be equal with input", function() {
expect(maskx.money(1234, 'input')).toEqual('12,34');
expect(maskx.money(12341, 'input')).toEqual('123,41');
expect(maskx.money('1.234,56', 'input')).toEqual('1.234,56');
expect(maskx.money('123456789', 'input')).toEqual('1.234.567,89');
expect(maskx.money(1234789123, 'input')).toEqual('12.347.891,23');
});
it("should be equal", function() {
expect(maskx.money('1.234,56')).toEqual('1.234,56');
});
it("should be equal", function() {
expect(maskx.money('123456789')).toEqual('1.234.567,89');
it("should be equal direct template", function() {
expect(maskx.money(1234)).toEqual('1.234,00');
expect(maskx.money(1234.1)).toEqual('1.234,10');
expect(maskx.money(1234.98)).toEqual('1.234,98');
expect(maskx.money('1234')).toEqual('1.234,00');
expect(maskx.money('12345')).toEqual('12.345,00');
expect(maskx.money('1234')).toEqual('1.234,00');
expect(maskx.money('1234.1')).toEqual('1.234,10');
expect(maskx.money('1234789123')).toEqual('1234.789.123,00');
});
});

Expand Down

0 comments on commit b7fdc9d

Please sign in to comment.