@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
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,6 +4,14 @@
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_timestamp from './src/clean/timestamp.js';
14
+ import _clean_uuid from './src/clean/uuid.js';
7
15
  import _combineFiles from './src/combineFiles.js';
8
16
  import _convertBytes from './src/convertBytes.js';
9
17
  import _decrypt from './src/decrypt.js';
@@ -18,9 +26,11 @@ import _md5 from './src/md5.js';
18
26
  import _password_check from './src/password/check.js';
19
27
  import _password_hash from './src/password/hash.js';
20
28
  import _setLocalEnvs from './src/setLocalEnvs.js';
29
+ import _thingType from './src/thingType.js';
21
30
  import _toPennies from './src/toPennies.js';
22
31
  import _utils_buildExportsTree from './src/utils/buildExportsTree.js';
23
32
  import _utils_buildFileDataList from './src/utils/buildFileDataList.js';
33
+ import _utils_clean from './src/utils/clean.js';
24
34
  import _utils_extractJSDocComment from './src/utils/extractJSDocComment.js';
25
35
  import _utils_generateFile from './src/utils/generateFile.js';
26
36
  import _utils_generateFlatExportLines from './src/utils/generateFlatExportLines.js';
@@ -30,6 +40,7 @@ import _utils_generateNamespaceCode from './src/utils/generateNamespaceCode.js';
30
40
  import _utils_generateNamespaceExportLines from './src/utils/generateNamespaceExportLines.js';
31
41
  import _utils_shouldIgnore from './src/utils/shouldIgnore.js';
32
42
  import _uuid from './src/uuid.js';
43
+ import _validateSchema from './src/validateSchema.js';
33
44
 
34
45
  export { _build as build };
35
46
  export { _combineFiles as combineFiles };
@@ -43,8 +54,10 @@ export { _ignoreMe as ignoreMe };
43
54
  export { _isUUID as isUUID };
44
55
  export { _md5 as md5 };
45
56
  export { _setLocalEnvs as setLocalEnvs };
57
+ export { _thingType as thingType };
46
58
  export { _toPennies as toPennies };
47
59
  export { _uuid as uuid };
60
+ export { _validateSchema as validateSchema };
48
61
 
49
62
  export default {
50
63
  /**
@@ -70,6 +83,15 @@ export default {
70
83
  */
71
84
  convertBytes: _convertBytes,
72
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
+ */
73
95
  each: _each,
74
96
  eachAsync: _eachAsync,
75
97
  encrypt: _encrypt,
@@ -78,8 +100,20 @@ export default {
78
100
  isUUID: _isUUID,
79
101
  md5: _md5,
80
102
  setLocalEnvs: _setLocalEnvs,
103
+ thingType: _thingType,
81
104
  toPennies: _toPennies,
82
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
+ timestamp: _clean_timestamp,
115
+ uuid: _clean_uuid,
116
+ },
83
117
  ignoreFolder: {
84
118
  ignoreMe: _ignoreFolder_ignoreMe,
85
119
  },
@@ -90,6 +124,7 @@ export default {
90
124
  utils: {
91
125
  buildExportsTree: _utils_buildExportsTree,
92
126
  buildFileDataList: _utils_buildFileDataList,
127
+ clean: _utils_clean,
93
128
  extractJSDocComment: _utils_extractJSDocComment,
94
129
  generateFile: _utils_generateFile,
95
130
  generateFlatExportLines: _utils_generateFlatExportLines,
package/package.json CHANGED
@@ -1,23 +1,26 @@
1
1
  {
2
2
  "name": "@awesomeness-js/utils",
3
- "version": "1.0.19",
3
+ "version": "1.0.21",
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
+ };
@@ -0,0 +1,2 @@
1
+ import clean from '../utils/clean.js';
2
+ export default clean.array;
@@ -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,74 @@
1
+ export default function cleanInt(x, {
2
+ required = false,
3
+ convertString = true,
4
+ min = false,
5
+ max = false,
6
+ } = {}){
7
+
8
+ try {
9
+
10
+ if (typeof x !== 'number' || !Number.isInteger(x)) {
11
+
12
+ // convert string to number if possible
13
+ // but not a decimal string
14
+ if (convertString && typeof x === 'string') {
15
+
16
+ if(x.includes('.')){
17
+ throw {
18
+ message: 'string contains decimal',
19
+ value: x
20
+ };
21
+ }
22
+
23
+ if(!isNaN(parseInt(x))){
24
+ x = parseInt(x);
25
+ } else {
26
+ throw {
27
+ message: 'string cannot be parsed to integer',
28
+ value: x
29
+ }
30
+ }
31
+
32
+
33
+ } else {
34
+ throw {
35
+ message: 'Input must be an integer',
36
+ value: x
37
+ }
38
+ }
39
+
40
+ }
41
+
42
+ if (min !== false && x < min) {
43
+
44
+ throw {
45
+ message: `Integer must be greater than or equal to ${min}`,
46
+ value: x,
47
+ min,
48
+ max
49
+ }
50
+
51
+ }
52
+
53
+ if (max !== false && x > max) {
54
+
55
+ throw {
56
+ message: `Integer must be less than or equal to ${max}`,
57
+ value: x,
58
+ min,
59
+ max
60
+ }
61
+
62
+ }
63
+
64
+ return x;
65
+
66
+ } catch (e) {
67
+
68
+ if(required) { throw e; } else { return null; }
69
+
70
+ }
71
+
72
+
73
+ }
74
+
@@ -0,0 +1,93 @@
1
+ export default function cleanNumber(x, {
2
+ required = false,
3
+ convertString = true,
4
+ min = false,
5
+ max = false,
6
+ maxDecimal = false,
7
+ minDecimal = false,
8
+ } = {}){
9
+
10
+ try {
11
+
12
+ if (typeof x !== 'number') {
13
+
14
+ // convert string to number if possible
15
+ if (convertString && typeof x === 'string') {
16
+
17
+ try {
18
+ x = parseFloat(x);
19
+ } catch(e){
20
+ throw {
21
+ message: 'Input cannot be parsed',
22
+ value: x
23
+ }
24
+ }
25
+
26
+
27
+ if(isNaN(x)){
28
+ throw {
29
+ message: 'Input cannot be parsed NaN',
30
+ value: x
31
+ }
32
+ }
33
+
34
+ } else {
35
+ throw {
36
+ message: 'Input must be a number',
37
+ value: x
38
+ }
39
+ }
40
+
41
+ }
42
+
43
+ if (min !== false && x < min) {
44
+
45
+ throw {
46
+ message: `Integer must be greater than or equal to ${min}`,
47
+ value: x,
48
+ min,
49
+ max
50
+ }
51
+
52
+ }
53
+
54
+ if (max !== false && x > max) {
55
+
56
+ throw {
57
+ message: `Integer must be less than or equal to ${max}`,
58
+ value: x,
59
+ min,
60
+ max
61
+ }
62
+
63
+ }
64
+
65
+ if (maxDecimal !== false && x.toString().split('.')[1]?.length > maxDecimal) {
66
+
67
+ throw {
68
+ message: `Number must have at most ${maxDecimal} decimal places`,
69
+ value: x,
70
+ maxDecimal
71
+ }
72
+
73
+ }
74
+
75
+ if (minDecimal !== false && x.toString().split('.')[1]?.length < minDecimal) {
76
+
77
+ throw {
78
+ message: `Number must have at least ${minDecimal} decimal places`,
79
+ value: x,
80
+ minDecimal
81
+ }
82
+
83
+ }
84
+
85
+ return x;
86
+
87
+ } catch (e) {
88
+
89
+ if(required) { throw e; } else { return null; }
90
+
91
+ }
92
+ }
93
+
@@ -0,0 +1,3 @@
1
+ import clean from '../utils/clean.js';
2
+ export default clean.object;
3
+
@@ -0,0 +1,60 @@
1
+ export default function cleanString(x, {
2
+ required = false,
3
+ minLength = false,
4
+ maxLength = false,
5
+ allowHtml = false,
6
+ allowScripts = false
7
+ } = {}){
8
+
9
+ if(allowScripts && !allowHtml){ allowHtml = true; }
10
+
11
+ try {
12
+
13
+ if(typeof x !== 'string') {
14
+ throw {
15
+ name: 'TypeError',
16
+ message: 'Input must be a string',
17
+ value: x
18
+ };
19
+ }
20
+
21
+ if(minLength !== false && x.length < minLength) {
22
+ throw {
23
+ message: `String length must be between ${minLength} `,
24
+ length: x.length,
25
+ value: x
26
+ };
27
+ }
28
+
29
+ if(maxLength !== false && x.length > maxLength) {
30
+ throw {
31
+ message: `String length must be less than or equal to ${maxLength}`,
32
+ length: x.length,
33
+ value: x
34
+ };
35
+ }
36
+
37
+ if(!allowHtml && /<[^>]*>/g.test(x)) {
38
+ throw {
39
+ message: 'HTML tags are not allowed',
40
+ value: x
41
+ };
42
+ }
43
+
44
+ if(!allowScripts && /<script[^>]*>.*<\/script>/g.test(x)) {
45
+ throw {
46
+ message: 'Script tags are not allowed',
47
+ value: x
48
+ };
49
+ }
50
+
51
+ return x;
52
+
53
+ } catch (e) {
54
+
55
+ if(required) { throw e; } else { return null; }
56
+
57
+ }
58
+
59
+ }
60
+