ngx-mask
Version:
awesome ngx mask
1,032 lines (1,026 loc) • 87.9 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/forms'), require('@angular/common')) :
typeof define === 'function' && define.amd ? define('ngx-mask', ['exports', '@angular/core', '@angular/forms', '@angular/common'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global['ngx-mask'] = {}, global.ng.core, global.ng.forms, global.ng.common));
}(this, (function (exports, core, forms, common) { 'use strict';
var config = new core.InjectionToken('config');
var NEW_CONFIG = new core.InjectionToken('NEW_CONFIG');
var INITIAL_CONFIG = new core.InjectionToken('INITIAL_CONFIG');
var initialConfig = {
suffix: '',
prefix: '',
thousandSeparator: ' ',
decimalMarker: '.',
clearIfNotMatch: false,
showTemplate: false,
showMaskTyped: false,
placeHolderCharacter: '_',
dropSpecialCharacters: true,
hiddenInput: undefined,
shownMaskExpression: '',
separatorLimit: '',
allowNegativeNumbers: false,
validation: true,
// tslint:disable-next-line: quotemark
specialCharacters: ['-', '/', '(', ')', '.', ':', ' ', '+', ',', '@', '[', ']', '"', "'"],
leadZeroDateTime: false,
patterns: {
'0': {
pattern: new RegExp('\\d'),
},
'9': {
pattern: new RegExp('\\d'),
optional: true,
},
X: {
pattern: new RegExp('\\d'),
symbol: '*',
},
A: {
pattern: new RegExp('[a-zA-Z0-9]'),
},
S: {
pattern: new RegExp('[a-zA-Z]'),
},
d: {
pattern: new RegExp('\\d'),
},
m: {
pattern: new RegExp('\\d'),
},
M: {
pattern: new RegExp('\\d'),
},
H: {
pattern: new RegExp('\\d'),
},
h: {
pattern: new RegExp('\\d'),
},
s: {
pattern: new RegExp('\\d'),
},
},
};
var timeMasks = ['Hh:m0:s0', 'Hh:m0', 'm0:s0'];
var withoutValidation = [
'percent',
'Hh',
's0',
'm0',
'separator',
'd0/M0/0000',
'd0/M0',
'd0',
'M0',
];
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise */
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b)
if (Object.prototype.hasOwnProperty.call(b, p))
d[p] = b[p]; };
return extendStatics(d, b);
};
function __extends(d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
var __assign = function () {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s)
if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function __rest(s, e) {
var t = {};
for (var p in s)
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
}
function __decorate(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
r = Reflect.decorate(decorators, target, key, desc);
else
for (var i = decorators.length - 1; i >= 0; i--)
if (d = decorators[i])
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
function __param(paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); };
}
function __metadata(metadataKey, metadataValue) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
return Reflect.metadata(metadataKey, metadataValue);
}
function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try {
step(generator.next(value));
}
catch (e) {
reject(e);
} }
function rejected(value) { try {
step(generator["throw"](value));
}
catch (e) {
reject(e);
} }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
function __generator(thisArg, body) {
var _ = { label: 0, sent: function () { if (t[0] & 1)
throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function () { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f)
throw new TypeError("Generator is already executing.");
while (_)
try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done)
return t;
if (y = 0, t)
op = [op[0] & 2, t.value];
switch (op[0]) {
case 0:
case 1:
t = op;
break;
case 4:
_.label++;
return { value: op[1], done: false };
case 5:
_.label++;
y = op[1];
op = [0];
continue;
case 7:
op = _.ops.pop();
_.trys.pop();
continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
_ = 0;
continue;
}
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) {
_.label = op[1];
break;
}
if (op[0] === 6 && _.label < t[1]) {
_.label = t[1];
t = op;
break;
}
if (t && _.label < t[2]) {
_.label = t[2];
_.ops.push(op);
break;
}
if (t[2])
_.ops.pop();
_.trys.pop();
continue;
}
op = body.call(thisArg, _);
}
catch (e) {
op = [6, e];
y = 0;
}
finally {
f = t = 0;
}
if (op[0] & 5)
throw op[1];
return { value: op[0] ? op[1] : void 0, done: true };
}
}
var __createBinding = Object.create ? (function (o, m, k, k2) {
if (k2 === undefined)
k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function () { return m[k]; } });
}) : (function (o, m, k, k2) {
if (k2 === undefined)
k2 = k;
o[k2] = m[k];
});
function __exportStar(m, o) {
for (var p in m)
if (p !== "default" && !Object.prototype.hasOwnProperty.call(o, p))
__createBinding(o, m, p);
}
function __values(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m)
return m.call(o);
if (o && typeof o.length === "number")
return {
next: function () {
if (o && i >= o.length)
o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
}
function __read(o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m)
return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
ar.push(r.value);
}
catch (error) {
e = { error: error };
}
finally {
try {
if (r && !r.done && (m = i["return"]))
m.call(i);
}
finally {
if (e)
throw e.error;
}
}
return ar;
}
/** @deprecated */
function __spread() {
for (var ar = [], i = 0; i < arguments.length; i++)
ar = ar.concat(__read(arguments[i]));
return ar;
}
/** @deprecated */
function __spreadArrays() {
for (var s = 0, i = 0, il = arguments.length; i < il; i++)
s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
}
function __spreadArray(to, from) {
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
to[j] = from[i];
return to;
}
function __await(v) {
return this instanceof __await ? (this.v = v, this) : new __await(v);
}
function __asyncGenerator(thisArg, _arguments, generator) {
if (!Symbol.asyncIterator)
throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
function verb(n) { if (g[n])
i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
function resume(n, v) { try {
step(g[n](v));
}
catch (e) {
settle(q[0][3], e);
} }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length)
resume(q[0][0], q[0][1]); }
}
function __asyncDelegator(o) {
var i, p;
return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i;
function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; }
}
function __asyncValues(o) {
if (!Symbol.asyncIterator)
throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function (v) { resolve({ value: v, done: d }); }, reject); }
}
function __makeTemplateObject(cooked, raw) {
if (Object.defineProperty) {
Object.defineProperty(cooked, "raw", { value: raw });
}
else {
cooked.raw = raw;
}
return cooked;
}
;
var __setModuleDefault = Object.create ? (function (o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function (o, v) {
o["default"] = v;
};
function __importStar(mod) {
if (mod && mod.__esModule)
return mod;
var result = {};
if (mod != null)
for (var k in mod)
if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k))
__createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
}
function __importDefault(mod) {
return (mod && mod.__esModule) ? mod : { default: mod };
}
function __classPrivateFieldGet(receiver, privateMap) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to get private field on non-instance");
}
return privateMap.get(receiver);
}
function __classPrivateFieldSet(receiver, privateMap, value) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to set private field on non-instance");
}
privateMap.set(receiver, value);
return value;
}
var MaskApplierService = /** @class */ (function () {
function MaskApplierService(_config) {
var _this = this;
this._config = _config;
this.maskExpression = '';
this.actualValue = '';
this.shownMaskExpression = '';
this._formatWithSeparators = function (str, thousandSeparatorChar, decimalChar, precision) {
var x = str.split(decimalChar);
var decimals = x.length > 1 ? "" + decimalChar + x[1] : '';
var res = x[0];
var separatorLimit = _this.separatorLimit.replace(/\s/g, '');
if (separatorLimit && +separatorLimit) {
if (res[0] === '-') {
res = "-" + res.slice(1, res.length).slice(0, separatorLimit.length);
}
else {
res = res.slice(0, separatorLimit.length);
}
}
var rgx = /(\d+)(\d{3})/;
while (thousandSeparatorChar && rgx.test(res)) {
res = res.replace(rgx, '$1' + thousandSeparatorChar + '$2');
}
if (precision === undefined) {
return res + decimals;
}
else if (precision === 0) {
return res;
}
return res + decimals.substr(0, precision + 1);
};
this.percentage = function (str) {
return Number(str) >= 0 && Number(str) <= 100;
};
this.getPrecision = function (maskExpression) {
var x = maskExpression.split('.');
if (x.length > 1) {
return Number(x[x.length - 1]);
}
return Infinity;
};
this.checkAndRemoveSuffix = function (inputValue) {
var _a, _b, _c;
for (var i = ((_a = _this.suffix) === null || _a === void 0 ? void 0 : _a.length) - 1; i >= 0; i--) {
var substr = _this.suffix.substr(i, (_b = _this.suffix) === null || _b === void 0 ? void 0 : _b.length);
if (inputValue.includes(substr) &&
(i - 1 < 0 || !inputValue.includes(_this.suffix.substr(i - 1, (_c = _this.suffix) === null || _c === void 0 ? void 0 : _c.length)))) {
return inputValue.replace(substr, '');
}
}
return inputValue;
};
this.checkInputPrecision = function (inputValue, precision, decimalMarker) {
if (precision < Infinity) {
var precisionRegEx = new RegExp(_this._charToRegExpExpression(decimalMarker) + ("\\d{" + precision + "}.*$"));
var precisionMatch = inputValue.match(precisionRegEx);
if (precisionMatch && precisionMatch[0].length - 1 > precision) {
var diff = precisionMatch[0].length - 1 - precision;
inputValue = inputValue.substring(0, inputValue.length - diff);
}
if (precision === 0 && inputValue.endsWith(decimalMarker)) {
inputValue = inputValue.substring(0, inputValue.length - 1);
}
}
return inputValue;
};
this._shift = new Set();
this.clearIfNotMatch = this._config.clearIfNotMatch;
this.dropSpecialCharacters = this._config.dropSpecialCharacters;
this.maskSpecialCharacters = this._config.specialCharacters;
this.maskAvailablePatterns = this._config.patterns;
this.prefix = this._config.prefix;
this.suffix = this._config.suffix;
this.thousandSeparator = this._config.thousandSeparator;
this.decimalMarker = this._config.decimalMarker;
this.hiddenInput = this._config.hiddenInput;
this.showMaskTyped = this._config.showMaskTyped;
this.placeHolderCharacter = this._config.placeHolderCharacter;
this.validation = this._config.validation;
this.separatorLimit = this._config.separatorLimit;
this.allowNegativeNumbers = this._config.allowNegativeNumbers;
this.leadZeroDateTime = this._config.leadZeroDateTime;
}
MaskApplierService.prototype.applyMaskWithPattern = function (inputValue, maskAndPattern) {
var _d = __read(maskAndPattern, 2), mask = _d[0], customPattern = _d[1];
this.customPattern = customPattern;
return this.applyMask(inputValue, mask);
};
MaskApplierService.prototype.applyMask = function (inputValue, maskExpression, position, justPasted, backspaced, cb) {
var _this = this;
if (position === void 0) { position = 0; }
if (justPasted === void 0) { justPasted = false; }
if (backspaced === void 0) { backspaced = false; }
if (cb === void 0) { cb = function () { }; }
if (inputValue === undefined || inputValue === null || maskExpression === undefined) {
return '';
}
var cursor = 0;
var result = '';
var multi = false;
var backspaceShift = false;
var shift = 1;
var stepBack = false;
if (inputValue.slice(0, this.prefix.length) === this.prefix) {
inputValue = inputValue.slice(this.prefix.length, inputValue.length);
}
if (!!this.suffix && (inputValue === null || inputValue === void 0 ? void 0 : inputValue.length) > 0) {
inputValue = this.checkAndRemoveSuffix(inputValue);
}
var inputArray = inputValue.toString().split('');
if (maskExpression === 'IP') {
this.ipError = !!(inputArray.filter(function (i) { return i === '.'; }).length < 3 && inputArray.length < 7);
maskExpression = '099.099.099.099';
}
var arr = [];
for (var i = 0; i < inputValue.length; i++) {
if (inputValue[i].match('\\d')) {
arr.push(inputValue[i]);
}
}
if (maskExpression === 'CPF_CNPJ') {
this.cpfCnpjError = !!(arr.length !== 11 && arr.length !== 14);
if (arr.length > 11) {
maskExpression = '00.000.000/0000-00';
}
else {
maskExpression = '000.000.000-00';
}
}
if (maskExpression.startsWith('percent')) {
if (inputValue.match('[a-z]|[A-Z]') || inputValue.match(/[-!$%^&*()_+|~=`{}\[\]:";'<>?,\/.]/)) {
inputValue = this._stripToDecimal(inputValue);
var precision = this.getPrecision(maskExpression);
inputValue = this.checkInputPrecision(inputValue, precision, this.decimalMarker);
}
if (inputValue.indexOf('.') > 0 && !this.percentage(inputValue.substring(0, inputValue.indexOf('.')))) {
var base = inputValue.substring(0, inputValue.indexOf('.') - 1);
inputValue = "" + base + inputValue.substring(inputValue.indexOf('.'), inputValue.length);
}
if (this.percentage(inputValue)) {
result = inputValue;
}
else {
result = inputValue.substring(0, inputValue.length - 1);
}
}
else if (maskExpression.startsWith('separator')) {
if (inputValue.match('[wа-яА-Я]') ||
inputValue.match('[ЁёА-я]') ||
inputValue.match('[a-z]|[A-Z]') ||
inputValue.match(/[-@#!$%\\^&*()_£¬'+|~=`{}\[\]:";<>.?\/]/) ||
inputValue.match('[^A-Za-z0-9,]')) {
inputValue = this._stripToDecimal(inputValue);
}
inputValue =
inputValue.length > 1 && inputValue[0] === '0' && inputValue[1] !== this.decimalMarker
? inputValue.slice(1, inputValue.length)
: inputValue;
// TODO: we had different rexexps here for the different cases... but tests dont seam to bother - check this
// separator: no COMMA, dot-sep: no SPACE, COMMA OK, comma-sep: no SPACE, COMMA OK
var thousandSeperatorCharEscaped = this._charToRegExpExpression(this.thousandSeparator);
var decimalMarkerEscaped = this._charToRegExpExpression(this.decimalMarker);
var invalidChars = '@#!$%^&*()_+|~=`{}\\[\\]:\\s,\\.";<>?\\/'
.replace(thousandSeperatorCharEscaped, '')
.replace(decimalMarkerEscaped, '');
var invalidCharRegexp = new RegExp('[' + invalidChars + ']');
if (inputValue.match(invalidCharRegexp)) {
inputValue = inputValue.substring(0, inputValue.length - 1);
}
var precision = this.getPrecision(maskExpression);
inputValue = this.checkInputPrecision(inputValue, precision, this.decimalMarker);
var strForSep = inputValue.replace(new RegExp(thousandSeperatorCharEscaped, 'g'), '');
result = this._formatWithSeparators(strForSep, this.thousandSeparator, this.decimalMarker, precision);
var commaShift = result.indexOf(',') - inputValue.indexOf(',');
var shiftStep = result.length - inputValue.length;
if (shiftStep > 0 && result[position] !== ',') {
backspaceShift = true;
var _shift = 0;
do {
this._shift.add(position + _shift);
_shift++;
} while (_shift < shiftStep);
}
else if ((commaShift !== 0 && position > 0 && !(result.indexOf(',') >= position && position > 3)) ||
(!(result.indexOf('.') >= position && position > 3) && shiftStep <= 0)) {
this._shift.clear();
backspaceShift = true;
shift = shiftStep;
position += shiftStep;
this._shift.add(position);
}
else {
this._shift.clear();
}
}
else {
for (
// tslint:disable-next-line
var i = 0, inputSymbol = inputArray[0]; i < inputArray.length; i++, inputSymbol = inputArray[i]) {
if (cursor === maskExpression.length) {
break;
}
if (this._checkSymbolMask(inputSymbol, maskExpression[cursor]) && maskExpression[cursor + 1] === '?') {
result += inputSymbol;
cursor += 2;
}
else if (maskExpression[cursor + 1] === '*' &&
multi &&
this._checkSymbolMask(inputSymbol, maskExpression[cursor + 2])) {
result += inputSymbol;
cursor += 3;
multi = false;
}
else if (this._checkSymbolMask(inputSymbol, maskExpression[cursor]) && maskExpression[cursor + 1] === '*') {
result += inputSymbol;
multi = true;
}
else if (maskExpression[cursor + 1] === '?' &&
this._checkSymbolMask(inputSymbol, maskExpression[cursor + 2])) {
result += inputSymbol;
cursor += 3;
}
else if (this._checkSymbolMask(inputSymbol, maskExpression[cursor])) {
if (maskExpression[cursor] === 'H') {
if (Number(inputSymbol) > 2) {
cursor += 1;
this._shiftStep(maskExpression, cursor, inputArray.length);
i--;
if (this.leadZeroDateTime) {
result += '0';
}
continue;
}
}
if (maskExpression[cursor] === 'h') {
if (result === '2' && Number(inputSymbol) > 3) {
cursor += 1;
i--;
continue;
}
}
if (maskExpression[cursor] === 'm') {
if (Number(inputSymbol) > 5) {
cursor += 1;
this._shiftStep(maskExpression, cursor, inputArray.length);
i--;
if (this.leadZeroDateTime) {
result += '0';
}
continue;
}
}
if (maskExpression[cursor] === 's') {
if (Number(inputSymbol) > 5) {
cursor += 1;
this._shiftStep(maskExpression, cursor, inputArray.length);
i--;
if (this.leadZeroDateTime) {
result += '0';
}
continue;
}
}
var daysCount = 31;
if (maskExpression[cursor] === 'd') {
if ((Number(inputSymbol) > 3 && this.leadZeroDateTime) ||
Number(inputValue.slice(cursor, cursor + 2)) > daysCount ||
inputValue[cursor + 1] === '/') {
cursor += 1;
this._shiftStep(maskExpression, cursor, inputArray.length);
i--;
if (this.leadZeroDateTime) {
result += '0';
}
continue;
}
}
if (maskExpression[cursor] === 'M') {
var monthsCount = 12;
// mask without day
var withoutDays = cursor === 0 &&
(Number(inputSymbol) > 2 ||
Number(inputValue.slice(cursor, cursor + 2)) > monthsCount ||
inputValue[cursor + 1] === '/');
// day<10 && month<12 for input
var day1monthInput = inputValue.slice(cursor - 3, cursor - 1).includes('/') &&
((inputValue[cursor - 2] === '/' &&
Number(inputValue.slice(cursor - 1, cursor + 1)) > monthsCount &&
inputValue[cursor] !== '/') ||
inputValue[cursor] === '/' ||
(inputValue[cursor - 3] === '/' &&
Number(inputValue.slice(cursor - 2, cursor)) > monthsCount &&
inputValue[cursor - 1] !== '/') ||
inputValue[cursor - 1] === '/');
// 10<day<31 && month<12 for input
var day2monthInput = Number(inputValue.slice(cursor - 3, cursor - 1)) <= daysCount &&
!inputValue.slice(cursor - 3, cursor - 1).includes('/') &&
inputValue[cursor - 1] === '/' &&
(Number(inputValue.slice(cursor, cursor + 2)) > monthsCount || inputValue[cursor + 1] === '/');
// day<10 && month<12 for paste whole data
var day1monthPaste = Number(inputValue.slice(cursor - 3, cursor - 1)) > daysCount &&
!inputValue.slice(cursor - 3, cursor - 1).includes('/') &&
!inputValue.slice(cursor - 2, cursor).includes('/') &&
Number(inputValue.slice(cursor - 2, cursor)) > monthsCount;
// 10<day<31 && month<12 for paste whole data
var day2monthPaste = Number(inputValue.slice(cursor - 3, cursor - 1)) <= daysCount &&
!inputValue.slice(cursor - 3, cursor - 1).includes('/') &&
inputValue[cursor - 1] !== '/' &&
Number(inputValue.slice(cursor - 1, cursor + 1)) > monthsCount;
if ((Number(inputSymbol) > 1 && this.leadZeroDateTime) ||
withoutDays ||
day1monthInput ||
day2monthInput ||
day1monthPaste ||
day2monthPaste) {
cursor += 1;
this._shiftStep(maskExpression, cursor, inputArray.length);
i--;
if (this.leadZeroDateTime) {
result += '0';
}
continue;
}
}
result += inputSymbol;
cursor++;
}
else if (this.maskSpecialCharacters.indexOf(maskExpression[cursor]) !== -1) {
result += maskExpression[cursor];
cursor++;
this._shiftStep(maskExpression, cursor, inputArray.length);
i--;
}
else if (this.maskSpecialCharacters.indexOf(inputSymbol) > -1 &&
this.maskAvailablePatterns[maskExpression[cursor]] &&
this.maskAvailablePatterns[maskExpression[cursor]].optional) {
if (!!inputArray[cursor] &&
maskExpression !== '099.099.099.099' &&
maskExpression !== '000.000.000-00' &&
maskExpression !== '00.000.000/0000-00') {
result += inputArray[cursor];
}
cursor++;
i--;
}
else if (this.maskExpression[cursor + 1] === '*' &&
this._findSpecialChar(this.maskExpression[cursor + 2]) &&
this._findSpecialChar(inputSymbol) === this.maskExpression[cursor + 2] &&
multi) {
cursor += 3;
result += inputSymbol;
}
else if (this.maskExpression[cursor + 1] === '?' &&
this._findSpecialChar(this.maskExpression[cursor + 2]) &&
this._findSpecialChar(inputSymbol) === this.maskExpression[cursor + 2] &&
multi) {
cursor += 3;
result += inputSymbol;
}
else if (this.showMaskTyped &&
this.maskSpecialCharacters.indexOf(inputSymbol) < 0 &&
inputSymbol !== this.placeHolderCharacter) {
stepBack = true;
}
}
}
if (result.length + 1 === maskExpression.length &&
this.maskSpecialCharacters.indexOf(maskExpression[maskExpression.length - 1]) !== -1) {
result += maskExpression[maskExpression.length - 1];
}
var newPosition = position + 1;
while (this._shift.has(newPosition)) {
shift++;
newPosition++;
}
var actualShift = justPasted ? cursor : this._shift.has(position) ? shift : 0;
if (stepBack) {
actualShift--;
}
cb(actualShift, backspaceShift);
if (shift < 0) {
this._shift.clear();
}
var onlySpecial = false;
if (backspaced) {
onlySpecial = inputArray.every(function (char) { return _this.maskSpecialCharacters.includes(char); });
}
var res = "" + this.prefix + (onlySpecial ? '' : result) + this.suffix;
if (result.length === 0) {
res = "" + this.prefix + result;
}
return res;
};
MaskApplierService.prototype._findSpecialChar = function (inputSymbol) {
return this.maskSpecialCharacters.find(function (val) { return val === inputSymbol; });
};
MaskApplierService.prototype._checkSymbolMask = function (inputSymbol, maskSymbol) {
this.maskAvailablePatterns = this.customPattern ? this.customPattern : this.maskAvailablePatterns;
return (this.maskAvailablePatterns[maskSymbol] &&
this.maskAvailablePatterns[maskSymbol].pattern &&
this.maskAvailablePatterns[maskSymbol].pattern.test(inputSymbol));
};
MaskApplierService.prototype._stripToDecimal = function (str) {
var _this = this;
return str
.split('')
.filter(function (i, idx) {
return (i.match('^-?\\d') ||
i.match('\\s') ||
i === '.' ||
i === ',' ||
(i === '-' && idx === 0 && _this.allowNegativeNumbers));
})
.join('');
};
MaskApplierService.prototype._charToRegExpExpression = function (char) {
if (char) {
var charsToEscape = '[\\^$.|?*+()';
return char === ' ' ? '\\s' : charsToEscape.indexOf(char) >= 0 ? '\\' + char : char;
}
return char;
};
MaskApplierService.prototype._shiftStep = function (maskExpression, cursor, inputLength) {
var shiftStep = /[*?]/g.test(maskExpression.slice(0, cursor)) ? inputLength : cursor;
this._shift.add(shiftStep + this.prefix.length || 0);
};
return MaskApplierService;
}());
MaskApplierService.decorators = [
{ type: core.Injectable }
];
MaskApplierService.ctorParameters = function () { return [
{ type: undefined, decorators: [{ type: core.Inject, args: [config,] }] }
]; };
var MaskService = /** @class */ (function (_super) {
__extends(MaskService, _super);
function MaskService(document, _config, _elementRef, _renderer) {
var _this = _super.call(this, _config) || this;
_this.document = document;
_this._config = _config;
_this._elementRef = _elementRef;
_this._renderer = _renderer;
_this.maskExpression = '';
_this.isNumberValue = false;
_this.placeHolderCharacter = '_';
_this.maskIsShown = '';
_this.selStart = null;
_this.selEnd = null;
/**
* Whether we are currently in writeValue function, in this case when applying the mask we don't want to trigger onChange function,
* since writeValue should be a one way only process of writing the DOM value based on the Angular model value.
*/
_this.writingValue = false;
_this.onChange = function (_) { };
return _this;
}
// tslint:disable-next-line:cyclomatic-complexity
MaskService.prototype.applyMask = function (inputValue, maskExpression, position, justPasted, backspaced, cb) {
var _this = this;
if (position === void 0) { position = 0; }
if (justPasted === void 0) { justPasted = false; }
if (backspaced === void 0) { backspaced = false; }
if (cb === void 0) { cb = function () { }; }
if (!maskExpression) {
return inputValue;
}
this.maskIsShown = this.showMaskTyped ? this.showMaskInInput() : '';
if (this.maskExpression === 'IP' && this.showMaskTyped) {
this.maskIsShown = this.showMaskInInput(inputValue || '#');
}
if (this.maskExpression === 'CPF_CNPJ' && this.showMaskTyped) {
this.maskIsShown = this.showMaskInInput(inputValue || '#');
}
if (!inputValue && this.showMaskTyped) {
this.formControlResult(this.prefix);
return this.prefix + this.maskIsShown;
}
var getSymbol = !!inputValue && typeof this.selStart === 'number' ? inputValue[this.selStart] : '';
var newInputValue = '';
if (this.hiddenInput && !this.writingValue) {
var actualResult = this.actualValue.split('');
// tslint:disable no-unused-expression
inputValue !== '' && actualResult.length
? typeof this.selStart === 'number' && typeof this.selEnd === 'number'
? inputValue.length > actualResult.length
? actualResult.splice(this.selStart, 0, getSymbol)
: inputValue.length < actualResult.length
? actualResult.length - inputValue.length === 1
? actualResult.splice(this.selStart - 1, 1)
: actualResult.splice(this.selStart, this.selEnd - this.selStart)
: null
: null
: (actualResult = []);
// tslint:enable no-unused-expression
newInputValue =
this.actualValue.length && actualResult.length <= inputValue.length
? this.shiftTypedSymbols(actualResult.join(''))
: inputValue;
}
newInputValue = Boolean(newInputValue) && newInputValue.length ? newInputValue : inputValue;
var result = _super.prototype.applyMask.call(this, newInputValue, maskExpression, position, justPasted, backspaced, cb);
this.actualValue = this.getActualValue(result);
// handle some separator implications:
// a.) adjust decimalMarker default (. -> ,) if thousandSeparator is a dot
if (this.thousandSeparator === '.' && this.decimalMarker === '.') {
this.decimalMarker = ',';
}
// b) remove decimal marker from list of special characters to mask
if (this.maskExpression.startsWith('separator') && this.dropSpecialCharacters === true) {
this.maskSpecialCharacters = this.maskSpecialCharacters.filter(function (item) { return item !== _this.decimalMarker; });
}
this.formControlResult(result);
if (!this.showMaskTyped) {
if (this.hiddenInput) {
return result && result.length ? this.hideInput(result, this.maskExpression) : result;
}
return result;
}
var resLen = result.length;
var prefNmask = this.prefix + this.maskIsShown;
if (this.maskExpression.includes('H')) {
var countSkipedSymbol = this._numberSkipedSymbols(result);
return result + prefNmask.slice(resLen + countSkipedSymbol);
}
else if (this.maskExpression === 'IP' || this.maskExpression === 'CPF_CNPJ') {
return result + prefNmask;
}
return result + prefNmask.slice(resLen);
};
// get the number of characters that were shifted
MaskService.prototype._numberSkipedSymbols = function (value) {
var regex = /(^|\D)(\d\D)/g;
var match = regex.exec(value);
var countSkipedSymbol = 0;
while (match != null) {
countSkipedSymbol += 1;
match = regex.exec(value);
}
return countSkipedSymbol;
};
MaskService.prototype.applyValueChanges = function (position, justPasted, backspaced, cb) {
if (position === void 0) { position = 0; }
if (cb === void 0) { cb = function () { }; }
var formElement = this._elementRef.nativeElement;
formElement.value = this.applyMask(formElement.value, this.maskExpression, position, justPasted, backspaced, cb);
if (formElement === this.document.activeElement) {
return;
}
this.clearIfNotMatchFn();
};
MaskService.prototype.hideInput = function (inputValue, maskExpression) {
var _this = this;
return inputValue
.split('')
.map(function (curr, index) {
if (_this.maskAvailablePatterns &&
_this.maskAvailablePatterns[maskExpression[index]] &&
_this.maskAvailablePatterns[maskExpression[index]].symbol) {
return _this.maskAvailablePatterns[maskExpression[index]].symbol;
}
return curr;
})
.join('');
};
// this function is not necessary, it checks result against maskExpression
MaskService.prototype.getActualValue = function (res) {
var _this = this;
var compare = res
.split('')
.filter(function (symbol, i) { return _this._checkSymbolMask(symbol, _this.maskExpression[i]) ||
(_this.maskSpecialCharacters.includes(_this.maskExpression[i]) && symbol === _this.maskExpression[i]); });
if (compare.join('') === res) {
return compare.join('');
}
return res;
};
MaskService.prototype.shiftTypedSymbols = function (inputValue) {
var _this = this;
var symbolToReplace = '';
var newInputValue = (inputValue &&
inputValue.split('').map(function (currSymbol, index) {
if (_this.maskSpecialCharacters.includes(inputValue[index + 1]) &&
inputValue[index + 1] !== _this.maskExpression[index + 1]) {
symbolToReplace = currSymbol;
return inputValue[index + 1];
}
if (symbolToReplace.length) {
var replaceSymbol = symbolToReplace;
symbolToReplace = '';
return replaceSymbol;
}
return currSymbol;
})) ||
[];
return newInputValue.join('');
};
MaskService.prototype.showMaskInInput = function (inputVal) {
if (this.showMaskTyped && !!this.shownMaskExpression) {
if (this.maskExpression.length !== this.shownMaskExpression.length) {
throw new Error('Mask expression must match mask placeholder length');
}
else {
return this.shownMaskExpression;
}
}
else if (this.showMaskTyped) {
if (inputVal) {
if (this.maskExpression === 'IP') {
return this._checkForIp(inputVal);
}
if (this.maskExpression === 'CPF_CNPJ') {
return this._checkForCpfCnpj(inputVal);
}
}
return this.maskExpression.replace(/\w/g, this.placeHolderCharacter);
}
return '';
};
MaskService.prototype.clearIfNotMatchFn = function () {
var formElement = this._elementRef.nativeElement;
if (this.clearIfNotMatch &&
this.prefix.length + this.maskExpression.length + this.suffix.length !==
formElement.value.replace(/_/g, '').length) {
this.formElementProperty = ['value', ''];
this.applyMask(formElement.value, this.maskExpression);
}
};
Object.defineProperty(MaskService.prototype, "formElementProperty", {
set: function (_a) {
var _this = this;
var _b = __read(_a, 2), name = _b[0], value = _b[1];
Promise.resolve().then(function () { return _this._renderer.setProperty(_this._elementRef.nativeElement, name, value); });
},
enumerable: false,
configurable: true
});
MaskService.prototype.checkSpecialCharAmount = function (mask) {
var _this = this;
var chars = mask.split('').filter(function (item) { return _this._findSpecialChar(item); });
return chars.length;
};
MaskService.prototype.removeMask = function (inputValue) {
return this._removeMask(this._removeSuffix(