@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,72 +1,96 @@
1
1
  export default function cleanString(x, {
2
- required = false,
3
- minLength = false,
4
- maxLength = false,
5
- allowHtml = false,
6
- allowScripts = false,
7
- validValues = false
2
+ required = false,
3
+ minLength = false,
4
+ maxLength = false,
5
+ allowHtml = false,
6
+ allowScripts = false,
7
+ validValues = false
8
8
  } = {}){
9
9
 
10
- if(allowScripts && !allowHtml){ allowHtml = true; }
11
-
12
- try {
13
-
14
- if(typeof x !== 'string') {
15
- throw {
16
- name: 'TypeError',
17
- message: 'Input must be a string',
18
- value: x
19
- };
20
- }
21
-
22
- if(minLength !== false && x.length < minLength) {
23
- throw {
24
- message: `String length must be between ${minLength} `,
25
- length: x.length,
26
- value: x
27
- };
28
- }
29
-
30
- if(maxLength !== false && x.length > maxLength) {
31
- throw {
32
- message: `String length must be less than or equal to ${maxLength}`,
33
- length: x.length,
34
- value: x
35
- };
36
- }
37
-
38
- if(!allowHtml && /<[^>]*>/g.test(x)) {
39
- throw {
40
- message: 'HTML tags are not allowed',
41
- value: x
42
- };
43
- }
44
-
45
- if(!allowScripts && /<script[^>]*>.*<\/script>/g.test(x)) {
46
- throw {
47
- message: 'Script tags are not allowed',
48
- value: x
49
- };
50
- }
51
-
52
- if(
53
- validValues
54
- && Array.isArray(validValues)
55
- && !validValues.includes(x)
56
- ) {
57
- throw {
58
- message: `Value invalid`,
59
- validValues,
60
- value: x
61
- };
62
- }
63
-
64
- return x;
65
-
66
- } catch (e) {
67
-
68
- if(required) { throw e; } else { return null; }
69
-
70
- }
10
+ if(allowScripts && !allowHtml){
11
+
12
+ allowHtml = true;
13
+
14
+ }
15
+
16
+ try {
17
+
18
+ if(typeof x !== 'string') {
19
+
20
+ throw {
21
+ name: 'TypeError',
22
+ message: 'Input must be a string',
23
+ value: x
24
+ };
25
+
26
+ }
27
+
28
+ if(minLength !== false && x.length < minLength) {
29
+
30
+ throw {
31
+ message: `String length must be between ${minLength} `,
32
+ length: x.length,
33
+ value: x
34
+ };
35
+
36
+ }
37
+
38
+ if(maxLength !== false && x.length > maxLength) {
39
+
40
+ throw {
41
+ message: `String length must be less than or equal to ${maxLength}`,
42
+ length: x.length,
43
+ value: x
44
+ };
45
+
46
+ }
47
+
48
+ if(!allowHtml && /<[^>]*>/g.test(x)) {
49
+
50
+ throw {
51
+ message: 'HTML tags are not allowed',
52
+ value: x
53
+ };
54
+
55
+ }
56
+
57
+ if(!allowScripts && /<script[^>]*>.*<\/script>/g.test(x)) {
58
+
59
+ throw {
60
+ message: 'Script tags are not allowed',
61
+ value: x
62
+ };
63
+
64
+ }
65
+
66
+ if(
67
+ validValues
68
+ && Array.isArray(validValues)
69
+ && !validValues.includes(x)
70
+ ) {
71
+
72
+ throw {
73
+ message: `Value invalid`,
74
+ validValues,
75
+ value: x
76
+ };
77
+
78
+ }
79
+
80
+ return x;
81
+
82
+ } catch (e) {
83
+
84
+ if(required) {
85
+
86
+ throw e;
87
+
88
+ } else {
89
+
90
+ return null;
91
+
92
+ }
93
+
94
+ }
71
95
 
72
96
  }
@@ -1,50 +1,66 @@
1
1
  export default function cleanTimestamp( isoDateTimeString , {
2
- required = false,
3
- maxDaysInFuture = false,
4
- maxDaysInFPast = false,
2
+ required = false,
3
+ maxDaysInFuture = false,
4
+ maxDaysInFPast = false,
5
5
  } = {}){
6
-
7
- try {
8
-
9
- if(typeof isoDateTimeString !== 'string') {
10
- throw {
11
- message: 'Input must be a string',
12
- isoDateTimeString
13
- };
14
- }
15
-
16
- const date = new Date(isoDateTimeString);
17
-
18
- if(isNaN(date.getTime())) {
19
- throw {
20
- message: 'Invalid date string',
21
- isoDateTimeString
22
- };
23
- }
24
-
25
- const now = new Date();
26
-
27
- if(maxDaysInFuture !== false && (date - now) > maxDaysInFuture * 24 * 60 * 60 * 1000) {
28
- throw {
29
- message: `Date is more than ${maxDaysInFuture} days in the future`,
30
- isoDateTimeString
31
- };
32
- }
33
-
34
- if(maxDaysInFPast !== false && (now - date) > maxDaysInFPast * 24 * 60 * 60 * 1000) {
35
- throw {
36
- message: `Date is more than ${maxDaysInFPast} days in the past`,
37
- value: isoDateTimeString
38
- };
39
- }
40
-
41
- return date.toISOString();
42
-
43
- } catch (e) {
44
-
45
- if(required) { throw e; } else { return null; }
46
-
47
- }
6
+
7
+ try {
8
+
9
+ if(typeof isoDateTimeString !== 'string') {
10
+
11
+ throw {
12
+ message: 'Input must be a string',
13
+ isoDateTimeString
14
+ };
15
+
16
+ }
17
+
18
+ const date = new Date(isoDateTimeString);
19
+
20
+ if(isNaN(date.getTime())) {
21
+
22
+ throw {
23
+ message: 'Invalid date string',
24
+ isoDateTimeString
25
+ };
26
+
27
+ }
28
+
29
+ const now = new Date();
30
+
31
+ if(maxDaysInFuture !== false && (date - now) > maxDaysInFuture * 24 * 60 * 60 * 1000) {
32
+
33
+ throw {
34
+ message: `Date is more than ${maxDaysInFuture} days in the future`,
35
+ isoDateTimeString
36
+ };
37
+
38
+ }
39
+
40
+ if(maxDaysInFPast !== false && (now - date) > maxDaysInFPast * 24 * 60 * 60 * 1000) {
41
+
42
+ throw {
43
+ message: `Date is more than ${maxDaysInFPast} days in the past`,
44
+ value: isoDateTimeString
45
+ };
46
+
47
+ }
48
+
49
+ return date.toISOString();
50
+
51
+ } catch (e) {
52
+
53
+ if(required) {
54
+
55
+ throw e;
56
+
57
+ } else {
58
+
59
+ return null;
60
+
61
+ }
62
+
63
+ }
48
64
 
49
65
 
50
66
 
package/src/clean/uuid.js CHANGED
@@ -1,31 +1,44 @@
1
1
  import isUUID from "../isUUID.js";
2
+
2
3
  export default function cleanUUID(uuid,{
3
- required = false,
4
+ required = false,
4
5
  } = {}){
5
-
6
- try {
7
-
8
- if(typeof uuid !== 'string'){
9
- throw {
10
- message: 'Input must be a string',
11
- uuid
12
- };
13
- }
14
-
15
- if(!isUUID(uuid)){
16
- throw {
17
- message: 'Invalid UUID format',
18
- uuid
19
- };
20
- }
21
-
22
- return uuid;
23
-
24
- } catch (e) {
25
-
26
- if(required) { throw e; } else { return null; }
27
-
28
- }
6
+
7
+ try {
8
+
9
+ if(typeof uuid !== 'string'){
10
+
11
+ throw {
12
+ message: 'Input must be a string',
13
+ uuid
14
+ };
15
+
16
+ }
17
+
18
+ if(!isUUID(uuid)){
19
+
20
+ throw {
21
+ message: 'Invalid UUID format',
22
+ uuid
23
+ };
24
+
25
+ }
26
+
27
+ return uuid;
28
+
29
+ } catch (e) {
30
+
31
+ if(required) {
32
+
33
+ throw e;
34
+
35
+ } else {
36
+
37
+ return null;
38
+
39
+ }
40
+
41
+ }
29
42
 
30
43
 
31
44
 
@@ -0,0 +1,195 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import md5 from './md5.js';
4
+ import each from './each.js';
5
+
6
+
7
+ function collectImports(filePath, seen = new Set(), collected = []) {
8
+
9
+ const fullPath = path.resolve(filePath);
10
+
11
+ if (seen.has(fullPath)) return;
12
+ seen.add(fullPath);
13
+
14
+ const code = fs.readFileSync(fullPath, 'utf-8');
15
+ const hash = md5(code);
16
+
17
+ collected.push({
18
+ fullPath,
19
+ hash,
20
+ code,
21
+ });
22
+
23
+ const importRegex = /import\s+.*?['"](.+?)['"];?/g;
24
+ let match;
25
+
26
+ while ((match = importRegex.exec(code)) !== null) {
27
+
28
+ const importPath = match[1];
29
+
30
+ // Skip package imports (non-relative paths)
31
+ if (!importPath.startsWith('.')) continue;
32
+
33
+ const resolved = resolveImport(path.dirname(fullPath), importPath);
34
+
35
+ if (resolved) collectImports(resolved, seen, collected);
36
+
37
+ }
38
+
39
+ return collected;
40
+
41
+ }
42
+
43
+ function findProjectRoot(from = process.cwd()) {
44
+
45
+ let currentPath = from;
46
+
47
+ while (true) {
48
+
49
+ if (fs.existsSync(path.join(currentPath, 'package.json'))) {
50
+
51
+ return currentPath;
52
+
53
+ }
54
+
55
+ const parent = path.dirname(currentPath);
56
+
57
+
58
+ if (parent === currentPath) {
59
+
60
+ throw new Error('Project root not found (no package.json)');
61
+
62
+ }
63
+
64
+ currentPath = parent;
65
+
66
+ }
67
+
68
+ }
69
+
70
+
71
+ function resolveImport(baseDir, importPath) {
72
+
73
+ const candidates = [
74
+ importPath,
75
+ `${importPath}.js`,
76
+ `${importPath}.css`,
77
+ path.join(importPath, 'index.js'),
78
+ path.join(importPath, 'index.css'),
79
+ ];
80
+
81
+ for (const candidate of candidates) {
82
+
83
+ const full = path.resolve(baseDir, candidate);
84
+
85
+
86
+ if (fs.existsSync(full) && fs.statSync(full).isFile()) {
87
+
88
+ return full;
89
+
90
+ }
91
+
92
+ }
93
+
94
+ return null;
95
+
96
+ }
97
+
98
+ function jsName( file ){
99
+
100
+ return file.rootPath
101
+ .replace('.'+file.type, '')
102
+ .replaceAll('/', '.');
103
+
104
+ }
105
+
106
+ function cssName( file ){
107
+
108
+ return file.rootPath
109
+ .replace('.'+file.type, '')
110
+ .replaceAll('/', '_');
111
+
112
+ }
113
+
114
+ function run(filePath, {
115
+ returnCode = true,
116
+ jsMaps = {},
117
+ cssMaps = {}
118
+ } = {}){
119
+
120
+ const root = findProjectRoot(__dirname);
121
+
122
+ const imports = collectImports(filePath);
123
+
124
+ imports.forEach((file) => {
125
+
126
+ const rootPath = path.relative(root, file.fullPath).replace(/\\/g, '/');
127
+
128
+ file.rootPath = rootPath;
129
+
130
+ // strip file type
131
+ const fileType = rootPath.split('.').pop();
132
+
133
+ file.type = fileType;
134
+
135
+ if(fileType === 'js'){
136
+
137
+ file.name = jsName(file);
138
+
139
+ if(Object.keys(jsMaps).length){
140
+
141
+ each(jsMaps,( v, k ) => {
142
+
143
+ file.name = file.name.replace(k.replace(/\.\//g, ''),v);
144
+
145
+ });
146
+
147
+ }
148
+
149
+ if(returnCode){
150
+
151
+ file.code = file.code.replace(/import\s+.*?['"](.+?)['"];?/g, '').replace('export default', `${file.name} =`);
152
+
153
+ } else {
154
+
155
+ delete file.code;
156
+
157
+ }
158
+
159
+
160
+ }
161
+
162
+ if(fileType === 'css'){
163
+
164
+ file.name = cssName(file);
165
+
166
+ if(Object.keys(cssMaps).length){
167
+
168
+ each(cssMaps,( v, k ) => {
169
+
170
+ file.name = file.name.replace(k.replace(/\.\//g, ''),v);
171
+
172
+ });
173
+
174
+ }
175
+
176
+ if(returnCode){
177
+
178
+ file.code = file.code.replace(/import\s+.*?['"](.+?)['"];?/g, '');
179
+
180
+ } else {
181
+
182
+ delete file.code;
183
+
184
+ }
185
+
186
+
187
+ }
188
+
189
+ });
190
+
191
+ return imports;
192
+
193
+ }
194
+
195
+ export default run;
@@ -1,66 +1,75 @@
1
1
  // main-module/index.js
2
- import { readdirSync, statSync, readFileSync } from 'fs';
2
+ import {
3
+ readdirSync, statSync, readFileSync
4
+ } from 'fs';
3
5
 
4
6
  function combineFiles(dir, fileType, {
5
7
  minify = false,
6
- processContent = ({ content, path }) => { return content; },
8
+ processContent = ({
9
+ content, path
10
+ }) => {
11
+
12
+ return content;
13
+
14
+ },
7
15
  } = {}) {
8
16
 
9
- var returnString = "";
17
+ var returnString = "";
10
18
 
11
- let stuff = readdirSync(dir);
19
+ let stuff = readdirSync(dir);
12
20
 
13
- // sort it
14
- stuff = stuff.sort();
21
+ // sort it
22
+ stuff = stuff.sort();
15
23
 
16
24
 
17
- stuff.forEach(stuff => {
25
+ stuff.forEach((stuff) => {
18
26
 
19
- const isDir = statSync(`${dir}/${stuff}`).isDirectory();
27
+ const isDir = statSync(`${dir}/${stuff}`).isDirectory();
20
28
 
21
- if(isDir){
29
+ if(isDir){
22
30
 
23
- returnString += combineFiles(`${dir}/${stuff}`, fileType, {
24
- minify,
25
- processContent,
26
- });
31
+ returnString += combineFiles(`${dir}/${stuff}`, fileType, {
32
+ minify,
33
+ processContent,
34
+ });
27
35
 
28
- } else {
36
+ } else {
29
37
 
30
- // is file
38
+ // is file
31
39
 
32
- // get file and extension from stuff
33
- let file = stuff.split('.');
34
- let ext = file[file.length - 1];
40
+ // get file and extension from stuff
41
+ let file = stuff.split('.');
42
+ let ext = file[file.length - 1];
35
43
 
36
44
 
37
- if(ext === fileType){
45
+ if(ext === fileType){
38
46
 
39
- let thisData = readFileSync(`${dir}/${stuff}`, 'utf8');
40
-
41
- if(typeof processContent === 'function'){
42
-
43
- thisData = processContent({
44
- path: `${dir}/${stuff}`,
45
- content: thisData,
46
- });
47
+ let thisData = readFileSync(`${dir}/${stuff}`, 'utf8');
47
48
 
48
- }
49
+ if(typeof processContent === 'function'){
49
50
 
50
- if(minify){
51
- // todo
52
- }
51
+ thisData = processContent({
52
+ path: `${dir}/${stuff}`,
53
+ content: thisData,
54
+ });
53
55
 
54
- returnString += thisData + " \n ";
56
+ }
55
57
 
58
+ if(minify){
59
+ // todo
56
60
  }
57
61
 
62
+ returnString += thisData + " \n ";
63
+
58
64
  }
59
65
 
60
- });
66
+ }
67
+
68
+ });
69
+
70
+ return returnString;
61
71
 
62
- return returnString;
72
+ }
63
73
 
64
- };
65
74
 
66
- export default combineFiles;
75
+ export default combineFiles;
@@ -7,10 +7,15 @@
7
7
  * @returns {string} The converted bytes in a string format with appropriate units.
8
8
  */
9
9
  export default function convertBytes(bytes, precision = 2) {
10
- const units = ['B', 'KB', 'MB', 'GB', 'TB'];
11
- bytes = Math.max(bytes, 0);
12
- const pow = Math.floor((bytes ? Math.log(bytes) : 0) / Math.log(1024));
13
- const index = Math.min(pow, units.length - 1);
14
- bytes /= 2 ** (10 * pow);
15
- return `${bytes.toFixed(precision)} ${units[index]}`;
16
- };
10
+
11
+ const units = [ 'B', 'KB', 'MB', 'GB', 'TB' ];
12
+
13
+ bytes = Math.max(bytes, 0);
14
+ const pow = Math.floor((bytes ? Math.log(bytes) : 0) / Math.log(1024));
15
+ const index = Math.min(pow, units.length - 1);
16
+
17
+ bytes /= 2 ** (10 * pow);
18
+
19
+ return `${bytes.toFixed(precision)} ${units[index]}`;
20
+
21
+ }