Skip to content

Commit

Permalink
Check on null/undefined onBeforeMask (numeric alias) #2789
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Herbots committed May 14, 2024
1 parent 25465ad commit 82a2777
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 57 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Enhance resolving of the ndxInitializer to determine the alternating tests.

### Fixed
- TypeError: Cannot read properties of null (reading 'charAt') at m.onBeforeMask #2789
- Cursor moves to start when there is a 0 in the decimal, How to stop this behavior? #2784
- Unexpected character removed #2765
- d/mm/yyyy is converted to d/m//yyyd #2394
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "inputmask",
"version": "5.0.9-beta.68",
"version": "5.0.9-beta.69",
"main": [
"./index.js",
"./css/inputmask.css"
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "robinherbots/inputmask",
"description": "Inputmask is a javascript library which creates an input mask. Inputmask can run against vanilla javascript, jQuery and jqlite.",
"version": "5.0.9-beta.68",
"version": "5.0.9-beta.69",
"type": "library",
"keywords": ["jquery", "plugins", "input", "form", "inputmask", "mask"],
"homepage": "http://robinherbots.github.io/Inputmask",
Expand Down
34 changes: 18 additions & 16 deletions dist/colormask.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* https://github.com/RobinHerbots/Inputmask
* Copyright (c) 2010 - 2024 Robin Herbots
* Licensed under the MIT license
* Version: 5.0.9-beta.68
* Version: 5.0.9-beta.69
*/
!function(e, t) {
if ("object" == typeof exports && "object" == typeof module) module.exports = t(); else if ("function" == typeof define && define.amd) define([], t); else {
Expand Down Expand Up @@ -1925,23 +1925,25 @@
i.default)(t.radixPoint), ".")), isFinite(n);
},
onBeforeMask: function(e, t) {
var n = t.radixPoint || ",";
isFinite(t.digits) && (t.digits = parseInt(t.digits)), "number" != typeof e && "number" !== t.inputType || "" === n || (e = e.toString().replace(".", n));
var a = "-" === e.charAt(0) || e.charAt(0) === t.negationSymbol.front, r = e.split(n), o = r[0].replace(/[^\-0-9]/g, ""), l = r.length > 1 ? r[1].replace(/[^0-9]/g, "") : "", s = r.length > 1;
e = o + ("" !== l ? n + l : l);
var c = 0;
if ("" !== n && (c = t.digitsOptional ? t.digits < l.length ? t.digits : l.length : t.digits,
"" !== l || !t.digitsOptional)) {
var f = Math.pow(10, c || 1);
e = e.replace((0, i.default)(n), "."), isNaN(parseFloat(e)) || (e = (t.roundingFN(parseFloat(e) * f) / f).toFixed(c)),
e = e.toString().replace(".", n);
}
if (0 === t.digits && -1 !== e.indexOf(n) && (e = e.substring(0, e.indexOf(n))),
var n;
e = null !== (n = e) && void 0 !== n ? n : "";
var a = t.radixPoint || ",";
isFinite(t.digits) && (t.digits = parseInt(t.digits)), "number" != typeof e && "number" !== t.inputType || "" === a || (e = e.toString().replace(".", a));
var r = "-" === e.charAt(0) || e.charAt(0) === t.negationSymbol.front, o = e.split(a), l = o[0].replace(/[^\-0-9]/g, ""), s = o.length > 1 ? o[1].replace(/[^0-9]/g, "") : "", c = o.length > 1;
e = l + ("" !== s ? a + s : s);
var f = 0;
if ("" !== a && (f = t.digitsOptional ? t.digits < s.length ? t.digits : s.length : t.digits,
"" !== s || !t.digitsOptional)) {
var p = Math.pow(10, f || 1);
e = e.replace((0, i.default)(a), "."), isNaN(parseFloat(e)) || (e = (t.roundingFN(parseFloat(e) * p) / p).toFixed(f)),
e = e.toString().replace(".", a);
}
if (0 === t.digits && -1 !== e.indexOf(a) && (e = e.substring(0, e.indexOf(a))),
null !== t.min || null !== t.max) {
var p = e.toString().replace(n, ".");
null !== t.min && p < t.min ? e = t.min.toString().replace(".", n) : null !== t.max && p > t.max && (e = t.max.toString().replace(".", n));
var d = e.toString().replace(a, ".");
null !== t.min && d < t.min ? e = t.min.toString().replace(".", a) : null !== t.max && d > t.max && (e = t.max.toString().replace(".", a));
}
return a && "-" !== e.charAt(0) && (e = "-" + e), u(e.toString().split(""), c, t, s).join("");
return r && "-" !== e.charAt(0) && (e = "-" + e), u(e.toString().split(""), f, t, c).join("");
},
onBeforeWrite: function(e, t, n, a) {
function r(e, t) {
Expand Down
4 changes: 2 additions & 2 deletions dist/colormask.min.js

Large diffs are not rendered by default.

34 changes: 18 additions & 16 deletions dist/inputmask.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* https://github.com/RobinHerbots/Inputmask
* Copyright (c) 2010 - 2024 Robin Herbots
* Licensed under the MIT license
* Version: 5.0.9-beta.68
* Version: 5.0.9-beta.69
*/
!function(e, t) {
if ("object" == typeof exports && "object" == typeof module) module.exports = t(); else if ("function" == typeof define && define.amd) define([], t); else {
Expand Down Expand Up @@ -1810,23 +1810,25 @@
i.default)(t.radixPoint), ".")), isFinite(n);
},
onBeforeMask: function(e, t) {
var n = t.radixPoint || ",";
isFinite(t.digits) && (t.digits = parseInt(t.digits)), "number" != typeof e && "number" !== t.inputType || "" === n || (e = e.toString().replace(".", n));
var a = "-" === e.charAt(0) || e.charAt(0) === t.negationSymbol.front, r = e.split(n), o = r[0].replace(/[^\-0-9]/g, ""), l = r.length > 1 ? r[1].replace(/[^0-9]/g, "") : "", s = r.length > 1;
e = o + ("" !== l ? n + l : l);
var c = 0;
if ("" !== n && (c = t.digitsOptional ? t.digits < l.length ? t.digits : l.length : t.digits,
"" !== l || !t.digitsOptional)) {
var f = Math.pow(10, c || 1);
e = e.replace((0, i.default)(n), "."), isNaN(parseFloat(e)) || (e = (t.roundingFN(parseFloat(e) * f) / f).toFixed(c)),
e = e.toString().replace(".", n);
}
if (0 === t.digits && -1 !== e.indexOf(n) && (e = e.substring(0, e.indexOf(n))),
var n;
e = null !== (n = e) && void 0 !== n ? n : "";
var a = t.radixPoint || ",";
isFinite(t.digits) && (t.digits = parseInt(t.digits)), "number" != typeof e && "number" !== t.inputType || "" === a || (e = e.toString().replace(".", a));
var r = "-" === e.charAt(0) || e.charAt(0) === t.negationSymbol.front, o = e.split(a), l = o[0].replace(/[^\-0-9]/g, ""), s = o.length > 1 ? o[1].replace(/[^0-9]/g, "") : "", c = o.length > 1;
e = l + ("" !== s ? a + s : s);
var f = 0;
if ("" !== a && (f = t.digitsOptional ? t.digits < s.length ? t.digits : s.length : t.digits,
"" !== s || !t.digitsOptional)) {
var p = Math.pow(10, f || 1);
e = e.replace((0, i.default)(a), "."), isNaN(parseFloat(e)) || (e = (t.roundingFN(parseFloat(e) * p) / p).toFixed(f)),
e = e.toString().replace(".", a);
}
if (0 === t.digits && -1 !== e.indexOf(a) && (e = e.substring(0, e.indexOf(a))),
null !== t.min || null !== t.max) {
var p = e.toString().replace(n, ".");
null !== t.min && p < t.min ? e = t.min.toString().replace(".", n) : null !== t.max && p > t.max && (e = t.max.toString().replace(".", n));
var d = e.toString().replace(a, ".");
null !== t.min && d < t.min ? e = t.min.toString().replace(".", a) : null !== t.max && d > t.max && (e = t.max.toString().replace(".", a));
}
return a && "-" !== e.charAt(0) && (e = "-" + e), u(e.toString().split(""), c, t, s).join("");
return r && "-" !== e.charAt(0) && (e = "-" + e), u(e.toString().split(""), f, t, c).join("");
},
onBeforeWrite: function(e, t, n, a) {
function r(e, t) {
Expand Down
4 changes: 2 additions & 2 deletions dist/inputmask.min.js

Large diffs are not rendered by default.

34 changes: 18 additions & 16 deletions dist/jquery.inputmask.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* https://github.com/RobinHerbots/Inputmask
* Copyright (c) 2010 - 2024 Robin Herbots
* Licensed under the MIT license
* Version: 5.0.9-beta.68
* Version: 5.0.9-beta.69
*/
!function(e, t) {
if ("object" == typeof exports && "object" == typeof module) module.exports = t(require("jquery")); else if ("function" == typeof define && define.amd) define([ "jquery" ], t); else {
Expand Down Expand Up @@ -1639,23 +1639,25 @@
i.default)(t.radixPoint), ".")), isFinite(n);
},
onBeforeMask: function(e, t) {
var n = t.radixPoint || ",";
isFinite(t.digits) && (t.digits = parseInt(t.digits)), "number" != typeof e && "number" !== t.inputType || "" === n || (e = e.toString().replace(".", n));
var a = "-" === e.charAt(0) || e.charAt(0) === t.negationSymbol.front, r = e.split(n), o = r[0].replace(/[^\-0-9]/g, ""), s = r.length > 1 ? r[1].replace(/[^0-9]/g, "") : "", l = r.length > 1;
e = o + ("" !== s ? n + s : s);
var c = 0;
if ("" !== n && (c = t.digitsOptional ? t.digits < s.length ? t.digits : s.length : t.digits,
"" !== s || !t.digitsOptional)) {
var f = Math.pow(10, c || 1);
e = e.replace((0, i.default)(n), "."), isNaN(parseFloat(e)) || (e = (t.roundingFN(parseFloat(e) * f) / f).toFixed(c)),
e = e.toString().replace(".", n);
}
if (0 === t.digits && -1 !== e.indexOf(n) && (e = e.substring(0, e.indexOf(n))),
var n;
e = null !== (n = e) && void 0 !== n ? n : "";
var a = t.radixPoint || ",";
isFinite(t.digits) && (t.digits = parseInt(t.digits)), "number" != typeof e && "number" !== t.inputType || "" === a || (e = e.toString().replace(".", a));
var r = "-" === e.charAt(0) || e.charAt(0) === t.negationSymbol.front, o = e.split(a), s = o[0].replace(/[^\-0-9]/g, ""), l = o.length > 1 ? o[1].replace(/[^0-9]/g, "") : "", c = o.length > 1;
e = s + ("" !== l ? a + l : l);
var f = 0;
if ("" !== a && (f = t.digitsOptional ? t.digits < l.length ? t.digits : l.length : t.digits,
"" !== l || !t.digitsOptional)) {
var p = Math.pow(10, f || 1);
e = e.replace((0, i.default)(a), "."), isNaN(parseFloat(e)) || (e = (t.roundingFN(parseFloat(e) * p) / p).toFixed(f)),
e = e.toString().replace(".", a);
}
if (0 === t.digits && -1 !== e.indexOf(a) && (e = e.substring(0, e.indexOf(a))),
null !== t.min || null !== t.max) {
var p = e.toString().replace(n, ".");
null !== t.min && p < t.min ? e = t.min.toString().replace(".", n) : null !== t.max && p > t.max && (e = t.max.toString().replace(".", n));
var d = e.toString().replace(a, ".");
null !== t.min && d < t.min ? e = t.min.toString().replace(".", a) : null !== t.max && d > t.max && (e = t.max.toString().replace(".", a));
}
return a && "-" !== e.charAt(0) && (e = "-" + e), u(e.toString().split(""), c, t, l).join("");
return r && "-" !== e.charAt(0) && (e = "-" + e), u(e.toString().split(""), f, t, c).join("");
},
onBeforeWrite: function(e, t, n, a) {
function r(e, t) {
Expand Down
4 changes: 2 additions & 2 deletions dist/jquery.inputmask.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/extensions/inputmask.numeric.extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ Inputmask.extendAliases({
return isFinite(maskedValue);
},
onBeforeMask: function (initialValue, opts) {
initialValue = initialValue ?? "";
const radixPoint = opts.radixPoint || ",";
if (isFinite(opts.digits)) opts.digits = parseInt(opts.digits);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "inputmask",
"version": "5.0.9-beta.68",
"version": "5.0.9-beta.69",
"description": "Inputmask is a javascript library which creates an input mask. Inputmask can run against vanilla javascript, jQuery and jqlite.",
"main": "dist/inputmask.js",
"exports": {
Expand Down
9 changes: 9 additions & 0 deletions qunit/tests_numeric.js
Original file line number Diff line number Diff line change
Expand Up @@ -2549,4 +2549,13 @@ export default function (qunit, Inputmask) {
assert.equal(testmask.value, "4", 'Result "' + testmask.value + '"');
}
);

qunit.test("Currency set null value - #2789", function (assert) {
const $fixture = $("#qunit-fixture");
$fixture.append('<input type="text" id="testmask"/>');
const testmask = document.getElementById("testmask");
Inputmask("currency").mask(testmask);
testmask.value = null;
assert.equal(testmask.value, "", 'Result "' + testmask.value + '"');
});
}

0 comments on commit 82a2777

Please sign in to comment.