From 28692f971c89d903b2eb10ee68cee9ce1a7bf7b0 Mon Sep 17 00:00:00 2001 From: "Daniel T. Sobrosa" Date: Mon, 6 Feb 2017 17:45:26 -0200 Subject: [PATCH 1/3] Fix unmasked with decimals --- src/jquery.maskMoney.js | 17 ++++++++--------- test/unmasked_test.js | 22 +++++++++++----------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/jquery.maskMoney.js b/src/jquery.maskMoney.js index 889ac5f..0020339 100644 --- a/src/jquery.maskMoney.js +++ b/src/jquery.maskMoney.js @@ -32,14 +32,12 @@ return this.map(function () { var value = ($(this).val() || "0"), isNegative = value.indexOf("-") !== -1, - decimalPart; - // get the last position of the array that is a number(coercion makes "" to be evaluated as false) - $(value.split(/\D/).reverse()).each(function (index, element) { - if(element) { - decimalPart = element; - return false; - } - }); + decimalPart, + settings = $(this).data("settings"); + + if (settings.precision > 0) { + decimalPart = value.split(settings.decimal).reverse()[0].substring(0, settings.precision); + } value = value.replace(/\D/g, ""); value = value.replace(new RegExp(decimalPart + "$"), "." + decimalPart); if (isNegative) { @@ -60,6 +58,7 @@ allowZero: false, allowNegative: false }, parameters); + $(this).data("settings", parameters); return this.each(function () { var $input = $(this), settings, @@ -399,4 +398,4 @@ $.error("Method " + method + " does not exist on jQuery.maskMoney"); } }; -})(window.jQuery || window.Zepto); +})(window.jQuery || window.Zepto); \ No newline at end of file diff --git a/test/unmasked_test.js b/test/unmasked_test.js index ab3c066..5891d0b 100644 --- a/test/unmasked_test.js +++ b/test/unmasked_test.js @@ -2,42 +2,42 @@ module("unmasked"); test("with prefix", function() { - var input = $("#input1"), + var input = $("#input1").maskMoney(), unmasked; - input.val("+ 123.456,78"); + input.val("+ 123,456.78"); unmasked = input.maskMoney("unmasked")[0]; equal(unmasked, 123456.78, "unmask method return the correct number when the field value has prefix"); }); test("with suffix", function() { - var input = $("#input1"), + var input = $("#input1").maskMoney(), unmasked; - input.val("123.456,78 €"); + input.val("123,456.78 €"); unmasked = input.maskMoney("unmasked")[0]; equal(unmasked, 123456.78, "unmask method return the correct number when the field value has suffix"); }); test("with prefix and suffix", function() { - var input = $("#input1"), + var input = $("#input1").maskMoney(), unmasked; - input.val("+ 123.456,78 €"); + input.val("+ 123,456.78 €"); unmasked = input.maskMoney("unmasked")[0]; equal(unmasked, 123456.78, "unmask method return the correct number when the field value has prefix and suffix"); }); test("with negative number", function() { - var input = $("#input1"), + var input = $("#input1").maskMoney(), unmasked; - input.val("-R$ 123.456,78"); + input.val("-R$ 123,456.78"); unmasked = input.maskMoney("unmasked")[0]; equal(unmasked, -123456.78, "unmask method return the correct number when the field value has prefix and suffix"); }); test("with collection of fields", function() { - var input = $(".all"), + var input = $(".all").maskMoney(), unmaskedCollection; - $("#input1").val("+ 123.456,78 €"); - $("#input2").val("R$ 876.543,21"); + $("#input1").val("+ 123,456.78 €"); + $("#input2").val("R$ 876,543.21"); unmaskedCollection = input.maskMoney("unmasked").get(); deepEqual(unmaskedCollection, [123456.78, 876543.21], "unmask method return the correct number when the field value has prefix and suffix"); }); From f1c954532dc0943536333f7599c8fcb4ac8c64b5 Mon Sep 17 00:00:00 2001 From: "Daniel T. Sobrosa" Date: Tue, 7 Feb 2017 09:14:08 -0200 Subject: [PATCH 2/3] Fix banner licence --- Gruntfile.js | 2 +- dist/jquery.maskMoney.js | 38 ++++++++++++++++++------------------ dist/jquery.maskMoney.min.js | 4 ++-- maskMoney.jquery.json | 4 ++-- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index ae5b686..baa0138 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -10,7 +10,7 @@ module.exports = function(grunt) { " * <%= pkg.homepage %>\n" + " *\n" + " * Made by <%= pkg.author.name %>\n" + - " * Under <%= pkg.licenses[0].type %> License (<%= pkg.licenses[0].url %>)\n" + + " * Under <%= pkg.license %> License\n" + " */\n" }, jshint: { diff --git a/dist/jquery.maskMoney.js b/dist/jquery.maskMoney.js index c594a6c..be56a4e 100644 --- a/dist/jquery.maskMoney.js +++ b/dist/jquery.maskMoney.js @@ -4,7 +4,7 @@ * https://github.com/plentz/jquery-maskmoney * * Made by Diego Plentz - * Under MIT License (https://raw.github.com/plentz/jquery-maskmoney/master/LICENSE) + * Under MIT License */ (function ($) { "use strict"; @@ -28,12 +28,8 @@ mask : function (value) { return this.each(function () { - var $this = $(this), - decimalSize; + var $this = $(this); if (typeof value === "number") { - $this.trigger("mask"); - decimalSize = $($this.val().split(/\D/)).last()[0].length; - value = value.toFixed(decimalSize); $this.val(value); } return $this.trigger("mask"); @@ -44,14 +40,12 @@ return this.map(function () { var value = ($(this).val() || "0"), isNegative = value.indexOf("-") !== -1, - decimalPart; - // get the last position of the array that is a number(coercion makes "" to be evaluated as false) - $(value.split(/\D/).reverse()).each(function (index, element) { - if(element) { - decimalPart = element; - return false; - } - }); + decimalPart, + settings = $(this).data("settings"); + + if (settings.precision > 0) { + decimalPart = value.split(settings.decimal).reverse()[0].substring(0, settings.precision); + } value = value.replace(/\D/g, ""); value = value.replace(new RegExp(decimalPart + "$"), "." + decimalPart); if (isNegative) { @@ -61,8 +55,8 @@ }); }, - init : function (settings) { - settings = $.extend({ + init : function (parameters) { + parameters = $.extend({ prefix: "", suffix: "", affixesStay: true, @@ -71,13 +65,15 @@ precision: 2, allowZero: false, allowNegative: false - }, settings); + }, parameters); + $(this).data("settings", parameters); return this.each(function () { - var $input = $(this), + var $input = $(this), settings, onFocusValue; // data-* api + settings = $.extend({}, parameters); settings = $.extend(settings, $input.data()); function getInputSelection() { @@ -191,6 +187,7 @@ return setSymbol(newValue); } + function maskAndPosition(startPos) { var originalLen = $input.val().length, newLen; @@ -202,6 +199,9 @@ function mask() { var value = $input.val(); + if (settings.precision > 0 && value.indexOf(settings.decimal) < 0) { + value += settings.decimal + new Array(settings.precision+1).join(0); + } $input.val(maskValue(value)); } @@ -406,4 +406,4 @@ $.error("Method " + method + " does not exist on jQuery.maskMoney"); } }; -})(window.jQuery || window.Zepto); +})(window.jQuery || window.Zepto); \ No newline at end of file diff --git a/dist/jquery.maskMoney.min.js b/dist/jquery.maskMoney.min.js index b6c66ce..17183cb 100644 --- a/dist/jquery.maskMoney.min.js +++ b/dist/jquery.maskMoney.min.js @@ -4,6 +4,6 @@ * https://github.com/plentz/jquery-maskmoney * * Made by Diego Plentz - * Under MIT License (https://raw.github.com/plentz/jquery-maskmoney/master/LICENSE) + * Under MIT License */ -!function($){"use strict";$.browser||($.browser={},$.browser.mozilla=/mozilla/.test(navigator.userAgent.toLowerCase())&&!/webkit/.test(navigator.userAgent.toLowerCase()),$.browser.webkit=/webkit/.test(navigator.userAgent.toLowerCase()),$.browser.opera=/opera/.test(navigator.userAgent.toLowerCase()),$.browser.msie=/msie/.test(navigator.userAgent.toLowerCase()));var a={destroy:function(){return $(this).unbind(".maskMoney"),$.browser.msie&&(this.onpaste=null),this},mask:function(a){return this.each(function(){var b,c=$(this);return"number"==typeof a&&(c.trigger("mask"),b=$(c.val().split(/\D/)).last()[0].length,a=a.toFixed(b),c.val(a)),c.trigger("mask")})},unmasked:function(){return this.map(function(){var a,b=$(this).val()||"0",c=-1!==b.indexOf("-");return $(b.split(/\D/).reverse()).each(function(b,c){return c?(a=c,!1):void 0}),b=b.replace(/\D/g,""),b=b.replace(new RegExp(a+"$"),"."+a),c&&(b="-"+b),parseFloat(b)})},init:function(a){return a=$.extend({prefix:"",suffix:"",affixesStay:!0,thousands:",",decimal:".",precision:2,allowZero:!1,allowNegative:!1},a),this.each(function(){function b(){var a,b,c,d,e,f=s.get(0),g=0,h=0;return"number"==typeof f.selectionStart&&"number"==typeof f.selectionEnd?(g=f.selectionStart,h=f.selectionEnd):(b=document.selection.createRange(),b&&b.parentElement()===f&&(d=f.value.length,a=f.value.replace(/\r\n/g,"\n"),c=f.createTextRange(),c.moveToBookmark(b.getBookmark()),e=f.createTextRange(),e.collapse(!1),c.compareEndPoints("StartToEnd",e)>-1?g=h=d:(g=-c.moveStart("character",-d),g+=a.slice(0,g).split("\n").length-1,c.compareEndPoints("EndToEnd",e)>-1?h=d:(h=-c.moveEnd("character",-d),h+=a.slice(0,h).split("\n").length-1)))),{start:g,end:h}}function c(){var a=!(s.val().length>=s.attr("maxlength")&&s.attr("maxlength")>=0),c=b(),d=c.start,e=c.end,f=c.start!==c.end&&s.val().substring(d,e).match(/\d/)?!0:!1,g="0"===s.val().substring(0,1);return a||f||g}function d(a){s.each(function(b,c){if(c.setSelectionRange)c.focus(),c.setSelectionRange(a,a);else if(c.createTextRange){var d=c.createTextRange();d.collapse(!0),d.moveEnd("character",a),d.moveStart("character",a),d.select()}})}function e(b){var c="";return b.indexOf("-")>-1&&(b=b.replace("-",""),c="-"),c+a.prefix+b+a.suffix}function f(b){var c,d,f,g=b.indexOf("-")>-1&&a.allowNegative?"-":"",h=b.replace(/[^0-9]/g,""),i=h.slice(0,h.length-a.precision);return i=i.replace(/^0*/g,""),i=i.replace(/\B(?=(\d{3})+(?!\d))/g,a.thousands),""===i&&(i="0"),c=g+i,a.precision>0&&(d=h.slice(h.length-a.precision),f=new Array(a.precision+1-d.length).join(0),c+=a.decimal+f+d),e(c)}function g(a){var b,c=s.val().length;s.val(f(s.val())),b=s.val().length,a-=c-b,d(a)}function h(){var a=s.val();s.val(f(a))}function i(){var b=s.val();return a.allowNegative?""!==b&&"-"===b.charAt(0)?b.replace("-",""):"-"+b:b}function j(a){a.preventDefault?a.preventDefault():a.returnValue=!1}function k(a){a=a||window.event;var d,e,f,h,k,l=a.which||a.charCode||a.keyCode;return void 0===l?!1:48>l||l>57?45===l?(s.val(i()),!1):43===l?(s.val(s.val().replace("-","")),!1):13===l||9===l?!0:!$.browser.mozilla||37!==l&&39!==l||0!==a.charCode?(j(a),!0):!0:c()?(j(a),d=String.fromCharCode(l),e=b(),f=e.start,h=e.end,k=s.val(),s.val(k.substring(0,f)+d+k.substring(h,k.length)),g(f+1),!1):!1}function l(c){c=c||window.event;var d,e,f,h,i,k=c.which||c.charCode||c.keyCode;return void 0===k?!1:(d=b(),e=d.start,f=d.end,8===k||46===k||63272===k?(j(c),h=s.val(),e===f&&(8===k?""===a.suffix?e-=1:(i=h.split("").reverse().join("").search(/\d/),e=h.length-i-1,f=e+1):f+=1),s.val(h.substring(0,e)+h.substring(f,h.length)),g(e),!1):9===k?!0:!0)}function m(){r=s.val(),h();var a,b=s.get(0);b.createTextRange&&(a=b.createTextRange(),a.collapse(!1),a.select())}function n(){setTimeout(function(){h()},0)}function o(){var b=parseFloat("0")/Math.pow(10,a.precision);return b.toFixed(a.precision).replace(new RegExp("\\.","g"),a.decimal)}function p(b){if($.browser.msie&&k(b),""===s.val()||s.val()===e(o()))a.allowZero?a.affixesStay?s.val(e(o())):s.val(o()):s.val("");else if(!a.affixesStay){var c=s.val().replace(a.prefix,"").replace(a.suffix,"");s.val(c)}s.val()!==r&&s.change()}function q(){var a,b=s.get(0);b.setSelectionRange?(a=s.val().length,b.setSelectionRange(a,a)):s.val(s.val())}var r,s=$(this);a=$.extend(a,s.data()),s.unbind(".maskMoney"),s.bind("keypress.maskMoney",k),s.bind("keydown.maskMoney",l),s.bind("blur.maskMoney",p),s.bind("focus.maskMoney",m),s.bind("click.maskMoney",q),s.bind("cut.maskMoney",n),s.bind("paste.maskMoney",n),s.bind("mask.maskMoney",h)})}};$.fn.maskMoney=function(b){return a[b]?a[b].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof b&&b?($.error("Method "+b+" does not exist on jQuery.maskMoney"),void 0):a.init.apply(this,arguments)}}(window.jQuery||window.Zepto); \ No newline at end of file +!function($){"use strict";$.browser||($.browser={},$.browser.mozilla=/mozilla/.test(navigator.userAgent.toLowerCase())&&!/webkit/.test(navigator.userAgent.toLowerCase()),$.browser.webkit=/webkit/.test(navigator.userAgent.toLowerCase()),$.browser.opera=/opera/.test(navigator.userAgent.toLowerCase()),$.browser.msie=/msie/.test(navigator.userAgent.toLowerCase()));var a={destroy:function(){return $(this).unbind(".maskMoney"),$.browser.msie&&(this.onpaste=null),this},mask:function(a){return this.each(function(){var b=$(this);return"number"==typeof a&&b.val(a),b.trigger("mask")})},unmasked:function(){return this.map(function(){var a,b=$(this).val()||"0",c=b.indexOf("-")!==-1,d=$(this).data("settings");return d.precision>0&&(a=b.split(d.decimal).reverse()[0].substring(0,d.precision)),b=b.replace(/\D/g,""),b=b.replace(new RegExp(a+"$"),"."+a),c&&(b="-"+b),parseFloat(b)})},init:function(a){return a=$.extend({prefix:"",suffix:"",affixesStay:!0,thousands:",",decimal:".",precision:2,allowZero:!1,allowNegative:!1},a),$(this).data("settings",a),this.each(function(){function b(){var a,b,c,d,e,f=t.get(0),g=0,h=0;return"number"==typeof f.selectionStart&&"number"==typeof f.selectionEnd?(g=f.selectionStart,h=f.selectionEnd):(b=document.selection.createRange(),b&&b.parentElement()===f&&(d=f.value.length,a=f.value.replace(/\r\n/g,"\n"),c=f.createTextRange(),c.moveToBookmark(b.getBookmark()),e=f.createTextRange(),e.collapse(!1),c.compareEndPoints("StartToEnd",e)>-1?g=h=d:(g=-c.moveStart("character",-d),g+=a.slice(0,g).split("\n").length-1,c.compareEndPoints("EndToEnd",e)>-1?h=d:(h=-c.moveEnd("character",-d),h+=a.slice(0,h).split("\n").length-1)))),{start:g,end:h}}function c(){var a=!(t.val().length>=t.attr("maxlength")&&t.attr("maxlength")>=0),c=b(),d=c.start,e=c.end,f=!(c.start===c.end||!t.val().substring(d,e).match(/\d/)),g="0"===t.val().substring(0,1);return a||f||g}function d(a){t.each(function(b,c){if(c.setSelectionRange)c.focus(),c.setSelectionRange(a,a);else if(c.createTextRange){var d=c.createTextRange();d.collapse(!0),d.moveEnd("character",a),d.moveStart("character",a),d.select()}})}function e(a){var b="";return a.indexOf("-")>-1&&(a=a.replace("-",""),b="-"),b+r.prefix+a+r.suffix}function f(a){var b,c,d,f=a.indexOf("-")>-1&&r.allowNegative?"-":"",g=a.replace(/[^0-9]/g,""),h=g.slice(0,g.length-r.precision);return h=h.replace(/^0*/g,""),h=h.replace(/\B(?=(\d{3})+(?!\d))/g,r.thousands),""===h&&(h="0"),b=f+h,r.precision>0&&(c=g.slice(g.length-r.precision),d=new Array(r.precision+1-c.length).join(0),b+=r.decimal+d+c),e(b)}function g(a){var b,c=t.val().length;t.val(f(t.val())),b=t.val().length,a-=c-b,d(a)}function h(){var a=t.val();r.precision>0&&a.indexOf(r.decimal)<0&&(a+=r.decimal+new Array(r.precision+1).join(0)),t.val(f(a))}function i(){var a=t.val();return r.allowNegative?""!==a&&"-"===a.charAt(0)?a.replace("-",""):"-"+a:a}function j(a){a.preventDefault?a.preventDefault():a.returnValue=!1}function k(a){a=a||window.event;var d,e,f,h,k,l=a.which||a.charCode||a.keyCode;return void 0!==l&&(l<48||l>57?45===l?(t.val(i()),!1):43===l?(t.val(t.val().replace("-","")),!1):13===l||9===l||(!(!$.browser.mozilla||37!==l&&39!==l||0!==a.charCode)||(j(a),!0)):!!c()&&(j(a),d=String.fromCharCode(l),e=b(),f=e.start,h=e.end,k=t.val(),t.val(k.substring(0,f)+d+k.substring(h,k.length)),g(f+1),!1))}function l(a){a=a||window.event;var c,d,e,f,h,i=a.which||a.charCode||a.keyCode;return void 0!==i&&(c=b(),d=c.start,e=c.end,8!==i&&46!==i&&63272!==i||(j(a),f=t.val(),d===e&&(8===i?""===r.suffix?d-=1:(h=f.split("").reverse().join("").search(/\d/),d=f.length-h-1,e=d+1):e+=1),t.val(f.substring(0,d)+f.substring(e,f.length)),g(d),!1))}function m(){s=t.val(),h();var a,b=t.get(0);b.createTextRange&&(a=b.createTextRange(),a.collapse(!1),a.select())}function n(){setTimeout(function(){h()},0)}function o(){var a=parseFloat("0")/Math.pow(10,r.precision);return a.toFixed(r.precision).replace(new RegExp("\\.","g"),r.decimal)}function p(a){if($.browser.msie&&k(a),""===t.val()||t.val()===e(o()))r.allowZero?r.affixesStay?t.val(e(o())):t.val(o()):t.val("");else if(!r.affixesStay){var b=t.val().replace(r.prefix,"").replace(r.suffix,"");t.val(b)}t.val()!==s&&t.change()}function q(){var a,b=t.get(0);b.setSelectionRange?(a=t.val().length,b.setSelectionRange(a,a)):t.val(t.val())}var r,s,t=$(this);r=$.extend({},a),r=$.extend(r,t.data()),t.unbind(".maskMoney"),t.bind("keypress.maskMoney",k),t.bind("keydown.maskMoney",l),t.bind("blur.maskMoney",p),t.bind("focus.maskMoney",m),t.bind("click.maskMoney",q),t.bind("cut.maskMoney",n),t.bind("paste.maskMoney",n),t.bind("mask.maskMoney",h)})}};$.fn.maskMoney=function(b){return a[b]?a[b].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof b&&b?void $.error("Method "+b+" does not exist on jQuery.maskMoney"):a.init.apply(this,arguments)}}(window.jQuery||window.Zepto); \ No newline at end of file diff --git a/maskMoney.jquery.json b/maskMoney.jquery.json index 4e014d9..1ef3f94 100644 --- a/maskMoney.jquery.json +++ b/maskMoney.jquery.json @@ -10,11 +10,11 @@ "licenses": [ { "type": "MIT", - "url": "https://raw.github.com/plentz/jquery-maskmoney/master/LICENSE" + "url": "http://opensource.org/licenses/MIT" } ], "dependencies": { - "jquery": ">=1.6" + "jquery": "latest" }, "description": "jQuery plugin to mask data entry in the input text in the form of money (currency)", "keywords": [ From fb4062ad745d57a06061c69ed0e0a46dbb066464 Mon Sep 17 00:00:00 2001 From: "Daniel T. Sobrosa" Date: Tue, 21 Mar 2017 14:58:22 -0300 Subject: [PATCH 3/3] Setting the settings to data atribute --- src/jquery.maskMoney.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/jquery.maskMoney.js b/src/jquery.maskMoney.js index 0020339..f75f53b 100644 --- a/src/jquery.maskMoney.js +++ b/src/jquery.maskMoney.js @@ -67,6 +67,7 @@ // data-* api settings = $.extend({}, parameters); settings = $.extend(settings, $input.data()); + $(this).data("settings", settings); function getInputSelection() { var el = $input.get(0),