UNPKG

ngx-mask

Version:
1 lines 245 kB
{"version":3,"file":"ngx-mask.mjs","sources":["../../../projects/ngx-mask-lib/src/lib/ngx-mask-expression.enum.ts","../../../projects/ngx-mask-lib/src/lib/ngx-mask.config.ts","../../../projects/ngx-mask-lib/src/lib/ngx-mask-applier.service.ts","../../../projects/ngx-mask-lib/src/lib/ngx-mask.service.ts","../../../projects/ngx-mask-lib/src/lib/ngx-mask.providers.ts","../../../projects/ngx-mask-lib/src/lib/ngx-mask.directive.ts","../../../projects/ngx-mask-lib/src/lib/ngx-mask.pipe.ts"],"sourcesContent":["export const enum MaskExpression {\n SEPARATOR = 'separator',\n PERCENT = 'percent',\n IP = 'IP',\n CPF_CNPJ = 'CPF_CNPJ',\n MONTH = 'M',\n MONTHS = 'M0',\n MINUTE = 'm',\n HOUR = 'h',\n HOURS = 'H',\n MINUTES = 'm0',\n HOURS_HOUR = 'Hh',\n SECONDS = 's0',\n HOURS_MINUTES_SECONDS = 'Hh:m0:s0',\n EMAIL_MASK = 'A*@A*.A*',\n HOURS_MINUTES = 'Hh:m0',\n MINUTES_SECONDS = 'm0:s0',\n DAYS_MONTHS_YEARS = 'd0/M0/0000',\n DAYS_MONTHS = 'd0/M0',\n DAYS = 'd0',\n DAY = 'd',\n SECOND = 's',\n LETTER_S = 'S',\n DOT = '.',\n COMMA = ',',\n CURLY_BRACKETS_LEFT = '{',\n CURLY_BRACKETS_RIGHT = '}',\n MINUS = '-',\n OR = '||',\n HASH = '#',\n EMPTY_STRING = '',\n SYMBOL_STAR = '*',\n SYMBOL_QUESTION = '?',\n SLASH = '/',\n WHITE_SPACE = ' ',\n NUMBER_ZERO = '0',\n NUMBER_NINE = '9',\n BACKSPACE = 'Backspace',\n DELETE = 'Delete',\n ARROW_LEFT = 'ArrowLeft',\n ARROW_UP = 'ArrowUp',\n DOUBLE_ZERO = '00',\n}\n","import { EventEmitter, InjectionToken } from '@angular/core';\nimport { MaskExpression } from './ngx-mask-expression.enum';\n\nexport type InputTransformFn = (value: unknown) => string | number;\n\nexport type OutputTransformFn = (value: string | number | undefined | null) => unknown;\n\nexport type NgxMaskConfig = {\n suffix: string;\n prefix: string;\n thousandSeparator: string;\n decimalMarker: '.' | ',' | ['.', ','];\n clearIfNotMatch: boolean;\n showMaskTyped: boolean;\n placeHolderCharacter: string;\n shownMaskExpression: string;\n specialCharacters: string[] | readonly string[];\n dropSpecialCharacters: boolean | string[] | readonly string[];\n hiddenInput: boolean;\n validation: boolean;\n instantPrefix: boolean;\n separatorLimit: string;\n apm: boolean;\n allowNegativeNumbers: boolean;\n leadZeroDateTime: boolean;\n leadZero: boolean;\n triggerOnMaskChange: boolean;\n keepCharacterPositions: boolean;\n inputTransformFn: InputTransformFn;\n outputTransformFn: OutputTransformFn;\n maskFilled: EventEmitter<void>;\n patterns: Record<\n string,\n {\n pattern: RegExp;\n optional?: boolean;\n symbol?: string;\n }\n >;\n};\n\nexport type NgxMaskOptions = Partial<NgxMaskConfig>;\nexport const NGX_MASK_CONFIG = new InjectionToken<NgxMaskConfig>('ngx-mask config');\nexport const NEW_CONFIG = new InjectionToken<NgxMaskConfig>('new ngx-mask config');\nexport const INITIAL_CONFIG = new InjectionToken<NgxMaskConfig>('initial ngx-mask config');\n\nexport const initialConfig: NgxMaskConfig = {\n suffix: '',\n prefix: '',\n thousandSeparator: ' ',\n decimalMarker: ['.', ','],\n clearIfNotMatch: false,\n showMaskTyped: false,\n instantPrefix: false,\n placeHolderCharacter: '_',\n dropSpecialCharacters: true,\n hiddenInput: false,\n shownMaskExpression: '',\n separatorLimit: '',\n allowNegativeNumbers: false,\n validation: true,\n specialCharacters: ['-', '/', '(', ')', '.', ':', ' ', '+', ',', '@', '[', ']', '\"', \"'\"],\n leadZeroDateTime: false,\n apm: false,\n leadZero: false,\n keepCharacterPositions: false,\n triggerOnMaskChange: false,\n inputTransformFn: (value: unknown) => value as string | number,\n outputTransformFn: (value: string | number | undefined | null) => value,\n maskFilled: new EventEmitter<void>(),\n patterns: {\n '0': {\n pattern: new RegExp('\\\\d'),\n },\n '9': {\n pattern: new RegExp('\\\\d'),\n optional: true,\n },\n X: {\n pattern: new RegExp('\\\\d'),\n symbol: '*',\n },\n A: {\n pattern: new RegExp('[a-zA-Z0-9]'),\n },\n S: {\n pattern: new RegExp('[a-zA-Z]'),\n },\n U: {\n pattern: new RegExp('[A-Z]'),\n },\n L: {\n pattern: new RegExp('[a-z]'),\n },\n d: {\n pattern: new RegExp('\\\\d'),\n },\n m: {\n pattern: new RegExp('\\\\d'),\n },\n M: {\n pattern: new RegExp('\\\\d'),\n },\n H: {\n pattern: new RegExp('\\\\d'),\n },\n h: {\n pattern: new RegExp('\\\\d'),\n },\n s: {\n pattern: new RegExp('\\\\d'),\n },\n },\n};\n\nexport const timeMasks: string[] = [\n MaskExpression.HOURS_MINUTES_SECONDS,\n MaskExpression.HOURS_MINUTES,\n MaskExpression.MINUTES_SECONDS,\n];\n\nexport const withoutValidation: string[] = [\n MaskExpression.PERCENT,\n MaskExpression.HOURS_HOUR,\n MaskExpression.SECONDS,\n MaskExpression.MINUTES,\n MaskExpression.SEPARATOR,\n MaskExpression.DAYS_MONTHS_YEARS,\n MaskExpression.DAYS_MONTHS,\n MaskExpression.DAYS,\n MaskExpression.MONTHS,\n];\n","import { inject, Injectable } from '@angular/core';\nimport type { NgxMaskConfig } from './ngx-mask.config';\nimport { NGX_MASK_CONFIG } from './ngx-mask.config';\nimport { MaskExpression } from './ngx-mask-expression.enum';\n\n@Injectable()\nexport class NgxMaskApplierService {\n protected _config = inject<NgxMaskConfig>(NGX_MASK_CONFIG);\n\n public dropSpecialCharacters: NgxMaskConfig['dropSpecialCharacters'] =\n this._config.dropSpecialCharacters;\n\n public hiddenInput: NgxMaskConfig['hiddenInput'] = this._config.hiddenInput;\n\n public clearIfNotMatch: NgxMaskConfig['clearIfNotMatch'] = this._config.clearIfNotMatch;\n\n public specialCharacters: NgxMaskConfig['specialCharacters'] = this._config.specialCharacters;\n\n public patterns: NgxMaskConfig['patterns'] = this._config.patterns;\n\n public prefix: NgxMaskConfig['prefix'] = this._config.prefix;\n\n public suffix: NgxMaskConfig['suffix'] = this._config.suffix;\n\n public thousandSeparator: NgxMaskConfig['thousandSeparator'] = this._config.thousandSeparator;\n\n public decimalMarker: NgxMaskConfig['decimalMarker'] = this._config.decimalMarker;\n\n public customPattern!: NgxMaskConfig['patterns'];\n\n public showMaskTyped: NgxMaskConfig['showMaskTyped'] = this._config.showMaskTyped;\n\n public placeHolderCharacter: NgxMaskConfig['placeHolderCharacter'] =\n this._config.placeHolderCharacter;\n\n public validation: NgxMaskConfig['validation'] = this._config.validation;\n\n public separatorLimit: NgxMaskConfig['separatorLimit'] = this._config.separatorLimit;\n\n public allowNegativeNumbers: NgxMaskConfig['allowNegativeNumbers'] =\n this._config.allowNegativeNumbers;\n\n public leadZeroDateTime: NgxMaskConfig['leadZeroDateTime'] = this._config.leadZeroDateTime;\n\n public leadZero: NgxMaskConfig['leadZero'] = this._config.leadZero;\n\n public apm: NgxMaskConfig['apm'] = this._config.apm;\n\n public inputTransformFn: NgxMaskConfig['inputTransformFn'] = this._config.inputTransformFn;\n\n public outputTransformFn: NgxMaskConfig['outputTransformFn'] = this._config.outputTransformFn;\n\n public keepCharacterPositions: NgxMaskConfig['keepCharacterPositions'] =\n this._config.keepCharacterPositions;\n\n public instantPrefix: NgxMaskConfig['instantPrefix'] = this._config.instantPrefix;\n\n private _shift = new Set<number>();\n\n public plusOnePosition = false;\n\n public maskExpression = '';\n\n public actualValue = '';\n\n public showKeepCharacterExp = '';\n\n public shownMaskExpression = '';\n\n public deletedSpecialCharacter = false;\n\n public ipError?: boolean;\n\n public cpfCnpjError?: boolean;\n\n public applyMask(\n inputValue: string | object | boolean | null | undefined,\n maskExpression: string,\n position = 0,\n justPasted = false,\n backspaced = false,\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n cb: (...args: any[]) => any = () => {}\n ): string {\n if (!maskExpression || typeof inputValue !== 'string') {\n return MaskExpression.EMPTY_STRING;\n }\n let cursor = 0;\n let result = '';\n let multi = false;\n let backspaceShift = false;\n let shift = 1;\n let stepBack = false;\n let processedValue = inputValue;\n let processedPosition = position;\n\n if (processedValue.slice(0, this.prefix.length) === this.prefix) {\n processedValue = processedValue.slice(this.prefix.length, processedValue.length);\n }\n if (!!this.suffix && processedValue.length > 0) {\n processedValue = this.checkAndRemoveSuffix(processedValue);\n }\n if (processedValue === '(' && this.prefix) {\n processedValue = '';\n }\n const inputArray: string[] = processedValue.toString().split(MaskExpression.EMPTY_STRING);\n if (\n this.allowNegativeNumbers &&\n processedValue.slice(cursor, cursor + 1) === MaskExpression.MINUS\n ) {\n result += processedValue.slice(cursor, cursor + 1);\n }\n if (maskExpression === MaskExpression.IP) {\n const valuesIP = processedValue.split(MaskExpression.DOT);\n this.ipError = this._validIP(valuesIP);\n\n // eslint-disable-next-line no-param-reassign\n maskExpression = '099.099.099.099';\n }\n const arr: string[] = [];\n // eslint-disable-next-line @typescript-eslint/prefer-for-of\n for (let i = 0; i < processedValue.length; i++) {\n if (processedValue[i]?.match('\\\\d')) {\n arr.push(processedValue[i] ?? MaskExpression.EMPTY_STRING);\n }\n }\n if (maskExpression === MaskExpression.CPF_CNPJ) {\n this.cpfCnpjError = arr.length !== 11 && arr.length !== 14;\n if (arr.length > 11) {\n // eslint-disable-next-line no-param-reassign\n maskExpression = '00.000.000/0000-00';\n } else {\n // eslint-disable-next-line no-param-reassign\n maskExpression = '000.000.000-00';\n }\n }\n if (maskExpression.startsWith(MaskExpression.PERCENT)) {\n if (\n processedValue.match('[a-z]|[A-Z]') ||\n // eslint-disable-next-line no-useless-escape\n (processedValue.match(/[-!$%^&*()_+|~=`{}\\[\\]:\";'<>?,\\/.]/) && !backspaced)\n ) {\n processedValue = this._stripToDecimal(processedValue);\n const precision: number = this.getPrecision(maskExpression);\n\n processedValue = this.checkInputPrecision(\n processedValue,\n precision,\n this.decimalMarker\n );\n }\n const decimalMarker =\n typeof this.decimalMarker === 'string' ? this.decimalMarker : MaskExpression.DOT;\n if (\n processedValue.indexOf(decimalMarker) > 0 &&\n !this.percentage(processedValue.substring(0, processedValue.indexOf(decimalMarker)))\n ) {\n let base: string = processedValue.substring(\n 0,\n processedValue.indexOf(decimalMarker) - 1\n );\n if (\n this.allowNegativeNumbers &&\n processedValue.slice(cursor, cursor + 1) === MaskExpression.MINUS &&\n !backspaced\n ) {\n base = processedValue.substring(0, processedValue.indexOf(decimalMarker));\n }\n\n processedValue = `${base}${processedValue.substring(\n processedValue.indexOf(decimalMarker),\n processedValue.length\n )}`;\n }\n let value = '';\n // eslint-disable-next-line @typescript-eslint/no-unused-expressions\n this.allowNegativeNumbers &&\n processedValue.slice(cursor, cursor + 1) === MaskExpression.MINUS\n ? (value = `${MaskExpression.MINUS}${processedValue.slice(cursor + 1, cursor + processedValue.length)}`)\n : (value = processedValue);\n if (this.percentage(value)) {\n result = this._splitPercentZero(processedValue);\n } else {\n result = this._splitPercentZero(\n processedValue.substring(0, processedValue.length - 1)\n );\n }\n } else if (maskExpression.startsWith(MaskExpression.SEPARATOR)) {\n if (\n processedValue.match('[wа-яА-Я]') ||\n processedValue.match('[ЁёА-я]') ||\n processedValue.match('[a-z]|[A-Z]') ||\n processedValue.match(/[-@#!$%\\\\^&*()_£¬'+|~=`{}\\]:\";<>.?/]/) ||\n processedValue.match('[^A-Za-z0-9,]')\n ) {\n processedValue = this._stripToDecimal(processedValue);\n }\n\n const precision: number = this.getPrecision(maskExpression);\n let decimalMarker = this.decimalMarker;\n\n if (Array.isArray(this.decimalMarker)) {\n if (\n this.actualValue.includes(this.decimalMarker[0]) ||\n this.actualValue.includes(this.decimalMarker[1])\n ) {\n decimalMarker = this.actualValue.includes(this.decimalMarker[0])\n ? this.decimalMarker[0]\n : this.decimalMarker[1];\n } else {\n decimalMarker = this.decimalMarker.find(\n (dm) => dm !== this.thousandSeparator\n ) as '.' | ',';\n }\n }\n\n if (backspaced) {\n const { decimalMarkerIndex, nonZeroIndex } = this._findFirstNonZeroAndDecimalIndex(\n processedValue,\n decimalMarker as '.' | ','\n );\n const zeroIndexMinus = processedValue[0] === MaskExpression.MINUS;\n const zeroIndexNumberZero = processedValue[0] === MaskExpression.NUMBER_ZERO;\n const zeroIndexDecimalMarker = processedValue[0] === decimalMarker;\n const firstIndexDecimalMarker = processedValue[1] === decimalMarker;\n\n if (\n (zeroIndexDecimalMarker && !nonZeroIndex) ||\n (zeroIndexMinus && firstIndexDecimalMarker && !nonZeroIndex) ||\n (zeroIndexNumberZero && !decimalMarkerIndex && !nonZeroIndex)\n ) {\n processedValue = MaskExpression.NUMBER_ZERO;\n }\n\n if (\n decimalMarkerIndex &&\n nonZeroIndex &&\n zeroIndexMinus &&\n processedPosition === 1\n ) {\n if (decimalMarkerIndex < nonZeroIndex || decimalMarkerIndex > nonZeroIndex) {\n processedValue = MaskExpression.MINUS + processedValue.slice(nonZeroIndex);\n }\n }\n\n if (!decimalMarkerIndex && nonZeroIndex && processedValue.length > nonZeroIndex) {\n processedValue = zeroIndexMinus\n ? MaskExpression.MINUS + processedValue.slice(nonZeroIndex)\n : processedValue.slice(nonZeroIndex);\n }\n\n if (decimalMarkerIndex && nonZeroIndex && processedPosition === 0) {\n if (decimalMarkerIndex < nonZeroIndex) {\n processedValue = processedValue.slice(decimalMarkerIndex - 1);\n }\n if (decimalMarkerIndex > nonZeroIndex) {\n processedValue = processedValue.slice(nonZeroIndex);\n }\n }\n }\n\n if (precision === 0) {\n processedValue = this.allowNegativeNumbers\n ? processedValue.length > 2 &&\n processedValue[0] === MaskExpression.MINUS &&\n processedValue[1] === MaskExpression.NUMBER_ZERO &&\n processedValue[2] !== this.thousandSeparator &&\n processedValue[2] !== MaskExpression.COMMA &&\n processedValue[2] !== MaskExpression.DOT\n ? '-' + processedValue.slice(2, processedValue.length)\n : processedValue[0] === MaskExpression.NUMBER_ZERO &&\n processedValue.length > 1 &&\n processedValue[1] !== this.thousandSeparator &&\n processedValue[1] !== MaskExpression.COMMA &&\n processedValue[1] !== MaskExpression.DOT\n ? processedValue.slice(1, processedValue.length)\n : processedValue\n : processedValue.length > 1 &&\n processedValue[0] === MaskExpression.NUMBER_ZERO &&\n processedValue[1] !== this.thousandSeparator &&\n processedValue[1] !== MaskExpression.COMMA &&\n processedValue[1] !== MaskExpression.DOT\n ? processedValue.slice(1, processedValue.length)\n : processedValue;\n } else {\n if (\n processedValue[0] === decimalMarker &&\n processedValue.length > 1 &&\n !backspaced\n ) {\n processedValue =\n MaskExpression.NUMBER_ZERO +\n processedValue.slice(0, processedValue.length + 1);\n this.plusOnePosition = true;\n }\n if (\n processedValue[0] === MaskExpression.NUMBER_ZERO &&\n processedValue[1] !== decimalMarker &&\n processedValue[1] !== this.thousandSeparator &&\n !backspaced\n ) {\n processedValue =\n processedValue.length > 1\n ? processedValue.slice(0, 1) +\n decimalMarker +\n processedValue.slice(1, processedValue.length + 1)\n : processedValue;\n this.plusOnePosition = true;\n }\n if (\n this.allowNegativeNumbers &&\n !backspaced &&\n processedValue[0] === MaskExpression.MINUS &&\n (processedValue[1] === decimalMarker ||\n processedValue[1] === MaskExpression.NUMBER_ZERO)\n ) {\n processedValue =\n processedValue[1] === decimalMarker && processedValue.length > 2\n ? processedValue.slice(0, 1) +\n MaskExpression.NUMBER_ZERO +\n processedValue.slice(1, processedValue.length)\n : processedValue[1] === MaskExpression.NUMBER_ZERO &&\n processedValue.length > 2 &&\n processedValue[2] !== decimalMarker\n ? processedValue.slice(0, 2) +\n decimalMarker +\n processedValue.slice(2, processedValue.length)\n : processedValue;\n this.plusOnePosition = true;\n }\n }\n\n // TODO: we had different rexexps here for the different cases... but tests dont seam to bother - check this\n // separator: no COMMA, dot-sep: no SPACE, COMMA OK, comma-sep: no SPACE, COMMA OK\n\n const thousandSeparatorCharEscaped: string = this._charToRegExpExpression(\n this.thousandSeparator\n );\n let invalidChars: string = '@#!$%^&*()_+|~=`{}\\\\[\\\\]:\\\\s,\\\\.\";<>?\\\\/'.replace(\n thousandSeparatorCharEscaped,\n ''\n );\n //.replace(decimalMarkerEscaped, '');\n if (Array.isArray(this.decimalMarker)) {\n for (const marker of this.decimalMarker) {\n invalidChars = invalidChars.replace(\n this._charToRegExpExpression(marker),\n MaskExpression.EMPTY_STRING\n );\n }\n } else {\n invalidChars = invalidChars.replace(\n this._charToRegExpExpression(this.decimalMarker),\n ''\n );\n }\n\n const invalidCharRegexp = new RegExp('[' + invalidChars + ']');\n if (processedValue.match(invalidCharRegexp)) {\n processedValue = processedValue.substring(0, processedValue.length - 1);\n }\n\n processedValue = this.checkInputPrecision(\n processedValue,\n precision,\n this.decimalMarker\n );\n const strForSep: string = processedValue.replace(\n new RegExp(thousandSeparatorCharEscaped, 'g'),\n ''\n );\n\n result = this._formatWithSeparators(\n strForSep,\n this.thousandSeparator,\n this.decimalMarker,\n precision\n );\n\n const commaShift: number =\n result.indexOf(MaskExpression.COMMA) - processedValue.indexOf(MaskExpression.COMMA);\n const shiftStep: number = result.length - processedValue.length;\n const backspacedDecimalMarkerWithSeparatorLimit =\n backspaced && result.length < inputValue.length && this.separatorLimit;\n\n if (\n (result[processedPosition - 1] === this.thousandSeparator ||\n result[processedPosition - this.prefix.length]) &&\n this.prefix &&\n backspaced\n ) {\n processedPosition = processedPosition - 1;\n } else if (\n (shiftStep > 0 && result[processedPosition] !== this.thousandSeparator) ||\n backspacedDecimalMarkerWithSeparatorLimit\n ) {\n backspaceShift = true;\n let _shift = 0;\n do {\n this._shift.add(processedPosition + _shift);\n _shift++;\n } while (_shift < shiftStep);\n } else if (\n result[processedPosition - 1] === this.thousandSeparator ||\n shiftStep === -4 ||\n shiftStep === -3 ||\n result[processedPosition] === this.thousandSeparator\n ) {\n this._shift.clear();\n this._shift.add(processedPosition - 1);\n } else if (\n (commaShift !== 0 &&\n processedPosition > 0 &&\n !(\n result.indexOf(MaskExpression.COMMA) >= processedPosition &&\n processedPosition > 3\n )) ||\n (!(\n result.indexOf(MaskExpression.DOT) >= processedPosition && processedPosition > 3\n ) &&\n shiftStep <= 0)\n ) {\n this._shift.clear();\n backspaceShift = true;\n shift = shiftStep;\n\n processedPosition += shiftStep;\n this._shift.add(processedPosition);\n } else {\n this._shift.clear();\n }\n } else {\n for (\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n let i = 0, inputSymbol: string = inputArray[0]!;\n i < inputArray.length;\n i++, inputSymbol = inputArray[i] ?? MaskExpression.EMPTY_STRING\n ) {\n if (cursor === maskExpression.length) {\n break;\n }\n\n const symbolStarInPattern: boolean = MaskExpression.SYMBOL_STAR in this.patterns;\n if (\n this._checkSymbolMask(\n inputSymbol,\n maskExpression[cursor] ?? MaskExpression.EMPTY_STRING\n ) &&\n maskExpression[cursor + 1] === MaskExpression.SYMBOL_QUESTION\n ) {\n result += inputSymbol;\n cursor += 2;\n } else if (\n maskExpression[cursor + 1] === MaskExpression.SYMBOL_STAR &&\n multi &&\n this._checkSymbolMask(\n inputSymbol,\n maskExpression[cursor + 2] ?? MaskExpression.EMPTY_STRING\n )\n ) {\n result += inputSymbol;\n cursor += 3;\n multi = false;\n } else if (\n this._checkSymbolMask(\n inputSymbol,\n maskExpression[cursor] ?? MaskExpression.EMPTY_STRING\n ) &&\n maskExpression[cursor + 1] === MaskExpression.SYMBOL_STAR &&\n !symbolStarInPattern\n ) {\n result += inputSymbol;\n multi = true;\n } else if (\n maskExpression[cursor + 1] === MaskExpression.SYMBOL_QUESTION &&\n this._checkSymbolMask(\n inputSymbol,\n maskExpression[cursor + 2] ?? MaskExpression.EMPTY_STRING\n )\n ) {\n result += inputSymbol;\n cursor += 3;\n } else if (\n this._checkSymbolMask(\n inputSymbol,\n maskExpression[cursor] ?? MaskExpression.EMPTY_STRING\n )\n ) {\n if (maskExpression[cursor] === MaskExpression.HOURS) {\n if (this.apm ? Number(inputSymbol) > 9 : Number(inputSymbol) > 2) {\n processedPosition = !this.leadZeroDateTime\n ? processedPosition + 1\n : processedPosition;\n cursor += 1;\n this._shiftStep(cursor);\n i--;\n if (this.leadZeroDateTime) {\n result += '0';\n }\n continue;\n }\n }\n if (maskExpression[cursor] === MaskExpression.HOUR) {\n if (\n this.apm\n ? (result.length === 1 && Number(result) > 1) ||\n (result === '1' && Number(inputSymbol) > 2) ||\n (processedValue.slice(cursor - 1, cursor).length === 1 &&\n Number(processedValue.slice(cursor - 1, cursor)) > 2) ||\n (processedValue.slice(cursor - 1, cursor) === '1' &&\n Number(inputSymbol) > 2)\n : (result === '2' && Number(inputSymbol) > 3) ||\n ((result.slice(cursor - 2, cursor) === '2' ||\n result.slice(cursor - 3, cursor) === '2' ||\n result.slice(cursor - 4, cursor) === '2' ||\n result.slice(cursor - 1, cursor) === '2') &&\n Number(inputSymbol) > 3 &&\n cursor > 10)\n ) {\n processedPosition = processedPosition + 1;\n cursor += 1;\n i--;\n continue;\n }\n }\n if (\n maskExpression[cursor] === MaskExpression.MINUTE ||\n maskExpression[cursor] === MaskExpression.SECOND\n ) {\n if (Number(inputSymbol) > 5) {\n processedPosition = !this.leadZeroDateTime\n ? processedPosition + 1\n : processedPosition;\n cursor += 1;\n this._shiftStep(cursor);\n i--;\n if (this.leadZeroDateTime) {\n result += '0';\n }\n continue;\n }\n }\n const daysCount = 31;\n const inputValueCursor = processedValue[cursor] as string;\n const inputValueCursorPlusOne = processedValue[cursor + 1] as string;\n const inputValueCursorPlusTwo = processedValue[cursor + 2] as string;\n const inputValueCursorMinusOne = processedValue[cursor - 1] as string;\n const inputValueCursorMinusTwo = processedValue[cursor - 2] as string;\n const inputValueSliceMinusThreeMinusOne = processedValue.slice(\n cursor - 3,\n cursor - 1\n );\n const inputValueSliceMinusOnePlusOne = processedValue.slice(\n cursor - 1,\n cursor + 1\n );\n const inputValueSliceCursorPlusTwo = processedValue.slice(cursor, cursor + 2);\n const inputValueSliceMinusTwoCursor = processedValue.slice(cursor - 2, cursor);\n if (maskExpression[cursor] === MaskExpression.DAY) {\n const maskStartWithMonth =\n maskExpression.slice(0, 2) === MaskExpression.MONTHS;\n const startWithMonthInput: boolean =\n maskExpression.slice(0, 2) === MaskExpression.MONTHS &&\n this.specialCharacters.includes(inputValueCursorMinusTwo);\n if (\n (Number(inputSymbol) > 3 && this.leadZeroDateTime) ||\n (!maskStartWithMonth &&\n (Number(inputValueSliceCursorPlusTwo) > daysCount ||\n Number(inputValueSliceMinusOnePlusOne) > daysCount ||\n this.specialCharacters.includes(inputValueCursorPlusOne))) ||\n (startWithMonthInput\n ? Number(inputValueSliceMinusOnePlusOne) > daysCount ||\n (!this.specialCharacters.includes(inputValueCursor) &&\n this.specialCharacters.includes(inputValueCursorPlusTwo)) ||\n this.specialCharacters.includes(inputValueCursor)\n : Number(inputValueSliceCursorPlusTwo) > daysCount ||\n (this.specialCharacters.includes(inputValueCursorPlusOne) &&\n !backspaced))\n ) {\n processedPosition = !this.leadZeroDateTime\n ? processedPosition + 1\n : processedPosition;\n cursor += 1;\n this._shiftStep(cursor);\n i--;\n\n if (this.leadZeroDateTime) {\n result += '0';\n }\n continue;\n }\n }\n if (maskExpression[cursor] === MaskExpression.MONTH) {\n const monthsCount = 12;\n // mask without day\n const withoutDays: boolean =\n cursor === 0 &&\n (Number(inputSymbol) > 2 ||\n Number(inputValueSliceCursorPlusTwo) > monthsCount ||\n (this.specialCharacters.includes(inputValueCursorPlusOne) &&\n !backspaced));\n // day<10 && month<12 for input\n const specialChart = maskExpression.slice(cursor + 2, cursor + 3);\n const day1monthInput: boolean =\n inputValueSliceMinusThreeMinusOne.includes(specialChart) &&\n maskExpression.includes('d0') &&\n ((this.specialCharacters.includes(inputValueCursorMinusTwo) &&\n Number(inputValueSliceMinusOnePlusOne) > monthsCount &&\n !this.specialCharacters.includes(inputValueCursor)) ||\n this.specialCharacters.includes(inputValueCursor));\n // month<12 && day<10 for input\n const day2monthInput: boolean =\n Number(inputValueSliceMinusThreeMinusOne) <= daysCount &&\n !this.specialCharacters.includes(\n inputValueSliceMinusThreeMinusOne as string\n ) &&\n this.specialCharacters.includes(inputValueCursorMinusOne) &&\n (Number(inputValueSliceCursorPlusTwo) > monthsCount ||\n this.specialCharacters.includes(inputValueCursorPlusOne));\n // cursor === 5 && without days\n const day2monthInputDot: boolean =\n (Number(inputValueSliceCursorPlusTwo) > monthsCount && cursor === 5) ||\n (this.specialCharacters.includes(inputValueCursorPlusOne) &&\n cursor === 5);\n // // day<10 && month<12 for paste whole data\n const day1monthPaste: boolean =\n Number(inputValueSliceMinusThreeMinusOne) > daysCount &&\n !this.specialCharacters.includes(\n inputValueSliceMinusThreeMinusOne as string\n ) &&\n !this.specialCharacters.includes(\n inputValueSliceMinusTwoCursor as string\n ) &&\n Number(inputValueSliceMinusTwoCursor) > monthsCount &&\n maskExpression.includes('d0');\n // 10<day<31 && month<12 for paste whole data\n const day2monthPaste: boolean =\n Number(inputValueSliceMinusThreeMinusOne) <= daysCount &&\n !this.specialCharacters.includes(\n inputValueSliceMinusThreeMinusOne as string\n ) &&\n !this.specialCharacters.includes(inputValueCursorMinusOne) &&\n Number(inputValueSliceMinusOnePlusOne) > monthsCount;\n if (\n (Number(inputSymbol) > 1 && this.leadZeroDateTime) ||\n withoutDays ||\n day1monthInput ||\n day2monthPaste ||\n day1monthPaste ||\n day2monthInput ||\n (day2monthInputDot && !this.leadZeroDateTime)\n ) {\n processedPosition = !this.leadZeroDateTime\n ? processedPosition + 1\n : processedPosition;\n cursor += 1;\n this._shiftStep(cursor);\n i--;\n if (this.leadZeroDateTime) {\n result += '0';\n }\n continue;\n }\n }\n result += inputSymbol;\n cursor++;\n } else if (\n this.specialCharacters.includes(inputSymbol) &&\n maskExpression[cursor] === inputSymbol\n ) {\n result += inputSymbol;\n cursor++;\n } else if (\n this.specialCharacters.indexOf(\n maskExpression[cursor] ?? MaskExpression.EMPTY_STRING\n ) !== -1\n ) {\n result += maskExpression[cursor];\n cursor++;\n this._shiftStep(cursor);\n i--;\n } else if (\n maskExpression[cursor] === MaskExpression.NUMBER_NINE &&\n this.showMaskTyped\n ) {\n this._shiftStep(cursor);\n } else if (\n this.patterns[maskExpression[cursor] ?? MaskExpression.EMPTY_STRING] &&\n this.patterns[maskExpression[cursor] ?? MaskExpression.EMPTY_STRING]?.optional\n ) {\n if (\n !!inputArray[cursor] &&\n maskExpression !== '099.099.099.099' &&\n maskExpression !== '000.000.000-00' &&\n maskExpression !== '00.000.000/0000-00' &&\n !maskExpression.match(/^9+\\.0+$/) &&\n !this.patterns[maskExpression[cursor] ?? MaskExpression.EMPTY_STRING]\n ?.optional\n ) {\n result += inputArray[cursor];\n }\n if (\n maskExpression.includes(\n MaskExpression.NUMBER_NINE + MaskExpression.SYMBOL_STAR\n ) &&\n maskExpression.includes(\n MaskExpression.NUMBER_ZERO + MaskExpression.SYMBOL_STAR\n )\n ) {\n cursor++;\n }\n\n cursor++;\n i--;\n } else if (\n this.maskExpression[cursor + 1] === MaskExpression.SYMBOL_STAR &&\n this._findSpecialChar(\n this.maskExpression[cursor + 2] ?? MaskExpression.EMPTY_STRING\n ) &&\n this._findSpecialChar(inputSymbol) === this.maskExpression[cursor + 2] &&\n multi\n ) {\n cursor += 3;\n result += inputSymbol;\n } else if (\n this.maskExpression[cursor + 1] === MaskExpression.SYMBOL_QUESTION &&\n this._findSpecialChar(\n this.maskExpression[cursor + 2] ?? MaskExpression.EMPTY_STRING\n ) &&\n this._findSpecialChar(inputSymbol) === this.maskExpression[cursor + 2] &&\n multi\n ) {\n cursor += 3;\n result += inputSymbol;\n } else if (\n this.showMaskTyped &&\n this.specialCharacters.indexOf(inputSymbol) < 0 &&\n inputSymbol !== this.placeHolderCharacter &&\n this.placeHolderCharacter.length === 1\n ) {\n stepBack = true;\n }\n }\n }\n if (\n result[processedPosition - 1] &&\n result.length + 1 === maskExpression.length &&\n this.specialCharacters.indexOf(\n maskExpression[maskExpression.length - 1] ?? MaskExpression.EMPTY_STRING\n ) !== -1\n ) {\n result += maskExpression[maskExpression.length - 1];\n }\n let newPosition: number = processedPosition + 1;\n\n while (this._shift.has(newPosition)) {\n shift++;\n newPosition++;\n }\n\n let actualShift: number =\n justPasted && !maskExpression.startsWith(MaskExpression.SEPARATOR)\n ? cursor\n : this._shift.has(processedPosition)\n ? shift\n : 0;\n if (stepBack) {\n actualShift--;\n }\n\n cb(actualShift, backspaceShift);\n if (shift < 0) {\n this._shift.clear();\n }\n let onlySpecial = false;\n if (backspaced) {\n onlySpecial = inputArray.every((char) => this.specialCharacters.includes(char));\n }\n\n let res = `${this.prefix}${onlySpecial ? MaskExpression.EMPTY_STRING : result}${\n this.showMaskTyped ? '' : this.suffix\n }`;\n\n if (result.length === 0) {\n res = this.instantPrefix ? `${this.prefix}${result}` : `${result}`;\n }\n\n const isSpecialCharacterMaskFirstSymbol =\n processedValue.length === 1 &&\n this.specialCharacters.includes(maskExpression[0] as string) &&\n processedValue !== maskExpression[0];\n\n if (\n !this._checkSymbolMask(processedValue, maskExpression[1] as string) &&\n isSpecialCharacterMaskFirstSymbol\n ) {\n return '';\n }\n\n if (result.includes(MaskExpression.MINUS) && this.prefix && this.allowNegativeNumbers) {\n if (backspaced && result === MaskExpression.MINUS) {\n return '';\n }\n res = `${MaskExpression.MINUS}${this.prefix}${result\n .split(MaskExpression.MINUS)\n .join(MaskExpression.EMPTY_STRING)}${this.suffix}`;\n }\n return res;\n }\n\n public _findDropSpecialChar(inputSymbol: string): undefined | string {\n if (Array.isArray(this.dropSpecialCharacters)) {\n return this.dropSpecialCharacters.find((val: string) => val === inputSymbol);\n }\n return this._findSpecialChar(inputSymbol);\n }\n\n public _findSpecialChar(inputSymbol: string): undefined | string {\n return this.specialCharacters.find((val: string) => val === inputSymbol);\n }\n\n public _checkSymbolMask(inputSymbol: string, maskSymbol: string): boolean {\n this.patterns = this.customPattern ? this.customPattern : this.patterns;\n return (\n (this.patterns[maskSymbol]?.pattern &&\n this.patterns[maskSymbol]?.pattern.test(inputSymbol)) ??\n false\n );\n }\n\n private _formatWithSeparators = (\n str: string,\n thousandSeparatorChar: string,\n decimalChars: string | string[],\n precision: number\n ) => {\n let x: string[] = [];\n let decimalChar = '';\n\n if (Array.isArray(decimalChars)) {\n const regExp = new RegExp(\n decimalChars.map((v) => ('[\\\\^$.|?*+()'.indexOf(v) >= 0 ? `\\\\${v}` : v)).join('|')\n );\n x = str.split(regExp);\n decimalChar = str.match(regExp)?.[0] ?? MaskExpression.EMPTY_STRING;\n } else {\n x = str.split(decimalChars);\n decimalChar = decimalChars;\n }\n const decimals: string =\n x.length > 1 ? `${decimalChar}${x[1]}` : MaskExpression.EMPTY_STRING;\n let res: string = x[0] ?? MaskExpression.EMPTY_STRING;\n const separatorLimit: string = this.separatorLimit.replace(\n /\\s/g,\n MaskExpression.EMPTY_STRING\n );\n if (separatorLimit && +separatorLimit) {\n if (res[0] === MaskExpression.MINUS) {\n res = `-${res.slice(1, res.length).slice(0, separatorLimit.length)}`;\n } else {\n res = res.slice(0, separatorLimit.length);\n }\n }\n const rgx = /(\\d+)(\\d{3})/;\n\n while (thousandSeparatorChar && rgx.test(res)) {\n res = res.replace(rgx, '$1' + thousandSeparatorChar + '$2');\n }\n\n if (typeof precision === 'undefined') {\n return res + decimals;\n } else if (precision === 0) {\n return res;\n }\n return res + decimals.substring(0, precision + 1);\n };\n\n private percentage = (str: string): boolean => {\n const sanitizedStr = str.replace(',', '.');\n const value = Number(\n this.allowNegativeNumbers && str.includes(MaskExpression.MINUS)\n ? sanitizedStr.slice(1, str.length)\n : sanitizedStr\n );\n\n return !isNaN(value) && value >= 0 && value <= 100;\n };\n\n public getPrecision = (maskExpression: string): number => {\n const x: string[] = maskExpression.split(MaskExpression.DOT);\n if (x.length > 1) {\n return Number(x[x.length - 1]);\n }\n\n return Infinity;\n };\n\n private checkAndRemoveSuffix = (inputValue: string): string => {\n for (let i = this.suffix?.length - 1; i >= 0; i--) {\n const substr = this.suffix.substring(i, this.suffix?.length);\n if (\n inputValue.includes(substr) &&\n i !== this.suffix?.length - 1 &&\n (i - 1 < 0 ||\n !inputValue.includes(this.suffix.substring(i - 1, this.suffix?.length)))\n ) {\n return inputValue.replace(substr, MaskExpression.EMPTY_STRING);\n }\n }\n return inputValue;\n };\n\n private checkInputPrecision = (\n inputValue: string,\n precision: number,\n decimalMarker: NgxMaskConfig['decimalMarker']\n ): string => {\n let processedInputValue = inputValue;\n let processedDecimalMarker = decimalMarker;\n\n if (precision < Infinity) {\n // TODO need think about decimalMarker\n if (Array.isArray(processedDecimalMarker)) {\n const marker = processedDecimalMarker.find((dm) => dm !== this.thousandSeparator);\n\n processedDecimalMarker = marker ? marker : processedDecimalMarker[0];\n }\n const precisionRegEx = new RegExp(\n this._charToRegExpExpression(processedDecimalMarker) + `\\\\d{${precision}}.*$`\n );\n const precisionMatch: RegExpMatchArray | null =\n processedInputValue.match(precisionRegEx);\n const precisionMatchLength: number = (precisionMatch && precisionMatch[0]?.length) ?? 0;\n if (precisionMatchLength - 1 > precision) {\n const diff = precisionMatchLength - 1 - precision;\n\n processedInputValue = processedInputValue.substring(\n 0,\n processedInputValue.length - diff\n );\n }\n if (\n precision === 0 &&\n this._compareOrIncludes(\n processedInputValue[processedInputValue.length - 1],\n processedDecimalMarker,\n this.thousandSeparator\n )\n ) {\n processedInputValue = processedInputValue.substring(\n 0,\n processedInputValue.length - 1\n );\n }\n }\n return processedInputValue;\n };\n\n private _stripToDecimal(str: string): string {\n return str\n .split(MaskExpression.EMPTY_STRING)\n .filter((i: string, idx: number) => {\n const isDecimalMarker =\n typeof this.decimalMarker === 'string'\n ? i === this.decimalMarker\n : // TODO (inepipenko) use utility type\n this.decimalMarker.includes(\n i as MaskExpression.COMMA | MaskExpression.DOT\n );\n return (\n i.match('^-?\\\\d') ||\n i === this.thousandSeparator ||\n isDecimalMarker ||\n (i === MaskExpression.MINUS && idx === 0 && this.allowNegativeNumbers)\n