Skip to content

Commit

Permalink
fix(#601): fix 601
Browse files Browse the repository at this point in the history
  • Loading branch information
andriikamaldinov1 committed Jun 8, 2023
1 parent 780b43c commit b5d5ec4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
24 changes: 11 additions & 13 deletions projects/ngx-mask-lib/src/lib/ngx-mask-applier.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { inject, Injectable } from '@angular/core';
import { NGX_MASK_CONFIG, IConfig } from './ngx-mask.config';

export const enum MaskExpression {
SEPARATOR = 'separator',
PERCENT = 'percent',
}
import { MaskExpression } from './ngx-mask-expression.enum';

@Injectable()

export class NgxMaskApplierService {
protected _config = inject<IConfig>(NGX_MASK_CONFIG);

Expand Down Expand Up @@ -107,7 +102,7 @@ export class NgxMaskApplierService {
// eslint-disable-next-line no-param-reassign
result += inputValue.slice(cursor, cursor + 1);
}
if (maskExpression === 'IP') {
if (maskExpression === MaskExpression.IP) {
const valuesIP = inputValue.split('.');
this.ipError = this._validIP(valuesIP);
// eslint-disable-next-line no-param-reassign
Expand All @@ -119,7 +114,7 @@ export class NgxMaskApplierService {
arr.push(inputValue[i] ?? '');
}
}
if (maskExpression === 'CPF_CNPJ') {
if (maskExpression === MaskExpression.CPF_CNPJ) {
this.cpfCnpjError = arr.length !== 11 && arr.length !== 14;
if (arr.length > 11) {
// eslint-disable-next-line no-param-reassign
Expand Down Expand Up @@ -310,7 +305,7 @@ export class NgxMaskApplierService {
result += inputSymbol;
cursor += 3;
} else if (this._checkSymbolMask(inputSymbol, maskExpression[cursor] ?? '')) {
if (maskExpression[cursor] === 'H') {
if (maskExpression[cursor] === MaskExpression.HOURS) {
if (Number(inputSymbol) > 2) {
// eslint-disable-next-line no-param-reassign
position = position + 1;
Expand All @@ -323,7 +318,7 @@ export class NgxMaskApplierService {
continue;
}
}
if (maskExpression[cursor] === 'h') {
if (maskExpression[cursor] === MaskExpression.HOUR) {
if (
(result === '2' && Number(inputSymbol) > 3) ||
((result.slice(cursor - 2, cursor) === '2' ||
Expand All @@ -340,7 +335,10 @@ export class NgxMaskApplierService {
continue;
}
}
if (maskExpression[cursor] === 'm' || maskExpression[cursor] === 's') {
if (
maskExpression[cursor] === MaskExpression.MINUTE ||
maskExpression[cursor] === MaskExpression.SECOND
) {
if (Number(inputSymbol) > 5) {
// eslint-disable-next-line no-param-reassign
position = position + 1;
Expand All @@ -367,7 +365,7 @@ export class NgxMaskApplierService {
const inputValueSliceMinusOnePlusOne = inputValue.slice(cursor - 1, cursor + 1);
const inputValueSliceCursorPlusTwo = inputValue.slice(cursor, cursor + 2);
const inputValueSliceMinusTwoCursor = inputValue.slice(cursor - 2, cursor);
if (maskExpression[cursor] === 'd') {
if (maskExpression[cursor] === MaskExpression.DAY) {
const maskStartWithMonth = maskExpression.slice(0, 2) === 'M0';
const startWithMonthInput: boolean =
maskExpression.slice(0, 2) === 'M0' &&
Expand Down Expand Up @@ -397,7 +395,7 @@ export class NgxMaskApplierService {
continue;
}
}
if (maskExpression[cursor] === 'M') {
if (maskExpression[cursor] === MaskExpression.MONTH) {
const monthsCount = 12;
// mask without day
const withoutDays: boolean =
Expand Down
12 changes: 12 additions & 0 deletions projects/ngx-mask-lib/src/lib/ngx-mask-expression.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const enum MaskExpression {
SEPARATOR = 'separator',
PERCENT = 'percent',
IP = 'IP',
CPF_CNPJ = 'CPF_CNPJ',
MONTH = 'M',
MINUTE = 'm',
HOUR = 'h',
HOURS = 'H',
DAY = 'd',
SECOND = 's',
}
7 changes: 5 additions & 2 deletions projects/ngx-mask-lib/src/lib/ngx-mask.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import { CustomKeyboardEvent } from './custom-keyboard-event';
import { IConfig, NGX_MASK_CONFIG, timeMasks, withoutValidation } from './ngx-mask.config';
import { NgxMaskService } from './ngx-mask.service';
import {MaskExpression} from "./ngx-mask-applier.service";
import {MaskExpression} from "./ngx-mask-expression.enum";

@Directive({
selector: 'input[mask], textarea[mask]',
Expand Down Expand Up @@ -584,7 +584,10 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
inputValue = inputValue.value;
}

if (typeof inputValue === 'number' || this._maskValue.startsWith(MaskExpression.SEPARATOR)) {
if (
typeof inputValue === 'number' ||
this._maskValue.startsWith(MaskExpression.SEPARATOR)
) {
// eslint-disable-next-line no-param-reassign
inputValue = this._maskService.numberToString(inputValue);
const localeDecimalMarker = this._currentLocaleDecimalMarker();
Expand Down
18 changes: 11 additions & 7 deletions projects/ngx-mask-lib/src/lib/ngx-mask.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { ElementRef, inject, Injectable, Renderer2 } from '@angular/core';
import { DOCUMENT } from '@angular/common';

import { NGX_MASK_CONFIG, IConfig } from './ngx-mask.config';
import { MaskExpression, NgxMaskApplierService } from './ngx-mask-applier.service';
import { NgxMaskApplierService } from './ngx-mask-applier.service';
import { MaskExpression } from './ngx-mask-expression.enum';

@Injectable()
export class NgxMaskService extends NgxMaskApplierService {
Expand Down Expand Up @@ -53,10 +54,10 @@ export class NgxMaskService extends NgxMaskApplierService {
return inputValue !== this.actualValue ? this.actualValue : inputValue;
}
this.maskIsShown = this.showMaskTyped ? this.showMaskInInput() : '';
if (this.maskExpression === 'IP' && this.showMaskTyped) {
if (this.maskExpression === MaskExpression.IP && this.showMaskTyped) {
this.maskIsShown = this.showMaskInInput(inputValue || '#');
}
if (this.maskExpression === 'CPF_CNPJ' && this.showMaskTyped) {
if (this.maskExpression === MaskExpression.CPF_CNPJ && this.showMaskTyped) {
this.maskIsShown = this.showMaskInInput(inputValue || '#');
}
if (!inputValue && this.showMaskTyped) {
Expand Down Expand Up @@ -148,10 +149,13 @@ export class NgxMaskService extends NgxMaskApplierService {
const resLen: number = result.length;
const prefNmask: string = this.prefix + this.maskIsShown;

if (this.maskExpression.includes('H')) {
if (this.maskExpression.includes(MaskExpression.HOURS)) {
const countSkipedSymbol = this._numberSkipedSymbols(result);
return result + prefNmask.slice(resLen + countSkipedSymbol);
} else if (this.maskExpression === 'IP' || this.maskExpression === 'CPF_CNPJ') {
} else if (
this.maskExpression === MaskExpression.IP ||
this.maskExpression === MaskExpression.CPF_CNPJ
) {
return result + prefNmask;
}
return result + prefNmask.slice(resLen);
Expand Down Expand Up @@ -276,10 +280,10 @@ export class NgxMaskService extends NgxMaskApplierService {
}
} else if (this.showMaskTyped) {
if (inputVal) {
if (this.maskExpression === 'IP') {
if (this.maskExpression === MaskExpression.IP) {
return this._checkForIp(inputVal);
}
if (this.maskExpression === 'CPF_CNPJ') {
if (this.maskExpression === MaskExpression.CPF_CNPJ) {
return this._checkForCpfCnpj(inputVal);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/assets/content/lists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export const lists: IListItem[] = [
scrollTo: 'sep',
},
{
content: 'Separator',
content: 'Separator leadZero',
id: 4,
scrollTo: 'lead-zero',
},
Expand Down

0 comments on commit b5d5ec4

Please sign in to comment.