forked from javascript-obfuscator/javascript-obfuscator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsIdentifierNamesCache.ts
More file actions
87 lines (72 loc) · 2.75 KB
/
Copy pathIsIdentifierNamesCache.ts
File metadata and controls
87 lines (72 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import equal from 'fast-deep-equal';
import { registerDecorator, ValidationArguments, ValidationOptions } from 'class-validator';
import { TIdentifierNamesCache } from '../../types/TIdentifierNamesCache';
import { TIdentifierNamesCacheDictionary } from '../../types/TIdentifierNamesCacheDictionary';
import { IOptions } from '../../interfaces/options/IOptions';
import { DEFAULT_PRESET } from '../presets/Default';
/**
* @param value
* @returns {boolean}
*/
const validateDictionary = (value: unknown | TIdentifierNamesCacheDictionary): boolean => {
if (value === undefined) {
return true;
}
if (typeof value !== 'object' || value === null) {
return false;
}
const objectValues: unknown[] = Object.values(value);
if (!objectValues.length) {
return true;
}
for (const objectValue of objectValues) {
if (typeof objectValue !== 'string') {
return false;
}
}
return true;
};
/**
* @param {ValidationOptions} validationOptions
* @returns {(options: IOptions, propertyName: keyof IOptions) => void}
*/
export function IsIdentifierNamesCache (
validationOptions?: ValidationOptions
): (options: IOptions, propertyName: keyof IOptions) => void {
return (optionsObject: IOptions, propertyName: keyof IOptions): void => {
registerDecorator({
propertyName,
constraints: [],
name: 'IsIdentifierNamesCache',
options: validationOptions,
target: optionsObject.constructor,
validator: {
/**
* @param value
* @param {ValidationArguments} validationArguments
* @returns {boolean}
*/
validate (value: unknown, validationArguments: ValidationArguments): boolean {
const defaultValue: IOptions[keyof IOptions] | undefined = DEFAULT_PRESET[propertyName];
const isDefaultValue: boolean = equal(value, defaultValue);
if (isDefaultValue || value === null) {
return true;
}
if (typeof value !== 'object') {
return false;
}
if (!validateDictionary((<TIdentifierNamesCache>value)?.globalIdentifiers)) {
return false;
}
return validateDictionary((<TIdentifierNamesCache>value)?.propertyIdentifiers);
},
/**
* @returns {string}
*/
defaultMessage (): string {
return 'Passed value must be an identifier names cache object or `null` value';
}
}
});
};
}