@awesomeness-js/utils 1.1.22 → 1.2.0
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/index.js +32 -0
- package/package.json +1 -1
- package/schemas/schema1.js +1 -8
- package/schemas/schema2.js +1 -8
- package/src/random/array.js +46 -0
- package/src/random/arrayValue.js +5 -0
- package/src/random/boolean.js +5 -0
- package/src/random/integer.js +8 -0
- package/src/random/number.js +17 -0
- package/src/random/object.js +46 -0
- package/src/random/string.js +10 -0
- package/src/random/timestamp.js +15 -0
- package/src/random/uuid.js +7 -0
- package/src/utils/clean.js +66 -73
- package/test/js/abc.test.js +1 -3
- package/tests/clean/array.test.js +1 -3
- package/tests/clean/boolean.test.js +1 -3
- package/tests/clean/integer.test.js +1 -3
- package/tests/clean/number.test.js +1 -3
- package/tests/clean/object.test.js +3 -4
- package/tests/clean/string.test.js +1 -3
- package/tests/clean/timestamp.test.js +1 -3
- package/tests/clean/uuid.test.js +1 -3
- package/tests/collectImports.test.js +1 -3
- package/tests/combineFiles.test.js +1 -3
- package/tests/convertBytes.test.js +1 -3
- package/tests/env.test.js +1 -3
- package/tests/example.test.js +1 -3
- package/tests/fileList.test.js +1 -3
- package/tests/hash-and-encrypt.test.js +1 -3
- package/tests/md5.test.js +1 -3
- package/tests/namedExports.test.js +1 -3
- package/tests/random.test.js +110 -0
- package/tests/uuid.test.js +1 -3
- package/tests/validateSchema.test.js +1 -3
- package/types/build.d.ts +10 -10
- package/types/clean/array.d.ts +6 -6
- package/types/clean/boolean.d.ts +3 -3
- package/types/clean/buffer.d.ts +12 -12
- package/types/clean/custom.d.ts +4 -4
- package/types/clean/file.d.ts +11 -11
- package/types/clean/integer.d.ts +6 -6
- package/types/clean/number.d.ts +8 -8
- package/types/clean/object.d.ts +6 -6
- package/types/clean/string.d.ts +10 -10
- package/types/clean/timestamp.d.ts +5 -5
- package/types/clean/uuid.d.ts +3 -3
- package/types/collectImports.d.ts +6 -6
- package/types/combineFiles.d.ts +8 -8
- package/types/convertBytes.d.ts +8 -8
- package/types/decrypt.d.ts +1 -1
- package/types/each.d.ts +10 -10
- package/types/eachAsync.d.ts +1 -1
- package/types/encrypt.d.ts +5 -5
- package/types/getAllFiles.d.ts +6 -6
- package/types/index.d.ts +150 -150
- package/types/isUUID.d.ts +1 -1
- package/types/md5.d.ts +2 -2
- package/types/password/check.d.ts +1 -1
- package/types/password/hash.d.ts +1 -1
- package/types/setLocalEnvs.d.ts +2 -2
- package/types/shouldIgnore.d.ts +1 -1
- package/types/thingType.d.ts +2 -2
- package/types/toPennies.d.ts +1 -1
- package/types/utils/buildExportsTree.d.ts +4 -4
- package/types/utils/buildFileDataList.d.ts +12 -12
- package/types/utils/clean.d.ts +27 -27
- package/types/utils/extractJSDocComment.d.ts +1 -1
- package/types/utils/generateFile.d.ts +8 -8
- package/types/utils/generateFlatExportLines.d.ts +7 -7
- package/types/utils/generateImportStatements.d.ts +5 -5
- package/types/utils/generateNamedExports.d.ts +7 -7
- package/types/utils/generateNamespaceCode.d.ts +7 -7
- package/types/utils/generateNamespaceExportLines.d.ts +6 -6
- package/types/uuid.d.ts +2 -2
- package/types/validateSchema.d.ts +2 -2
- package/types/wait.d.ts +1 -1
package/index.js
CHANGED
|
@@ -26,6 +26,15 @@ import _isUUID from './src/isUUID.js';
|
|
|
26
26
|
import _md5 from './src/md5.js';
|
|
27
27
|
import _password_check from './src/password/check.js';
|
|
28
28
|
import _password_hash from './src/password/hash.js';
|
|
29
|
+
import _random_array from './src/random/array.js';
|
|
30
|
+
import _random_arrayValue from './src/random/arrayValue.js';
|
|
31
|
+
import _random_boolean from './src/random/boolean.js';
|
|
32
|
+
import _random_integer from './src/random/integer.js';
|
|
33
|
+
import _random_number from './src/random/number.js';
|
|
34
|
+
import _random_object from './src/random/object.js';
|
|
35
|
+
import _random_string from './src/random/string.js';
|
|
36
|
+
import _random_timestamp from './src/random/timestamp.js';
|
|
37
|
+
import _random_uuid from './src/random/uuid.js';
|
|
29
38
|
import _setLocalEnvs from './src/setLocalEnvs.js';
|
|
30
39
|
import _shouldIgnore from './src/shouldIgnore.js';
|
|
31
40
|
import _thingType from './src/thingType.js';
|
|
@@ -81,6 +90,18 @@ export const password = {
|
|
|
81
90
|
hash: _password_hash
|
|
82
91
|
};
|
|
83
92
|
|
|
93
|
+
export const random = {
|
|
94
|
+
array: _random_array,
|
|
95
|
+
arrayValue: _random_arrayValue,
|
|
96
|
+
boolean: _random_boolean,
|
|
97
|
+
integer: _random_integer,
|
|
98
|
+
number: _random_number,
|
|
99
|
+
object: _random_object,
|
|
100
|
+
string: _random_string,
|
|
101
|
+
timestamp: _random_timestamp,
|
|
102
|
+
uuid: _random_uuid
|
|
103
|
+
};
|
|
104
|
+
|
|
84
105
|
export const utils = {
|
|
85
106
|
buildExportsTree: _utils_buildExportsTree,
|
|
86
107
|
buildFileDataList: _utils_buildFileDataList,
|
|
@@ -158,6 +179,17 @@ export default {
|
|
|
158
179
|
check: _password_check,
|
|
159
180
|
hash: _password_hash,
|
|
160
181
|
},
|
|
182
|
+
random: {
|
|
183
|
+
array: _random_array,
|
|
184
|
+
arrayValue: _random_arrayValue,
|
|
185
|
+
boolean: _random_boolean,
|
|
186
|
+
integer: _random_integer,
|
|
187
|
+
number: _random_number,
|
|
188
|
+
object: _random_object,
|
|
189
|
+
string: _random_string,
|
|
190
|
+
timestamp: _random_timestamp,
|
|
191
|
+
uuid: _random_uuid,
|
|
192
|
+
},
|
|
161
193
|
utils: {
|
|
162
194
|
buildExportsTree: _utils_buildExportsTree,
|
|
163
195
|
buildFileDataList: _utils_buildFileDataList,
|
package/package.json
CHANGED
package/schemas/schema1.js
CHANGED
|
@@ -65,15 +65,7 @@ const properties = {
|
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
|
-
},
|
|
69
|
-
|
|
70
|
-
exampleRef: {
|
|
71
|
-
type: 'array',
|
|
72
|
-
items: {
|
|
73
|
-
ref: 'someThing'
|
|
74
|
-
}
|
|
75
68
|
}
|
|
76
|
-
|
|
77
69
|
};
|
|
78
70
|
|
|
79
71
|
const edges = Object.freeze({
|
|
@@ -118,6 +110,7 @@ const relatedKVs = {
|
|
|
118
110
|
export default {
|
|
119
111
|
name: 'Blog Post',
|
|
120
112
|
description: `a blog post`,
|
|
113
|
+
type: 'object',
|
|
121
114
|
properties,
|
|
122
115
|
edges,
|
|
123
116
|
edgesIn,
|
package/schemas/schema2.js
CHANGED
|
@@ -65,15 +65,7 @@ const properties = {
|
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
|
-
},
|
|
69
|
-
|
|
70
|
-
exampleRef: {
|
|
71
|
-
type: 'array',
|
|
72
|
-
items: {
|
|
73
|
-
ref: 'someThing'
|
|
74
|
-
}
|
|
75
68
|
}
|
|
76
|
-
|
|
77
69
|
};
|
|
78
70
|
|
|
79
71
|
const edges = Object.freeze({
|
|
@@ -118,6 +110,7 @@ const relatedKVs = {
|
|
|
118
110
|
export default {
|
|
119
111
|
name: 'Blog Post',
|
|
120
112
|
description: `a blog post`,
|
|
113
|
+
type: 'object',
|
|
121
114
|
properties,
|
|
122
115
|
edges,
|
|
123
116
|
edgesIn,
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import randomArrayValue from './arrayValue.js';
|
|
2
|
+
import randomBoolean from './boolean.js';
|
|
3
|
+
import randomInteger from './integer.js';
|
|
4
|
+
import randomNumber from './number.js';
|
|
5
|
+
import randomObject from './object.js';
|
|
6
|
+
import randomString from './string.js';
|
|
7
|
+
import randomTimestamp from './timestamp.js';
|
|
8
|
+
import randomUUID from './uuid.js';
|
|
9
|
+
|
|
10
|
+
function randomArray({
|
|
11
|
+
items = {
|
|
12
|
+
type: 'string',
|
|
13
|
+
minLength: 1,
|
|
14
|
+
maxLength: 10
|
|
15
|
+
},
|
|
16
|
+
min = 1,
|
|
17
|
+
max = 10
|
|
18
|
+
} = {}){
|
|
19
|
+
|
|
20
|
+
const randomTypes = {
|
|
21
|
+
array: randomArray,
|
|
22
|
+
arrayValue: randomArrayValue,
|
|
23
|
+
boolean: randomBoolean,
|
|
24
|
+
integer: randomInteger,
|
|
25
|
+
number: randomNumber,
|
|
26
|
+
object: randomObject,
|
|
27
|
+
string: randomString,
|
|
28
|
+
timestamp: randomTimestamp,
|
|
29
|
+
uuid: randomUUID
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const array = [];
|
|
33
|
+
|
|
34
|
+
for(let i = 0; i < Math.floor(Math.random() * (max - min + 1)) + min; i++) {
|
|
35
|
+
|
|
36
|
+
const item = randomTypes[items.type](items);
|
|
37
|
+
|
|
38
|
+
array.push(item);
|
|
39
|
+
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return array;
|
|
43
|
+
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export default randomArray;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export default ({
|
|
2
|
+
min = 1,
|
|
3
|
+
max = 1_000_000,
|
|
4
|
+
decimalPlaces = 0
|
|
5
|
+
} = {}) => {
|
|
6
|
+
const dp = Math.max(0, Math.floor(decimalPlaces));
|
|
7
|
+
const factor = 10 ** dp;
|
|
8
|
+
|
|
9
|
+
const lo = Math.min(min, max);
|
|
10
|
+
const hi = Math.max(min, max);
|
|
11
|
+
|
|
12
|
+
const loInt = Math.ceil(lo * factor);
|
|
13
|
+
const hiInt = Math.floor(hi * factor);
|
|
14
|
+
|
|
15
|
+
const rInt = Math.floor(Math.random() * (hiInt - loInt + 1)) + loInt;
|
|
16
|
+
return rInt / factor;
|
|
17
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import randomArrayValue from './arrayValue.js';
|
|
2
|
+
import randomBoolean from './boolean.js';
|
|
3
|
+
import randomInteger from './integer.js';
|
|
4
|
+
import randomNumber from './number.js';
|
|
5
|
+
import randomArray from './array.js';
|
|
6
|
+
import randomString from './string.js';
|
|
7
|
+
import randomTimestamp from './timestamp.js';
|
|
8
|
+
import randomUUID from './uuid.js';
|
|
9
|
+
import each from '../each.js';
|
|
10
|
+
|
|
11
|
+
function randomObject({
|
|
12
|
+
properties = {},
|
|
13
|
+
} = {}) {
|
|
14
|
+
|
|
15
|
+
const result = {};
|
|
16
|
+
|
|
17
|
+
const randomTypes = {
|
|
18
|
+
array: randomArray,
|
|
19
|
+
arrayValue: randomArrayValue,
|
|
20
|
+
boolean: randomBoolean,
|
|
21
|
+
integer: randomInteger,
|
|
22
|
+
number: randomNumber,
|
|
23
|
+
object: randomObject,
|
|
24
|
+
string: randomString,
|
|
25
|
+
timestamp: randomTimestamp,
|
|
26
|
+
uuid: randomUUID
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
each(properties, (value, key) => {
|
|
30
|
+
|
|
31
|
+
if(typeof randomTypes[value.type] !== 'function') {
|
|
32
|
+
|
|
33
|
+
throw new Error(`Random function for type "${value.type}" is not defined.`);
|
|
34
|
+
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
result[key] = randomTypes[value.type](value);
|
|
38
|
+
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
return result;
|
|
42
|
+
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
export default randomObject;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export default ({
|
|
2
|
+
maxDays = 365,
|
|
3
|
+
minDays = 1,
|
|
4
|
+
future = false
|
|
5
|
+
} = {}) => {
|
|
6
|
+
|
|
7
|
+
const randomDays = Math.floor(Math.random() * (maxDays - minDays + 1)) + minDays;
|
|
8
|
+
|
|
9
|
+
const randomTimestamp = future
|
|
10
|
+
? Date.now() + randomDays * 24 * 60 * 60 * 1000
|
|
11
|
+
: Date.now() - randomDays * 24 * 60 * 60 * 1000;
|
|
12
|
+
|
|
13
|
+
return new Date(randomTimestamp).toISOString();
|
|
14
|
+
|
|
15
|
+
};
|
package/src/utils/clean.js
CHANGED
|
@@ -75,7 +75,11 @@ function cleanArray(arr, schema = {}, {
|
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
|
|
78
|
-
if(testMode){
|
|
78
|
+
if(testMode){
|
|
79
|
+
|
|
80
|
+
console.log(`cleaning ${supposedToBeType}`, item, schema.items);
|
|
81
|
+
|
|
82
|
+
}
|
|
79
83
|
|
|
80
84
|
|
|
81
85
|
if(supposedToBeType === 'boolean'){
|
|
@@ -256,131 +260,120 @@ function cleanObject(obj, schema, {
|
|
|
256
260
|
}
|
|
257
261
|
|
|
258
262
|
if(testMode){
|
|
263
|
+
|
|
259
264
|
console.log('cleanObject keysPassed:', keysPassed);
|
|
265
|
+
|
|
260
266
|
}
|
|
261
267
|
|
|
262
268
|
const cleanObj = {};
|
|
263
269
|
const errors = {};
|
|
264
270
|
|
|
265
271
|
// Iterate over the schema keys
|
|
266
|
-
each(
|
|
272
|
+
each(schema.properties, (schemaItemProperties, key) => {
|
|
267
273
|
|
|
268
274
|
let cleanedValue;
|
|
269
275
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
const valType = thingType(value);
|
|
273
|
-
const supposedToBeType = schema.properties[key].type;
|
|
276
|
+
const value = obj[key];
|
|
274
277
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
valType,
|
|
278
|
-
supposedToBeType,
|
|
279
|
-
value,
|
|
280
|
-
});
|
|
281
|
-
}
|
|
278
|
+
const valType = thingType(value);
|
|
279
|
+
const supposedToBeType = schemaItemProperties.type;
|
|
282
280
|
|
|
283
|
-
|
|
281
|
+
if(valType === 'undefined'){
|
|
282
|
+
|
|
283
|
+
if(schemaItemProperties.required){
|
|
284
284
|
|
|
285
285
|
throw {
|
|
286
|
-
message: '
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
key
|
|
286
|
+
message: 'key required',
|
|
287
|
+
key,
|
|
288
|
+
schemaItemProperties
|
|
290
289
|
};
|
|
291
|
-
|
|
290
|
+
|
|
292
291
|
}
|
|
293
292
|
|
|
293
|
+
return true;
|
|
294
|
+
|
|
295
|
+
}
|
|
294
296
|
|
|
295
|
-
|
|
297
|
+
if(valType !== supposedToBeType){
|
|
296
298
|
|
|
299
|
+
if(schemaItemProperties.required){
|
|
300
|
+
|
|
297
301
|
throw {
|
|
298
|
-
message: '
|
|
302
|
+
message: 'type invalid',
|
|
303
|
+
valType,
|
|
299
304
|
supposedToBeType,
|
|
300
305
|
key
|
|
301
306
|
};
|
|
302
|
-
|
|
303
|
-
}
|
|
304
|
-
|
|
305
307
|
|
|
306
|
-
if(testMode){ console.log(`cleaning ${supposedToBeType}`, value, schema.properties[key]); }
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
if(supposedToBeType === 'boolean'){
|
|
310
|
-
|
|
311
|
-
cleanedValue = cleanBoolean(value, schema.properties[key]);
|
|
312
|
-
|
|
313
308
|
}
|
|
314
309
|
|
|
315
|
-
|
|
310
|
+
}
|
|
316
311
|
|
|
317
|
-
cleanedValue = cleanInteger(value, schema.properties[key]);
|
|
318
312
|
|
|
319
|
-
|
|
313
|
+
if(!knownTypesToClean.includes(supposedToBeType)){
|
|
320
314
|
|
|
321
|
-
|
|
315
|
+
throw {
|
|
316
|
+
message: 'Unknown type to clean in schema',
|
|
317
|
+
supposedToBeType,
|
|
318
|
+
key
|
|
319
|
+
};
|
|
322
320
|
|
|
323
|
-
|
|
321
|
+
}
|
|
324
322
|
|
|
325
|
-
|
|
323
|
+
|
|
324
|
+
if(testMode){
|
|
326
325
|
|
|
327
|
-
|
|
326
|
+
console.log(`cleaning ${supposedToBeType}`, value, schema.properties[key]);
|
|
328
327
|
|
|
329
|
-
|
|
328
|
+
}
|
|
330
329
|
|
|
331
|
-
}
|
|
332
330
|
|
|
333
|
-
|
|
331
|
+
if(supposedToBeType === 'boolean'){
|
|
334
332
|
|
|
335
|
-
|
|
333
|
+
cleanedValue = cleanBoolean(value, schema.properties[key]);
|
|
336
334
|
|
|
337
|
-
|
|
335
|
+
}
|
|
338
336
|
|
|
339
|
-
|
|
337
|
+
if(supposedToBeType === 'integer'){
|
|
340
338
|
|
|
341
|
-
|
|
339
|
+
cleanedValue = cleanInteger(value, schema.properties[key]);
|
|
342
340
|
|
|
343
|
-
|
|
341
|
+
}
|
|
344
342
|
|
|
345
|
-
|
|
343
|
+
if(supposedToBeType === 'number'){
|
|
346
344
|
|
|
347
|
-
|
|
348
|
-
testMode,
|
|
349
|
-
allOrNothing,
|
|
350
|
-
path: path ? `${path}.${key}` : key
|
|
351
|
-
});
|
|
352
|
-
|
|
353
|
-
}
|
|
345
|
+
cleanedValue = cleanNumber(value, schema.properties[key]);
|
|
354
346
|
|
|
355
|
-
|
|
347
|
+
}
|
|
356
348
|
|
|
357
|
-
|
|
358
|
-
testMode,
|
|
359
|
-
allOrNothing,
|
|
360
|
-
path: path ? `${path}.${key}` : key
|
|
361
|
-
});
|
|
362
|
-
|
|
363
|
-
}
|
|
349
|
+
if(supposedToBeType === 'string'){
|
|
364
350
|
|
|
365
|
-
|
|
351
|
+
cleanedValue = cleanString(value, schema.properties[key]);
|
|
366
352
|
|
|
367
|
-
|
|
353
|
+
}
|
|
368
354
|
|
|
369
|
-
|
|
355
|
+
if(supposedToBeType === 'timestamp'){
|
|
370
356
|
|
|
371
|
-
|
|
357
|
+
cleanedValue = cleanTimestamp(value, schema.properties[key]);
|
|
372
358
|
|
|
373
|
-
|
|
359
|
+
}
|
|
374
360
|
|
|
375
|
-
|
|
376
|
-
error: err,
|
|
377
|
-
value: value,
|
|
378
|
-
requirements: schema.properties[key]
|
|
379
|
-
};
|
|
361
|
+
if(supposedToBeType === 'uuid'){
|
|
380
362
|
|
|
381
|
-
|
|
363
|
+
cleanedValue = cleanUUID(value, schema.properties[key]);
|
|
364
|
+
|
|
365
|
+
}
|
|
382
366
|
|
|
367
|
+
if(supposedToBeType === 'object'){
|
|
368
|
+
|
|
369
|
+
cleanedValue = cleanObject(value, schema.properties[key]);
|
|
370
|
+
|
|
371
|
+
}
|
|
383
372
|
|
|
373
|
+
if(supposedToBeType === 'array'){
|
|
374
|
+
|
|
375
|
+
cleanedValue = cleanArray(value, schema.properties[key]);
|
|
376
|
+
|
|
384
377
|
}
|
|
385
378
|
|
|
386
379
|
cleanObj[key] = cleanedValue;
|
package/test/js/abc.test.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
// example.test.js
|
|
2
|
-
import {
|
|
3
|
-
expect, test
|
|
4
|
-
} from 'vitest';
|
|
2
|
+
import { expect, test } from 'vitest';
|
|
5
3
|
import utils from '../../index.js';
|
|
6
4
|
|
|
7
5
|
const testObject = {
|
|
@@ -201,7 +199,8 @@ test('testObject2', () => {
|
|
|
201
199
|
try {
|
|
202
200
|
|
|
203
201
|
const cleanedObject = utils.clean.object(testObject2, schema2);
|
|
204
|
-
|
|
202
|
+
|
|
203
|
+
console.log('cleanedObject', cleanedObject);
|
|
205
204
|
|
|
206
205
|
expect(cleanedObject).toStrictEqual(testObject2);
|
|
207
206
|
|
package/tests/clean/uuid.test.js
CHANGED
package/tests/env.test.js
CHANGED
package/tests/example.test.js
CHANGED
package/tests/fileList.test.js
CHANGED
package/tests/md5.test.js
CHANGED