ngx-mask 16.4.0 → 16.4.2

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.
@@ -116,6 +116,7 @@ class NgxMaskApplierService {
116
116
  this.outputTransformFn = this._config.outputTransformFn;
117
117
  this.keepCharacterPositions = this._config.keepCharacterPositions;
118
118
  this._shift = new Set();
119
+ this.plusOnePosition = false;
119
120
  this.maskExpression = '';
120
121
  this.actualValue = '';
121
122
  this.showKeepCharacterExp = '';
@@ -222,7 +223,7 @@ class NgxMaskApplierService {
222
223
  let backspaceShift = false;
223
224
  let shift = 1;
224
225
  let stepBack = false;
225
- if (inputValue.slice(0, this.prefix.length) === this.prefix && !this.showMaskTyped) {
226
+ if (inputValue.slice(0, this.prefix.length) === this.prefix) {
226
227
  // eslint-disable-next-line no-param-reassign
227
228
  inputValue = inputValue.slice(this.prefix.length, inputValue.length);
228
229
  }
@@ -306,24 +307,91 @@ class NgxMaskApplierService {
306
307
  // eslint-disable-next-line no-param-reassign
307
308
  inputValue = this._stripToDecimal(inputValue);
308
309
  }
309
- // eslint-disable-next-line no-param-reassign
310
- inputValue =
311
- inputValue[0] === '-' && this.allowNegativeNumbers
310
+ const precision = this.getPrecision(maskExpression);
311
+ const decimalMarker = Array.isArray(this.decimalMarker)
312
+ ? "." /* MaskExpression.DOT */
313
+ : this.decimalMarker;
314
+ if (precision === 0) {
315
+ // eslint-disable-next-line no-param-reassign
316
+ inputValue = this.allowNegativeNumbers
312
317
  ? inputValue.length > 2 &&
313
- inputValue[1] === '0' &&
318
+ inputValue[0] === "-" /* MaskExpression.MINUS */ &&
319
+ inputValue[1] === "0" /* MaskExpression.NUMBER_ZERO */ &&
314
320
  inputValue[2] !== this.thousandSeparator &&
315
- !this._compareOrIncludes(inputValue[2], this.decimalMarker, this.thousandSeparator) &&
316
- !backspaced
317
- ? inputValue.slice(0, inputValue.length - 1)
318
- : inputValue
321
+ inputValue[2] !== "," /* MaskExpression.COMMA */ &&
322
+ inputValue[2] !== "." /* MaskExpression.DOT */
323
+ ? '-' + inputValue.slice(2, inputValue.length)
324
+ : inputValue[0] === "0" /* MaskExpression.NUMBER_ZERO */ &&
325
+ inputValue.length > 1 &&
326
+ inputValue[1] !== this.thousandSeparator &&
327
+ inputValue[1] !== "," /* MaskExpression.COMMA */ &&
328
+ inputValue[1] !== "." /* MaskExpression.DOT */
329
+ ? inputValue.slice(1, inputValue.length)
330
+ : inputValue
319
331
  : inputValue.length > 1 &&
320
- inputValue[0] === '0' &&
332
+ inputValue[0] === "0" /* MaskExpression.NUMBER_ZERO */ &&
321
333
  inputValue[1] !== this.thousandSeparator &&
322
- !this._compareOrIncludes(inputValue[1], this.decimalMarker, this.thousandSeparator) &&
323
- !backspaced
324
- ? inputValue.slice(0, inputValue.length - 1)
334
+ inputValue[1] !== "," /* MaskExpression.COMMA */ &&
335
+ inputValue[1] !== "." /* MaskExpression.DOT */
336
+ ? inputValue.slice(1, inputValue.length)
325
337
  : inputValue;
338
+ }
339
+ else {
340
+ // eslint-disable-next-line no-param-reassign
341
+ if (inputValue[0] === decimalMarker && inputValue.length > 1) {
342
+ // eslint-disable-next-line no-param-reassign
343
+ inputValue =
344
+ "0" /* MaskExpression.NUMBER_ZERO */ + inputValue.slice(0, inputValue.length + 1);
345
+ this.plusOnePosition = true;
346
+ }
347
+ if (inputValue[0] === "0" /* MaskExpression.NUMBER_ZERO */ &&
348
+ inputValue[1] !== decimalMarker &&
349
+ inputValue[1] !== this.thousandSeparator) {
350
+ // eslint-disable-next-line no-param-reassign
351
+ inputValue =
352
+ inputValue.length > 1
353
+ ? inputValue.slice(0, 1) +
354
+ decimalMarker +
355
+ inputValue.slice(1, inputValue.length + 1)
356
+ : inputValue;
357
+ this.plusOnePosition = true;
358
+ }
359
+ if (this.allowNegativeNumbers &&
360
+ inputValue[0] === "-" /* MaskExpression.MINUS */ &&
361
+ (inputValue[1] === decimalMarker ||
362
+ inputValue[1] === "0" /* MaskExpression.NUMBER_ZERO */)) {
363
+ // eslint-disable-next-line no-param-reassign
364
+ inputValue =
365
+ inputValue[1] === decimalMarker && inputValue.length > 2
366
+ ? inputValue.slice(0, 1) +
367
+ "0" /* MaskExpression.NUMBER_ZERO */ +
368
+ inputValue.slice(1, inputValue.length)
369
+ : inputValue[1] === "0" /* MaskExpression.NUMBER_ZERO */ &&
370
+ inputValue.length > 2 &&
371
+ inputValue[2] !== decimalMarker
372
+ ? inputValue.slice(0, 2) +
373
+ decimalMarker +
374
+ inputValue.slice(2, inputValue.length)
375
+ : inputValue;
376
+ this.plusOnePosition = true;
377
+ }
378
+ }
326
379
  if (backspaced) {
380
+ if (inputValue[0] === "0" /* MaskExpression.NUMBER_ZERO */ &&
381
+ inputValue[1] === this.decimalMarker &&
382
+ (inputValue[position] === "0" /* MaskExpression.NUMBER_ZERO */ ||
383
+ inputValue[position] === this.decimalMarker)) {
384
+ // eslint-disable-next-line no-param-reassign
385
+ inputValue = inputValue.slice(2, inputValue.length);
386
+ }
387
+ if (inputValue[0] === "-" /* MaskExpression.MINUS */ &&
388
+ inputValue[1] === "0" /* MaskExpression.NUMBER_ZERO */ &&
389
+ inputValue[2] === this.decimalMarker &&
390
+ (inputValue[position] === "0" /* MaskExpression.NUMBER_ZERO */ ||
391
+ inputValue[position] === this.decimalMarker)) {
392
+ // eslint-disable-next-line no-param-reassign
393
+ inputValue = "-" /* MaskExpression.MINUS */ + inputValue.slice(3, inputValue.length);
394
+ }
327
395
  // eslint-disable-next-line no-param-reassign
328
396
  inputValue = this._compareOrIncludes(inputValue[inputValue.length - 1], this.decimalMarker, this.thousandSeparator)
329
397
  ? inputValue.slice(0, inputValue.length - 1)
@@ -343,13 +411,10 @@ class NgxMaskApplierService {
343
411
  invalidChars = invalidChars.replace(this._charToRegExpExpression(this.decimalMarker), '');
344
412
  }
345
413
  const invalidCharRegexp = new RegExp('[' + invalidChars + ']');
346
- if (inputValue.match(invalidCharRegexp) ||
347
- (inputValue.length === 1 &&
348
- this._compareOrIncludes(inputValue, this.decimalMarker, this.thousandSeparator))) {
414
+ if (inputValue.match(invalidCharRegexp)) {
349
415
  // eslint-disable-next-line no-param-reassign
350
416
  inputValue = inputValue.substring(0, inputValue.length - 1);
351
417
  }
352
- const precision = this.getPrecision(maskExpression);
353
418
  // eslint-disable-next-line no-param-reassign
354
419
  inputValue = this.checkInputPrecision(inputValue, precision, this.decimalMarker);
355
420
  const strForSep = inputValue.replace(new RegExp(thousandSeparatorCharEscaped, 'g'), '');
@@ -647,7 +712,7 @@ class NgxMaskApplierService {
647
712
  if (backspaced) {
648
713
  onlySpecial = inputArray.every((char) => this.specialCharacters.includes(char));
649
714
  }
650
- let res = `${this.prefix}${onlySpecial ? "" /* MaskExpression.EMPTY_STRING */ : result}${this.suffix}`;
715
+ let res = `${this.prefix}${onlySpecial ? "" /* MaskExpression.EMPTY_STRING */ : result}${this.showMaskTyped ? '' : this.suffix}`;
651
716
  if (result.length === 0) {
652
717
  res = !this.dropSpecialCharacters ? `${this.prefix}${result}` : `${result}`;
653
718
  }
@@ -789,7 +854,7 @@ class NgxMaskService extends NgxMaskApplierService {
789
854
  }
790
855
  if (!inputValue && this.showMaskTyped) {
791
856
  this.formControlResult(this.prefix);
792
- return this.prefix + this.maskIsShown;
857
+ return this.prefix + this.maskIsShown + this.suffix;
793
858
  }
794
859
  const getSymbol = !!inputValue && typeof this.selStart === 'number'
795
860
  ? inputValue[this.selStart] ?? "" /* MaskExpression.EMPTY_STRING */
@@ -908,7 +973,7 @@ class NgxMaskService extends NgxMaskApplierService {
908
973
  return result;
909
974
  }
910
975
  const resLen = result.length;
911
- const prefNmask = this.prefix + this.maskIsShown;
976
+ const prefNmask = this.prefix + this.maskIsShown + this.suffix;
912
977
  if (this.maskExpression.includes("H" /* MaskExpression.HOURS */)) {
913
978
  const countSkipedSymbol = this._numberSkipedSymbols(result);
914
979
  return result + prefNmask.slice(resLen + countSkipedSymbol);
@@ -1228,7 +1293,8 @@ class NgxMaskService extends NgxMaskApplierService {
1228
1293
  : this.specialCharacters;
1229
1294
  if (!this.deletedSpecialCharacter &&
1230
1295
  this._checkPatternForSpace() &&
1231
- result.includes(" " /* MaskExpression.WHITE_SPACE */)) {
1296
+ result.includes(" " /* MaskExpression.WHITE_SPACE */) &&
1297
+ this.maskExpression.includes("*" /* MaskExpression.SYMBOL_STAR */)) {
1232
1298
  specialCharacters = specialCharacters.filter((char) => char !== " " /* MaskExpression.WHITE_SPACE */);
1233
1299
  }
1234
1300
  return this._removeMask(result, specialCharacters);
@@ -1434,12 +1500,6 @@ class NgxMaskDirective {
1434
1500
  this._maskService.maskExpression = this._maskValue;
1435
1501
  }
1436
1502
  }
1437
- if (allowNegativeNumbers) {
1438
- this._maskService.allowNegativeNumbers = allowNegativeNumbers.currentValue;
1439
- if (this._maskService.allowNegativeNumbers) {
1440
- this._maskService.specialCharacters = this._maskService.specialCharacters.filter((c) => c !== "-" /* MaskExpression.MINUS */);
1441
- }
1442
- }
1443
1503
  if (specialCharacters) {
1444
1504
  if (!specialCharacters.currentValue || !Array.isArray(specialCharacters.currentValue)) {
1445
1505
  return;
@@ -1448,6 +1508,12 @@ class NgxMaskDirective {
1448
1508
  this._maskService.specialCharacters = specialCharacters.currentValue || [];
1449
1509
  }
1450
1510
  }
1511
+ if (allowNegativeNumbers) {
1512
+ this._maskService.allowNegativeNumbers = allowNegativeNumbers.currentValue;
1513
+ if (this._maskService.allowNegativeNumbers) {
1514
+ this._maskService.specialCharacters = this._maskService.specialCharacters.filter((c) => c !== "-" /* MaskExpression.MINUS */);
1515
+ }
1516
+ }
1451
1517
  // Only overwrite the mask available patterns if a pattern has actually been passed in
1452
1518
  if (patterns && patterns.currentValue) {
1453
1519
  this._maskService.patterns = patterns.currentValue;
@@ -1744,13 +1810,33 @@ class NgxMaskDirective {
1744
1810
  position = position + 1;
1745
1811
  }
1746
1812
  else if (checkSymbols) {
1813
+ if (el.value.length === 1 && position === 1) {
1814
+ this._maskService.actualValue =
1815
+ this.prefix +
1816
+ inputSymbol +
1817
+ this._maskService.maskIsShown.slice(1, this._maskService.maskIsShown.length) +
1818
+ this.suffix;
1819
+ }
1820
+ else {
1821
+ this._maskService.actualValue =
1822
+ el.value.slice(0, position - 1) +
1823
+ inputSymbol +
1824
+ el.value
1825
+ .slice(position + 1)
1826
+ .split(this.suffix)
1827
+ .join('') +
1828
+ this.suffix;
1829
+ }
1830
+ }
1831
+ else if (this.prefix &&
1832
+ el.value.length === 1 &&
1833
+ position - prefixLength === 1 &&
1834
+ this._maskService._checkSymbolMask(el.value, this._maskService.maskExpression[position - 1 - prefixLength] ??
1835
+ "" /* MaskExpression.EMPTY_STRING */)) {
1747
1836
  this._maskService.actualValue =
1748
- el.value.slice(0, position - 1) +
1749
- inputSymbol +
1750
- el.value
1751
- .slice(position + 1)
1752
- .split(this.suffix)
1753
- .join('') +
1837
+ this.prefix +
1838
+ el.value +
1839
+ this._maskService.maskIsShown.slice(1, this._maskService.maskIsShown.length) +
1754
1840
  this.suffix;
1755
1841
  }
1756
1842
  }
@@ -1770,6 +1856,13 @@ class NgxMaskDirective {
1770
1856
  inputSymbol +
1771
1857
  this._inputValue.slice(position + 1);
1772
1858
  }
1859
+ if (this._maskService.maskExpression === "d0/M0/0000" /* MaskExpression.DAYS_MONTHS_YEARS */ &&
1860
+ this.leadZeroDateTime) {
1861
+ if ((position < 3 && Number(el.value) > 31 && Number(el.value) < 40) ||
1862
+ (position === 5 && Number(el.value.slice(3, 5)) > 12)) {
1863
+ position = position + 2;
1864
+ }
1865
+ }
1773
1866
  if (this._maskService.maskExpression === "Hh:m0:s0" /* MaskExpression.HOURS_MINUTES_SECONDS */ &&
1774
1867
  this.apm) {
1775
1868
  if (this._justPasted && el.value.slice(0, 2) === "00" /* MaskExpression.DOUBLE_ZERO */) {
@@ -1789,6 +1882,10 @@ class NgxMaskDirective {
1789
1882
  if (this._getActiveElement() !== el) {
1790
1883
  return;
1791
1884
  }
1885
+ if (this._maskService.plusOnePosition) {
1886
+ position = position + 1;
1887
+ this._maskService.plusOnePosition = false;
1888
+ }
1792
1889
  // update position after applyValueChanges to prevent cursor on wrong position when it has an array of maskExpression
1793
1890
  if (this._maskExpressionArray.length) {
1794
1891
  if (this._code === "Backspace" /* MaskExpression.BACKSPACE */) {
@@ -1810,7 +1907,10 @@ class NgxMaskDirective {
1810
1907
  : position +
1811
1908
  (this._code === "Backspace" /* MaskExpression.BACKSPACE */ && !backspaceShift ? 0 : caretShift);
1812
1909
  if (positionToApply > this._getActualInputLength()) {
1813
- positionToApply = this._getActualInputLength();
1910
+ positionToApply =
1911
+ el.value === this._maskService.decimalMarker && el.value.length === 1
1912
+ ? this._getActualInputLength() + 1
1913
+ : this._getActualInputLength();
1814
1914
  }
1815
1915
  if (positionToApply < 0) {
1816
1916
  positionToApply = 0;
@@ -1823,6 +1923,10 @@ class NgxMaskDirective {
1823
1923
  }
1824
1924
  }
1825
1925
  else {
1926
+ if (!this._maskValue) {
1927
+ this.onChange(el.value);
1928
+ return;
1929
+ }
1826
1930
  this._maskService.applyValueChanges(el.value.length, this._justPasted, this._code === "Backspace" /* MaskExpression.BACKSPACE */ || this._code === "Delete" /* MaskExpression.DELETE */);
1827
1931
  }
1828
1932
  }
@@ -2010,7 +2114,7 @@ class NgxMaskDirective {
2010
2114
  typeof controlValue === 'number' ||
2011
2115
  controlValue === null ||
2012
2116
  controlValue === undefined) {
2013
- if (controlValue === null || controlValue === undefined) {
2117
+ if (controlValue === null || controlValue === undefined || controlValue === '') {
2014
2118
  this._maskService._currentValue = '';
2015
2119
  this._maskService._previousValue = '';
2016
2120
  }
@@ -2165,7 +2269,8 @@ class NgxMaskDirective {
2165
2269
  }
2166
2270
  }
2167
2271
  else {
2168
- const check = this._inputValue
2272
+ const check = this._maskService
2273
+ .removeMask(this._inputValue)
2169
2274
  ?.split("" /* MaskExpression.EMPTY_STRING */)
2170
2275
  .every((character, index) => {
2171
2276
  const indexMask = mask.charAt(index);