Skip to content

Commit

Permalink
feat(ref: #188): add strict eslint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
andriikamaldinov1 committed Oct 29, 2024
1 parent 25b6ab4 commit 2e45d01
Show file tree
Hide file tree
Showing 15 changed files with 446 additions and 360 deletions.
Binary file modified bun.lockb
Binary file not shown.
26 changes: 10 additions & 16 deletions cypress.config.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { defineConfig } from "cypress";
import { defineConfig } from 'cypress';

export default defineConfig({
projectId: "qhyo66",

component: {
devServer: {
framework: "angular",
bundler: "webpack",
projectId: 'qhyo66',

component: {
devServer: {
framework: 'angular',
bundler: 'webpack',
},
specPattern: 'projects/ngx-mask-lib/src/test/**/*.cy-spec.ts',
},
specPattern: "projects/ngx-mask-lib/src/test/**/*.cy-spec.ts",
},

defaultCommandTimeout: 10000,

e2e: {
setupNodeEvents(on, config) {
// implement node event listeners here
},
},
defaultCommandTimeout: 10000,
});
7 changes: 3 additions & 4 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,9 @@ module.exports = tseslint.config(
'no-unused-expressions': ['error', { allowShortCircuit: true, allowTernary: true }],

//THINK ABOUT THIS
'no-param-reassign': 'off',
'no-undefined': 'off',
// 'no-param-reassign': 'error', 43
// 'no-undefined': 'error', 9
// 'no-param-reassign': 'off',
'no-param-reassign': 'error',
'no-undefined': 'error',

'@typescript-eslint/no-unused-expressions': 'error',
'@typescript-eslint/array-type': 'error',
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@
"typescript": "5.5.4",
"angular-eslint": "^18.4.0",
"typescript-eslint": "^8.11.0",
"tailwindcss": "^3.4.14"
"tailwindcss": "^3.4.14",
"bun-types": "^1.1.33"
},
"typeCoverage": {
"atLeast": 92,
Expand Down
353 changes: 202 additions & 151 deletions projects/ngx-mask-lib/src/lib/ngx-mask-applier.service.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions projects/ngx-mask-lib/src/lib/ngx-mask.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type Config = {
shownMaskExpression: string;
specialCharacters: string[] | readonly string[];
dropSpecialCharacters: boolean | string[] | readonly string[];
hiddenInput: boolean | undefined;
hiddenInput: boolean;
validation: boolean;
separatorLimit: string;
apm: boolean;
Expand Down Expand Up @@ -54,7 +54,7 @@ export const initialConfig: Config = {
showMaskTyped: false,
placeHolderCharacter: '_',
dropSpecialCharacters: true,
hiddenInput: undefined,
hiddenInput: false,
shownMaskExpression: '',
separatorLimit: '',
allowNegativeNumbers: false,
Expand Down
85 changes: 41 additions & 44 deletions projects/ngx-mask-lib/src/lib/ngx-mask.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,16 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
}

public validate({ value }: FormControl): ValidationErrors | null {
const processedValue: string = typeof value === 'number' ? String(value) : value;

if (!this._maskService.validation || !this._maskValue) {
return null;
}
if (this._maskService.ipError) {
return this._createValidationError(value);
return this._createValidationError(processedValue);
}
if (this._maskService.cpfCnpjError) {
return this._createValidationError(value);
return this._createValidationError(processedValue);
}
if (this._maskValue.startsWith(MaskExpression.SEPARATOR)) {
return null;
Expand All @@ -284,10 +286,10 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
return null;
}
if (timeMasks.includes(this._maskValue)) {
return this._validateTime(value);
return this._validateTime(processedValue);
}

if (value && value.toString().length >= 1) {
if (processedValue && processedValue.length >= 1) {
let counterOfOpt = 0;

if (
Expand All @@ -299,9 +301,9 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
this._maskValue.indexOf(MaskExpression.CURLY_BRACKETS_RIGHT)
);

return lengthInsideCurlyBrackets === String(value.length)
return lengthInsideCurlyBrackets === String(processedValue.length)
? null
: this._createValidationError(value);
: this._createValidationError(processedValue);
}
if (this._maskValue.startsWith(MaskExpression.PERCENT)) {
return null;
Expand All @@ -319,7 +321,7 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
}
if (
this._maskValue.indexOf(key) !== -1 &&
value.toString().length >= this._maskValue.indexOf(key)
processedValue.length >= this._maskValue.indexOf(key)
) {
return null;
}
Expand All @@ -330,19 +332,16 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
}
if (
(this._maskValue.indexOf(MaskExpression.SYMBOL_STAR) > 1 &&
value.toString().length <
this._maskValue.indexOf(MaskExpression.SYMBOL_STAR)) ||
processedValue.length < this._maskValue.indexOf(MaskExpression.SYMBOL_STAR)) ||
(this._maskValue.indexOf(MaskExpression.SYMBOL_QUESTION) > 1 &&
value.toString().length <
this._maskValue.indexOf(MaskExpression.SYMBOL_QUESTION))
processedValue.length < this._maskValue.indexOf(MaskExpression.SYMBOL_QUESTION))
) {
return this._createValidationError(value);
return this._createValidationError(processedValue);
}
if (
this._maskValue.indexOf(MaskExpression.SYMBOL_STAR) === -1 ||
this._maskValue.indexOf(MaskExpression.SYMBOL_QUESTION) === -1
) {
value = typeof value === 'number' ? String(value) : value;
const array = this._maskValue.split('*');
const length: number = this._maskService.dropSpecialCharacters
? this._maskValue.length -
Expand All @@ -353,34 +352,34 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
: this._maskValue.length - counterOfOpt;

if (array.length === 1) {
if (value.toString().length < length) {
return this._createValidationError(value);
if (processedValue.length < length) {
return this._createValidationError(processedValue);
}
}
if (array.length > 1) {
const lastIndexArray = array[array.length - 1];
if (
lastIndexArray &&
this._maskService.specialCharacters.includes(lastIndexArray[0] as string) &&
String(value).includes(lastIndexArray[0] ?? '') &&
String(processedValue).includes(lastIndexArray[0] ?? '') &&
!this.dropSpecialCharacters
) {
const special = value.split(lastIndexArray[0]);
return special[special.length - 1].length === lastIndexArray.length - 1
? null
: this._createValidationError(value);
: this._createValidationError(processedValue);
} else if (
((lastIndexArray &&
!this._maskService.specialCharacters.includes(
lastIndexArray[0] as string
)) ||
!lastIndexArray ||
this._maskService.dropSpecialCharacters) &&
value.length >= length - 1
processedValue.length >= length - 1
) {
return null;
} else {
return this._createValidationError(value);
return this._createValidationError(processedValue);
}
}
}
Expand Down Expand Up @@ -411,7 +410,9 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
public onModelChange(value: string | undefined | null | number): void {
// on form reset we need to update the actualValue
if (
(value === MaskExpression.EMPTY_STRING || value === null || value === undefined) &&
(value === MaskExpression.EMPTY_STRING ||
value === null ||
typeof value === 'undefined') &&
this._maskService.actualValue
) {
this._maskService.actualValue = this._maskService.getActualValue(
Expand Down Expand Up @@ -899,31 +900,30 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida

/** It writes the value in the input */
public async writeValue(controlValue: unknown): Promise<void> {
if (typeof controlValue === 'object' && controlValue !== null && 'value' in controlValue) {
if ('disable' in controlValue) {
this.setDisabledState(Boolean(controlValue.disable));
let value = controlValue;
if (typeof value === 'object' && value !== null && 'value' in value) {
if ('disable' in value) {
this.setDisabledState(Boolean(value.disable));
}

controlValue = controlValue.value;
value = value.value;
}
if (controlValue !== null) {
controlValue = this.inputTransformFn
? this.inputTransformFn(controlValue)
: controlValue;
if (value !== null) {
value = this.inputTransformFn ? this.inputTransformFn(value) : value;
}

if (
typeof controlValue === 'string' ||
typeof controlValue === 'number' ||
controlValue === null ||
controlValue === undefined
typeof value === 'string' ||
typeof value === 'number' ||
value === null ||
typeof value === 'undefined'
) {
if (controlValue === null || controlValue === undefined || controlValue === '') {
if (value === null || typeof value === 'undefined' || value === '') {
this._maskService._currentValue = '';
this._maskService._previousValue = '';
}

let inputValue: string | number | null | undefined = controlValue;
let inputValue: string | number | null | undefined = value;
if (
typeof inputValue === 'number' ||
this._maskValue.startsWith(MaskExpression.SEPARATOR)
Expand Down Expand Up @@ -984,20 +984,17 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
(this._maskService.prefix || this._maskService.showMaskTyped))
) {
// Let the service we know we are writing value so that triggering onChange function won't happen during applyMask
// eslint-disable-next-line no-unused-expressions,@typescript-eslint/no-unused-expressions
typeof this.inputTransformFn !== 'function'
? (this._maskService.writingValue = true)
: '';

if (typeof this.inputTransformFn !== 'function') {
this._maskService.writingValue = true;
}
this._maskService.formElementProperty = [
'value',
this._maskService.applyMask(inputValue, this._maskService.maskExpression),
];
// Let the service know we've finished writing value
// eslint-disable-next-line no-unused-expressions,@typescript-eslint/no-unused-expressions
typeof this.inputTransformFn !== 'function'
? (this._maskService.writingValue = false)
: '';
if (typeof this.inputTransformFn !== 'function') {
this._maskService.writingValue = false;
}
} else {
this._maskService.formElementProperty = ['value', inputValue];
}
Expand All @@ -1006,7 +1003,7 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
// eslint-disable-next-line no-console
console.warn(
'Ngx-mask writeValue work with string | number, your current value:',
typeof controlValue
typeof value
);
}
}
Expand Down
57 changes: 38 additions & 19 deletions projects/ngx-mask-lib/src/lib/ngx-mask.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export class NgxMaskPipe implements PipeTransform {
mask: string,
{ patterns, ...config }: Partial<Config> = {} as Partial<Config>
): string {
let processedValue: string | number = value;

const currentConfig = {
maskExpression: mask,
...this.defaultOptions,
Expand All @@ -32,27 +34,32 @@ export class NgxMaskPipe implements PipeTransform {
...patterns,
},
};
Object.entries(currentConfig).forEach(([key, value]) => {
(this._maskService as any)[key] = value;

Object.entries(currentConfig).forEach(([key, val]) => {
(this._maskService as any)[key] = val;
});

if (mask.includes('||')) {
if (mask.split('||').length > 1) {
this._maskExpressionArray = mask.split('||').sort((a: string, b: string) => {
return a.length - b.length;
});
this._setMask(value as string);
return this._maskService.applyMask(`${value}`, this.mask);
const maskParts = mask.split('||');
if (maskParts.length > 1) {
this._maskExpressionArray = maskParts.sort(
(a: string, b: string) => a.length - b.length
);
this._setMask(processedValue as string);
return this._maskService.applyMask(`${processedValue}`, this.mask);
} else {
this._maskExpressionArray = [];
return this._maskService.applyMask(`${value}`, this.mask);
return this._maskService.applyMask(`${processedValue}`, this.mask);
}
}

if (mask.includes(MaskExpression.CURLY_BRACKETS_LEFT)) {
return this._maskService.applyMask(
`${value}`,
`${processedValue}`,
this._maskService._repeatPatternSymbols(mask)
);
}

if (mask.startsWith(MaskExpression.SEPARATOR)) {
if (config.decimalMarker) {
this._maskService.decimalMarker = config.decimalMarker;
Expand All @@ -64,30 +71,42 @@ export class NgxMaskPipe implements PipeTransform {
this._maskService.leadZero = config.leadZero;
}

value = String(value);
processedValue = String(processedValue);
const localeDecimalMarker = this._maskService.currentLocaleDecimalMarker();

if (!Array.isArray(this._maskService.decimalMarker)) {
value =
processedValue =
this._maskService.decimalMarker !== localeDecimalMarker
? value.replace(localeDecimalMarker, this._maskService.decimalMarker)
: value;
? (processedValue as string).replace(
localeDecimalMarker,
this._maskService.decimalMarker
)
: processedValue;
}

if (
this._maskService.leadZero &&
value &&
processedValue &&
this._maskService.dropSpecialCharacters !== false
) {
value = this._maskService._checkPrecision(mask, value as string);
processedValue = this._maskService._checkPrecision(mask, processedValue as string);
}

if (this._maskService.decimalMarker === MaskExpression.COMMA) {
value = value.toString().replace(MaskExpression.DOT, MaskExpression.COMMA);
processedValue = (processedValue as string).replace(
MaskExpression.DOT,
MaskExpression.COMMA
);
}

this._maskService.isNumberValue = true;
}
if (value === null || value === undefined) {

if (processedValue === null || typeof processedValue === 'undefined') {
return this._maskService.applyMask('', mask);
}
return this._maskService.applyMask(`${value}`, mask);

return this._maskService.applyMask(`${processedValue}`, mask);
}

private _setMask(value: string) {
Expand Down
Loading

0 comments on commit 2e45d01

Please sign in to comment.