use-mask-input 1.0.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dist/example/App.example.d.ts +3 -0
  2. package/dist/{useMaskInput.test.d.ts → example/index.d.ts} +0 -0
  3. package/dist/index.js +1 -2
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.modern.js +1 -2
  6. package/dist/index.modern.js.map +1 -1
  7. package/dist/useMaskInput.d.ts +3 -3
  8. package/node_modules/inputmask/README.md +109 -79
  9. package/node_modules/inputmask/bundle.js +6 -5
  10. package/node_modules/inputmask/dist/inputmask.es6.js +5 -0
  11. package/node_modules/inputmask/dist/inputmask.js +2892 -2608
  12. package/node_modules/inputmask/dist/inputmask.min.js +3 -3
  13. package/node_modules/inputmask/dist/jquery.inputmask.js +2829 -2534
  14. package/node_modules/inputmask/dist/jquery.inputmask.min.js +3 -3
  15. package/node_modules/inputmask/lib/bindings/inputmask.es6.js +5 -0
  16. package/node_modules/inputmask/lib/canUseDOM.js +7 -0
  17. package/node_modules/inputmask/lib/defaults.js +101 -0
  18. package/node_modules/inputmask/lib/definitions.js +13 -0
  19. package/node_modules/inputmask/lib/dependencyLibs/data.js +8 -0
  20. package/node_modules/inputmask/lib/dependencyLibs/events.js +199 -0
  21. package/node_modules/inputmask/lib/dependencyLibs/extend.js +58 -0
  22. package/node_modules/inputmask/lib/dependencyLibs/inputmask.dependencyLib.jquery.js +4 -3
  23. package/node_modules/inputmask/lib/dependencyLibs/inputmask.dependencyLib.js +12 -343
  24. package/node_modules/inputmask/lib/environment.js +9 -0
  25. package/node_modules/inputmask/lib/escapeRegex.js +4 -0
  26. package/node_modules/inputmask/lib/eventhandlers.js +513 -0
  27. package/node_modules/inputmask/lib/eventruler.js +124 -0
  28. package/node_modules/inputmask/lib/extensions/inputmask.date.extensions.js +552 -385
  29. package/node_modules/inputmask/lib/extensions/inputmask.extensions.js +116 -97
  30. package/node_modules/inputmask/lib/extensions/inputmask.numeric.extensions.js +594 -565
  31. package/node_modules/inputmask/lib/global/window.js +2 -6
  32. package/node_modules/inputmask/lib/inputHandling.js +252 -0
  33. package/node_modules/inputmask/lib/inputmask.js +129 -126
  34. package/node_modules/inputmask/lib/inputmaskElement.js +26 -20
  35. package/node_modules/inputmask/lib/jquery.inputmask.js +3 -1
  36. package/node_modules/inputmask/lib/keycode.json +6 -1
  37. package/node_modules/inputmask/lib/mask-lexer.js +467 -0
  38. package/node_modules/inputmask/lib/mask.js +244 -0
  39. package/node_modules/inputmask/lib/masktoken.js +13 -0
  40. package/node_modules/inputmask/lib/polyfills/Array.includes.js +48 -0
  41. package/node_modules/inputmask/lib/polyfills/Object.getPrototypeOf.js +7 -0
  42. package/node_modules/inputmask/lib/positioning.js +348 -0
  43. package/node_modules/inputmask/lib/validation-tests.js +597 -0
  44. package/node_modules/inputmask/lib/validation.js +664 -0
  45. package/node_modules/inputmask/package.json +41 -71
  46. package/package.json +40 -43
  47. package/node_modules/inputmask/CHANGELOG.md +0 -714
  48. package/node_modules/inputmask/index.js +0 -1
  49. package/node_modules/inputmask/lib/dependencyLibs/inputmask.dependencyLib.jqlite.js +0 -170
  50. package/node_modules/inputmask/lib/maskScope.js +0 -2498
  51. package/node_modules/inputmask/lib/maskset.js +0 -466
@@ -4,599 +4,628 @@
4
4
  Copyright (c) Robin Herbots
5
5
  Licensed under the MIT license
6
6
  */
7
- var Inputmask = require("../inputmask"), $ = Inputmask.dependencyLib, keyCode = require("../keycode");
7
+ import Inputmask from "../inputmask";
8
+ import keyCode from "../keycode.json";
9
+ import escapeRegex from "../escapeRegex";
10
+ import {seekNext} from "../positioning";
11
+
12
+ const $ = Inputmask.dependencyLib;
8
13
 
9
14
  function autoEscape(txt, opts) {
10
- var escapedTxt = "";
11
- for (var i = 0; i < txt.length; i++) {
12
- if (Inputmask.prototype.definitions[txt.charAt(i)] ||
13
- opts.definitions[txt.charAt(i)] ||
14
- opts.optionalmarker[0] === txt.charAt(i) ||
15
- opts.optionalmarker[1] === txt.charAt(i) ||
16
- opts.quantifiermarker[0] === txt.charAt(i) ||
17
- opts.quantifiermarker[1] === txt.charAt(i) ||
18
- opts.groupmarker[0] === txt.charAt(i) ||
19
- opts.groupmarker[1] === txt.charAt(i) ||
20
- opts.alternatormarker === txt.charAt(i)) {
21
- escapedTxt += "\\" + txt.charAt(i);
22
- } else {
23
- escapedTxt += txt.charAt(i);
24
- }
25
- }
26
- return escapedTxt;
15
+ var escapedTxt = "";
16
+ for (var i = 0; i < txt.length; i++) {
17
+ if (Inputmask.prototype.definitions[txt.charAt(i)] ||
18
+ opts.definitions[txt.charAt(i)] ||
19
+ opts.optionalmarker[0] === txt.charAt(i) ||
20
+ opts.optionalmarker[1] === txt.charAt(i) ||
21
+ opts.quantifiermarker[0] === txt.charAt(i) ||
22
+ opts.quantifiermarker[1] === txt.charAt(i) ||
23
+ opts.groupmarker[0] === txt.charAt(i) ||
24
+ opts.groupmarker[1] === txt.charAt(i) ||
25
+ opts.alternatormarker === txt.charAt(i)) {
26
+ escapedTxt += "\\" + txt.charAt(i);
27
+ } else {
28
+ escapedTxt += txt.charAt(i);
29
+ }
30
+ }
31
+ return escapedTxt;
27
32
  }
28
33
 
29
34
  function alignDigits(buffer, digits, opts, force) {
30
- if (buffer.length > 0 && digits > 0 && (!opts.digitsOptional || force)) {
31
- var radixPosition = $.inArray(opts.radixPoint, buffer);
32
- if (radixPosition === -1) {
33
- buffer.push(opts.radixPoint);
34
- radixPosition = buffer.length - 1;
35
- }
36
- for (var i = 1; i <= digits; i++) {
37
- if (!isFinite(buffer[radixPosition + i])) {
38
- buffer[radixPosition + i] = "0";
39
- }
40
- }
41
- }
42
-
43
-
44
- return buffer;
35
+ if (buffer.length > 0 && digits > 0 && (!opts.digitsOptional || force)) {
36
+ var radixPosition = buffer.indexOf(opts.radixPoint), negationBack = false;
37
+ if (opts.negationSymbol.back === buffer[buffer.length - 1]) {
38
+ negationBack = true;
39
+ buffer.length--;
40
+ }
41
+
42
+ if (radixPosition === -1) {
43
+ buffer.push(opts.radixPoint);
44
+ radixPosition = buffer.length - 1;
45
+ }
46
+ for (var i = 1; i <= digits; i++) {
47
+ if (!isFinite(buffer[radixPosition + i])) {
48
+ buffer[radixPosition + i] = "0";
49
+ }
50
+ }
51
+ }
52
+
53
+ if (negationBack)
54
+ buffer.push(opts.negationSymbol.back);
55
+ return buffer;
45
56
  }
46
57
 
47
58
  function findValidator(symbol, maskset) {
48
- var posNdx = 0;
49
- if (symbol === "+") {
50
- for (posNdx in maskset.validPositions);
51
- posNdx = parseInt(posNdx);
52
- }
53
- for (var tstNdx in maskset.tests) {
54
- tstNdx = parseInt(tstNdx);
55
- if (tstNdx >= posNdx) {
56
- for (var ndx = 0, ndxl = maskset.tests[tstNdx].length; ndx < ndxl; ndx++) {
57
- if ((maskset.validPositions[tstNdx] === undefined || symbol === "-") && maskset.tests[tstNdx][ndx].match.def === symbol) {
58
- return tstNdx + ((maskset.validPositions[tstNdx] !== undefined && symbol !== "-") ? 1 : 0);
59
- }
60
- }
61
- }
62
- }
63
- return posNdx;
59
+ var posNdx = 0;
60
+ if (symbol === "+") {
61
+ for (posNdx in maskset.validPositions) ;
62
+ posNdx = seekNext.call(this, parseInt(posNdx));
63
+ }
64
+ for (var tstNdx in maskset.tests) {
65
+ tstNdx = parseInt(tstNdx);
66
+ if (tstNdx >= posNdx) {
67
+ for (var ndx = 0, ndxl = maskset.tests[tstNdx].length; ndx < ndxl; ndx++) {
68
+ if ((maskset.validPositions[tstNdx] === undefined || symbol === "-") && maskset.tests[tstNdx][ndx].match.def === symbol) {
69
+ return tstNdx + ((maskset.validPositions[tstNdx] !== undefined && symbol !== "-") ? 1 : 0);
70
+ }
71
+ }
72
+ }
73
+ }
74
+ return posNdx;
64
75
  }
65
76
 
66
77
  function findValid(symbol, maskset) {
67
- var ret = -1;
68
- $.each(maskset.validPositions, function (ndx, tst) {
69
- if (tst && tst.match.def === symbol) {
70
- ret = parseInt(ndx);
71
- return false;
72
- }
73
- });
74
- return ret;
78
+ var ret = -1;
79
+ for (let ndx in maskset.validPositions) {
80
+ let tst = maskset.validPositions[ndx];
81
+ if (tst && tst.match.def === symbol) {
82
+ ret = parseInt(ndx);
83
+ break;
84
+ }
85
+ }
86
+ return ret;
75
87
  }
76
88
 
77
89
  function parseMinMaxOptions(opts) {
78
- if (opts.parseMinMaxOptions === undefined) {
79
- // convert min and max options
80
- if (opts.min !== null) {
81
- opts.min = opts.min.toString().replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
82
- if (opts.radixPoint === ",") opts.min = opts.min.replace(opts.radixPoint, ".");
83
- opts.min = isFinite(opts.min) ? parseFloat(opts.min) : NaN;
84
- if (isNaN(opts.min)) opts.min = Number.MIN_VALUE;
85
- }
86
- if (opts.max !== null) {
87
- opts.max = opts.max.toString().replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
88
- if (opts.radixPoint === ",") opts.max = opts.max.replace(opts.radixPoint, ".");
89
- opts.max = isFinite(opts.max) ? parseFloat(opts.max) : NaN;
90
- if (isNaN(opts.max)) opts.max = Number.MAX_VALUE;
91
- }
92
- opts.parseMinMaxOptions = "done";
93
- }
90
+ if (opts.parseMinMaxOptions === undefined) {
91
+ // convert min and max options
92
+ if (opts.min !== null) {
93
+ opts.min = opts.min.toString().replace(new RegExp(escapeRegex(opts.groupSeparator), "g"), "");
94
+ if (opts.radixPoint === ",") opts.min = opts.min.replace(opts.radixPoint, ".");
95
+ opts.min = isFinite(opts.min) ? parseFloat(opts.min) : NaN;
96
+ if (isNaN(opts.min)) opts.min = Number.MIN_VALUE;
97
+ }
98
+ if (opts.max !== null) {
99
+ opts.max = opts.max.toString().replace(new RegExp(escapeRegex(opts.groupSeparator), "g"), "");
100
+ if (opts.radixPoint === ",") opts.max = opts.max.replace(opts.radixPoint, ".");
101
+ opts.max = isFinite(opts.max) ? parseFloat(opts.max) : NaN;
102
+ if (isNaN(opts.max)) opts.max = Number.MAX_VALUE;
103
+ }
104
+ opts.parseMinMaxOptions = "done";
105
+ }
94
106
  }
95
107
 
96
108
  function genMask(opts) {
97
- opts.repeat = 0;
98
- //treat equal separator and radixpoint
99
- if (opts.groupSeparator === opts.radixPoint && opts.digits && opts.digits !== "0") {
100
- if (opts.radixPoint === ".") {
101
- opts.groupSeparator = ",";
102
- } else if (opts.radixPoint === ",") {
103
- opts.groupSeparator = ".";
104
- } else {
105
- opts.groupSeparator = "";
106
- }
107
- }
108
- //prevent conflict with default skipOptionalPartCharacter
109
- if (opts.groupSeparator === " ") {
110
- opts.skipOptionalPartCharacter = undefined;
111
- }
112
-
113
- //enforce placeholder to single
114
- if (opts.placeholder.length > 1) {
115
- opts.placeholder = opts.placeholder.charAt(0);
116
- }
117
- //only allow radixfocus when placeholder = 0
118
- if (opts.positionCaretOnClick === "radixFocus" && opts.placeholder === "") {
119
- opts.positionCaretOnClick = "lvp";
120
- }
121
-
122
- var decimalDef = "0", radixPointDef = opts.radixPoint;
123
- if (opts.numericInput === true && opts.__financeInput === undefined) { //finance people input style
124
- decimalDef = "1";
125
- opts.positionCaretOnClick = opts.positionCaretOnClick === "radixFocus" ? "lvp" : opts.positionCaretOnClick;
126
- opts.digitsOptional = false;
127
- if (isNaN(opts.digits)) opts.digits = 2;
128
- opts._radixDance = false;
129
- radixPointDef = (opts.radixPoint === "," ? "?" : "!");
130
- if (opts.radixPoint !== "" && opts.definitions[radixPointDef] === undefined) {
131
- //update separator definition
132
- opts.definitions[radixPointDef] = {};
133
- opts.definitions[radixPointDef].validator = "[" + opts.radixPoint + "]";
134
- opts.definitions[radixPointDef].placeholder = opts.radixPoint;
135
- opts.definitions[radixPointDef].static = true;
136
- opts.definitions[radixPointDef].generated = true; //forced marker as generated input
137
- }
138
- } else {
139
- opts.__financeInput = false; //needed to keep original selection when remasking
140
- opts.numericInput = true;
141
- }
142
-
143
- var mask = "[+]", altMask;
144
- mask += autoEscape(opts.prefix, opts);
145
- if (opts.groupSeparator !== "") {
146
- if (opts.definitions[opts.groupSeparator] === undefined) {
147
- //update separatot definition
148
- opts.definitions[opts.groupSeparator] = {};
149
- opts.definitions[opts.groupSeparator].validator = "[" + opts.groupSeparator + "]";
150
- opts.definitions[opts.groupSeparator].placeholder = opts.groupSeparator;
151
- opts.definitions[opts.groupSeparator].static = true;
152
- opts.definitions[opts.groupSeparator].generated = true; //forced marker as generated input
153
- }
154
- mask += opts._mask(opts);
155
- } else {
156
- mask += "9{+}";
157
- }
158
- if (opts.digits !== undefined && opts.digits !== 0) {
159
- var dq = opts.digits.toString().split(",");
160
- if (isFinite(dq[0]) && dq[1] && isFinite(dq[1])) {
161
- mask += radixPointDef + decimalDef + "{" + opts.digits + "}";
162
- } else if (isNaN(opts.digits) || parseInt(opts.digits) > 0) {
163
- if (opts.digitsOptional) {
164
- altMask = mask + radixPointDef + decimalDef + "{0," + opts.digits + "}";
165
- // mask += "[" + opts.radixPoint + "]";
166
- opts.keepStatic = true;
167
- } else {
168
- mask += radixPointDef + decimalDef + "{" + opts.digits + "}";
169
- }
170
- }
171
- }
172
- mask += autoEscape(opts.suffix, opts);
173
- mask += "[-]";
174
-
175
- if (altMask) {
176
- mask = [(altMask + autoEscape(opts.suffix, opts) + "[-]"), mask];
177
- }
178
-
179
-
180
- opts.greedy = false; //enforce greedy false
181
-
182
- parseMinMaxOptions(opts);
183
-
184
- // console.log(mask);
185
- return mask;
109
+ opts.repeat = 0;
110
+ //treat equal separator and radixpoint
111
+ if (opts.groupSeparator === opts.radixPoint && opts.digits && opts.digits !== "0") {
112
+ if (opts.radixPoint === ".") {
113
+ opts.groupSeparator = ",";
114
+ } else if (opts.radixPoint === ",") {
115
+ opts.groupSeparator = ".";
116
+ } else {
117
+ opts.groupSeparator = "";
118
+ }
119
+ }
120
+ //prevent conflict with default skipOptionalPartCharacter
121
+ if (opts.groupSeparator === " ") {
122
+ opts.skipOptionalPartCharacter = undefined;
123
+ }
124
+
125
+ //enforce placeholder to single
126
+ if (opts.placeholder.length > 1) {
127
+ opts.placeholder = opts.placeholder.charAt(0);
128
+ }
129
+ //only allow radixfocus when placeholder = 0
130
+ if (opts.positionCaretOnClick === "radixFocus" && opts.placeholder === "") {
131
+ opts.positionCaretOnClick = "lvp";
132
+ }
133
+
134
+ var decimalDef = "0", radixPointDef = opts.radixPoint;
135
+ if (opts.numericInput === true && opts.__financeInput === undefined) { //finance people input style
136
+ decimalDef = "1";
137
+ opts.positionCaretOnClick = opts.positionCaretOnClick === "radixFocus" ? "lvp" : opts.positionCaretOnClick;
138
+ opts.digitsOptional = false;
139
+ if (isNaN(opts.digits)) opts.digits = 2;
140
+ opts._radixDance = false;
141
+ radixPointDef = (opts.radixPoint === "," ? "?" : "!");
142
+ if (opts.radixPoint !== "" && opts.definitions[radixPointDef] === undefined) {
143
+ //update separator definition
144
+ opts.definitions[radixPointDef] = {};
145
+ opts.definitions[radixPointDef].validator = "[" + opts.radixPoint + "]";
146
+ opts.definitions[radixPointDef].placeholder = opts.radixPoint;
147
+ opts.definitions[radixPointDef].static = true;
148
+ opts.definitions[radixPointDef].generated = true; //forced marker as generated input
149
+ }
150
+ } else {
151
+ opts.__financeInput = false; //needed to keep original selection when remasking
152
+ opts.numericInput = true;
153
+ }
154
+
155
+ var mask = "[+]", altMask;
156
+ mask += autoEscape(opts.prefix, opts);
157
+ if (opts.groupSeparator !== "") {
158
+ if (opts.definitions[opts.groupSeparator] === undefined) {
159
+ //update separatot definition
160
+ opts.definitions[opts.groupSeparator] = {};
161
+ opts.definitions[opts.groupSeparator].validator = "[" + opts.groupSeparator + "]";
162
+ opts.definitions[opts.groupSeparator].placeholder = opts.groupSeparator;
163
+ opts.definitions[opts.groupSeparator].static = true;
164
+ opts.definitions[opts.groupSeparator].generated = true; //forced marker as generated input
165
+ }
166
+ mask += opts._mask(opts);
167
+ } else {
168
+ mask += "9{+}";
169
+ }
170
+ if (opts.digits !== undefined && opts.digits !== 0) {
171
+ var dq = opts.digits.toString().split(",");
172
+ if (isFinite(dq[0]) && dq[1] && isFinite(dq[1])) {
173
+ mask += radixPointDef + decimalDef + "{" + opts.digits + "}";
174
+ } else if (isNaN(opts.digits) || parseInt(opts.digits) > 0) {
175
+ if (opts.digitsOptional || opts.jitMasking) {
176
+ altMask = mask + radixPointDef + decimalDef + "{0," + opts.digits + "}";
177
+ // mask += "[" + opts.radixPoint + "]";
178
+ opts.keepStatic = true;
179
+ } else {
180
+ mask += radixPointDef + decimalDef + "{" + opts.digits + "}";
181
+ }
182
+ }
183
+ } else {
184
+ opts.inputmode = "numeric";
185
+ }
186
+ mask += autoEscape(opts.suffix, opts);
187
+ mask += "[-]";
188
+
189
+ if (altMask) {
190
+ mask = [(altMask + autoEscape(opts.suffix, opts) + "[-]"), mask];
191
+ }
192
+
193
+
194
+ opts.greedy = false; //enforce greedy false
195
+
196
+ parseMinMaxOptions(opts);
197
+ if (opts.radixPoint !== "")
198
+ opts.substitutes[opts.radixPoint == "." ? "," : "."] = opts.radixPoint;
199
+ // console.log(mask);
200
+ return mask;
186
201
  }
187
202
 
188
203
  function hanndleRadixDance(pos, c, radixPos, maskset, opts) {
189
- if (opts._radixDance && opts.numericInput && c !== opts.negationSymbol.back) {
190
- if (pos <= radixPos && (radixPos > 0 || c == opts.radixPoint) && (maskset.validPositions[pos - 1] === undefined || maskset.validPositions[pos - 1].input !== opts.negationSymbol.back)) {
191
- pos -= 1;
192
- }
193
- }
194
- return pos;
204
+ if (opts._radixDance && opts.numericInput && c !== opts.negationSymbol.back) {
205
+ if (pos <= radixPos && (radixPos > 0 || c == opts.radixPoint) && (maskset.validPositions[pos - 1] === undefined || maskset.validPositions[pos - 1].input !== opts.negationSymbol.back)) {
206
+ pos -= 1;
207
+ }
208
+ }
209
+ return pos;
195
210
  }
196
211
 
197
212
  function decimalValidator(chrs, maskset, pos, strict, opts) {
198
- var radixPos = maskset.buffer ? maskset.buffer.indexOf(opts.radixPoint) : -1,
199
- result = radixPos !== -1 && new RegExp("[0-9\uFF11-\uFF19]").test(chrs);
200
- if (opts._radixDance && result && maskset.validPositions[radixPos] == undefined) {
201
- return {
202
- insert: {
203
- pos: radixPos === pos ? radixPos + 1 : radixPos,
204
- c: opts.radixPoint
205
- },
206
- pos: pos
207
- };
208
- }
209
-
210
- return result;
213
+ var radixPos = maskset.buffer ? maskset.buffer.indexOf(opts.radixPoint) : -1,
214
+ result = (radixPos !== -1 || (strict && opts.jitMasking)) && new RegExp(opts.definitions["9"].validator).test(chrs);
215
+ if (opts._radixDance && radixPos !== -1 && result && maskset.validPositions[radixPos] == undefined) {
216
+ return {
217
+ insert: {
218
+ pos: radixPos === pos ? radixPos + 1 : radixPos,
219
+ c: opts.radixPoint
220
+ },
221
+ pos: pos
222
+ };
223
+ }
224
+
225
+ return result;
211
226
  }
212
227
 
213
228
  function checkForLeadingZeroes(buffer, opts) {
214
- //check leading zeros
215
- var numberMatches = new RegExp("(^" + (opts.negationSymbol.front !== "" ? Inputmask.escapeRegex(opts.negationSymbol.front) + "?" : "") + Inputmask.escapeRegex(opts.prefix) + ")(.*)(" + Inputmask.escapeRegex(opts.suffix) + (opts.negationSymbol.back != "" ? Inputmask.escapeRegex(opts.negationSymbol.back) + "?" : "") + "$)").exec(buffer.slice().reverse().join("")),
216
- number = numberMatches ? numberMatches[2] : "", leadingzeroes = false;
217
- if (number) {
218
- number = number.split(opts.radixPoint.charAt(0))[0];
219
- leadingzeroes = new RegExp("^[0" + opts.groupSeparator + "]*").exec(number);
220
- }
221
- return leadingzeroes && (leadingzeroes[0].length > 1 || leadingzeroes[0].length > 0 && leadingzeroes[0].length < number.length) ? leadingzeroes : false;
229
+ //check leading zeros
230
+ var numberMatches = new RegExp("(^" + (opts.negationSymbol.front !== "" ? escapeRegex(opts.negationSymbol.front) + "?" : "") + escapeRegex(opts.prefix) + ")(.*)(" + escapeRegex(opts.suffix) + (opts.negationSymbol.back != "" ? escapeRegex(opts.negationSymbol.back) + "?" : "") + "$)").exec(buffer.slice().reverse().join("")),
231
+ number = numberMatches ? numberMatches[2] : "", leadingzeroes = false;
232
+ if (number) {
233
+ number = number.split(opts.radixPoint.charAt(0))[0];
234
+ leadingzeroes = new RegExp("^[0" + opts.groupSeparator + "]*").exec(number);
235
+ }
236
+ return leadingzeroes && (leadingzeroes[0].length > 1 || leadingzeroes[0].length > 0 && leadingzeroes[0].length < number.length) ? leadingzeroes : false;
222
237
  }
223
238
 
224
239
  //number aliases
225
240
  Inputmask.extendAliases({
226
- "numeric": {
227
- mask: genMask,
228
- _mask: function (opts) {
229
- return "(" + opts.groupSeparator + "999){+|1}";
230
- },
231
- digits: "*", //number of fractionalDigits
232
- digitsOptional: true,
233
- enforceDigitsOnBlur: false,
234
- radixPoint: ".",
235
- positionCaretOnClick: "radixFocus",
236
- _radixDance: true,
237
- groupSeparator: "",
238
- allowMinus: true,
239
- negationSymbol: {
240
- front: "-", //"("
241
- back: "" //")"
242
- },
243
- prefix: "",
244
- suffix: "",
245
- min: null, //minimum value
246
- max: null, //maximum value
247
- step: 1,
248
- unmaskAsNumber: false,
249
- roundingFN: Math.round, //Math.floor , fn(x)
250
- inputmode: "numeric",
251
- shortcuts: { k: "000", m: "000000" },
252
- //global options
253
- placeholder: "0",
254
- greedy: false,
255
- rightAlign: true,
256
- insertMode: true,
257
- autoUnmask: false,
258
- skipOptionalPartCharacter: "",
259
- definitions: {
260
- "0": {
261
- validator: decimalValidator
262
- },
263
- "1": {
264
- validator: decimalValidator,
265
- definitionSymbol: "9"
266
- },
267
- "+": {
268
- validator: function (chrs, maskset, pos, strict, opts) {
269
- return (opts.allowMinus && (chrs === "-" || chrs === opts.negationSymbol.front));
270
-
271
- }
272
- },
273
- "-": {
274
- validator: function (chrs, maskset, pos, strict, opts) {
275
- return (opts.allowMinus && chrs === opts.negationSymbol.back);
276
- }
277
- }
278
- },
279
- preValidation: function (buffer, pos, c, isSelection, opts, maskset, caretPos, strict) {
280
- if (opts.__financeInput !== false && c === opts.radixPoint) return false;
281
-
282
- var pattern;
283
- if ((pattern = (opts.shortcuts && opts.shortcuts[c]))) {
284
- if (pattern.length > 1) {
285
- var inserts = [];
286
- for (var i = 0; i < pattern.length; i++) {
287
- inserts.push({ pos: pos + i, c: pattern[i], strict: false });
288
- }
289
- }
290
- return {
291
- insert: inserts
292
- };
293
- }
294
-
295
- var radixPos = $.inArray(opts.radixPoint, buffer), initPos = pos;
296
- pos = hanndleRadixDance(pos, c, radixPos, maskset, opts);
297
- if (c === "-" || c === opts.negationSymbol.front) {
298
- if (opts.allowMinus !== true) return false;
299
- var isNegative = false,
300
- front = findValid("+", maskset), back = findValid("-", maskset);
301
- if (front !== -1) {
302
- isNegative = [front, back];
303
- }
304
-
305
- return isNegative !== false ? {
306
- remove: isNegative,
307
- caret: initPos
308
- } : {
309
- insert: [
310
- { pos: findValidator("+", maskset), c: opts.negationSymbol.front, fromIsValid: true },
311
- { pos: findValidator("-", maskset), c: opts.negationSymbol.back, fromIsValid: undefined }],
312
- caret: initPos + opts.negationSymbol.back.length
313
- };
314
- }
315
-
316
- if (strict) return true;
317
- if (radixPos !== -1 && (opts._radixDance === true && isSelection === false && c === opts.radixPoint && (opts.digits !== undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0)) && radixPos !== pos)) {
318
- return {
319
- "caret": opts._radixDance && pos === radixPos - 1 ? radixPos + 1 : radixPos
320
- };
321
- }
322
- if (opts.__financeInput === false) {
323
- if (isSelection) {
324
- if (opts.digitsOptional) {
325
- return { rewritePosition: caretPos.end };
326
- } else if (!opts.digitsOptional) {
327
- if (caretPos.begin > radixPos && caretPos.end <= radixPos) {
328
- if (c === opts.radixPoint) {
329
- return {
330
- insert: { pos: radixPos + 1, c: "0", fromIsValid: true },
331
- rewritePosition: radixPos
332
- };
333
- } else {
334
- return { rewritePosition: radixPos + 1 };
335
- }
336
- } else if (caretPos.begin < radixPos) {
337
- return { rewritePosition: caretPos.begin - 1 };
338
- }
339
- }
340
- } else if (!opts.showMaskOnHover && !opts.showMaskOnFocus && !opts.digitsOptional && opts.digits > 0 && this.inputmask.__valueGet.call(this) === "") {
341
- return { rewritePosition: radixPos };
342
- }
343
- }
344
- return { rewritePosition: pos };
345
- },
346
- postValidation: function (buffer, pos, c, currentResult, opts, maskset, strict) {
347
- if (currentResult === false) return currentResult;
348
- if (strict) return true;
349
- if (opts.min !== null || opts.max !== null) {
350
- var unmasked = opts.onUnMask(buffer.slice().reverse().join(""), undefined, $.extend({}, opts, {
351
- unmaskAsNumber: true
352
- }));
353
- if (opts.min !== null && unmasked < opts.min && (unmasked.toString().length >= opts.min.toString().length || unmasked < 0)) {
354
- return false;
355
- // return {
356
- // refreshFromBuffer: true,
357
- // buffer: alignDigits(opts.min.toString().replace(".", opts.radixPoint).split(""), opts.digits, opts).reverse()
358
- // };
359
- }
360
-
361
- if (opts.max !== null && unmasked > opts.max) {
362
- return false;
363
- // return {
364
- // refreshFromBuffer: true,
365
- // buffer: alignDigits(opts.max.toString().replace(".", opts.radixPoint).split(""), opts.digits, opts).reverse()
366
- // };
367
- }
368
- }
369
-
370
- return currentResult;
371
- },
372
- onUnMask: function (maskedValue, unmaskedValue, opts) {
373
- if (unmaskedValue === "" && opts.nullable === true) {
374
- return unmaskedValue;
375
- }
376
- var processValue = maskedValue.replace(opts.prefix, "");
377
- processValue = processValue.replace(opts.suffix, "");
378
- processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
379
- if (opts.placeholder.charAt(0) !== "") {
380
- processValue = processValue.replace(new RegExp(opts.placeholder.charAt(0), "g"), "0");
381
- }
382
- if (opts.unmaskAsNumber) {
383
- if (opts.radixPoint !== "" && processValue.indexOf(opts.radixPoint) !== -1) processValue = processValue.replace(Inputmask.escapeRegex.call(this, opts.radixPoint), ".");
384
- processValue = processValue.replace(new RegExp("^" + Inputmask.escapeRegex(opts.negationSymbol.front)), "-");
385
- processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
386
- return Number(processValue);
387
- }
388
- return processValue;
389
- }
390
- ,
391
- isComplete: function (buffer, opts) {
392
- var maskedValue = (opts.numericInput ? buffer.slice().reverse() : buffer).join("");
393
- maskedValue = maskedValue.replace(new RegExp("^" + Inputmask.escapeRegex(opts.negationSymbol.front)), "-");
394
- maskedValue = maskedValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
395
- maskedValue = maskedValue.replace(opts.prefix, "");
396
- maskedValue = maskedValue.replace(opts.suffix, "");
397
- maskedValue = maskedValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator) + "([0-9]{3})", "g"), "$1");
398
- if (opts.radixPoint === ",") maskedValue = maskedValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
399
- return isFinite(maskedValue);
400
- }
401
- ,
402
- onBeforeMask: function (initialValue, opts) {
403
- var radixPoint = opts.radixPoint || ",";
404
- if (isFinite(opts.digits)) opts.digits = parseInt(opts.digits);
405
-
406
- if ((typeof initialValue == "number" || opts.inputType === "number") && radixPoint !== "") {
407
- initialValue = initialValue.toString().replace(".", radixPoint);
408
- }
409
-
410
- var valueParts = initialValue.split(radixPoint),
411
- integerPart = valueParts[0].replace(/[^\-0-9]/g, ""),
412
- decimalPart = valueParts.length > 1 ? valueParts[1].replace(/[^0-9]/g, "") : "",
413
- forceDigits = valueParts.length > 1;
414
-
415
- initialValue = integerPart + (decimalPart !== "" ? radixPoint + decimalPart : decimalPart);
416
-
417
- var digits = 0;
418
- if (radixPoint !== "") {
419
- digits = !opts.digitsOptional ? opts.digits : (opts.digits < decimalPart.length ? opts.digits : decimalPart.length);
420
- if (decimalPart !== "" || !opts.digitsOptional) {
421
- var digitsFactor = Math.pow(10, digits || 1);
422
-
423
- //make the initialValue a valid javascript number for the parsefloat
424
- initialValue = initialValue.replace(Inputmask.escapeRegex(radixPoint), ".");
425
- if (!isNaN(parseFloat(initialValue))) {
426
- initialValue = (opts.roundingFN(parseFloat(initialValue) * digitsFactor) / digitsFactor).toFixed(digits);
427
- }
428
- initialValue = initialValue.toString().replace(".", radixPoint);
429
- }
430
- }
431
- //this needs to be in a separate part and not directly in decimalPart to allow rounding
432
- if (opts.digits === 0 && initialValue.indexOf(radixPoint) !== -1) {
433
- initialValue = initialValue.substring(0, initialValue.indexOf(radixPoint));
434
- }
435
-
436
- if (opts.min !== null || opts.max !== null) {
437
- var numberValue = initialValue.toString().replace(radixPoint, ".");
438
- if (opts.min !== null && numberValue < opts.min) {
439
- initialValue = opts.min.toString().replace(".", radixPoint);
440
- } else if (opts.max !== null && numberValue > opts.max) {
441
- initialValue = opts.max.toString().replace(".", radixPoint);
442
- }
443
- }
444
-
445
- return alignDigits(initialValue.toString().split(""), digits, opts, forceDigits).join("");
446
- }
447
- ,
448
- onBeforeWrite: function (e, buffer, caretPos, opts) {
449
- function stripBuffer(buffer, stripRadix) {
450
- if (opts.__financeInput !== false || stripRadix) {
451
- var position = $.inArray(opts.radixPoint, buffer);
452
- if (position !== -1) {
453
- buffer.splice(position, 1);
454
- }
455
- }
456
- if (opts.groupSeparator !== "") {
457
- while ((position = buffer.indexOf(opts.groupSeparator)) !== -1) {
458
- buffer.splice(position, 1);
459
- }
460
- }
461
-
462
- return buffer;
463
- }
464
-
465
- var result,
466
- leadingzeroes = checkForLeadingZeroes(buffer, opts);
467
-
468
- if (leadingzeroes) {
469
- var buf = buffer.slice().reverse(), caretNdx = buf.join("").indexOf(leadingzeroes[0]);
470
- buf.splice(caretNdx, leadingzeroes[0].length);
471
- var newCaretPos = buf.length - caretNdx;
472
- stripBuffer(buf);
473
- result = {
474
- refreshFromBuffer: true,
475
- buffer: buf.reverse(),
476
- caret: caretPos < newCaretPos ? caretPos : newCaretPos
477
- };
478
- }
479
-
480
- if (e) {
481
- switch (e.type) {
482
- case "blur":
483
- case "checkval":
484
- if (opts.min !== null) {
485
- var unmasked = opts.onUnMask(buffer.slice().reverse().join(""), undefined, $.extend({}, opts, {
486
- unmaskAsNumber: true
487
- }));
488
- if (opts.min !== null && unmasked < opts.min) {
489
- return {
490
- refreshFromBuffer: true,
491
- buffer: alignDigits(opts.min.toString().replace(".", opts.radixPoint).split(""), opts.digits, opts).reverse()
492
- };
493
- }
494
- }
495
- if (buffer[buffer.length - 1] === opts.negationSymbol.front) { //strip negation symbol on blur when value is 0
496
- var nmbrMtchs = new RegExp("(^" + (opts.negationSymbol.front != "" ? Inputmask.escapeRegex(opts.negationSymbol.front) + "?" : "") + Inputmask.escapeRegex(opts.prefix) + ")(.*)(" + Inputmask.escapeRegex(opts.suffix) + (opts.negationSymbol.back != "" ? Inputmask.escapeRegex(opts.negationSymbol.back) + "?" : "") + "$)").exec(stripBuffer(buffer.slice(), true).reverse().join("")),
497
- number = nmbrMtchs ? nmbrMtchs[2] : "";
498
- if (number == 0) {
499
- result = { refreshFromBuffer: true, buffer: [0] };
500
- }
501
- } else if (opts.radixPoint !== "" && buffer[0] === opts.radixPoint) { //strip radixpoint on blur when it is the latest char
502
- if (result && result.buffer) {
503
- result.buffer.shift();
504
- } else {
505
- buffer.shift();
506
- result =
507
- { refreshFromBuffer: true, buffer: stripBuffer(buffer) };
508
- }
509
- }
510
-
511
- if (opts.enforceDigitsOnBlur) {
512
- result = result || {};
513
- var bffr = (result && result.buffer) || buffer.slice().reverse();
514
- result.refreshFromBuffer = true;
515
- result.buffer = alignDigits(bffr, opts.digits, opts, true).reverse();
516
- }
517
- }
518
- }
519
-
520
-
521
- return result;
522
- },
523
- onKeyDown: function (e, buffer, caretPos, opts) {
524
- var $input = $(this), bffr;
525
- if (e.ctrlKey) {
526
- switch (e.keyCode) {
527
- case keyCode.UP:
528
- this.inputmask.__valueSet.call(this, parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step));
529
- $input.trigger("setvalue");
530
- return false;
531
- case keyCode.DOWN:
532
- this.inputmask.__valueSet.call(this, parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step));
533
- $input.trigger("setvalue");
534
- return false;
535
- }
536
- }
537
- if (!e.shiftKey && (e.keyCode === keyCode.DELETE || e.keyCode === keyCode.BACKSPACE || e.keyCode === keyCode.BACKSPACE_SAFARI) && caretPos.begin !== buffer.length) {
538
- if (buffer[e.keyCode === keyCode.DELETE ? caretPos.begin - 1 : caretPos.end] === opts.negationSymbol.front) {
539
- bffr = buffer.slice().reverse();
540
- if (opts.negationSymbol.front !== "") bffr.shift();
541
- if (opts.negationSymbol.back !== "") bffr.pop();
542
- $input.trigger("setvalue", [bffr.join(""), caretPos.begin]);
543
- return false;
544
- } else if (opts._radixDance === true) {
545
- var radixPos = $.inArray(opts.radixPoint, buffer);
546
- if (!opts.digitsOptional) {
547
- if (radixPos !== -1 && (caretPos.begin < radixPos || caretPos.end < radixPos || (e.keyCode === keyCode.DELETE && caretPos.begin === radixPos))) {
548
- if (caretPos.begin === caretPos.end && (e.keyCode === keyCode.BACKSPACE || e.keyCode === keyCode.BACKSPACE_SAFARI)) { //only adjust when not a selection
549
- caretPos.begin++;
550
- }
551
- bffr = buffer.slice().reverse();
552
- bffr.splice(bffr.length - caretPos.begin, caretPos.begin - caretPos.end + 1);
553
- // console.log(caretPos);
554
- bffr = alignDigits(bffr, opts.digits, opts).join("");
555
- $input.trigger("setvalue", [bffr, caretPos.begin >= bffr.length ? radixPos + 1 : caretPos.begin]);
556
- return false;
557
- }
558
- } else if (radixPos === 0) {
559
- bffr = buffer.slice().reverse();
560
- bffr.pop();
561
- $input.trigger("setvalue", [bffr.join(""), caretPos.begin >= bffr.length ? bffr.length : caretPos.begin]);
562
- return false;
563
- }
564
- }
565
- }
566
- }
567
- },
568
- "currency": {
569
- prefix: "", //"$ ",
570
- groupSeparator: ",",
571
- alias: "numeric",
572
- digits: 2,
573
- digitsOptional: false
574
- },
575
- "decimal": {
576
- alias: "numeric"
577
- },
578
- "integer": {
579
- alias: "numeric",
580
- digits: 0
581
- },
582
- "percentage": {
583
- alias: "numeric",
584
- min: 0,
585
- max: 100,
586
- suffix: " %",
587
- digits: 0,
588
- allowMinus: false
589
- },
590
- "indianns": { //indian numbering system
591
- alias: "numeric",
592
- _mask: function (opts) {
593
- return "(" + opts.groupSeparator + "99){*|1}(" + opts.groupSeparator + "999){1|1}";
594
- },
595
- groupSeparator: ",",
596
- radixPoint: ".",
597
- placeholder: "0",
598
- digits: 2,
599
- digitsOptional: false
600
- }
241
+ "numeric": {
242
+ mask: genMask,
243
+ _mask: function (opts) {
244
+ return "(" + opts.groupSeparator + "999){+|1}";
245
+ },
246
+ digits: "*", //number of fractionalDigits
247
+ digitsOptional: true,
248
+ enforceDigitsOnBlur: false,
249
+ radixPoint: ".",
250
+ positionCaretOnClick: "radixFocus",
251
+ _radixDance: true,
252
+ groupSeparator: "",
253
+ allowMinus: true,
254
+ negationSymbol: {
255
+ front: "-", //"("
256
+ back: "" //")"
257
+ },
258
+ prefix: "",
259
+ suffix: "",
260
+ min: null, //minimum value
261
+ max: null, //maximum value
262
+ SetMaxOnOverflow: false,
263
+ step: 1,
264
+ inputType: "text", //number ~ specify that values which are set are in textform (radix point is same as in the options) or in numberform (radixpoint = .)
265
+ unmaskAsNumber: false,
266
+ roundingFN: Math.round, //Math.floor , fn(x)
267
+ inputmode: "decimal",
268
+ shortcuts: {k: "1000", m: "1000000"},
269
+ //global options
270
+ placeholder: "0",
271
+ greedy: false,
272
+ rightAlign: true,
273
+ insertMode: true,
274
+ autoUnmask: false,
275
+ skipOptionalPartCharacter: "",
276
+ usePrototypeDefinitions: false,
277
+ stripLeadingZeroes: true,
278
+ definitions: {
279
+ "0": {
280
+ validator: decimalValidator
281
+ },
282
+ "1": {
283
+ validator: decimalValidator,
284
+ definitionSymbol: "9"
285
+ },
286
+ "9": { //\uFF11-\uFF19 #1606
287
+ validator: "[0-9\uFF10-\uFF19\u0660-\u0669\u06F0-\u06F9]",
288
+ definitionSymbol: "*"
289
+ },
290
+ "+": {
291
+ validator: function (chrs, maskset, pos, strict, opts) {
292
+ return (opts.allowMinus && (chrs === "-" || chrs === opts.negationSymbol.front));
293
+
294
+ }
295
+ },
296
+ "-": {
297
+ validator: function (chrs, maskset, pos, strict, opts) {
298
+ return (opts.allowMinus && chrs === opts.negationSymbol.back);
299
+ }
300
+ }
301
+ },
302
+ preValidation: function (buffer, pos, c, isSelection, opts, maskset, caretPos, strict) {
303
+ const inputmask = this;
304
+
305
+ if (opts.__financeInput !== false && c === opts.radixPoint) return false;
306
+ var radixPos = buffer.indexOf(opts.radixPoint), initPos = pos;
307
+ pos = hanndleRadixDance(pos, c, radixPos, maskset, opts);
308
+ if (c === "-" || c === opts.negationSymbol.front) {
309
+ if (opts.allowMinus !== true) return false;
310
+ var isNegative = false,
311
+ front = findValid("+", maskset), back = findValid("-", maskset);
312
+ if (front !== -1) {
313
+ isNegative = [front, back];
314
+ }
315
+
316
+ return isNegative !== false ? {
317
+ remove: isNegative,
318
+ caret: initPos - opts.negationSymbol.back.length
319
+ } : {
320
+ insert: [
321
+ {
322
+ pos: findValidator.call(inputmask, "+", maskset),
323
+ c: opts.negationSymbol.front,
324
+ fromIsValid: true
325
+ },
326
+ {
327
+ pos: findValidator.call(inputmask, "-", maskset),
328
+ c: opts.negationSymbol.back,
329
+ fromIsValid: undefined
330
+ }],
331
+ caret: initPos + opts.negationSymbol.back.length
332
+ };
333
+ }
334
+
335
+ if (c === opts.groupSeparator) {
336
+ return {caret: initPos};
337
+ }
338
+
339
+ if (strict) return true;
340
+ if (radixPos !== -1 && (opts._radixDance === true && isSelection === false && c === opts.radixPoint && (opts.digits !== undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0)) && radixPos !== pos)) {
341
+ return {
342
+ "caret": opts._radixDance && pos === radixPos - 1 ? radixPos + 1 : radixPos
343
+ };
344
+ }
345
+ if (opts.__financeInput === false) {
346
+ if (isSelection) {
347
+ if (opts.digitsOptional) {
348
+ return {rewritePosition: caretPos.end};
349
+ } else if (!opts.digitsOptional) {
350
+ if (caretPos.begin > radixPos && caretPos.end <= radixPos) {
351
+ if (c === opts.radixPoint) {
352
+ return {
353
+ insert: {pos: radixPos + 1, c: "0", fromIsValid: true},
354
+ rewritePosition: radixPos
355
+ };
356
+ } else {
357
+ return {rewritePosition: radixPos + 1};
358
+ }
359
+ } else if (caretPos.begin < radixPos) {
360
+ return {rewritePosition: caretPos.begin - 1};
361
+ }
362
+ }
363
+ } else if (!opts.showMaskOnHover && !opts.showMaskOnFocus && !opts.digitsOptional && opts.digits > 0 && this.__valueGet.call(this.el) === "") {
364
+ return {rewritePosition: radixPos};
365
+ }
366
+ }
367
+ return {rewritePosition: pos};
368
+ },
369
+ postValidation: function (buffer, pos, c, currentResult, opts, maskset, strict) {
370
+ if (currentResult === false) return currentResult;
371
+ if (strict) return true;
372
+ if (opts.min !== null || opts.max !== null) {
373
+ var unmasked = opts.onUnMask(buffer.slice().reverse().join(""), undefined, $.extend({}, opts, {
374
+ unmaskAsNumber: true
375
+ }));
376
+ if (opts.min !== null && unmasked < opts.min && (unmasked.toString().length > opts.min.toString().length || unmasked < 0)) {
377
+ return false;
378
+ // return {
379
+ // refreshFromBuffer: true,
380
+ // buffer: alignDigits(opts.min.toString().replace(".", opts.radixPoint).split(""), opts.digits, opts).reverse()
381
+ // };
382
+ }
383
+
384
+ if (opts.max !== null && unmasked > opts.max) {
385
+ return opts.SetMaxOnOverflow ? {
386
+ refreshFromBuffer: true,
387
+ buffer: alignDigits(opts.max.toString().replace(".", opts.radixPoint).split(""), opts.digits, opts).reverse()
388
+ } : false;
389
+ }
390
+ }
391
+
392
+ return currentResult;
393
+ },
394
+ onUnMask: function (maskedValue, unmaskedValue, opts) {
395
+ if (unmaskedValue === "" && opts.nullable === true) {
396
+ return unmaskedValue;
397
+ }
398
+ var processValue = maskedValue.replace(opts.prefix, "");
399
+ processValue = processValue.replace(opts.suffix, "");
400
+ processValue = processValue.replace(new RegExp(escapeRegex(opts.groupSeparator), "g"), "");
401
+ if (opts.placeholder.charAt(0) !== "") {
402
+ processValue = processValue.replace(new RegExp(opts.placeholder.charAt(0), "g"), "0");
403
+ }
404
+ if (opts.unmaskAsNumber) {
405
+ if (opts.radixPoint !== "" && processValue.indexOf(opts.radixPoint) !== -1) processValue = processValue.replace(escapeRegex.call(this, opts.radixPoint), ".");
406
+ processValue = processValue.replace(new RegExp("^" + escapeRegex(opts.negationSymbol.front)), "-");
407
+ processValue = processValue.replace(new RegExp(escapeRegex(opts.negationSymbol.back) + "$"), "");
408
+ return Number(processValue);
409
+ }
410
+ return processValue;
411
+ }
412
+ ,
413
+ isComplete: function (buffer, opts) {
414
+ var maskedValue = (opts.numericInput ? buffer.slice().reverse() : buffer).join("");
415
+ maskedValue = maskedValue.replace(new RegExp("^" + escapeRegex(opts.negationSymbol.front)), "-");
416
+ maskedValue = maskedValue.replace(new RegExp(escapeRegex(opts.negationSymbol.back) + "$"), "");
417
+ maskedValue = maskedValue.replace(opts.prefix, "");
418
+ maskedValue = maskedValue.replace(opts.suffix, "");
419
+ maskedValue = maskedValue.replace(new RegExp(escapeRegex(opts.groupSeparator) + "([0-9]{3})", "g"), "$1");
420
+ if (opts.radixPoint === ",") maskedValue = maskedValue.replace(escapeRegex(opts.radixPoint), ".");
421
+ return isFinite(maskedValue);
422
+ },
423
+ onBeforeMask: function (initialValue, opts) {
424
+ var radixPoint = opts.radixPoint || ",";
425
+ if (isFinite(opts.digits)) opts.digits = parseInt(opts.digits);
426
+
427
+ if ((typeof initialValue == "number" || opts.inputType === "number") && radixPoint !== "") {
428
+ initialValue = initialValue.toString().replace(".", radixPoint);
429
+ }
430
+ var isNagtive = initialValue.charAt(0) === "-" || initialValue.charAt(0) === opts.negationSymbol.front;
431
+ var valueParts = initialValue.split(radixPoint),
432
+ integerPart = valueParts[0].replace(/[^\-0-9]/g, ""),
433
+ decimalPart = valueParts.length > 1 ? valueParts[1].replace(/[^0-9]/g, "") : "",
434
+ forceDigits = valueParts.length > 1;
435
+
436
+ initialValue = integerPart + (decimalPart !== "" ? radixPoint + decimalPart : decimalPart);
437
+
438
+ var digits = 0;
439
+ if (radixPoint !== "") {
440
+ digits = !opts.digitsOptional ? opts.digits : (opts.digits < decimalPart.length ? opts.digits : decimalPart.length);
441
+ if (decimalPart !== "" || !opts.digitsOptional) {
442
+ var digitsFactor = Math.pow(10, digits || 1);
443
+
444
+ //make the initialValue a valid javascript number for the parsefloat
445
+ initialValue = initialValue.replace(escapeRegex(radixPoint), ".");
446
+ if (!isNaN(parseFloat(initialValue))) {
447
+ initialValue = (opts.roundingFN(parseFloat(initialValue) * digitsFactor) / digitsFactor).toFixed(digits);
448
+ }
449
+ initialValue = initialValue.toString().replace(".", radixPoint);
450
+ }
451
+ }
452
+ //this needs to be in a separate part and not directly in decimalPart to allow rounding
453
+ if (opts.digits === 0 && initialValue.indexOf(radixPoint) !== -1) {
454
+ initialValue = initialValue.substring(0, initialValue.indexOf(radixPoint));
455
+ }
456
+
457
+ if (opts.min !== null || opts.max !== null) {
458
+ var numberValue = initialValue.toString().replace(radixPoint, ".");
459
+ if (opts.min !== null && numberValue < opts.min) {
460
+ initialValue = opts.min.toString().replace(".", radixPoint);
461
+ } else if (opts.max !== null && numberValue > opts.max) {
462
+ initialValue = opts.max.toString().replace(".", radixPoint);
463
+ }
464
+ }
465
+
466
+ if (isNagtive && initialValue.charAt(0) !== "-") {
467
+ initialValue = "-" + initialValue;
468
+ }
469
+ return alignDigits(initialValue.toString().split(""), digits, opts, forceDigits).join("");
470
+ }
471
+ ,
472
+ onBeforeWrite: function (e, buffer, caretPos, opts) {
473
+ function stripBuffer(buffer, stripRadix) {
474
+ if (opts.__financeInput !== false || stripRadix) {
475
+ var position = buffer.indexOf(opts.radixPoint);
476
+ if (position !== -1) {
477
+ buffer.splice(position, 1);
478
+ }
479
+ }
480
+ if (opts.groupSeparator !== "") {
481
+ while ((position = buffer.indexOf(opts.groupSeparator)) !== -1) {
482
+ buffer.splice(position, 1);
483
+ }
484
+ }
485
+
486
+ return buffer;
487
+ }
488
+
489
+ let result, leadingzeroes;
490
+ if (opts.stripLeadingZeroes && (leadingzeroes = checkForLeadingZeroes(buffer, opts))) {
491
+ const caretNdx = buffer.join("").lastIndexOf(leadingzeroes[0].split("").reverse().join("")) - (leadingzeroes[0] == leadingzeroes.input ? 0 : 1),
492
+ offset = (leadingzeroes[0] == leadingzeroes.input ? 1 : 0);
493
+ for (let i = leadingzeroes[0].length - offset; i > 0; i--) {
494
+ delete this.maskset.validPositions[caretNdx + i];
495
+ delete buffer[caretNdx + i];
496
+ }
497
+ }
498
+
499
+ if (e) {
500
+ switch (e.type) {
501
+ case "blur":
502
+ case "checkval":
503
+ if (opts.min !== null) {
504
+ var unmasked = opts.onUnMask(buffer.slice().reverse().join(""), undefined, $.extend({}, opts, {
505
+ unmaskAsNumber: true
506
+ }));
507
+ if (opts.min !== null && unmasked < opts.min) {
508
+ return {
509
+ refreshFromBuffer: true,
510
+ buffer: alignDigits(opts.min.toString().replace(".", opts.radixPoint).split(""), opts.digits, opts).reverse()
511
+ };
512
+ }
513
+ }
514
+ if (buffer[buffer.length - 1] === opts.negationSymbol.front) { //strip negation symbol on blur when value is 0
515
+ var nmbrMtchs = new RegExp("(^" + (opts.negationSymbol.front != "" ? escapeRegex(opts.negationSymbol.front) + "?" : "") + escapeRegex(opts.prefix) + ")(.*)(" + escapeRegex(opts.suffix) + (opts.negationSymbol.back != "" ? escapeRegex(opts.negationSymbol.back) + "?" : "") + "$)").exec(stripBuffer(buffer.slice(), true).reverse().join("")),
516
+ number = nmbrMtchs ? nmbrMtchs[2] : "";
517
+ if (number == 0) {
518
+ result = {refreshFromBuffer: true, buffer: [0]};
519
+ }
520
+ } else if (opts.radixPoint !== "") { //strip radixpoint on blur when it is the latest char
521
+ var radixNDX = buffer.indexOf(opts.radixPoint);
522
+ if (radixNDX === opts.suffix.length) {
523
+ if (result && result.buffer) {
524
+ result.buffer.splice(0, 1 + opts.suffix.length);
525
+ } else {
526
+ buffer.splice(0, 1 + opts.suffix.length);
527
+ result =
528
+ {refreshFromBuffer: true, buffer: stripBuffer(buffer)};
529
+ }
530
+ }
531
+ }
532
+
533
+ if (opts.enforceDigitsOnBlur) {
534
+ result = result || {};
535
+ var bffr = (result && result.buffer) || buffer.slice().reverse();
536
+ result.refreshFromBuffer = true;
537
+ result.buffer = alignDigits(bffr, opts.digits, opts, true).reverse();
538
+ }
539
+ }
540
+ }
541
+
542
+ return result;
543
+ },
544
+ onKeyDown: function (e, buffer, caretPos, opts) {
545
+ var $input = $(this), bffr;
546
+ var pattern, c = String.fromCharCode(e.keyCode).toLowerCase();
547
+ if ((pattern = (opts.shortcuts && opts.shortcuts[c]))) {
548
+ if (pattern.length > 1) {
549
+ this.inputmask.__valueSet.call(this, parseFloat(this.inputmask.unmaskedvalue()) * parseInt(pattern));
550
+ $input.trigger("setvalue");
551
+ return false;
552
+ }
553
+ }
554
+ if (e.ctrlKey) {
555
+ switch (e.keyCode) {
556
+ case keyCode.UP:
557
+ this.inputmask.__valueSet.call(this, parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step));
558
+ $input.trigger("setvalue");
559
+ return false;
560
+ case keyCode.DOWN:
561
+ this.inputmask.__valueSet.call(this, parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step));
562
+ $input.trigger("setvalue");
563
+ return false;
564
+ }
565
+ }
566
+ if (!e.shiftKey && (e.keyCode === keyCode.DELETE || e.keyCode === keyCode.BACKSPACE || e.keyCode === keyCode.BACKSPACE_SAFARI) && caretPos.begin !== buffer.length) {
567
+ if (buffer[e.keyCode === keyCode.DELETE ? caretPos.begin - 1 : caretPos.end] === opts.negationSymbol.front) {
568
+ bffr = buffer.slice().reverse();
569
+ if (opts.negationSymbol.front !== "") bffr.shift();
570
+ if (opts.negationSymbol.back !== "") bffr.pop();
571
+ $input.trigger("setvalue", [bffr.join(""), caretPos.begin]);
572
+ return false;
573
+ } else if (opts._radixDance === true) {
574
+ var radixPos = buffer.indexOf(opts.radixPoint);
575
+ if (!opts.digitsOptional) {
576
+ if (radixPos !== -1 && (caretPos.begin < radixPos || caretPos.end < radixPos || (e.keyCode === keyCode.DELETE && caretPos.begin === radixPos))) {
577
+ if (caretPos.begin === caretPos.end && (e.keyCode === keyCode.BACKSPACE || e.keyCode === keyCode.BACKSPACE_SAFARI)) { //only adjust when not a selection
578
+ caretPos.begin++;
579
+ }
580
+ bffr = buffer.slice().reverse();
581
+ bffr.splice(bffr.length - caretPos.begin, caretPos.begin - caretPos.end + 1);
582
+ // console.log(caretPos);
583
+ bffr = alignDigits(bffr, opts.digits, opts).join("");
584
+ $input.trigger("setvalue", [bffr, caretPos.begin >= bffr.length ? radixPos + 1 : caretPos.begin]);
585
+ return false;
586
+ }
587
+ } else if (radixPos === 0) {
588
+ bffr = buffer.slice().reverse();
589
+ bffr.pop();
590
+ $input.trigger("setvalue", [bffr.join(""), caretPos.begin >= bffr.length ? bffr.length : caretPos.begin]);
591
+ return false;
592
+ }
593
+ }
594
+ }
595
+ }
596
+ },
597
+ "currency": {
598
+ prefix: "", //"$ ",
599
+ groupSeparator: ",",
600
+ alias: "numeric",
601
+ digits: 2,
602
+ digitsOptional: false
603
+ },
604
+ "decimal": {
605
+ alias: "numeric"
606
+ },
607
+ "integer": {
608
+ alias: "numeric",
609
+ inputmode: "numeric",
610
+ digits: 0
611
+ },
612
+ "percentage": {
613
+ alias: "numeric",
614
+ min: 0,
615
+ max: 100,
616
+ suffix: " %",
617
+ digits: 0,
618
+ allowMinus: false
619
+ },
620
+ "indianns": { //indian numbering system
621
+ alias: "numeric",
622
+ _mask: function (opts) {
623
+ return "(" + opts.groupSeparator + "99){*|1}(" + opts.groupSeparator + "999){1|1}";
624
+ },
625
+ groupSeparator: ",",
626
+ radixPoint: ".",
627
+ placeholder: "0",
628
+ digits: 2,
629
+ digitsOptional: false
630
+ }
601
631
  });
602
- module.exports = Inputmask;