@awesomeness-js/utils 1.0.24 → 1.0.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/build.js +11 -11
- package/build/postBuild.js +8 -8
- package/eslint.config.js +90 -0
- package/index.js +77 -74
- package/package.json +25 -25
- package/schemas/schema1.js +5 -1
- package/schemas/schema2.js +5 -1
- package/schemas.js +2 -2
- package/src/build.js +12 -8
- package/src/clean/boolean.js +27 -17
- package/src/clean/integer.js +85 -69
- package/src/clean/number.js +103 -86
- package/src/clean/string.js +91 -67
- package/src/clean/timestamp.js +61 -45
- package/src/clean/uuid.js +38 -25
- package/src/collectImports.js +195 -0
- package/src/combineFiles.js +45 -36
- package/src/convertBytes.js +12 -7
- package/src/decrypt.js +17 -9
- package/src/each.js +26 -4
- package/src/eachAsync.js +35 -19
- package/src/encrypt.js +22 -17
- package/src/getAllFiles.js +55 -41
- package/src/ignoreFolder/ignoreMe.js +6 -2
- package/src/ignoreMe.js +4 -2
- package/src/isUUID.js +4 -0
- package/src/md5.js +5 -1
- package/src/password/check.js +7 -3
- package/src/password/hash.js +12 -7
- package/src/setLocalEnvs.js +16 -3
- package/src/thingType.js +62 -24
- package/src/toPennies.js +23 -5
- package/src/utils/buildExportsTree.js +45 -17
- package/src/utils/buildFileDataList.js +32 -17
- package/src/utils/clean.js +205 -120
- package/src/utils/extractJSDocComment.js +14 -7
- package/src/utils/generateFile.js +20 -18
- package/src/utils/generateFlatExportLines.js +34 -17
- package/src/utils/generateImportStatements.js +15 -7
- package/src/utils/generateNamedExports.js +20 -9
- package/src/utils/generateNamespaceCode.js +45 -24
- package/src/utils/generateNamespaceExportLines.js +16 -7
- package/src/utils/shouldIgnore.js +57 -37
- package/src/uuid.js +9 -7
- package/src/validateSchema.js +95 -77
- package/test/collectImports.js +8 -0
- package/test/css/some.css +3 -0
- package/test/css/some.js +5 -0
- package/test/ignoreFolder/ignoreMe.js +3 -1
- package/test/ignoreFolder/ignoreMe2.js +3 -1
- package/test/ignoreFolder2/ignoreMe.js +6 -2
- package/test/js/abc.test.js +7 -3
- package/test/js/some.js +5 -0
- package/test/secret.test.js +1 -1
- package/tests/clean/array.test.js +122 -74
- package/tests/clean/boolean.test.js +18 -6
- package/tests/clean/integer.test.js +25 -9
- package/tests/clean/number.test.js +49 -13
- package/tests/clean/object.test.js +190 -119
- package/tests/clean/string.test.js +48 -17
- package/tests/clean/timestamp.test.js +12 -5
- package/tests/clean/uuid.test.js +13 -6
- package/tests/collectImports.test.js +66 -0
- package/tests/combineFiles.test.js +28 -26
- package/tests/convertBytes.test.js +8 -3
- package/tests/env.test.js +9 -3
- package/tests/example.test.js +7 -3
- package/tests/fileList.test.js +16 -12
- package/tests/hash-and-encrypt.test.js +13 -4
- package/tests/md5.test.js +7 -2
- package/tests/uuid.test.js +10 -4
- package/tests/validateSchema.test.js +21 -9
- package/tsconfig.json +20 -16
- package/types/collectImports.d.ts +6 -0
- package/types/index.d.ts +3 -0
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// example.test.js
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
expect, test
|
|
4
|
+
} from 'vitest';
|
|
3
5
|
import utils from '../../index.js';
|
|
4
6
|
|
|
5
7
|
let x = true;
|
|
@@ -7,21 +9,31 @@ let y = false;
|
|
|
7
9
|
let z = 1;
|
|
8
10
|
|
|
9
11
|
test('boolean - true', () => {
|
|
10
|
-
|
|
12
|
+
|
|
13
|
+
expect(utils.clean.boolean(x)).toBe(true);
|
|
14
|
+
|
|
11
15
|
});
|
|
12
16
|
|
|
13
17
|
test('boolean - false', () => {
|
|
14
|
-
|
|
18
|
+
|
|
19
|
+
expect(utils.clean.boolean(y)).toBe(false);
|
|
20
|
+
|
|
15
21
|
});
|
|
16
22
|
|
|
17
23
|
test('boolean - number', () => {
|
|
18
|
-
|
|
24
|
+
|
|
25
|
+
expect(utils.clean.boolean(z)).toBe(null);
|
|
26
|
+
|
|
19
27
|
});
|
|
20
28
|
|
|
21
29
|
test('boolean - NOT to throw', () => {
|
|
22
|
-
|
|
30
|
+
|
|
31
|
+
expect(() => utils.clean.boolean(y, { required: true })).not.toThrow();
|
|
32
|
+
|
|
23
33
|
});
|
|
24
34
|
|
|
25
35
|
test('boolean - to throw', () => {
|
|
26
|
-
|
|
36
|
+
|
|
37
|
+
expect(() => utils.clean.boolean(z, { required: true })).toThrow();
|
|
38
|
+
|
|
27
39
|
});
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// example.test.js
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
expect, test
|
|
4
|
+
} from 'vitest';
|
|
3
5
|
import utils from '../../index.js';
|
|
4
6
|
|
|
5
7
|
let x = 1;
|
|
@@ -9,31 +11,45 @@ let z2 = '102.45';
|
|
|
9
11
|
let z3 = 102.45;
|
|
10
12
|
|
|
11
13
|
test('integer - 1', () => {
|
|
12
|
-
|
|
14
|
+
|
|
15
|
+
expect(utils.clean.integer(x)).toBe(1);
|
|
16
|
+
|
|
13
17
|
});
|
|
14
18
|
|
|
15
19
|
test('integer - null', () => {
|
|
16
|
-
|
|
20
|
+
|
|
21
|
+
expect(utils.clean.integer(y)).toBe(null);
|
|
22
|
+
|
|
17
23
|
});
|
|
18
24
|
|
|
19
25
|
|
|
20
26
|
test('integer as string - 102', () => {
|
|
21
|
-
|
|
27
|
+
|
|
28
|
+
expect(utils.clean.integer(z)).toBe(102);
|
|
29
|
+
|
|
22
30
|
});
|
|
23
31
|
|
|
24
32
|
test('integer - NOT to throw - 102', () => {
|
|
25
|
-
|
|
33
|
+
|
|
34
|
+
expect(() => utils.clean.integer(z, { required: true })).not.toThrow();
|
|
35
|
+
|
|
26
36
|
});
|
|
27
37
|
|
|
28
38
|
test('integer - to throw', () => {
|
|
29
|
-
|
|
39
|
+
|
|
40
|
+
expect(() => utils.clean.integer(y, { required: true })).toThrow();
|
|
41
|
+
|
|
30
42
|
});
|
|
31
43
|
|
|
32
44
|
test('integer - to throw - "102.45"', () => {
|
|
33
|
-
|
|
45
|
+
|
|
46
|
+
expect(() => utils.clean.integer(z2, { required: true })).toThrow();
|
|
47
|
+
|
|
34
48
|
});
|
|
35
49
|
|
|
36
50
|
test('integer - to throw - 102.45', () => {
|
|
37
|
-
|
|
38
|
-
|
|
51
|
+
|
|
52
|
+
console.log(utils.clean.integer(z3));
|
|
53
|
+
expect(() => utils.clean.integer(z3, { required: true })).toThrow();
|
|
54
|
+
|
|
39
55
|
});
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// example.test.js
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
expect, test
|
|
4
|
+
} from 'vitest';
|
|
3
5
|
import utils from '../../index.js';
|
|
4
6
|
|
|
5
7
|
let x = 1;
|
|
@@ -9,41 +11,75 @@ let z2 = '102.45';
|
|
|
9
11
|
let bad = 'bad';
|
|
10
12
|
|
|
11
13
|
test('number - 1', () => {
|
|
12
|
-
|
|
14
|
+
|
|
15
|
+
expect(utils.clean.number(x)).toBe(1);
|
|
16
|
+
|
|
13
17
|
});
|
|
14
18
|
|
|
15
19
|
test('number - 1.2', () => {
|
|
16
|
-
|
|
20
|
+
|
|
21
|
+
expect(utils.clean.number(y)).toBe(1.2);
|
|
22
|
+
|
|
17
23
|
});
|
|
18
24
|
|
|
19
25
|
test('number as string - 102', () => {
|
|
20
|
-
|
|
26
|
+
|
|
27
|
+
expect(utils.clean.number(z)).toBe(102);
|
|
28
|
+
|
|
21
29
|
});
|
|
22
30
|
|
|
23
31
|
test('number - string to 102', () => {
|
|
24
|
-
|
|
32
|
+
|
|
33
|
+
expect(utils.clean.number(z, { required: true })).toBe(102);
|
|
34
|
+
|
|
25
35
|
});
|
|
26
36
|
|
|
27
37
|
test('number - string to 102.45', () => {
|
|
28
|
-
|
|
29
|
-
|
|
38
|
+
|
|
39
|
+
console.log(utils.clean.number(z2));
|
|
40
|
+
expect(utils.clean.number(z2, { required: true })).toBe(102.45);
|
|
41
|
+
|
|
30
42
|
});
|
|
31
43
|
|
|
32
44
|
test('number as string - bad to null', () => {
|
|
33
|
-
|
|
45
|
+
|
|
46
|
+
expect(utils.clean.number(bad)).toBe(null);
|
|
47
|
+
|
|
34
48
|
});
|
|
35
49
|
|
|
36
50
|
test('number as string - bad to throw', () => {
|
|
37
|
-
|
|
51
|
+
|
|
52
|
+
expect(()=> utils.clean.number(bad, { required: true }) ).toThrow();
|
|
53
|
+
|
|
38
54
|
});
|
|
39
55
|
|
|
40
56
|
test('number is too high', () => {
|
|
41
|
-
|
|
42
|
-
|
|
57
|
+
|
|
58
|
+
expect(()=> utils.clean.number(100, {
|
|
59
|
+
min: 1,
|
|
60
|
+
max: 10,
|
|
61
|
+
required: true
|
|
62
|
+
}) ).toThrow();
|
|
63
|
+
expect(()=> utils.clean.number(100.11, {
|
|
64
|
+
min: 1,
|
|
65
|
+
max: 100.1,
|
|
66
|
+
required: true
|
|
67
|
+
}) ).toThrow();
|
|
68
|
+
|
|
43
69
|
});
|
|
44
70
|
|
|
45
71
|
test('number is too low', () => {
|
|
46
|
-
|
|
47
|
-
|
|
72
|
+
|
|
73
|
+
expect(()=> utils.clean.number(0, {
|
|
74
|
+
min: 1,
|
|
75
|
+
max: 10,
|
|
76
|
+
required: true
|
|
77
|
+
}) ).toThrow();
|
|
78
|
+
expect(()=> utils.clean.number(1.2, {
|
|
79
|
+
min: 1.3,
|
|
80
|
+
max: 10,
|
|
81
|
+
required: true
|
|
82
|
+
}) ).toThrow();
|
|
83
|
+
|
|
48
84
|
});
|
|
49
85
|
|
|
@@ -1,172 +1,243 @@
|
|
|
1
1
|
// example.test.js
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
expect, test
|
|
4
|
+
} from 'vitest';
|
|
3
5
|
import utils from '../../index.js';
|
|
4
6
|
|
|
5
7
|
const testObject = {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
stringProp: 'a string',
|
|
9
|
+
integerProp: 42,
|
|
10
|
+
numberProp: 3.14,
|
|
11
|
+
booleanProp: true,
|
|
12
|
+
timestampProp: new Date().toISOString(),
|
|
13
|
+
uuidProp: utils.uuid(), // generate a valid UUID
|
|
12
14
|
};
|
|
13
15
|
|
|
14
16
|
const invalidTestObject = {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
17
|
+
stringProp: 123, // should be a string
|
|
18
|
+
integerProp: 'not an integer', // should be an integer
|
|
19
|
+
numberProp: 'not a number', // should be a number
|
|
20
|
+
booleanProp: 'not a boolean', // should be a boolean
|
|
21
|
+
timestampProp: 'not a timestamp', // should be a timestamp
|
|
22
|
+
uuidProp: 'not a uuid', // should be a uuid
|
|
23
|
+
optionalStringProp: 456, // should be a string
|
|
24
|
+
optionalIntegerProp: 'not an integer', // should be an integer
|
|
25
|
+
optionalNumberProp: 'not a number', // should be a number
|
|
26
|
+
optionalBooleanProp: 'not a boolean', // should be a boolean
|
|
27
|
+
optionalTimestampProp: 'not a timestamp', // should be a timestamp
|
|
28
|
+
optionalUuidProp: 'not a uuid' // should be a uuid
|
|
27
29
|
};
|
|
28
30
|
|
|
29
31
|
const testObject2 = {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
+
obj1: { ...testObject },
|
|
33
|
+
obj2: { ...testObject }
|
|
32
34
|
};
|
|
33
35
|
|
|
34
36
|
const invalidTestObject2 = {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
+
obj1: { ...invalidTestObject },
|
|
38
|
+
obj2: { ...invalidTestObject }
|
|
37
39
|
};
|
|
38
40
|
|
|
39
41
|
const schema = {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
42
|
+
type: 'object',
|
|
43
|
+
properties: {
|
|
44
|
+
stringProp: {
|
|
45
|
+
type: 'string',
|
|
46
|
+
required: true
|
|
47
|
+
},
|
|
48
|
+
integerProp: {
|
|
49
|
+
type: 'integer',
|
|
50
|
+
required: true
|
|
51
|
+
},
|
|
52
|
+
numberProp: {
|
|
53
|
+
type: 'number',
|
|
54
|
+
required: true
|
|
55
|
+
},
|
|
56
|
+
booleanProp: {
|
|
57
|
+
type: 'boolean',
|
|
58
|
+
required: true
|
|
59
|
+
},
|
|
60
|
+
timestampProp: {
|
|
61
|
+
type: 'timestamp',
|
|
62
|
+
required: true
|
|
63
|
+
},
|
|
64
|
+
uuidProp: {
|
|
65
|
+
type: 'uuid',
|
|
66
|
+
required: true
|
|
67
|
+
},
|
|
68
|
+
optionalStringProp: {
|
|
69
|
+
type: 'string',
|
|
70
|
+
required: false
|
|
71
|
+
},
|
|
72
|
+
optionalIntegerProp: {
|
|
73
|
+
type: 'integer',
|
|
74
|
+
required: false
|
|
75
|
+
},
|
|
76
|
+
optionalNumberProp: {
|
|
77
|
+
type: 'number',
|
|
78
|
+
required: false
|
|
79
|
+
},
|
|
80
|
+
optionalBooleanProp: {
|
|
81
|
+
type: 'boolean',
|
|
82
|
+
required: false
|
|
83
|
+
},
|
|
84
|
+
optionalTimestampProp: {
|
|
85
|
+
type: 'timestamp',
|
|
86
|
+
required: false
|
|
87
|
+
},
|
|
88
|
+
optionalUuidProp: {
|
|
89
|
+
type: 'uuid',
|
|
90
|
+
required: false
|
|
91
|
+
}
|
|
92
|
+
},
|
|
55
93
|
};
|
|
56
94
|
|
|
57
95
|
const schema2 = {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
96
|
+
type: 'object',
|
|
97
|
+
properties: {
|
|
98
|
+
obj1: {
|
|
99
|
+
type: 'object',
|
|
100
|
+
required: true,
|
|
101
|
+
properties: { ... schema.properties }
|
|
102
|
+
},
|
|
103
|
+
obj2: {
|
|
104
|
+
type: 'object',
|
|
105
|
+
required: true,
|
|
106
|
+
properties: { ... schema.properties }
|
|
107
|
+
}
|
|
108
|
+
},
|
|
71
109
|
};
|
|
72
110
|
|
|
73
111
|
|
|
74
112
|
const testArraysOfIntegers = [
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
113
|
+
[ 1, 2, 3 ],
|
|
114
|
+
[ 4, 5, 6 ],
|
|
115
|
+
[ 7, 8, 9 ]
|
|
78
116
|
];
|
|
79
117
|
|
|
80
118
|
const testStringArrayOfArrays = [
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
119
|
+
[ 'a', 'b', 'c' ],
|
|
120
|
+
[ 'd', 'e', 'f' ],
|
|
121
|
+
[ 'g', 'h', 'i' ],
|
|
84
122
|
];
|
|
85
123
|
|
|
86
124
|
const testArrayOfBooleans = [
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
125
|
+
[ true, false, true ],
|
|
126
|
+
[ false, true, false ],
|
|
127
|
+
[ true, true, false ]
|
|
90
128
|
];
|
|
91
129
|
|
|
92
130
|
const objectOfArrays = {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
131
|
+
testArraysOfIntegers: [ ...testArraysOfIntegers ],
|
|
132
|
+
testStringArrayOfArrays: [ ...testStringArrayOfArrays ],
|
|
133
|
+
testArrayOfBooleans: [ ...testArrayOfBooleans ],
|
|
96
134
|
};
|
|
97
135
|
|
|
98
136
|
const objectOfArrays_schema = {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
137
|
+
type: 'object',
|
|
138
|
+
properties: {
|
|
139
|
+
testArraysOfIntegers: {
|
|
140
|
+
type: 'array',
|
|
141
|
+
items: {
|
|
142
|
+
type: 'array',
|
|
143
|
+
items: { type: 'integer' }
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
testStringArrayOfArrays: {
|
|
147
|
+
type: 'array',
|
|
148
|
+
items: {
|
|
149
|
+
type: 'array',
|
|
150
|
+
items: { type: 'string' }
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
testArrayOfBooleans: {
|
|
154
|
+
type: 'array',
|
|
155
|
+
items: {
|
|
156
|
+
type: 'array',
|
|
157
|
+
items: { type: 'boolean' }
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
123
161
|
};
|
|
124
162
|
|
|
125
163
|
test('testObject', () => {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
164
|
+
|
|
165
|
+
try {
|
|
166
|
+
|
|
167
|
+
const cleanedObject = utils.clean.object(testObject, schema);
|
|
168
|
+
|
|
169
|
+
expect(cleanedObject).toStrictEqual(testObject);
|
|
170
|
+
|
|
171
|
+
} catch (error) {
|
|
172
|
+
|
|
173
|
+
console.error('Error cleaning object:', error);
|
|
174
|
+
|
|
175
|
+
throw error; // rethrow to fail the test
|
|
176
|
+
|
|
177
|
+
}
|
|
178
|
+
|
|
133
179
|
});
|
|
134
180
|
|
|
135
181
|
test('invalidTestObject', () => {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
182
|
+
|
|
183
|
+
try {
|
|
184
|
+
|
|
185
|
+
const cleanedObject = utils.clean.object(invalidTestObject, schema);
|
|
186
|
+
//console.log({ cleanedObject })
|
|
187
|
+
|
|
188
|
+
expect(cleanedObject).toStrictEqual(invalidTestObject);
|
|
189
|
+
|
|
190
|
+
} catch (error) {
|
|
191
|
+
|
|
192
|
+
//console.error('Error cleaning object:', error);
|
|
193
|
+
expect(error.message).toBe('type invalid');
|
|
194
|
+
|
|
195
|
+
}
|
|
196
|
+
|
|
144
197
|
});
|
|
145
198
|
|
|
146
199
|
test('testObject2', () => {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
200
|
+
|
|
201
|
+
try {
|
|
202
|
+
|
|
203
|
+
const cleanedObject = utils.clean.object(testObject2, schema2);
|
|
204
|
+
//console.log('cleanedObject', cleanedObject);
|
|
205
|
+
|
|
206
|
+
expect(cleanedObject).toStrictEqual(testObject2);
|
|
207
|
+
|
|
208
|
+
} catch (error) {
|
|
209
|
+
|
|
210
|
+
console.error('Error cleaning object:', error);
|
|
211
|
+
|
|
212
|
+
throw error; // rethrow to fail the test
|
|
213
|
+
|
|
214
|
+
}
|
|
215
|
+
|
|
155
216
|
});
|
|
156
217
|
|
|
157
218
|
|
|
158
219
|
test('incorrect should throw', () => {
|
|
159
|
-
|
|
220
|
+
|
|
221
|
+
expect(()=> utils.clean.object(testObject, schema2)).toThrow();
|
|
222
|
+
|
|
160
223
|
});
|
|
161
224
|
|
|
162
225
|
|
|
163
226
|
test('object of arrays test', () => {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
227
|
+
|
|
228
|
+
try {
|
|
229
|
+
|
|
230
|
+
const cleanedObject = utils.clean.object(objectOfArrays, objectOfArrays_schema);
|
|
231
|
+
//console.log('cleanedObject', cleanedObject);
|
|
232
|
+
|
|
233
|
+
expect(cleanedObject).toStrictEqual(objectOfArrays);
|
|
234
|
+
|
|
235
|
+
} catch (error) {
|
|
236
|
+
|
|
237
|
+
console.error('Error cleaning object:', error);
|
|
238
|
+
|
|
239
|
+
throw error; // rethrow to fail the test
|
|
240
|
+
|
|
241
|
+
}
|
|
242
|
+
|
|
172
243
|
});
|
|
@@ -1,44 +1,75 @@
|
|
|
1
1
|
// example.test.js
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
expect, test
|
|
4
|
+
} from 'vitest';
|
|
3
5
|
import utils from '../../index.js';
|
|
4
6
|
|
|
5
7
|
test('good string', () => {
|
|
6
|
-
|
|
8
|
+
|
|
9
|
+
expect(utils.clean.string('good string')).toBe('good string');
|
|
10
|
+
|
|
7
11
|
});
|
|
8
12
|
|
|
9
13
|
test('number', () => {
|
|
10
|
-
|
|
11
|
-
|
|
14
|
+
|
|
15
|
+
expect(utils.clean.string(123)).toBe(null);
|
|
16
|
+
expect(()=> utils.clean.string(123, { required: true })).toThrow();
|
|
17
|
+
|
|
12
18
|
});
|
|
13
19
|
|
|
14
20
|
test('string too short', () => {
|
|
15
|
-
|
|
16
|
-
|
|
21
|
+
|
|
22
|
+
expect(utils.clean.string('123456789', { minLength: 10 })).toBe(null);
|
|
23
|
+
expect(()=> utils.clean.string('123456789', {
|
|
24
|
+
minLength: 10,
|
|
25
|
+
required: true
|
|
26
|
+
})).toThrow();
|
|
27
|
+
|
|
17
28
|
});
|
|
18
29
|
|
|
19
30
|
test('string too long', () => {
|
|
20
|
-
|
|
21
|
-
|
|
31
|
+
|
|
32
|
+
expect(utils.clean.string('123456789', { maxLength: 5 })).toBe(null);
|
|
33
|
+
expect(()=> utils.clean.string('123456789', {
|
|
34
|
+
maxLength: 5,
|
|
35
|
+
required: true
|
|
36
|
+
})).toThrow();
|
|
37
|
+
|
|
22
38
|
});
|
|
23
39
|
|
|
24
40
|
test('no html', () => {
|
|
25
|
-
|
|
26
|
-
|
|
41
|
+
|
|
42
|
+
expect(utils.clean.string('<div>no go</div>')).toBe(null);
|
|
43
|
+
expect(()=> utils.clean.string('<div>no go</div>', { required: true })).toThrow();
|
|
44
|
+
|
|
27
45
|
});
|
|
28
46
|
|
|
29
47
|
test('no script', () => {
|
|
30
|
-
|
|
31
|
-
|
|
48
|
+
|
|
49
|
+
expect(utils.clean.string('<script>no go</script>')).toBe(null);
|
|
50
|
+
expect(()=> utils.clean.string('<script>no go</script>', { required: true })).toThrow();
|
|
51
|
+
|
|
32
52
|
});
|
|
33
53
|
|
|
34
54
|
test('allow html', () => {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
55
|
+
|
|
56
|
+
let x = utils.clean.string('<div>all good/div>', { allowHtml: true });
|
|
57
|
+
|
|
58
|
+
expect(utils.clean.string('<div>all good</div>', { allowHtml: true })).toBe('<div>all good</div>');
|
|
59
|
+
expect(utils.clean.string('<div>all good</div>', {
|
|
60
|
+
allowHtml: true,
|
|
61
|
+
required: true
|
|
62
|
+
})).toBe('<div>all good</div>');
|
|
63
|
+
|
|
38
64
|
});
|
|
39
65
|
|
|
40
66
|
test('allow script', () => {
|
|
41
|
-
|
|
42
|
-
|
|
67
|
+
|
|
68
|
+
expect(utils.clean.string('<script>all good</script>', { allowScripts: true })).toBe('<script>all good</script>');
|
|
69
|
+
expect(utils.clean.string('<script>all good</script>', {
|
|
70
|
+
allowScripts: true,
|
|
71
|
+
required: true
|
|
72
|
+
})).toBe('<script>all good</script>');
|
|
73
|
+
|
|
43
74
|
});
|
|
44
75
|
|