diff --git a/src/jquery.maskx.js b/src/jquery.maskx.js index f5085f9..0f2fa73 100644 --- a/src/jquery.maskx.js +++ b/src/jquery.maskx.js @@ -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; @@ -41,8 +41,8 @@ }); }; - $.fn.maskx = plugin; - $.maskx = plugin; + $.fn.maskx = plugin; + $.maskx = plugin; plugin.defaults = { maskx: '', @@ -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"); diff --git a/test/test.js b/test/test.js index eff0428..3863c7b 100644 --- a/test/test.js +++ b/test/test.js @@ -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'); }); });