Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with 'unmasked' using precision #214

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
38 changes: 19 additions & 19 deletions dist/jquery.maskMoney.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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");
Expand All @@ -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) {
Expand All @@ -61,8 +55,8 @@
});
},

init : function (settings) {
settings = $.extend({
init : function (parameters) {
parameters = $.extend({
prefix: "",
suffix: "",
affixesStay: true,
Expand All @@ -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() {
Expand Down Expand Up @@ -191,6 +187,7 @@
return setSymbol(newValue);
}


function maskAndPosition(startPos) {
var originalLen = $input.val().length,
newLen;
Expand All @@ -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));
}

Expand Down Expand Up @@ -406,4 +406,4 @@
$.error("Method " + method + " does not exist on jQuery.maskMoney");
}
};
})(window.jQuery || window.Zepto);
})(window.jQuery || window.Zepto);
4 changes: 2 additions & 2 deletions dist/jquery.maskMoney.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions maskMoney.jquery.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
18 changes: 9 additions & 9 deletions src/jquery.maskMoney.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -60,6 +58,7 @@
allowZero: false,
allowNegative: false
}, parameters);
$(this).data("settings", parameters);

return this.each(function () {
var $input = $(this), settings,
Expand All @@ -68,6 +67,7 @@
// data-* api
settings = $.extend({}, parameters);
settings = $.extend(settings, $input.data());
$(this).data("settings", settings);

function getInputSelection() {
var el = $input.get(0),
Expand Down Expand Up @@ -399,4 +399,4 @@
$.error("Method " + method + " does not exist on jQuery.maskMoney");
}
};
})(window.jQuery || window.Zepto);
})(window.jQuery || window.Zepto);
22 changes: 11 additions & 11 deletions test/unmasked_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});