@awesomeness-js/utils 1.0.24 → 1.1.2

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 (90) hide show
  1. package/README.md +112 -86
  2. package/build/build.js +16 -11
  3. package/build/postBuild.js +8 -8
  4. package/eslint.config.js +90 -0
  5. package/index.js +108 -78
  6. package/package.json +26 -25
  7. package/schemas/schema1.js +5 -1
  8. package/schemas/schema2.js +5 -1
  9. package/schemas.js +3 -2
  10. package/src/build.js +88 -8
  11. package/src/clean/boolean.js +27 -17
  12. package/src/clean/integer.js +85 -69
  13. package/src/clean/number.js +103 -86
  14. package/src/clean/string.js +91 -67
  15. package/src/clean/timestamp.js +61 -45
  16. package/src/clean/uuid.js +38 -25
  17. package/src/collectImports.js +195 -0
  18. package/src/combineFiles.js +45 -36
  19. package/src/convertBytes.js +12 -7
  20. package/src/decrypt.js +17 -9
  21. package/src/each.js +26 -4
  22. package/src/eachAsync.js +35 -19
  23. package/src/encrypt.js +22 -17
  24. package/src/getAllFiles.js +56 -42
  25. package/src/ignoreFolder/ignoreMe.js +6 -2
  26. package/src/ignoreMe.js +4 -2
  27. package/src/isUUID.js +4 -0
  28. package/src/md5.js +5 -1
  29. package/src/password/check.js +7 -3
  30. package/src/password/hash.js +12 -7
  31. package/src/setLocalEnvs.js +16 -3
  32. package/src/shouldIgnore.js +99 -0
  33. package/src/thingType.js +62 -24
  34. package/src/toPennies.js +23 -5
  35. package/src/utils/buildExportsTree.js +45 -17
  36. package/src/utils/buildFileDataList.js +37 -18
  37. package/src/utils/clean.js +205 -120
  38. package/src/utils/extractJSDocComment.js +14 -7
  39. package/src/utils/generateFile.js +54 -18
  40. package/src/utils/generateFlatExportLines.js +40 -17
  41. package/src/utils/generateImportStatements.js +20 -8
  42. package/src/utils/generateNamedExports.js +55 -10
  43. package/src/utils/generateNamespaceCode.js +56 -24
  44. package/src/utils/generateNamespaceExportLines.js +27 -7
  45. package/src/utils/writeHotWrapper.js +42 -0
  46. package/src/uuid.js +9 -7
  47. package/src/validateSchema.js +95 -77
  48. package/test/collectImports.js +8 -0
  49. package/test/css/some.css +3 -0
  50. package/test/css/some.js +5 -0
  51. package/test/ignoreFolder/ignoreMe.js +3 -1
  52. package/test/ignoreFolder/ignoreMe2.js +3 -1
  53. package/test/ignoreFolder2/ignoreMe.js +6 -2
  54. package/test/js/abc.test.js +7 -3
  55. package/test/js/some.js +5 -0
  56. package/test/secret.test.js +1 -1
  57. package/tests/clean/array.test.js +122 -74
  58. package/tests/clean/boolean.test.js +18 -6
  59. package/tests/clean/integer.test.js +25 -9
  60. package/tests/clean/number.test.js +49 -13
  61. package/tests/clean/object.test.js +190 -119
  62. package/tests/clean/string.test.js +48 -17
  63. package/tests/clean/timestamp.test.js +12 -5
  64. package/tests/clean/uuid.test.js +13 -6
  65. package/tests/collectImports.test.js +66 -0
  66. package/tests/combineFiles.test.js +28 -26
  67. package/tests/convertBytes.test.js +8 -3
  68. package/tests/env.test.js +9 -3
  69. package/tests/example.test.js +7 -3
  70. package/tests/fileList.test.js +16 -12
  71. package/tests/hash-and-encrypt.test.js +13 -4
  72. package/tests/ignore.test.js +55 -0
  73. package/tests/md5.test.js +7 -2
  74. package/tests/namedExports.test.js +13 -0
  75. package/tests/uuid.test.js +10 -4
  76. package/tests/validateSchema.test.js +21 -9
  77. package/tsconfig.json +20 -16
  78. package/types/build.d.ts +5 -1
  79. package/types/collectImports.d.ts +6 -0
  80. package/types/index.d.ts +108 -78
  81. package/types/shouldIgnore.d.ts +1 -0
  82. package/types/utils/buildFileDataList.d.ts +5 -1
  83. package/types/utils/generateFile.d.ts +8 -1
  84. package/types/utils/generateFlatExportLines.d.ts +7 -1
  85. package/types/utils/generateImportStatements.d.ts +5 -1
  86. package/types/utils/generateNamedExports.d.ts +7 -1
  87. package/types/utils/generateNamespaceCode.d.ts +7 -1
  88. package/types/utils/generateNamespaceExportLines.d.ts +6 -1
  89. package/types/utils/writeHotWrapper.d.ts +4 -0
  90. package/src/utils/shouldIgnore.js +0 -43
@@ -1,9 +1,21 @@
1
- export default function generateImportStatements(fileDataList, dts, src) {
2
- let statements = '';
3
- fileDataList.forEach(({ importVarName, importPath }) => {
4
- statements += dts
5
- ? `import type ${importVarName} from '.${importPath.replace(src,'')}';\n`
6
- : `import ${importVarName} from '${importPath}.js';\n`;
7
- });
8
- return statements;
1
+ export default function generateImportStatements({
2
+ fileDataList,
3
+ dts,
4
+ src
5
+ }) {
6
+
7
+ let statements = '';
8
+
9
+ fileDataList.forEach(({
10
+ importVarName, importPath
11
+ }) => {
12
+
13
+ statements += dts
14
+ ? `import type ${importVarName} from '.${importPath.replace(src,'')}';\n`
15
+ : `import ${importVarName} from '${importPath}.js';\n`;
16
+
17
+ });
18
+
19
+ return statements;
20
+
9
21
  }
@@ -1,11 +1,56 @@
1
- export default function generateNamedExports(flatExports, exportRoots, dts) {
2
- let lines = '';
3
- if (exportRoots) {
4
- flatExports.forEach(({ functionName, importVarName }) => {
5
- lines += dts
6
- ? `export declare const ${functionName}: typeof ${importVarName};\n`
7
- : `export { ${importVarName} as ${functionName} };\n`;
8
- });
9
- }
10
- return lines;
1
+ export default function generateNamedExports({
2
+ flatExports,
3
+ exportRoots,
4
+ nestedExports,
5
+ dts,
6
+ useTabs = true
7
+ }) {
8
+
9
+ const indentStyle = useTabs ? '\t' : ' ';
10
+ let lines = '';
11
+
12
+ if (!exportRoots) return lines;
13
+
14
+ // existing flat exports
15
+ flatExports.forEach(({
16
+ functionName, importVarName
17
+ }) => {
18
+
19
+ lines += dts
20
+ ? `export declare const ${functionName}: typeof ${importVarName};\n`
21
+ : `export { ${importVarName} as ${functionName} };\n`;
22
+
23
+ });
24
+
25
+ // grouped namespaces as named exports
26
+ if (nestedExports) {
27
+
28
+ lines += '\n';
29
+
30
+ for (const [ groupName, members ] of Object.entries(nestedExports)) {
31
+
32
+ if (dts) {
33
+
34
+ const fields = Object.entries(members)
35
+ .map(([ k, v ]) => `${indentStyle}${k}: typeof ${v.importVarName};`)
36
+ .join('\n');
37
+
38
+ lines += `export declare const ${groupName}: {\n${fields}\n};\n\n`;
39
+
40
+ } else {
41
+
42
+ const fields = Object.entries(members)
43
+ .map(([ k, v ]) => `${indentStyle}${k}: ${v.importVarName}`)
44
+ .join(',\n');
45
+
46
+ lines += `export const ${groupName} = {\n${fields}\n};\n\n`;
47
+
48
+ }
49
+
50
+ }
51
+
52
+ }
53
+
54
+ return lines;
55
+
11
56
  }
@@ -1,25 +1,57 @@
1
- export default function generateNamespaceCode(nsObj, indentLevel, includeComments, dts) {
2
- const indent = ' '.repeat(indentLevel);
3
- let lines = ['{'];
4
- for (const key in nsObj) {
5
- const value = nsObj[key];
6
- if (typeof value === 'object' && value.hasOwnProperty('importVarName')) {
7
- if (includeComments && value.jsDocComment) {
8
- const indentedComment = value.jsDocComment
9
- .split('\n')
10
- .map(line => indent + ' ' + line)
11
- .join('\n');
12
- lines.push(indentedComment);
13
- }
14
- lines.push(dts
15
- ? `${indent} ${key}: typeof ${value.importVarName},`
16
- : `${indent} ${key}: ${value.importVarName},`
17
- );
18
- } else {
19
- const nestedCode = generateNamespaceCode(value, indentLevel + 1, includeComments, dts);
20
- lines.push(`${indent} ${key}: ${nestedCode},`);
21
- }
22
- }
23
- lines.push(indent + '}');
24
- return lines.join('\n');
1
+ export default function generateNamespaceCode({
2
+ nsObj,
3
+ indentLevel,
4
+ includeComments,
5
+ dts,
6
+ useTabs = true
7
+ }) {
8
+
9
+ let indentStyle = useTabs ? '\t' : ' ';
10
+
11
+ const indent = indentStyle.repeat(indentLevel);
12
+ let lines = [ '{' ];
13
+
14
+
15
+ for (const key in nsObj) {
16
+
17
+ const value = nsObj[key];
18
+
19
+
20
+ if (typeof value === 'object' && value.hasOwnProperty('importVarName')) {
21
+
22
+ if (includeComments && value.jsDocComment) {
23
+
24
+ const indentedComment = value.jsDocComment
25
+ .split('\n')
26
+ .map((line) => indent + indentStyle + line)
27
+ .join('\n');
28
+
29
+ lines.push(indentedComment);
30
+
31
+ }
32
+
33
+ lines.push(dts
34
+ ? `${indent}${indentStyle}${key}: typeof ${value.importVarName},`
35
+ : `${indent}${indentStyle}${key}: ${value.importVarName},`
36
+ );
37
+
38
+ } else {
39
+
40
+ const nestedCode = generateNamespaceCode({
41
+ nsObj: value,
42
+ indentLevel: indentLevel + 1,
43
+ includeComments,
44
+ dts
45
+ });
46
+
47
+ lines.push(`${indent}${indentStyle}${key}: ${nestedCode},`);
48
+
49
+ }
50
+
51
+ }
52
+
53
+ lines.push(indent + '}');
54
+
55
+ return lines.join('\n');
56
+
25
57
  }
@@ -1,10 +1,30 @@
1
1
  import generateNamespaceCode from './generateNamespaceCode.js';
2
2
 
3
- export default function generateNamespaceExportLines(nestedExports, includeComments, dts) {
4
- let lines = '';
5
- for (const ns in nestedExports) {
6
- const nsCode = generateNamespaceCode(nestedExports[ns], 1, includeComments, dts);
7
- lines += ` ${ns}: ${nsCode},\n`;
8
- }
9
- return lines;
3
+ export default function generateNamespaceExportLines({
4
+ nestedExports,
5
+ includeComments,
6
+ dts,
7
+ useTabs = true
8
+ }) {
9
+
10
+ const indentStyle = useTabs ? '\t' : ' ';
11
+
12
+ let lines = '';
13
+
14
+ for (const ns in nestedExports) {
15
+
16
+ const nsCode = generateNamespaceCode({
17
+ nsObj: nestedExports[ns],
18
+ indentLevel: 1,
19
+ includeComments,
20
+ dts,
21
+ useTabs
22
+ });
23
+
24
+ lines += `${indentStyle}${ns}: ${nsCode},\n`;
25
+
26
+ }
27
+
28
+ return lines;
29
+
10
30
  }
@@ -0,0 +1,42 @@
1
+ import { writeFileSync } from 'node:fs';
2
+
3
+ export default function writeHotWrapper({
4
+ dest,
5
+ hotSource
6
+ }) {
7
+
8
+ const wrapper = `// auto-generated wrapper
9
+ import chokidar from 'chokidar';
10
+ import path from 'node:path';
11
+ import { pathToFileURL } from 'node:url';
12
+
13
+ let _default;
14
+
15
+ export { _default as default };
16
+
17
+ const target = path.resolve(${JSON.stringify(hotSource)});
18
+
19
+ async function reload() {
20
+
21
+ const mod = await import(\`\${pathToFileURL(target)}?t=\${Date.now()}\`);
22
+
23
+ _default = mod.default;
24
+ console.log('[hot] reloaded', target);
25
+
26
+ }
27
+
28
+ await reload();
29
+
30
+ let t;
31
+
32
+ chokidar.watch(target, { ignoreInitial: true }).on('all', () => {
33
+
34
+ clearTimeout(t);
35
+ t = setTimeout(() => reload().catch(err => console.error('[hot] failed:', err)), 50);
36
+
37
+ });
38
+ `;
39
+
40
+ writeFileSync(dest, wrapper);
41
+
42
+ }
package/src/uuid.js CHANGED
@@ -2,15 +2,17 @@ import crypto from 'crypto';
2
2
 
3
3
  // V4
4
4
  function uuid() {
5
- const arr = crypto.randomBytes(16);
6
5
 
7
- // Set the UUID version and clock sequence bits as per RFC4122
8
- arr[6] = (arr[6] & 0x0f) | 0x40;
9
- arr[8] = (arr[8] & 0x3f) | 0x80;
6
+ const arr = crypto.randomBytes(16);
7
+
8
+ // Set the UUID version and clock sequence bits as per RFC4122
9
+ arr[6] = (arr[6] & 0x0f) | 0x40;
10
+ arr[8] = (arr[8] & 0x3f) | 0x80;
11
+
12
+ return [ ...arr ].map((b, i) =>
13
+ [ 4, 6, 8, 10 ].includes(i) ? '-' + b.toString(16).padStart(2, '0') : b.toString(16).padStart(2, '0')
14
+ ).join('');
10
15
 
11
- return [...arr].map((b, i) =>
12
- [4, 6, 8, 10].includes(i) ? '-' + b.toString(16).padStart(2, '0') : b.toString(16).padStart(2, '0')
13
- ).join('');
14
16
  }
15
17
 
16
18
  export default uuid;
@@ -2,83 +2,101 @@ import each from './each.js';
2
2
 
3
3
  function validateSchema(schema){
4
4
 
5
- if(!schema){
6
- throw {
7
- message: 'Schema is required.',
8
- schema
9
- };
10
- }
11
-
12
- // ref todo
13
- if(schema.ref){ return true; }
14
-
15
- let schemaType = typeof schema;
16
-
17
- if(schemaType !== 'object' || schema === null) {
18
- throw {
19
- message: 'Schema Invalid - Input must be an object',
20
- schema,
21
- schemaType
22
- };
23
- }
24
-
25
- // object properties dont have type
26
- if(!schema.type && schema.properties){ schema.type = 'object'; }
27
-
28
- const validTypes = [
29
- 'array',
30
- 'boolean',
31
- 'integer',
32
- 'number',
33
- 'object',
34
- 'string',
35
- 'timestamp',
36
- 'uuid'
37
- ];
38
-
39
- if(!schema.type || !validTypes.includes(schema.type)) {
40
- throw {
41
- message: `Schema must have a valid type.`,
42
- validTypes,
43
- schema
44
- };
45
- }
46
-
47
-
48
- if(schema.type === 'object') {
49
-
50
- if(schema.properties){
51
-
52
- // run through each property
53
- each(schema.properties, (v,k) => {
54
- validateSchema(v);
55
- });
56
-
57
- } else {
58
-
59
- throw {
60
- message: 'Object invalid - needs "properties"',
61
- schema
62
- }
63
-
64
- }
65
-
66
- }
67
-
68
- if(schema.type === 'array') {
69
-
70
- if(!schema.items){
71
- throw {
72
- message: 'Array schema must have items defined.',
73
- schema
74
- };
75
- }
76
-
77
- validateSchema(schema.items);
78
-
79
- }
80
-
81
- return true;
5
+ if(!schema){
6
+
7
+ throw {
8
+ message: 'Schema is required.',
9
+ schema
10
+ };
11
+
12
+ }
13
+
14
+ // ref todo
15
+ if(schema.ref){
16
+
17
+ return true;
18
+
19
+ }
20
+
21
+ let schemaType = typeof schema;
22
+
23
+ if(schemaType !== 'object' || schema === null) {
24
+
25
+ throw {
26
+ message: 'Schema Invalid - Input must be an object',
27
+ schema,
28
+ schemaType
29
+ };
30
+
31
+ }
32
+
33
+ // object properties dont have type
34
+ if(!schema.type && schema.properties){
35
+
36
+ schema.type = 'object';
37
+
38
+ }
39
+
40
+ const validTypes = [
41
+ 'array',
42
+ 'boolean',
43
+ 'integer',
44
+ 'number',
45
+ 'object',
46
+ 'string',
47
+ 'timestamp',
48
+ 'uuid'
49
+ ];
50
+
51
+ if(!schema.type || !validTypes.includes(schema.type)) {
52
+
53
+ throw {
54
+ message: `Schema must have a valid type.`,
55
+ validTypes,
56
+ schema
57
+ };
58
+
59
+ }
60
+
61
+
62
+ if(schema.type === 'object') {
63
+
64
+ if(schema.properties){
65
+
66
+ // run through each property
67
+ each(schema.properties, (v,k) => {
68
+
69
+ validateSchema(v);
70
+
71
+ });
72
+
73
+ } else {
74
+
75
+ throw {
76
+ message: 'Object invalid - needs "properties"',
77
+ schema
78
+ };
79
+
80
+ }
81
+
82
+ }
83
+
84
+ if(schema.type === 'array') {
85
+
86
+ if(!schema.items){
87
+
88
+ throw {
89
+ message: 'Array schema must have items defined.',
90
+ schema
91
+ };
92
+
93
+ }
94
+
95
+ validateSchema(schema.items);
96
+
97
+ }
98
+
99
+ return true;
82
100
 
83
101
  }
84
102
 
@@ -0,0 +1,8 @@
1
+ import './css/some.css';
2
+ import './js/some.js';
3
+
4
+ export default () => {
5
+
6
+ console.log('collectImports.js');
7
+
8
+ };
package/test/css/some.css CHANGED
@@ -0,0 +1,3 @@
1
+ .justATest {
2
+ background-color: aliceblue;
3
+ }
package/test/css/some.js CHANGED
@@ -0,0 +1,5 @@
1
+ export default () => {
2
+
3
+ console.log('some.js');
4
+
5
+ };
@@ -1,3 +1,5 @@
1
1
  export default () => {
2
- console.log('I should not be in the build!');
2
+
3
+ console.log('I should not be in the build!');
4
+
3
5
  };
@@ -1,3 +1,5 @@
1
1
  export default async () => {
2
- console.log('I should not be in the build!');
2
+
3
+ console.log('I should not be in the build!');
4
+
3
5
  };
@@ -1,3 +1,7 @@
1
1
  export default function ignoreMeTest() {
2
- console.log('I should not be in the build!');
3
- };
2
+
3
+ console.log('I should not be in the build!');
4
+
5
+ }
6
+
7
+ ;
@@ -1,6 +1,10 @@
1
1
  // example.test.js
2
- import { expect, test } from 'vitest'
2
+ import {
3
+ expect, test
4
+ } from 'vitest';
3
5
 
4
6
  test('adds 2 + 3 to equal 5', () => {
5
- expect(2 + 3).toBe(5)
6
- })
7
+
8
+ expect(2 + 3).toBe(5);
9
+
10
+ });
package/test/js/some.js CHANGED
@@ -0,0 +1,5 @@
1
+ export default () => {
2
+
3
+ console.log('some.js');
4
+
5
+ };
@@ -3,5 +3,5 @@
3
3
  import { expect, test } from 'vitest'
4
4
 
5
5
  test('adds 2 + 3 to equal 5', () => {
6
- expect(2 + 3).toBe(5)
6
+ expect(2 + 3).toBe(5)
7
7
  })