@awesomeness-js/utils 1.0.18 → 1.0.20

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 (58) hide show
  1. package/build/build.js +7 -0
  2. package/index.js +47 -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/secrets/dev.env +2 -1
  8. package/secrets/local.env +2 -1
  9. package/src/clean/array.js +43 -0
  10. package/src/clean/boolean.js +22 -0
  11. package/src/clean/integer.js +55 -0
  12. package/src/clean/number.js +75 -0
  13. package/src/clean/object.js +67 -0
  14. package/src/clean/string.js +58 -0
  15. package/src/clean/thing.js +31 -0
  16. package/src/clean/timestamp.js +56 -0
  17. package/src/clean/uuid.js +33 -0
  18. package/src/combineFiles.js +10 -1
  19. package/src/decrypt.js +19 -0
  20. package/src/each.js +10 -0
  21. package/src/encrypt.js +29 -0
  22. package/src/password/check.js +9 -0
  23. package/src/password/hash.js +11 -0
  24. package/src/setLocalEnvs.js +9 -4
  25. package/src/thingType.js +33 -0
  26. package/src/validateSchema.js +78 -0
  27. package/test/js/abc.test.js +6 -0
  28. package/test/secret.test.js +7 -1
  29. package/tests/combineFiles.test.js +24 -0
  30. package/tests/convertBytes.test.js +9 -0
  31. package/tests/env.test.js +17 -0
  32. package/tests/example.test.js +6 -0
  33. package/tests/fileList.test.js +21 -0
  34. package/tests/hash-and-encrypt.test.js +26 -0
  35. package/tests/md5.test.js +7 -0
  36. package/tests/uuid.test.js +15 -0
  37. package/tests/validateSchema.test.js +30 -0
  38. package/types/clean/array.d.ts +2 -0
  39. package/types/clean/boolean.d.ts +3 -0
  40. package/types/clean/integer.d.ts +5 -0
  41. package/types/clean/number.d.ts +7 -0
  42. package/types/clean/object.d.ts +2 -0
  43. package/types/clean/string.d.ts +7 -0
  44. package/types/clean/thing.d.ts +2 -0
  45. package/types/clean/timestamp.d.ts +5 -0
  46. package/types/clean/uuid.d.ts +3 -0
  47. package/types/decrypt.d.ts +1 -0
  48. package/types/each.d.ts +10 -1
  49. package/types/encrypt.d.ts +5 -0
  50. package/types/hashPassword.d.ts +1 -0
  51. package/types/index.d.ts +47 -0
  52. package/types/password/check.d.ts +1 -0
  53. package/types/password/hash.d.ts +1 -0
  54. package/types/thingType.d.ts +2 -0
  55. package/types/validatePassword.d.ts +1 -0
  56. package/types/validateSchema.d.ts +2 -0
  57. package/vitest.config.js +6 -0
  58. package/test.js +0 -42
package/build/build.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import build from '../src/build.js';
2
+
2
3
  build({
3
4
  src: './src',
4
5
  dest: './index.js',
@@ -8,4 +9,10 @@ build({
8
9
  'ignoreFolder/*',
9
10
  //'namespaceExample/*',
10
11
  ],
12
+ });
13
+
14
+ build({
15
+ src: './schemas',
16
+ dest: './schemas.js',
17
+ dts: false
11
18
  });
package/index.js CHANGED
@@ -4,16 +4,30 @@
4
4
  * Do not edit manually.
5
5
  */
6
6
  import _build from './src/build.js';
7
+ import _clean_array from './src/clean/array.js';
8
+ import _clean_boolean from './src/clean/boolean.js';
9
+ import _clean_integer from './src/clean/integer.js';
10
+ import _clean_number from './src/clean/number.js';
11
+ import _clean_object from './src/clean/object.js';
12
+ import _clean_string from './src/clean/string.js';
13
+ import _clean_thing from './src/clean/thing.js';
14
+ import _clean_timestamp from './src/clean/timestamp.js';
15
+ import _clean_uuid from './src/clean/uuid.js';
7
16
  import _combineFiles from './src/combineFiles.js';
8
17
  import _convertBytes from './src/convertBytes.js';
18
+ import _decrypt from './src/decrypt.js';
9
19
  import _each from './src/each.js';
10
20
  import _eachAsync from './src/eachAsync.js';
21
+ import _encrypt from './src/encrypt.js';
11
22
  import _getAllFiles from './src/getAllFiles.js';
12
23
  import _ignoreFolder_ignoreMe from './src/ignoreFolder/ignoreMe.js';
13
24
  import _ignoreMe from './src/ignoreMe.js';
14
25
  import _isUUID from './src/isUUID.js';
15
26
  import _md5 from './src/md5.js';
27
+ import _password_check from './src/password/check.js';
28
+ import _password_hash from './src/password/hash.js';
16
29
  import _setLocalEnvs from './src/setLocalEnvs.js';
30
+ import _thingType from './src/thingType.js';
17
31
  import _toPennies from './src/toPennies.js';
18
32
  import _utils_buildExportsTree from './src/utils/buildExportsTree.js';
19
33
  import _utils_buildFileDataList from './src/utils/buildFileDataList.js';
@@ -26,19 +40,24 @@ import _utils_generateNamespaceCode from './src/utils/generateNamespaceCode.js';
26
40
  import _utils_generateNamespaceExportLines from './src/utils/generateNamespaceExportLines.js';
27
41
  import _utils_shouldIgnore from './src/utils/shouldIgnore.js';
28
42
  import _uuid from './src/uuid.js';
43
+ import _validateSchema from './src/validateSchema.js';
29
44
 
30
45
  export { _build as build };
31
46
  export { _combineFiles as combineFiles };
32
47
  export { _convertBytes as convertBytes };
48
+ export { _decrypt as decrypt };
33
49
  export { _each as each };
34
50
  export { _eachAsync as eachAsync };
51
+ export { _encrypt as encrypt };
35
52
  export { _getAllFiles as getAllFiles };
36
53
  export { _ignoreMe as ignoreMe };
37
54
  export { _isUUID as isUUID };
38
55
  export { _md5 as md5 };
39
56
  export { _setLocalEnvs as setLocalEnvs };
57
+ export { _thingType as thingType };
40
58
  export { _toPennies as toPennies };
41
59
  export { _uuid as uuid };
60
+ export { _validateSchema as validateSchema };
42
61
 
43
62
  export default {
44
63
  /**
@@ -63,18 +82,46 @@ export default {
63
82
  * @returns {string} The converted bytes in a string format with appropriate units.
64
83
  */
65
84
  convertBytes: _convertBytes,
85
+ decrypt: _decrypt,
86
+ /**
87
+ * Iterates over elements of an array or properties of an object, invoking a callback for each element/property.
88
+ * The iteration stops if the callback returns `false`.
89
+ *
90
+ * @example each({ a: 1, b: 2 }, (value, key) => { console.log(value, key); });
91
+ * @param {Object|Array} objectOrArray - The object or array to iterate over.
92
+ * @param {Function} callback - The function to invoke per iteration. It is invoked with two arguments: (value, key/index).
93
+ * @returns {void}
94
+ */
66
95
  each: _each,
67
96
  eachAsync: _eachAsync,
97
+ encrypt: _encrypt,
68
98
  getAllFiles: _getAllFiles,
69
99
  ignoreMe: _ignoreMe,
70
100
  isUUID: _isUUID,
71
101
  md5: _md5,
72
102
  setLocalEnvs: _setLocalEnvs,
103
+ thingType: _thingType,
73
104
  toPennies: _toPennies,
74
105
  uuid: _uuid,
106
+ validateSchema: _validateSchema,
107
+ clean: {
108
+ array: _clean_array,
109
+ boolean: _clean_boolean,
110
+ integer: _clean_integer,
111
+ number: _clean_number,
112
+ object: _clean_object,
113
+ string: _clean_string,
114
+ thing: _clean_thing,
115
+ timestamp: _clean_timestamp,
116
+ uuid: _clean_uuid,
117
+ },
75
118
  ignoreFolder: {
76
119
  ignoreMe: _ignoreFolder_ignoreMe,
77
120
  },
121
+ password: {
122
+ check: _password_check,
123
+ hash: _password_hash,
124
+ },
78
125
  utils: {
79
126
  buildExportsTree: _utils_buildExportsTree,
80
127
  buildFileDataList: _utils_buildFileDataList,
package/package.json CHANGED
@@ -1,23 +1,26 @@
1
1
  {
2
2
  "name": "@awesomeness-js/utils",
3
- "version": "1.0.18",
3
+ "version": "1.0.20",
4
4
  "description": "Awesomeness - Utils",
5
5
  "repository": {
6
- "type": "git",
7
- "url": "git+https://github.com/awesomeness-js/utils.git"
6
+ "type": "git",
7
+ "url": "git+https://github.com/awesomeness-js/utils.git"
8
8
  },
9
9
  "scripts": {
10
- "prebuild": "node ./build/build.js",
11
- "build": "tsc && npm run postBuild",
12
- "postBuild": "node ./build/postBuild.js"
13
- },
10
+ "prebuild": "node ./build/build.js",
11
+ "build": "tsc && npm run postBuild",
12
+ "postBuild": "node ./build/postBuild.js",
13
+ "test": "vitest"
14
+ },
14
15
  "type": "module",
15
16
  "types": "./types/index.d.ts",
16
17
  "main": "index.js",
17
18
  "author": "Scott Forte",
18
19
  "license": "MIT",
19
20
  "publishConfig": {
20
- "access": "public"
21
+ "access": "public"
22
+ },
23
+ "devDependencies": {
24
+ "vitest": "^3.0.9"
21
25
  }
22
- }
23
-
26
+ }
@@ -0,0 +1,121 @@
1
+ import { uuid } from '../index.js';
2
+
3
+ const properties = {
4
+
5
+ exampleId: {
6
+ type: 'uuid',
7
+ description: 'the id of the vertex',
8
+ default: () => { return uuid(); },
9
+ example: '00000000-0000-0000-0000-000000000000',
10
+ immutable: true,
11
+ required: true
12
+ },
13
+
14
+ exampleString: {
15
+ type: 'string',
16
+ minLength: 1,
17
+ maxLength: 100,
18
+ example: 'This is my post',
19
+ description: 'main title of the post',
20
+ default: null,
21
+ required: true,
22
+ },
23
+
24
+ exampleArray: {
25
+ type: 'array',
26
+ items: {
27
+ type: 'string',
28
+ description: 'any tag, can have spaces',
29
+ minLength: 1,
30
+ maxLength: 100,
31
+ }
32
+ },
33
+
34
+ exampleArrayOfObjects: {
35
+ type: 'array',
36
+ items: {
37
+ type: 'object',
38
+ properties: {
39
+ name: {
40
+ type: 'string'
41
+ }
42
+ }
43
+ }
44
+ },
45
+
46
+ exampleObject: {
47
+ type: 'object',
48
+ properties: {
49
+ someString:{
50
+ type: 'string',
51
+ },
52
+ someArrayOfObjects:{
53
+ type: 'array',
54
+ items: {
55
+ type: 'object',
56
+ properties: {
57
+ name: {
58
+ type: 'string'
59
+ }
60
+ }
61
+ }
62
+ }
63
+ }
64
+ },
65
+
66
+ exampleRef: {
67
+ type: 'array',
68
+ items: {
69
+ ref: 'someThing'
70
+ }
71
+ }
72
+
73
+ };
74
+
75
+ const edges = Object.freeze({
76
+
77
+ });
78
+
79
+ const edgesIn = Object.freeze({
80
+ author: 'author',
81
+ viewed: 'viewed',
82
+ });
83
+
84
+ const relatedKVs = {
85
+ 'blogPost::${id}::body': {
86
+ type: 'array',
87
+ description: 'body of the post - stored off vertex for query performance',
88
+ items: {
89
+ type: 'object',
90
+ description: 'any part of a blog post on order',
91
+ properties: {
92
+ fn: {
93
+ type: 'string',
94
+ description: 'the name of the fn such a "body" (app.print.blogPost.body)',
95
+ enum: [
96
+ "body",
97
+ "h1",
98
+ "h2",
99
+ "image",
100
+ "div",
101
+ "p",
102
+ "callToAction"
103
+ ]
104
+ },
105
+ data: {
106
+ type: 'object',
107
+ description: 'data passed to the fn',
108
+ }
109
+ }
110
+ }
111
+ }
112
+ };
113
+
114
+ export default {
115
+ name: 'Blog Post',
116
+ description: `a blog post`,
117
+ properties,
118
+ edges,
119
+ edgesIn,
120
+ relatedKVs
121
+ };
@@ -0,0 +1,121 @@
1
+ import { uuid } from '../index.js';
2
+
3
+ const properties = {
4
+
5
+ exampleId: {
6
+ type: 'uuid',
7
+ description: 'the id of the vertex',
8
+ default: () => { return uuid(); },
9
+ example: '00000000-0000-0000-0000-000000000000',
10
+ immutable: true,
11
+ required: true
12
+ },
13
+
14
+ exampleString: {
15
+ type: 'string',
16
+ minLength: 1,
17
+ maxLength: 100,
18
+ example: 'This is my post',
19
+ description: 'main title of the post',
20
+ default: null,
21
+ required: true,
22
+ },
23
+
24
+ exampleArray: {
25
+ type: 'array',
26
+ items: {
27
+ type: 'string',
28
+ description: 'any tag, can have spaces',
29
+ minLength: 1,
30
+ maxLength: 100,
31
+ }
32
+ },
33
+
34
+ exampleArrayOfObjects: {
35
+ type: 'array',
36
+ items: {
37
+ type: 'object',
38
+ properties: {
39
+ name: {
40
+ type: 'string'
41
+ }
42
+ }
43
+ }
44
+ },
45
+
46
+ exampleObject: {
47
+ type: 'object',
48
+ properties: {
49
+ someString:{
50
+ type: 'string',
51
+ },
52
+ someArrayOfObjects:{
53
+ type: 'array',
54
+ items: {
55
+ type: 'object',
56
+ properties: {
57
+ name: {
58
+ type: 'string'
59
+ }
60
+ }
61
+ }
62
+ }
63
+ }
64
+ },
65
+
66
+ exampleRef: {
67
+ type: 'array',
68
+ items: {
69
+ ref: 'someThing'
70
+ }
71
+ }
72
+
73
+ };
74
+
75
+ const edges = Object.freeze({
76
+
77
+ });
78
+
79
+ const edgesIn = Object.freeze({
80
+ author: 'author',
81
+ viewed: 'viewed',
82
+ });
83
+
84
+ const relatedKVs = {
85
+ 'blogPost::${id}::body': {
86
+ type: 'array',
87
+ description: 'body of the post - stored off vertex for query performance',
88
+ items: {
89
+ type: 'object',
90
+ description: 'any part of a blog post on order',
91
+ properties: {
92
+ fn: {
93
+ type: 'string',
94
+ description: 'the name of the fn such a "body" (app.print.blogPost.body)',
95
+ enum: [
96
+ "body",
97
+ "h1",
98
+ "h2",
99
+ "image",
100
+ "div",
101
+ "p",
102
+ "callToAction"
103
+ ]
104
+ },
105
+ data: {
106
+ type: 'object',
107
+ description: 'data passed to the fn',
108
+ }
109
+ }
110
+ }
111
+ }
112
+ };
113
+
114
+ export default {
115
+ name: 'Blog Post',
116
+ description: `a blog post`,
117
+ properties,
118
+ edges,
119
+ edgesIn,
120
+ relatedKVs
121
+ };
package/schemas.js ADDED
@@ -0,0 +1,15 @@
1
+ /**
2
+ * This file is auto-generated by the build script.
3
+ * It consolidates API functions for use in the application.
4
+ * Do not edit manually.
5
+ */
6
+ import _schema1 from './schemas/schema1.js';
7
+ import _schema2 from './schemas/schema2.js';
8
+
9
+ export { _schema1 as schema1 };
10
+ export { _schema2 as schema2 };
11
+
12
+ export default {
13
+ schema1: _schema1,
14
+ schema2: _schema2,
15
+ };
package/secrets/dev.env CHANGED
@@ -1 +1,2 @@
1
- JUST_A_TEST='Dev just a test'
1
+ JUST_A_TEST='Dev just a test'
2
+ AWESOMENESS_ENCRYPTION_KEY='12345678901234567890123456789012'
package/secrets/local.env CHANGED
@@ -1 +1,2 @@
1
- JUST_A_TEST='Local just a test'
1
+ JUST_A_TEST='Local just a test'
2
+ AWESOMENESS_ENCRYPTION_KEY='12345678901234567890123456789012'
@@ -0,0 +1,43 @@
1
+ import thingType from '../thingType.js';
2
+ function cleanArray(arr, schema = {}){
3
+
4
+ if(!Array.isArray(arr)) {
5
+ throw {
6
+ name: 'TypeError',
7
+ message: 'Input must be an array',
8
+ arr
9
+ };
10
+ }
11
+
12
+ validateSchema(schema);
13
+
14
+ const cleanArrayItems = [];
15
+
16
+ const supposedToBeType = schema.type;
17
+
18
+ arr.forEach( item => {
19
+
20
+ const itemType = thingType(item);
21
+
22
+ if(itemType !== supposedToBeType){
23
+
24
+ throw {
25
+ message: 'type invalid',
26
+ itemType,
27
+ supposedToBeType
28
+ };
29
+
30
+ }
31
+
32
+ const cleanFnImported = import(`./${supposedToBeType}.js`);
33
+ const cleanItem = cleanFnImported(value, schema[key]);
34
+
35
+ cleanArrayItems.push(cleanItem);
36
+
37
+ });
38
+
39
+ return cleanArrayItems;
40
+
41
+ }
42
+
43
+ export default cleanArray;
@@ -0,0 +1,22 @@
1
+ export default function cleanBoolean(x, {
2
+ required = false
3
+ } = {}){
4
+
5
+ try {
6
+
7
+ if(x !== true && x !== false){
8
+ throw {
9
+ message: `Invalid boolean value "${x}"`,
10
+ }
11
+ }
12
+
13
+ return x;
14
+
15
+ } catch (e) {
16
+
17
+ if(required) { throw e; } else { return null; }
18
+
19
+ }
20
+
21
+ }
22
+
@@ -0,0 +1,55 @@
1
+ export default function cleanInt(x, {
2
+ required = false,
3
+ min = false,
4
+ max = false,
5
+ } = {}){
6
+
7
+ try {
8
+
9
+ if (typeof x !== 'number' || !Number.isInteger(x)) {
10
+
11
+ // convert string to number if possible
12
+ if (typeof x === 'string' && !isNaN(parseInt(x))) {
13
+ x = parseInt(x);
14
+ } else {
15
+ throw {
16
+ message: 'Input must be an integer',
17
+ value: x
18
+ }
19
+ }
20
+
21
+ }
22
+
23
+ if (min !== false && x < min) {
24
+
25
+ throw {
26
+ message: `Integer must be greater than or equal to ${min}`,
27
+ value: x,
28
+ min,
29
+ max
30
+ }
31
+
32
+ }
33
+
34
+ if (max !== false && x > max) {
35
+
36
+ throw {
37
+ message: `Integer must be less than or equal to ${max}`,
38
+ value: x,
39
+ min,
40
+ max
41
+ }
42
+
43
+ }
44
+
45
+ return x;
46
+
47
+ } catch (e) {
48
+
49
+ if(required) { throw e; } else { return null; }
50
+
51
+ }
52
+
53
+
54
+ }
55
+
@@ -0,0 +1,75 @@
1
+ export default function cleanNumber(x, {
2
+ required = false,
3
+ min = false,
4
+ max = false,
5
+ maxDecimal = false,
6
+ minDecimal = false,
7
+ } = {}){
8
+
9
+ try {
10
+
11
+ if (typeof x !== 'number') {
12
+
13
+ // convert string to number if possible
14
+ if (typeof x === 'string' && !isNaN(parseFloat(x))) {
15
+ x = parseFloat(x);
16
+ } else {
17
+ throw {
18
+ message: 'Input must be a number',
19
+ value: x
20
+ }
21
+ }
22
+
23
+ }
24
+
25
+ if (min !== false && x < min) {
26
+
27
+ throw {
28
+ message: `Integer must be greater than or equal to ${min}`,
29
+ value: x,
30
+ min,
31
+ max
32
+ }
33
+
34
+ }
35
+
36
+ if (max !== false && x > max) {
37
+
38
+ throw {
39
+ message: `Integer must be less than or equal to ${max}`,
40
+ value: x,
41
+ min,
42
+ max
43
+ }
44
+
45
+ }
46
+
47
+ if (maxDecimal !== false && x.toString().split('.')[1]?.length > maxDecimal) {
48
+
49
+ throw {
50
+ message: `Number must have at most ${maxDecimal} decimal places`,
51
+ value: x,
52
+ maxDecimal
53
+ }
54
+
55
+ }
56
+
57
+ if (minDecimal !== false && x.toString().split('.')[1]?.length < minDecimal) {
58
+
59
+ throw {
60
+ message: `Number must have at least ${minDecimal} decimal places`,
61
+ value: x,
62
+ minDecimal
63
+ }
64
+
65
+ }
66
+
67
+ return x;
68
+
69
+ } catch (e) {
70
+
71
+ if(required) { throw e; } else { return null; }
72
+
73
+ }
74
+ }
75
+
@@ -0,0 +1,67 @@
1
+ import each from '../each.js';
2
+ import validateSchema from '../validateSchema.js';
3
+ import thingType from '../thingType.js';
4
+
5
+ function cleanObject(obj, schema = {}){
6
+
7
+ try {
8
+
9
+ validateSchema(schema);
10
+
11
+ if(typeof obj !== 'object' || obj === null){
12
+ throw {
13
+ message: 'Input must be an object',
14
+ obj
15
+ };
16
+ }
17
+
18
+ let keysPassed = Object.keys(obj);
19
+ let keysSchema = Object.keys(schema.properties);
20
+
21
+ const origLength = keysPassed.length;
22
+ keysPassed = keysPassed.filter(key => keysSchema.includes(key));
23
+
24
+ if(origLength !== keysPassed.length){
25
+ throw {
26
+ name: 'KeyError',
27
+ message: 'Object contains keys not in schema',
28
+ keysPassed,
29
+ keysSchema
30
+ };
31
+ }
32
+
33
+ const cleanObj = {};
34
+
35
+ // Iterate over the schema keys
36
+ each(obj, (value, key) => {
37
+
38
+ const valType = thingType(value);
39
+ const supposedToBeType = schema[key].type;
40
+
41
+ if(valType !== supposedToBeType){
42
+
43
+ throw {
44
+ message: 'type invalid',
45
+ valType,
46
+ supposedToBeType
47
+ };
48
+
49
+ }
50
+
51
+ const cleanFnImported = import(`./${supposedToBeType}.js`);
52
+ cleanObj[key] = cleanFnImported(value, schema[key]);
53
+
54
+ });
55
+
56
+ return cleanObj;
57
+
58
+ } catch(e){
59
+
60
+ if(required) { throw e; } else { return null; }
61
+
62
+ }
63
+
64
+ }
65
+
66
+ export default cleanObject;
67
+