UNPKG

ngx-mask-fixed

Version:
107 lines (106 loc) 4.59 kB
import { Inject, Injectable } from '@angular/core'; import { config } from './config'; var MaskApplierService = (function () { function MaskApplierService(_config) { this._config = _config; this.maskExpression = ''; this._shift = new Set(); this.maskSpecialCharacters = this._config.specialCharacters; this.maskAvailablePatterns = this._config.patterns; 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.sufix = this._config.sufix; } 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; if (inputValue.slice(0, this.prefix.length) === this.prefix) { inputValue = inputValue.slice(this.prefix.length, inputValue.length); } var inputArray = inputValue.toString() .split(''); // tslint:disable-next-line for (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 (this._checkSymbolMask(inputSymbol, maskExpression[cursor]) && maskExpression[cursor + 1] === '*') { result += inputSymbol; multi = true; } else if (maskExpression[cursor + 1] === '*' && multi && this._checkSymbolMask(inputSymbol, maskExpression[cursor + 2])) { result += inputSymbol; cursor += 3; multi = false; } else if (maskExpression[cursor + 1] === '?' && this._checkSymbolMask(inputSymbol, maskExpression[cursor + 2])) { result += inputSymbol; cursor += 3; } else if (this._checkSymbolMask(inputSymbol, maskExpression[cursor])) { 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) { cursor++; i--; } } if (result.length + 1 === maskExpression.length && this.maskSpecialCharacters.indexOf(maskExpression[maskExpression.length - 1]) !== -1) { result += maskExpression[maskExpression.length - 1]; } var shift = 1; var newPosition = position + 1; while (this._shift.has(newPosition)) { shift++; newPosition++; } cb(this._shift.has(position) ? shift : 0); return this.prefix + result; }; MaskApplierService.prototype._checkSymbolMask = function (inputSymbol, maskSymbol) { return inputSymbol === maskSymbol || this.maskAvailablePatterns[maskSymbol] && this.maskAvailablePatterns[maskSymbol].pattern && this.maskAvailablePatterns[maskSymbol].pattern.test(inputSymbol); }; MaskApplierService.decorators = [ { type: Injectable }, ]; /** @nocollapse */ MaskApplierService.ctorParameters = function () { return [ { type: undefined, decorators: [{ type: Inject, args: [config,] },] }, ]; }; return MaskApplierService; }()); export { MaskApplierService };