@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.
Files changed (77) hide show
  1. package/index.js +32 -0
  2. package/package.json +1 -1
  3. package/schemas/schema1.js +1 -8
  4. package/schemas/schema2.js +1 -8
  5. package/src/random/array.js +46 -0
  6. package/src/random/arrayValue.js +5 -0
  7. package/src/random/boolean.js +5 -0
  8. package/src/random/integer.js +8 -0
  9. package/src/random/number.js +17 -0
  10. package/src/random/object.js +46 -0
  11. package/src/random/string.js +10 -0
  12. package/src/random/timestamp.js +15 -0
  13. package/src/random/uuid.js +7 -0
  14. package/src/utils/clean.js +66 -73
  15. package/test/js/abc.test.js +1 -3
  16. package/tests/clean/array.test.js +1 -3
  17. package/tests/clean/boolean.test.js +1 -3
  18. package/tests/clean/integer.test.js +1 -3
  19. package/tests/clean/number.test.js +1 -3
  20. package/tests/clean/object.test.js +3 -4
  21. package/tests/clean/string.test.js +1 -3
  22. package/tests/clean/timestamp.test.js +1 -3
  23. package/tests/clean/uuid.test.js +1 -3
  24. package/tests/collectImports.test.js +1 -3
  25. package/tests/combineFiles.test.js +1 -3
  26. package/tests/convertBytes.test.js +1 -3
  27. package/tests/env.test.js +1 -3
  28. package/tests/example.test.js +1 -3
  29. package/tests/fileList.test.js +1 -3
  30. package/tests/hash-and-encrypt.test.js +1 -3
  31. package/tests/md5.test.js +1 -3
  32. package/tests/namedExports.test.js +1 -3
  33. package/tests/random.test.js +110 -0
  34. package/tests/uuid.test.js +1 -3
  35. package/tests/validateSchema.test.js +1 -3
  36. package/types/build.d.ts +10 -10
  37. package/types/clean/array.d.ts +6 -6
  38. package/types/clean/boolean.d.ts +3 -3
  39. package/types/clean/buffer.d.ts +12 -12
  40. package/types/clean/custom.d.ts +4 -4
  41. package/types/clean/file.d.ts +11 -11
  42. package/types/clean/integer.d.ts +6 -6
  43. package/types/clean/number.d.ts +8 -8
  44. package/types/clean/object.d.ts +6 -6
  45. package/types/clean/string.d.ts +10 -10
  46. package/types/clean/timestamp.d.ts +5 -5
  47. package/types/clean/uuid.d.ts +3 -3
  48. package/types/collectImports.d.ts +6 -6
  49. package/types/combineFiles.d.ts +8 -8
  50. package/types/convertBytes.d.ts +8 -8
  51. package/types/decrypt.d.ts +1 -1
  52. package/types/each.d.ts +10 -10
  53. package/types/eachAsync.d.ts +1 -1
  54. package/types/encrypt.d.ts +5 -5
  55. package/types/getAllFiles.d.ts +6 -6
  56. package/types/index.d.ts +150 -150
  57. package/types/isUUID.d.ts +1 -1
  58. package/types/md5.d.ts +2 -2
  59. package/types/password/check.d.ts +1 -1
  60. package/types/password/hash.d.ts +1 -1
  61. package/types/setLocalEnvs.d.ts +2 -2
  62. package/types/shouldIgnore.d.ts +1 -1
  63. package/types/thingType.d.ts +2 -2
  64. package/types/toPennies.d.ts +1 -1
  65. package/types/utils/buildExportsTree.d.ts +4 -4
  66. package/types/utils/buildFileDataList.d.ts +12 -12
  67. package/types/utils/clean.d.ts +27 -27
  68. package/types/utils/extractJSDocComment.d.ts +1 -1
  69. package/types/utils/generateFile.d.ts +8 -8
  70. package/types/utils/generateFlatExportLines.d.ts +7 -7
  71. package/types/utils/generateImportStatements.d.ts +5 -5
  72. package/types/utils/generateNamedExports.d.ts +7 -7
  73. package/types/utils/generateNamespaceCode.d.ts +7 -7
  74. package/types/utils/generateNamespaceExportLines.d.ts +6 -6
  75. package/types/uuid.d.ts +2 -2
  76. package/types/validateSchema.d.ts +2 -2
  77. 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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@awesomeness-js/utils",
3
- "version": "1.1.22",
3
+ "version": "1.2.0",
4
4
  "description": "Awesomeness - Utils",
5
5
  "repository": {
6
6
  "type": "git",
@@ -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,
@@ -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,5 @@
1
+ export default (array) => {
2
+
3
+ return array[Math.floor(Math.random() * array.length)];
4
+
5
+ };
@@ -0,0 +1,5 @@
1
+ export default () => {
2
+
3
+ return Math.random() < 0.5;
4
+
5
+ };
@@ -0,0 +1,8 @@
1
+ export default ({
2
+ min = 1,
3
+ max = 1_000_000,
4
+ } = {}) => {
5
+
6
+ return Math.floor(Math.random() * (max - min + 1)) + min;
7
+
8
+ };
@@ -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,10 @@
1
+ export default ({
2
+ minLength = 5,
3
+ maxLength = 10,
4
+ } = {}) => {
5
+
6
+ const length = Math.floor(Math.random() * (maxLength - minLength + 1)) + minLength;
7
+
8
+ return Math.random().toString(36).substring(2, 2 + length);
9
+
10
+ };
@@ -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
+ };
@@ -0,0 +1,7 @@
1
+ import uuid from '../uuid.js'
2
+
3
+ export default () => {
4
+
5
+ return uuid();
6
+
7
+ };
@@ -75,7 +75,11 @@ function cleanArray(arr, schema = {}, {
75
75
  }
76
76
 
77
77
 
78
- if(testMode){ console.log(`cleaning ${supposedToBeType}`, item, schema.items); }
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(obj, (value, key) => {
272
+ each(schema.properties, (schemaItemProperties, key) => {
267
273
 
268
274
  let cleanedValue;
269
275
 
270
- try {
271
-
272
- const valType = thingType(value);
273
- const supposedToBeType = schema.properties[key].type;
276
+ const value = obj[key];
274
277
 
275
- if(testMode){
276
- console.log(`cleaning ${key}`,{
277
- valType,
278
- supposedToBeType,
279
- value,
280
- });
281
- }
278
+ const valType = thingType(value);
279
+ const supposedToBeType = schemaItemProperties.type;
282
280
 
283
- if(valType !== supposedToBeType){
281
+ if(valType === 'undefined'){
282
+
283
+ if(schemaItemProperties.required){
284
284
 
285
285
  throw {
286
- message: 'type invalid',
287
- valType,
288
- supposedToBeType,
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
- if(!knownTypesToClean.includes(supposedToBeType)){
297
+ if(valType !== supposedToBeType){
296
298
 
299
+ if(schemaItemProperties.required){
300
+
297
301
  throw {
298
- message: 'Unknown type to clean in schema',
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
- if(supposedToBeType === 'integer'){
310
+ }
316
311
 
317
- cleanedValue = cleanInteger(value, schema.properties[key]);
318
312
 
319
- }
313
+ if(!knownTypesToClean.includes(supposedToBeType)){
320
314
 
321
- if(supposedToBeType === 'number'){
315
+ throw {
316
+ message: 'Unknown type to clean in schema',
317
+ supposedToBeType,
318
+ key
319
+ };
322
320
 
323
- cleanedValue = cleanNumber(value, schema.properties[key]);
321
+ }
324
322
 
325
- }
323
+
324
+ if(testMode){
326
325
 
327
- if(supposedToBeType === 'string'){
326
+ console.log(`cleaning ${supposedToBeType}`, value, schema.properties[key]);
328
327
 
329
- cleanedValue = cleanString(value, schema.properties[key]);
328
+ }
330
329
 
331
- }
332
330
 
333
- if(supposedToBeType === 'timestamp'){
331
+ if(supposedToBeType === 'boolean'){
334
332
 
335
- cleanedValue = cleanTimestamp(value, schema.properties[key]);
333
+ cleanedValue = cleanBoolean(value, schema.properties[key]);
336
334
 
337
- }
335
+ }
338
336
 
339
- if(supposedToBeType === 'uuid'){
337
+ if(supposedToBeType === 'integer'){
340
338
 
341
- cleanedValue = cleanUUID(value, schema.properties[key]);
339
+ cleanedValue = cleanInteger(value, schema.properties[key]);
342
340
 
343
- }
341
+ }
344
342
 
345
- if(supposedToBeType === 'object'){
343
+ if(supposedToBeType === 'number'){
346
344
 
347
- cleanedValue = cleanObject(value, schema.properties[key], {
348
- testMode,
349
- allOrNothing,
350
- path: path ? `${path}.${key}` : key
351
- });
352
-
353
- }
345
+ cleanedValue = cleanNumber(value, schema.properties[key]);
354
346
 
355
- if(supposedToBeType === 'array'){
347
+ }
356
348
 
357
- cleanedValue = cleanArray(value, schema.properties[key], {
358
- testMode,
359
- allOrNothing,
360
- path: path ? `${path}.${key}` : key
361
- });
362
-
363
- }
349
+ if(supposedToBeType === 'string'){
364
350
 
365
- } catch (err) {
351
+ cleanedValue = cleanString(value, schema.properties[key]);
366
352
 
367
- if(allOrNothing){
353
+ }
368
354
 
369
- throw err;
355
+ if(supposedToBeType === 'timestamp'){
370
356
 
371
- } else {
357
+ cleanedValue = cleanTimestamp(value, schema.properties[key]);
372
358
 
373
- const errorPath = path ? `${path}.${key}` : key;
359
+ }
374
360
 
375
- errors[errorPath] = {
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;
@@ -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
 
6
4
  test('adds 2 + 3 to equal 5', () => {
7
5
 
@@ -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 testStringArray = [ 'a', 'b', 'c' ];
@@ -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
  let x = true;
@@ -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
  let x = 1;
@@ -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
  let x = 1;
@@ -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
- //console.log('cleanedObject', cleanedObject);
202
+
203
+ console.log('cleanedObject', cleanedObject);
205
204
 
206
205
  expect(cleanedObject).toStrictEqual(testObject2);
207
206
 
@@ -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
  test('good string', () => {
@@ -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
  test('good timestamp', () => {
@@ -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
  test('good uuid', () => {
@@ -1,7 +1,5 @@
1
1
  import utils from '../index.js';
2
- import {
3
- expect, test
4
- } from 'vitest';
2
+ import { expect, test } from 'vitest';
5
3
 
6
4
  test('collectImports', () => {
7
5
 
@@ -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
  let combineTest = utils.combineFiles('./test', 'js');
@@ -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
  let convertBytesTest = utils.convertBytes(1024);
package/tests/env.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
  await utils.setLocalEnvs('./secrets/local.env');
@@ -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
 
6
4
  test('adds 2 + 3 to equal 5', () => {
7
5
 
@@ -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
 
@@ -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
  // set AWESOMENESS_ENCRYPTION_KEY
package/tests/md5.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
  let md5Test = utils.md5('test');
7
5
 
@@ -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
 
6
4
  import { password } from '../index.js';
7
5