@awesomeness-js/utils 1.0.19 → 1.0.21

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 (60) hide show
  1. package/build/build.js +7 -0
  2. package/index.js +35 -0
  3. package/package.json +13 -10
  4. package/schemas/schema1.js +121 -0
  5. package/schemas/schema2.js +121 -0
  6. package/schemas.js +15 -0
  7. package/src/clean/array.js +2 -0
  8. package/src/clean/boolean.js +22 -0
  9. package/src/clean/integer.js +74 -0
  10. package/src/clean/number.js +93 -0
  11. package/src/clean/object.js +3 -0
  12. package/src/clean/string.js +60 -0
  13. package/src/clean/timestamp.js +52 -0
  14. package/src/clean/uuid.js +33 -0
  15. package/src/combineFiles.js +10 -1
  16. package/src/decrypt.js +5 -3
  17. package/src/each.js +10 -0
  18. package/src/encrypt.js +5 -2
  19. package/src/setLocalEnvs.js +9 -4
  20. package/src/thingType.js +35 -0
  21. package/src/utils/clean.js +183 -0
  22. package/src/validateSchema.js +86 -0
  23. package/test/js/abc.test.js +6 -0
  24. package/test/secret.test.js +7 -1
  25. package/tests/clean/array.test.js +154 -0
  26. package/tests/clean/boolean.test.js +27 -0
  27. package/tests/clean/integer.test.js +39 -0
  28. package/tests/clean/number.test.js +49 -0
  29. package/tests/clean/object.test.js +172 -0
  30. package/tests/clean/string.test.js +44 -0
  31. package/tests/clean/timestamp.test.js +13 -0
  32. package/tests/clean/uuid.test.js +14 -0
  33. package/tests/combineFiles.test.js +24 -0
  34. package/tests/convertBytes.test.js +9 -0
  35. package/tests/env.test.js +17 -0
  36. package/tests/example.test.js +6 -0
  37. package/tests/fileList.test.js +21 -0
  38. package/tests/hash-and-encrypt.test.js +26 -0
  39. package/tests/md5.test.js +7 -0
  40. package/tests/uuid.test.js +15 -0
  41. package/tests/validateSchema.test.js +30 -0
  42. package/tsconfig.json +2 -1
  43. package/types/clean/array.d.ts +1 -0
  44. package/types/clean/boolean.d.ts +3 -0
  45. package/types/clean/integer.d.ts +6 -0
  46. package/types/clean/number.d.ts +8 -0
  47. package/types/clean/object.d.ts +1 -0
  48. package/types/clean/string.d.ts +7 -0
  49. package/types/clean/thing.d.ts +2 -0
  50. package/types/clean/timestamp.d.ts +5 -0
  51. package/types/clean/uuid.d.ts +3 -0
  52. package/types/each.d.ts +10 -1
  53. package/types/hashPassword.d.ts +1 -1
  54. package/types/index.d.ts +35 -0
  55. package/types/thingType.d.ts +2 -0
  56. package/types/utils/clean.d.ts +2 -0
  57. package/types/validatePassword.d.ts +1 -1
  58. package/types/validateSchema.d.ts +2 -0
  59. package/vitest.config.js +16 -0
  60. package/test.js +0 -58
@@ -0,0 +1,39 @@
1
+ // example.test.js
2
+ import { expect, test } from 'vitest'
3
+ import utils from '../../index.js';
4
+
5
+ let x = 1;
6
+ let y = 1.2;
7
+ let z = '102';
8
+ let z2 = '102.45';
9
+ let z3 = 102.45;
10
+
11
+ test('integer - 1', () => {
12
+ expect(utils.clean.integer(x)).toBe(1);
13
+ });
14
+
15
+ test('integer - null', () => {
16
+ expect(utils.clean.integer(y)).toBe(null);
17
+ });
18
+
19
+
20
+ test('integer as string - 102', () => {
21
+ expect(utils.clean.integer(z)).toBe(102);
22
+ });
23
+
24
+ test('integer - NOT to throw - 102', () => {
25
+ expect(() => utils.clean.integer(z, { required: true })).not.toThrow();
26
+ });
27
+
28
+ test('integer - to throw', () => {
29
+ expect(() => utils.clean.integer(y, { required: true })).toThrow();
30
+ });
31
+
32
+ test('integer - to throw - "102.45"', () => {
33
+ expect(() => utils.clean.integer(z2, { required: true })).toThrow();
34
+ });
35
+
36
+ test('integer - to throw - 102.45', () => {
37
+ console.log(utils.clean.integer(z3));
38
+ expect(() => utils.clean.integer(z3, { required: true })).toThrow();
39
+ });
@@ -0,0 +1,49 @@
1
+ // example.test.js
2
+ import { expect, test } from 'vitest'
3
+ import utils from '../../index.js';
4
+
5
+ let x = 1;
6
+ let y = 1.2;
7
+ let z = '102';
8
+ let z2 = '102.45';
9
+ let bad = 'bad';
10
+
11
+ test('number - 1', () => {
12
+ expect(utils.clean.number(x)).toBe(1);
13
+ });
14
+
15
+ test('number - 1.2', () => {
16
+ expect(utils.clean.number(y)).toBe(1.2);
17
+ });
18
+
19
+ test('number as string - 102', () => {
20
+ expect(utils.clean.number(z)).toBe(102);
21
+ });
22
+
23
+ test('number - string to 102', () => {
24
+ expect(utils.clean.number(z, { required: true })).toBe(102);
25
+ });
26
+
27
+ test('number - string to 102.45', () => {
28
+ console.log(utils.clean.number(z2));
29
+ expect(utils.clean.number(z2, { required: true })).toBe(102.45);
30
+ });
31
+
32
+ test('number as string - bad to null', () => {
33
+ expect(utils.clean.number(bad)).toBe(null);
34
+ });
35
+
36
+ test('number as string - bad to throw', () => {
37
+ expect(()=> utils.clean.number(bad, { required: true }) ).toThrow();
38
+ });
39
+
40
+ test('number is too high', () => {
41
+ expect(()=> utils.clean.number(100, { min: 1, max: 10, required: true }) ).toThrow();
42
+ expect(()=> utils.clean.number(100.11, { min: 1, max: 100.1, required: true }) ).toThrow();
43
+ });
44
+
45
+ test('number is too low', () => {
46
+ expect(()=> utils.clean.number(0, { min: 1, max: 10, required: true }) ).toThrow();
47
+ expect(()=> utils.clean.number(1.2, { min: 1.3, max: 10, required: true }) ).toThrow();
48
+ });
49
+
@@ -0,0 +1,172 @@
1
+ // example.test.js
2
+ import { expect, test } from 'vitest'
3
+ import utils from '../../index.js';
4
+
5
+ const testObject = {
6
+ stringProp: 'a string',
7
+ integerProp: 42,
8
+ numberProp: 3.14,
9
+ booleanProp: true,
10
+ timestampProp: new Date().toISOString(),
11
+ uuidProp: utils.uuid(), // generate a valid UUID
12
+ };
13
+
14
+ const invalidTestObject = {
15
+ stringProp: 123, // should be a string
16
+ integerProp: 'not an integer', // should be an integer
17
+ numberProp: 'not a number', // should be a number
18
+ booleanProp: 'not a boolean', // should be a boolean
19
+ timestampProp: 'not a timestamp', // should be a timestamp
20
+ uuidProp: 'not a uuid', // should be a uuid
21
+ optionalStringProp: 456, // should be a string
22
+ optionalIntegerProp: 'not an integer', // should be an integer
23
+ optionalNumberProp: 'not a number', // should be a number
24
+ optionalBooleanProp: 'not a boolean', // should be a boolean
25
+ optionalTimestampProp: 'not a timestamp', // should be a timestamp
26
+ optionalUuidProp: 'not a uuid' // should be a uuid
27
+ };
28
+
29
+ const testObject2 = {
30
+ obj1: { ...testObject },
31
+ obj2: { ...testObject }
32
+ };
33
+
34
+ const invalidTestObject2 = {
35
+ obj1: { ...invalidTestObject },
36
+ obj2: { ...invalidTestObject }
37
+ };
38
+
39
+ const schema = {
40
+ type: 'object',
41
+ properties: {
42
+ stringProp: { type: 'string', required: true },
43
+ integerProp: { type: 'integer', required: true },
44
+ numberProp: { type: 'number', required: true },
45
+ booleanProp: { type: 'boolean', required: true },
46
+ timestampProp: { type: 'timestamp', required: true },
47
+ uuidProp: { type: 'uuid', required: true },
48
+ optionalStringProp: { type: 'string', required: false },
49
+ optionalIntegerProp: { type: 'integer', required: false },
50
+ optionalNumberProp: { type: 'number', required: false },
51
+ optionalBooleanProp: { type: 'boolean', required: false },
52
+ optionalTimestampProp: { type: 'timestamp', required: false },
53
+ optionalUuidProp: { type: 'uuid', required: false }
54
+ },
55
+ };
56
+
57
+ const schema2 = {
58
+ type: 'object',
59
+ properties: {
60
+ obj1: {
61
+ type: 'object',
62
+ required: true,
63
+ properties: { ... schema.properties }
64
+ },
65
+ obj2: {
66
+ type: 'object',
67
+ required: true,
68
+ properties: { ... schema.properties }
69
+ }
70
+ },
71
+ };
72
+
73
+
74
+ const testArraysOfIntegers = [
75
+ [1, 2, 3],
76
+ [4, 5, 6],
77
+ [7, 8, 9]
78
+ ];
79
+
80
+ const testStringArrayOfArrays = [
81
+ ['a', 'b', 'c'],
82
+ ['d', 'e', 'f'],
83
+ ['g', 'h', 'i'],
84
+ ];
85
+
86
+ const testArrayOfBooleans = [
87
+ [true, false, true],
88
+ [false, true, false],
89
+ [true, true, false]
90
+ ];
91
+
92
+ const objectOfArrays = {
93
+ testArraysOfIntegers: [...testArraysOfIntegers],
94
+ testStringArrayOfArrays: [...testStringArrayOfArrays],
95
+ testArrayOfBooleans: [...testArrayOfBooleans],
96
+ };
97
+
98
+ const objectOfArrays_schema = {
99
+ type: 'object',
100
+ properties: {
101
+ testArraysOfIntegers: {
102
+ type: 'array',
103
+ items: {
104
+ type: 'array',
105
+ items: { type: 'integer' }
106
+ }
107
+ },
108
+ testStringArrayOfArrays: {
109
+ type: 'array',
110
+ items: {
111
+ type: 'array',
112
+ items: { type: 'string' }
113
+ }
114
+ },
115
+ testArrayOfBooleans: {
116
+ type: 'array',
117
+ items: {
118
+ type: 'array',
119
+ items: { type: 'boolean' }
120
+ }
121
+ }
122
+ }
123
+ };
124
+
125
+ test('testObject', () => {
126
+ try {
127
+ const cleanedObject = utils.clean.object(testObject, schema);
128
+ expect(cleanedObject).toStrictEqual(testObject);
129
+ } catch (error) {
130
+ console.error('Error cleaning object:', error);
131
+ throw error; // rethrow to fail the test
132
+ }
133
+ });
134
+
135
+ test('invalidTestObject', () => {
136
+ try {
137
+ const cleanedObject = utils.clean.object(invalidTestObject, schema);
138
+ //console.log({ cleanedObject })
139
+ expect(cleanedObject).toStrictEqual(invalidTestObject);
140
+ } catch (error) {
141
+ //console.error('Error cleaning object:', error);
142
+ expect(error.message).toBe('type invalid');
143
+ }
144
+ });
145
+
146
+ test('testObject2', () => {
147
+ try {
148
+ const cleanedObject = utils.clean.object(testObject2, schema2);
149
+ //console.log('cleanedObject', cleanedObject);
150
+ expect(cleanedObject).toStrictEqual(testObject2);
151
+ } catch (error) {
152
+ console.error('Error cleaning object:', error);
153
+ throw error; // rethrow to fail the test
154
+ }
155
+ });
156
+
157
+
158
+ test('incorrect should throw', () => {
159
+ expect(()=> utils.clean.object(testObject, schema2)).toThrow();
160
+ });
161
+
162
+
163
+ test('object of arrays test', () => {
164
+ try {
165
+ const cleanedObject = utils.clean.object(objectOfArrays, objectOfArrays_schema);
166
+ //console.log('cleanedObject', cleanedObject);
167
+ expect(cleanedObject).toStrictEqual(objectOfArrays);
168
+ } catch (error) {
169
+ console.error('Error cleaning object:', error);
170
+ throw error; // rethrow to fail the test
171
+ }
172
+ });
@@ -0,0 +1,44 @@
1
+ // example.test.js
2
+ import { expect, test } from 'vitest'
3
+ import utils from '../../index.js';
4
+
5
+ test('good string', () => {
6
+ expect(utils.clean.string('good string')).toBe('good string');
7
+ });
8
+
9
+ test('number', () => {
10
+ expect(utils.clean.string(123)).toBe(null);
11
+ expect(()=> utils.clean.string(123, { required: true })).toThrow();
12
+ });
13
+
14
+ test('string too short', () => {
15
+ expect(utils.clean.string('123456789', { minLength: 10 })).toBe(null);
16
+ expect(()=> utils.clean.string('123456789', { minLength: 10, required: true })).toThrow();
17
+ });
18
+
19
+ test('string too long', () => {
20
+ expect(utils.clean.string('123456789', { maxLength: 5 })).toBe(null);
21
+ expect(()=> utils.clean.string('123456789', { maxLength: 5, required: true })).toThrow();
22
+ });
23
+
24
+ test('no html', () => {
25
+ expect(utils.clean.string('<div>no go</div>')).toBe(null);
26
+ expect(()=> utils.clean.string('<div>no go</div>', { required: true })).toThrow();
27
+ });
28
+
29
+ test('no script', () => {
30
+ expect(utils.clean.string('<script>no go</script>')).toBe(null);
31
+ expect(()=> utils.clean.string('<script>no go</script>', { required: true })).toThrow();
32
+ });
33
+
34
+ test('allow html', () => {
35
+ let x = utils.clean.string('<div>all good/div>', { allowHtml: true });
36
+ expect(utils.clean.string('<div>all good</div>', { allowHtml: true })).toBe('<div>all good</div>');
37
+ expect(utils.clean.string('<div>all good</div>', { allowHtml: true, required: true })).toBe('<div>all good</div>');
38
+ });
39
+
40
+ test('allow script', () => {
41
+ expect(utils.clean.string('<script>all good</script>', { allowScripts: true })).toBe('<script>all good</script>');
42
+ expect(utils.clean.string('<script>all good</script>', { allowScripts: true, required: true })).toBe('<script>all good</script>');
43
+ });
44
+
@@ -0,0 +1,13 @@
1
+ // example.test.js
2
+ import { expect, test } from 'vitest'
3
+ import utils from '../../index.js';
4
+
5
+ test('good timestamp', () => {
6
+ const x = utils.clean.timestamp('2024-12-30');
7
+ expect(x).toBe('2024-12-30T00:00:00.000Z');
8
+ });
9
+
10
+ test('bad timestamp', () => {
11
+ expect(utils.clean.timestamp('2024-12-32')).toBe(null);
12
+ expect(()=>utils.clean.timestamp('2024-12-32', { required: true })).toThrow();
13
+ });
@@ -0,0 +1,14 @@
1
+ // example.test.js
2
+ import { expect, test } from 'vitest'
3
+ import utils from '../../index.js';
4
+
5
+ test('good uuid', () => {
6
+ const id = utils.uuid();
7
+ const x = utils.clean.uuid(id);
8
+ expect(x).toBe(id);
9
+ });
10
+
11
+ test('bad uuid', () => {
12
+ expect(utils.clean.uuid('2024-12-32')).toBe(null);
13
+ expect(()=>utils.clean.uuid('2024-12-32', { required: true })).toThrow();
14
+ });
@@ -0,0 +1,24 @@
1
+ // example.test.js
2
+ import { expect, test } from 'vitest'
3
+ import utils from '../index.js';
4
+
5
+ let combineTest = utils.combineFiles('./src', 'js');
6
+
7
+ let combineTestBrowser = utils.combineFiles('./src', 'js', {
8
+ moduleToBrowser: true
9
+ });
10
+
11
+ test('combineFiles - combine all files in src/js', () => {
12
+ expect(combineTest).toBeDefined();
13
+ expect(combineTestBrowser).toBeDefined();
14
+ expect(combineTest.length).toBeGreaterThan(combineTestBrowser.length);
15
+ })
16
+
17
+
18
+ test('has import', () => {
19
+ expect(combineTest.includes('import')).toBe(true);
20
+ })
21
+
22
+ test('should not have import', () => {
23
+ expect(combineTestBrowser.includes('import')).toBe(false);
24
+ })
@@ -0,0 +1,9 @@
1
+ // example.test.js
2
+ import { expect, test } from 'vitest'
3
+ import utils from '../index.js';
4
+
5
+ let convertBytesTest = utils.convertBytes(1024);
6
+ test('convertBytes 1024 => 1.00 KB', () => {
7
+ expect(convertBytesTest).toBeDefined();
8
+ expect(convertBytesTest).toEqual('1.00 KB');
9
+ });
@@ -0,0 +1,17 @@
1
+ // example.test.js
2
+ import { expect, test } from 'vitest'
3
+ import utils from '../index.js';
4
+
5
+ await utils.setLocalEnvs('./secrets/local.env');
6
+
7
+ const env1 = process.env.JUST_A_TEST;
8
+
9
+ test('localEnv - should be testValue', () => {
10
+ expect(env1).toBe('Local just a test');
11
+ });
12
+
13
+ await utils.setLocalEnvs('./secrets/dev.env');
14
+
15
+ test('localEnv - should be testValue', () => {
16
+ expect(process.env.JUST_A_TEST).toBe('Dev just a test');
17
+ });
@@ -0,0 +1,6 @@
1
+ // example.test.js
2
+ import { expect, test } from 'vitest'
3
+
4
+ test('adds 2 + 3 to equal 5', () => {
5
+ expect(2 + 3).toBe(5)
6
+ })
@@ -0,0 +1,21 @@
1
+ // example.test.js
2
+ import { expect, test } from 'vitest'
3
+ import utils from '../index.js';
4
+
5
+
6
+ let fileList2 = utils.getAllFiles('./test', {
7
+ // fileTypes: ['.css'],
8
+ // fileTypes: ['.js'],
9
+ ignore: [
10
+ "/ignoreFolder",
11
+ "/ignoreFolder2/",
12
+ "*.env",
13
+ "*.test.js",
14
+ "/css/*.js",
15
+ "/js/*.css"
16
+ ]
17
+ });
18
+
19
+ test('file list of 3', () => {
20
+ expect(fileList2.length).toBe(3); // Adjust this number based on your test folder contents
21
+ });
@@ -0,0 +1,26 @@
1
+ // example.test.js
2
+ import { expect, test } from 'vitest'
3
+ import utils from '../index.js';
4
+
5
+ // set AWESOMENESS_ENCRYPTION_KEY
6
+ await utils.setLocalEnvs('./secrets/dev.env');
7
+
8
+ const storedHash = utils.password.hash('mySecret123');
9
+
10
+ test('correct password check', () => {
11
+ expect(utils.password.check('mySecret123', storedHash)).toBe(true)
12
+ });
13
+
14
+ test('incorrect password check', () => {
15
+ expect(utils.password.check('wrongPassword', storedHash)).toBe(false)
16
+ });
17
+
18
+ // Example 256-bit key for AES-256
19
+ // In real usage, store this securely (e.g., an environment variable or key vault).
20
+
21
+ const secretMessage = 'This is top secret!';
22
+ const encrypted = utils.encrypt(secretMessage);
23
+ const decrypted = utils.decrypt(encrypted);
24
+ test('encryption and decryption', () => {
25
+ expect(decrypted).toBe(secretMessage)
26
+ });
@@ -0,0 +1,7 @@
1
+ // example.test.js
2
+ import { expect, test } from 'vitest'
3
+ import utils from '../index.js';
4
+ let md5Test = utils.md5('test');
5
+ test('md5 test', () => {
6
+ expect(md5Test).toBe('098f6bcd4621d373cade4e832627b4f6');
7
+ });
@@ -0,0 +1,15 @@
1
+ // example.test.js
2
+ import { expect, test } from 'vitest'
3
+ import utils from '../index.js';
4
+
5
+ let uuid = utils.uuid();
6
+ let isValid = utils.isUUID(uuid);
7
+
8
+ test('uuid create', () => {
9
+ expect(uuid).toBeDefined();
10
+ expect(uuid.length).toBe(36);
11
+ });
12
+
13
+ test('uuid isValid', () => {
14
+ expect(isValid).toBe(true);
15
+ });
@@ -0,0 +1,30 @@
1
+ import { expect, test } from 'vitest'
2
+ import utils from '../index.js';
3
+ import schemas from '../schemas.js';
4
+
5
+ let schema1 = utils.validateSchema(schemas.schema1);
6
+ let schema2 = utils.validateSchema(schemas.schema2);
7
+
8
+
9
+ test('schema1 is valid', () => {
10
+ expect(schema1).toBe(true);
11
+ });
12
+
13
+ test('schema2 is valid', () => {
14
+ expect(schema2).toBe(true);
15
+ });
16
+
17
+ let expectedError;
18
+
19
+ try {
20
+ utils.validateSchema({
21
+ notReal: true
22
+ });
23
+ expectedError = false; // should not reach here
24
+ } catch(e){
25
+ expectedError = true;
26
+ }
27
+
28
+ test('invalid schema throws error', () => {
29
+ expect(expectedError).toBe(true);
30
+ });
package/tsconfig.json CHANGED
@@ -6,7 +6,8 @@
6
6
  "outDir": "types", // Output directory for type files
7
7
  "moduleResolution": "node",
8
8
  "skipLibCheck": true,
9
- "esModuleInterop": true
9
+ "esModuleInterop": true,
10
+ "types": ["vitest/globals"]
10
11
  },
11
12
  "include": ["src/**/*"],
12
13
  "exclude": [
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ export default function cleanBoolean(x: any, { required }?: {
2
+ required?: boolean;
3
+ }): any;
@@ -0,0 +1,6 @@
1
+ export default function cleanInt(x: any, { required, convertString, min, max, }?: {
2
+ required?: boolean;
3
+ convertString?: boolean;
4
+ min?: boolean;
5
+ max?: boolean;
6
+ }): any;
@@ -0,0 +1,8 @@
1
+ export default function cleanNumber(x: any, { required, convertString, min, max, maxDecimal, minDecimal, }?: {
2
+ required?: boolean;
3
+ convertString?: boolean;
4
+ min?: boolean;
5
+ max?: boolean;
6
+ maxDecimal?: boolean;
7
+ minDecimal?: boolean;
8
+ }): any;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ export default function cleanString(x: any, { required, minLength, maxLength, allowHtml, allowScripts }?: {
2
+ required?: boolean;
3
+ minLength?: boolean;
4
+ maxLength?: boolean;
5
+ allowHtml?: boolean;
6
+ allowScripts?: boolean;
7
+ }): string;
@@ -0,0 +1,2 @@
1
+ declare function _default(thing: any, schema: any): any;
2
+ export default _default;
@@ -0,0 +1,5 @@
1
+ export default function cleanTimestamp(isoDateTimeString: any, { required, maxDaysInFuture, maxDaysInFPast, }?: {
2
+ required?: boolean;
3
+ maxDaysInFuture?: boolean;
4
+ maxDaysInFPast?: boolean;
5
+ }): string;
@@ -0,0 +1,3 @@
1
+ export default function cleanUUID(uuid: any, { required, }?: {
2
+ required?: boolean;
3
+ }): string;
package/types/each.d.ts CHANGED
@@ -1 +1,10 @@
1
- export default function each(objectOrArray: any, callback: any): void;
1
+ /**
2
+ * Iterates over elements of an array or properties of an object, invoking a callback for each element/property.
3
+ * The iteration stops if the callback returns `false`.
4
+ *
5
+ * @example each({ a: 1, b: 2 }, (value, key) => { console.log(value, key); });
6
+ * @param {Object|Array} objectOrArray - The object or array to iterate over.
7
+ * @param {Function} callback - The function to invoke per iteration. It is invoked with two arguments: (value, key/index).
8
+ * @returns {void}
9
+ */
10
+ export default function each(objectOrArray: any | any[], callback: Function): void;
@@ -1 +1 @@
1
- export default function hashPassword(password: any): string;
1
+ export default function hashPassword(password: any): string;