ngx-mask-fork
Version:
awesome ngx mask
982 lines (954 loc) • 83.5 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-fork', ['exports', '@angular/core', '@angular/forms', '@angular/common'], factory) :
(global = global || self, factory(global['ngx-mask-fork'] = {}, global.ng.core, global.ng.forms, global.ng.common));
}(this, (function (exports, core, forms, common) { 'use strict';
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
/* 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 (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
function __extends(d, b) {
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) {
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) : new P(function (resolve) { resolve(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 };
}
}
function __exportStar(m, exports) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
function __values(o) {
var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
if (m) return m.call(o);
return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
}
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;
}
function __spread() {
for (var ar = [], i = 0; i < arguments.length; i++)
ar = ar.concat(__read(arguments[i]));
return ar;
}
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 __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;
};
function __importStar(mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result.default = mod;
return result;
}
function __importDefault(mod) {
return (mod && mod.__esModule) ? mod : { default: mod };
}
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: ['-', '/', '(', ')', '.', ':', ' ', '+', ',', '@', '[', ']', '"', "'"],
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',
];
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.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) {
inputValue = inputValue.substring(0, inputValue.length - 1);
}
else 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;
}
MaskApplierService.prototype.applyMaskWithPattern = function (inputValue, maskAndPattern) {
var _a = __read(maskAndPattern, 2), mask = _a[0], customPattern = _a[1];
this.customPattern = customPattern;
return this.applyMask(inputValue, mask);
};
MaskApplierService.prototype.applyMask = function (inputValue, maskExpression, position, cb) {
if (position === void 0) { position = 0; }
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.endsWith(this.suffix)) {
inputValue = inputValue.slice(0, inputValue.length - this.suffix.length);
}
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 && arr.length !== 15);
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, '.');
}
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]) ||
(this.hiddenInput &&
this.maskAvailablePatterns[maskExpression[cursor]] &&
this.maskAvailablePatterns[maskExpression[cursor]].symbol === inputSymbol)) {
if (maskExpression[cursor] === 'H') {
if (Number(inputSymbol) > 2) {
cursor += 1;
var shiftStep = /[*?]/g.test(maskExpression.slice(0, cursor)) ? inputArray.length : cursor;
this._shift.add(shiftStep + this.prefix.length || 0);
i--;
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;
var shiftStep = /[*?]/g.test(maskExpression.slice(0, cursor)) ? inputArray.length : cursor;
this._shift.add(shiftStep + this.prefix.length || 0);
i--;
continue;
}
}
if (maskExpression[cursor] === 's') {
if (Number(inputSymbol) > 5) {
cursor += 1;
var shiftStep = /[*?]/g.test(maskExpression.slice(0, cursor)) ? inputArray.length : cursor;
this._shift.add(shiftStep + this.prefix.length || 0);
i--;
continue;
}
}
var daysCount = 31;
if (maskExpression[cursor] === 'd') {
if (Number(inputValue.slice(cursor, cursor + 2)) > daysCount || inputValue[cursor + 1] === '/') {
cursor += 1;
var shiftStep = /[*?]/g.test(maskExpression.slice(0, cursor)) ? inputArray.length : cursor;
this._shift.add(shiftStep + this.prefix.length || 0);
i--;
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 (withoutDays || day1monthInput || day2monthInput || day1monthPaste || day2monthPaste) {
cursor += 1;
var shiftStep = /[*?]/g.test(maskExpression.slice(0, cursor)) ? inputArray.length : cursor;
this._shift.add(shiftStep + this.prefix.length || 0);
i--;
continue;
}
}
result += inputSymbol;
cursor++;
}
else if (this.maskSpecialCharacters.indexOf(maskExpression[cursor]) !== -1) {
result += maskExpression[cursor];
cursor++;
var shiftStep = /[*?]/g.test(maskExpression.slice(0, cursor)) ? inputArray.length : cursor;
this._shift.add(shiftStep + this.prefix.length || 0);
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 = this._shift.has(position) ? shift : 0;
if (stepBack) {
actualShift--;
}
cb(actualShift, backspaceShift);
if (shift < 0) {
this._shift.clear();
}
var res = "" + this.prefix + 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 === '.' || 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.ctorParameters = function () { return [
{ type: undefined, decorators: [{ type: core.Inject, args: [config,] }] }
]; };
MaskApplierService = __decorate([
core.Injectable(),
__param(0, core.Inject(config)),
__metadata("design:paramtypes", [Object])
], MaskApplierService);
return MaskApplierService;
}());
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 (_) { };
_this._formElement = _this._elementRef.nativeElement;
return _this;
}
// tslint:disable-next-line:cyclomatic-complexity
MaskService.prototype.applyMask = function (inputValue, maskExpression, position, cb) {
var _this = this;
if (position === void 0) { position = 0; }
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 !== undefined) {
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 ? this.shiftTypedSymbols(actualResult.join('')) : inputValue;
}
newInputValue = Boolean(newInputValue) && newInputValue.length ? newInputValue : inputValue;
var result = _super.prototype.applyMask.call(this, newInputValue, maskExpression, position, 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;
return (result +
(this.maskExpression === 'IP' || this.maskExpression === 'CPF_CNPJ' ? prefNmask : prefNmask.slice(resLen)));
};
MaskService.prototype.applyValueChanges = function (position, cb) {
if (position === void 0) { position = 0; }
if (cb === void 0) { cb = function () { }; }
this._formElement.value = this.applyMask(this._formElement.value, this.maskExpression, position, cb);
if (this._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 () {
if (this.clearIfNotMatch &&
this.prefix.length + this.maskExpression.length + this.suffix.length !==
this._formElement.value.replace(/_/g, '').length) {
this.formElementProperty(['value', '']);
this.applyMask(this._formElement.value, this.maskExpression);
}
};
MaskService.prototype.formElementProperty = function (_a) {
var _b = __read(_a, 2), name = _b[0], value = _b[1];
this._renderer.setProperty(this._formElement, name, value);
};
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(this._removePrefix(inputValue)), this.maskSpecialCharacters.concat('_').concat(this.placeHolderCharacter));
};
MaskService.prototype._checkForIp = function (inputVal) {
if (inputVal === '#') {
return this.placeHolderCharacter + "." + this.placeHolderCharacter + "." + this.placeHolderCharacter + "." + this.placeHolderCharacter;
}
var arr = [];
for (var i = 0; i < inputVal.length; i++) {
if (inputVal[i].match('\\d')) {
arr.push(inputVal[i]);
}
}
if (arr.length <= 3) {
return this.placeHolderCharacter + "." + this.placeHolderCharacter + "." + this.placeHolderCharacter;
}
if (arr.length > 3 && arr.length <= 6) {
return this.placeHolderCharacter + "." + this.placeHolderCharacter;
}
if (arr.length > 6 && arr.length <= 9) {
return this.placeHolderCharacter;
}
if (arr.length > 9 && arr.length <= 12) {
return '';
}
return '';
};
MaskService.prototype._checkForCpfCnpj = function (inputVal) {
var cpf = "" + this.placeHolderCharacter + this.placeHolderCharacter + this.placeHolderCharacter +
("." + this.placeHolderCharacter + this.placeHolderCharacter + this.placeHolderCharacter) +
("." + this.placeHolderCharacter + this.placeHolderCharacter + this.placeHolderCharacter) +
("-" + this.placeHolderCharacter + this.placeHolderCharacter);
var cnpj = "" + this.placeHolderCharacter + this.placeHolderCharacter +
("." + this.placeHolderCharacter + this.placeHolderCharacter + this.placeHolderCharacter) +
("." + this.placeHolderCharacter + this.placeHolderCharacter + this.placeHolderCharacter) +
("/" + this.placeHolderCharacter + this.placeHolderCharacter + this.placeHolderCharacter + this.placeHolderCharacter) +
("-" + this.placeHolderCharacter + this.placeHolderCharacter);
if (inputVal === '#') {
return cpf;
}
var arr = [];
for (var i = 0; i < inputVal.length; i++) {
if (inputVal[i].match('\\d')) {
arr.push(inputVal[i]);
}
}
if (arr.length <= 3) {
return cpf.slice(arr.length, cpf.length);
}
if (arr.length > 3 && arr.length <= 6) {
return cpf.slice(arr.length + 1, cpf.length);
}
if (arr.length > 6 && arr.length <= 9) {
return cpf.slice(arr.length + 2, cpf.length);
}
if (arr.length > 9 && arr.length < 11) {
return cpf.slice(arr.length + 3, cpf.length);
}
if (arr.length === 11) {
return '';
}
if (arr.length === 12) {
if (inputVal.length === 17) {
return cnpj.slice(16, cnpj.length);
}
return cnpj.slice(15, cnpj.length);
}
if (arr.length > 12 && arr.length <= 14) {
return cnpj.slice(arr.length + 4, cnpj.length);
}
return '';
};
/**
* Propogates the input value back to the Angular model by triggering the onChange function. It won't do this if writingValue
* is true. If that is true it means we are currently in the writeValue function, which is supposed to only update the actual
* DOM element based on the Angular model value. It should be a one way process, i.e. writeValue should not be modifying the Angular
* model value too. Therefore, we don't trigger onChange in this scenario.
* @param inputValue the current form input value
*/
MaskService.prototype.formControlResult = function (inputValue) {
if (!this.writingValue) {
if (Array.isArray(this.dropSpecialCharacters)) {
this.onChange(this._removeMask(this._removeSuffix(this._removePrefix(inputValue)), this.dropSpecialCharacters));
}
else if (this.dropSpecialCharacters) {
this.onChange(this._checkSymbols(inputValue));
}
else {
this.onChange(this._removeSuffix(this._removePrefix(inputValue)));
}
}
};
MaskService.prototype._removeMask = function (value, specialCharactersForRemove) {
return value ? value.replace(this._regExpForRemove(specialCharactersForRemove), '') : value;
};
MaskService.prototype._removePrefix = function (value) {
if (!this.prefix) {
return value;
}
return value ? value.replace(this.prefix, '') : value;
};
MaskService.prototype._removeSuffix = function (value) {
if (!this.suffix) {
return value;
}
return value ? value.replace(this.suffix, '') : value;
};
MaskService.prototype._retrieveSeparatorValue = function (result) {
return this._removeMask(this._removeSuffix(this._removePrefix(result)), this.maskSpecialCharacters);
};
MaskService.prototype._regExpForRemove = function (specialCharactersForRemove) {
return new RegExp(specialCharactersForRemove.map(function (item) { return "\\" + item; }).join('|'), 'gi');
};
MaskService.prototype._checkSymbols = function (result) {
if (result === '') {
return result;
}
var separatorPrecision = this._retrieveSeparatorPrecision(this.maskExpression);
var separatorValue = this._retrieveSeparatorValue(result);
if (this.decimalMarker !== '.') {
separatorValue = separatorValue.replace(this.decimalMarker, '.');
}
if (this.isNumberValue) {
if (separatorPrecision) {
if (result === this.decimalMarker) {
return null;
}
return this._checkPrecision(this.maskExpression, separatorValue);
}