Skip to content

Commit

Permalink
fix: money mask (#2)
Browse files Browse the repository at this point in the history
* fix: toFixed(2) before apply money mask

* add: test

* fix: identation

* add: more tests
  • Loading branch information
franklinjavier authored and wbruno committed Oct 11, 2018
1 parent 9d756ea commit 2201190
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/jquery.maskx.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,21 @@
return v;
};
plugin.money = function (v, input) {
if (!input && /^\d+$/.test(v)) {
v = parseInt(v, 10) * 100;
}
if (!/number|string/.test(typeof v)) return ''

if (!input) {
v = parseFloat(v).toFixed(2)
}


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");
v = v.replace(/(\d)(\d{2})$/, "$1,$2");
return v;

};
plugin.phone = function (v) {
v = String(v || '');
Expand Down
23 changes: 22 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,36 @@
expect(maskx.money('123456789', 'input')).toEqual('1.234.567,89');
expect(maskx.money(1234789123, 'input')).toEqual('12.347.891,23');
});

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(101010)).toEqual('101.010,00');
expect(maskx.money(100)).toEqual('100,00');
expect(maskx.money(100.1)).toEqual('100,10');
expect(maskx.money(100.19)).toEqual('100,19');
expect(maskx.money(200.191)).toEqual('200,19');
expect(maskx.money(200.1919)).toEqual('200,19');
expect(maskx.money(96.00358749)).toEqual('96,00');
expect(maskx.money(1.1)).toEqual('1,10');
expect(maskx.money(1.127)).toEqual('1,13');
expect(maskx.money('1.127')).toEqual('1,13');
expect(maskx.money('200.1919')).toEqual('200,19');
expect(maskx.money('200.999')).toEqual('201,00');
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');
expect(maskx.money(1)).toEqual('1,00');
expect(maskx.money(0)).toEqual('0,00');
expect(maskx.money({})).toEqual('');
expect(maskx.money([{}])).toEqual('');
expect(maskx.money()).toEqual('');
expect(maskx.money(undefined)).toEqual('');
expect(maskx.money(null)).toEqual('');
expect(maskx.money(NaN)).toEqual('');
});
});

Expand All @@ -94,4 +115,4 @@
expect(maskx.time('122745')).toEqual('1:22.745');
});
});
});
});

0 comments on commit 2201190

Please sign in to comment.