Skip to content

Commit 6c64332

Browse files
author
Diego Plentz
committed
adding a few more tests and also fixing a bug when the field has a suffix and we call the unmasked method. related plentz#12
1 parent 68b54f1 commit 6c64332

File tree

6 files changed

+50
-8
lines changed

6 files changed

+50
-8
lines changed

dist/jquery.maskMoney.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* jquery-maskMoney - v2.9.5
2+
* jquery-maskMoney - v2.9.6
33
* jQuery plugin to mask data entry in the input text in the form of money (currency)
44
* https://github.com/plentz/jquery-maskmoney
55
*
@@ -43,7 +43,14 @@
4343
return this.map(function () {
4444
var unmaskedStr = ($(this).val() || "0"),
4545
isNegative = unmaskedStr.indexOf("-") !== -1,
46-
decimalPart = $(unmaskedStr.split(/\D/)).last()[0];
46+
decimalPart;
47+
// get the last position of the array that is a number(probably there's a better way to do that)
48+
$(unmaskedStr.split(/\D/).reverse()).each(function (index, value) {
49+
if(value.match(/\d/)) {
50+
decimalPart = value;
51+
return false;
52+
}
53+
});
4754
unmaskedStr = unmaskedStr.replace(/\D/g, "");
4855
unmaskedStr = unmaskedStr.replace(new RegExp(decimalPart + "$"), "." + decimalPart);
4956
if (isNegative) {

dist/jquery.maskMoney.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

maskMoney.jquery.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "maskMoney",
3-
"version": "2.9.5",
3+
"version": "2.9.6",
44
"title": "jQuery maskMoney",
55
"author": {
66
"name": "Diego Plentz",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "jquery-maskMoney",
33
"description": "jQuery plugin to mask data entry in the input text in the form of money (currency)",
4-
"version": "2.9.5",
4+
"version": "2.9.6",
55
"homepage": "https://github.com/plentz/jquery-maskmoney",
66
"repository": {
77
"type": "git",

src/jquery.maskMoney.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@
3535
return this.map(function () {
3636
var unmaskedStr = ($(this).val() || "0"),
3737
isNegative = unmaskedStr.indexOf("-") !== -1,
38-
decimalPart = $(unmaskedStr.split(/\D/)).last()[0];
38+
decimalPart;
39+
// get the last position of the array that is a number(probably there's a better way to do that)
40+
$(unmaskedStr.split(/\D/).reverse()).each(function (index, value) {
41+
if(value.match(/\d/)) {
42+
decimalPart = value;
43+
return false;
44+
}
45+
});
3946
unmaskedStr = unmaskedStr.replace(/\D/g, "");
4047
unmaskedStr = unmaskedStr.replace(new RegExp(decimalPart + "$"), "." + decimalPart);
4148
if (isNegative) {

test/tests.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,41 @@ test("chainable", function() {
33
equal($("input:first").val(), "123", "class was added correctly from chaining");
44
});
55

6-
test("mask", function() {
6+
module("mask");
7+
test("simple", function() {
78
var $mm = $("input").maskMoney();
89
$mm.val("");
910
$mm.maskMoney("mask");
1011
equal($mm.val(), "0.00", "mask method when trigged correctly formatted input value");
12+
});
13+
14+
test("simple", function() {
15+
var $mm = $("input").maskMoney();
1116
$mm.maskMoney("mask", 123456.78);
1217
equal($mm.val(), "123,456.78", "mask method when trigged with a number as parameter correctly formatted input value");
1318
});
1419

1520

21+
22+
module("unmasked");
23+
test("with prefix", function() {
24+
var unmasked, $mm = $("input");
25+
$mm.val("+ 123.456,78");
26+
unmasked = $mm.maskMoney("unmasked")[0];
27+
equal(unmasked, 123456.78, "unmask method return the correct number when the field value has prefix");
28+
});
29+
30+
test("with suffix", function() {
31+
var unmasked, $mm = $("input");
32+
$mm.val("123.456,78 €");
33+
unmasked = $mm.maskMoney("unmasked")[0];
34+
equal(unmasked, 123456.78, "unmask method return the correct number when the field value has suffix");
35+
});
36+
37+
test("with prefix and suffix", function() {
38+
var unmasked, $mm = $("input");
39+
$mm.val("+ 123.456,78 €");
40+
unmasked = $mm.maskMoney("unmasked")[0];
41+
equal(unmasked, 123456.78, "unmask method return the correct number when the field value has prefix and suffix");
42+
});
43+

0 commit comments

Comments
 (0)