@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.
- package/README.md +112 -86
- package/build/build.js +16 -11
- package/build/postBuild.js +8 -8
- package/eslint.config.js +90 -0
- package/index.js +108 -78
- package/package.json +26 -25
- package/schemas/schema1.js +5 -1
- package/schemas/schema2.js +5 -1
- package/schemas.js +3 -2
- package/src/build.js +88 -8
- package/src/clean/boolean.js +27 -17
- package/src/clean/integer.js +85 -69
- package/src/clean/number.js +103 -86
- package/src/clean/string.js +91 -67
- package/src/clean/timestamp.js +61 -45
- package/src/clean/uuid.js +38 -25
- package/src/collectImports.js +195 -0
- package/src/combineFiles.js +45 -36
- package/src/convertBytes.js +12 -7
- package/src/decrypt.js +17 -9
- package/src/each.js +26 -4
- package/src/eachAsync.js +35 -19
- package/src/encrypt.js +22 -17
- package/src/getAllFiles.js +56 -42
- package/src/ignoreFolder/ignoreMe.js +6 -2
- package/src/ignoreMe.js +4 -2
- package/src/isUUID.js +4 -0
- package/src/md5.js +5 -1
- package/src/password/check.js +7 -3
- package/src/password/hash.js +12 -7
- package/src/setLocalEnvs.js +16 -3
- package/src/shouldIgnore.js +99 -0
- package/src/thingType.js +62 -24
- package/src/toPennies.js +23 -5
- package/src/utils/buildExportsTree.js +45 -17
- package/src/utils/buildFileDataList.js +37 -18
- package/src/utils/clean.js +205 -120
- package/src/utils/extractJSDocComment.js +14 -7
- package/src/utils/generateFile.js +54 -18
- package/src/utils/generateFlatExportLines.js +40 -17
- package/src/utils/generateImportStatements.js +20 -8
- package/src/utils/generateNamedExports.js +55 -10
- package/src/utils/generateNamespaceCode.js +56 -24
- package/src/utils/generateNamespaceExportLines.js +27 -7
- package/src/utils/writeHotWrapper.js +42 -0
- package/src/uuid.js +9 -7
- package/src/validateSchema.js +95 -77
- package/test/collectImports.js +8 -0
- package/test/css/some.css +3 -0
- package/test/css/some.js +5 -0
- package/test/ignoreFolder/ignoreMe.js +3 -1
- package/test/ignoreFolder/ignoreMe2.js +3 -1
- package/test/ignoreFolder2/ignoreMe.js +6 -2
- package/test/js/abc.test.js +7 -3
- package/test/js/some.js +5 -0
- package/test/secret.test.js +1 -1
- package/tests/clean/array.test.js +122 -74
- package/tests/clean/boolean.test.js +18 -6
- package/tests/clean/integer.test.js +25 -9
- package/tests/clean/number.test.js +49 -13
- package/tests/clean/object.test.js +190 -119
- package/tests/clean/string.test.js +48 -17
- package/tests/clean/timestamp.test.js +12 -5
- package/tests/clean/uuid.test.js +13 -6
- package/tests/collectImports.test.js +66 -0
- package/tests/combineFiles.test.js +28 -26
- package/tests/convertBytes.test.js +8 -3
- package/tests/env.test.js +9 -3
- package/tests/example.test.js +7 -3
- package/tests/fileList.test.js +16 -12
- package/tests/hash-and-encrypt.test.js +13 -4
- package/tests/ignore.test.js +55 -0
- package/tests/md5.test.js +7 -2
- package/tests/namedExports.test.js +13 -0
- package/tests/uuid.test.js +10 -4
- package/tests/validateSchema.test.js +21 -9
- package/tsconfig.json +20 -16
- package/types/build.d.ts +5 -1
- package/types/collectImports.d.ts +6 -0
- package/types/index.d.ts +108 -78
- package/types/shouldIgnore.d.ts +1 -0
- package/types/utils/buildFileDataList.d.ts +5 -1
- package/types/utils/generateFile.d.ts +8 -1
- package/types/utils/generateFlatExportLines.d.ts +7 -1
- package/types/utils/generateImportStatements.d.ts +5 -1
- package/types/utils/generateNamedExports.d.ts +7 -1
- package/types/utils/generateNamespaceCode.d.ts +7 -1
- package/types/utils/generateNamespaceExportLines.d.ts +6 -1
- package/types/utils/writeHotWrapper.d.ts +4 -0
- package/src/utils/shouldIgnore.js +0 -43
package/package.json
CHANGED
|
@@ -1,26 +1,27 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
2
|
+
"name": "@awesomeness-js/utils",
|
|
3
|
+
"version": "1.1.2",
|
|
4
|
+
"description": "Awesomeness - Utils",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/awesomeness-js/utils.git"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"prebuild": "node ./build/build.js",
|
|
11
|
+
"build": "tsc && npm run postBuild",
|
|
12
|
+
"postBuild": "node ./build/postBuild.js",
|
|
13
|
+
"test": "vitest"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"types": "./types/index.d.ts",
|
|
17
|
+
"main": "index.js",
|
|
18
|
+
"author": "Scott Forte",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"vitest": "^3.0.9",
|
|
25
|
+
"chokidar": "^4.0.3"
|
|
26
|
+
}
|
|
27
|
+
}
|
package/schemas/schema1.js
CHANGED
|
@@ -5,7 +5,11 @@ const properties = {
|
|
|
5
5
|
exampleId: {
|
|
6
6
|
type: 'uuid',
|
|
7
7
|
description: 'the id of the vertex',
|
|
8
|
-
default: () => {
|
|
8
|
+
default: () => {
|
|
9
|
+
|
|
10
|
+
return uuid();
|
|
11
|
+
|
|
12
|
+
},
|
|
9
13
|
example: '00000000-0000-0000-0000-000000000000',
|
|
10
14
|
immutable: true,
|
|
11
15
|
required: true
|
package/schemas/schema2.js
CHANGED
|
@@ -5,7 +5,11 @@ const properties = {
|
|
|
5
5
|
exampleId: {
|
|
6
6
|
type: 'uuid',
|
|
7
7
|
description: 'the id of the vertex',
|
|
8
|
-
default: () => {
|
|
8
|
+
default: () => {
|
|
9
|
+
|
|
10
|
+
return uuid();
|
|
11
|
+
|
|
12
|
+
},
|
|
9
13
|
example: '00000000-0000-0000-0000-000000000000',
|
|
10
14
|
immutable: true,
|
|
11
15
|
required: true
|
package/schemas.js
CHANGED
package/src/build.js
CHANGED
|
@@ -12,17 +12,97 @@
|
|
|
12
12
|
*/
|
|
13
13
|
import { writeFileSync } from 'fs';
|
|
14
14
|
import generateFile from './utils/generateFile.js';
|
|
15
|
+
import writeHotWrapper from './utils/writeHotWrapper.js';
|
|
15
16
|
|
|
16
17
|
async function build({
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
src = './src',
|
|
19
|
+
dest = './index.js',
|
|
20
|
+
exportRoots = true,
|
|
21
|
+
ignore = [],
|
|
22
|
+
includeComments = true,
|
|
23
|
+
dts = false,
|
|
24
|
+
useTabs = true,
|
|
25
|
+
hotModuleReload = false,
|
|
26
|
+
hotCallback = null,
|
|
27
|
+
hotSource = './hot-source.js' // generated only when hot is enabled
|
|
23
28
|
} = {}) {
|
|
24
|
-
|
|
25
|
-
|
|
29
|
+
|
|
30
|
+
const gen = () => generateFile({
|
|
31
|
+
src,
|
|
32
|
+
exportRoots,
|
|
33
|
+
ignore,
|
|
34
|
+
includeComments,
|
|
35
|
+
dts,
|
|
36
|
+
useTabs
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const buildHot = () => {
|
|
40
|
+
|
|
41
|
+
// 1) write payload that the wrapper imports
|
|
42
|
+
writeFileSync(hotSource, gen());
|
|
43
|
+
console.log(`[build] wrote ${hotSource}`);
|
|
44
|
+
|
|
45
|
+
// 2) ensure index.js is a hot wrapper
|
|
46
|
+
writeHotWrapper({
|
|
47
|
+
dest,
|
|
48
|
+
hotSource
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
console.log(`[build] ensured hot wrapper ${dest}`);
|
|
52
|
+
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const buildCold = () => {
|
|
56
|
+
|
|
57
|
+
// single-file classic output (no second file)
|
|
58
|
+
writeFileSync(dest, gen());
|
|
59
|
+
console.log(`[build] wrote ${dest}`);
|
|
60
|
+
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
if (hotModuleReload) {
|
|
64
|
+
|
|
65
|
+
buildHot();
|
|
66
|
+
|
|
67
|
+
// watch only src; each change regenerates hot-source.js (wrapper auto-reloads it)
|
|
68
|
+
const chokidar = (await import('chokidar')).default;
|
|
69
|
+
|
|
70
|
+
console.log(`[build] watching ${src} for changes...`);
|
|
71
|
+
const watcher = chokidar.watch(src, {
|
|
72
|
+
ignoreInitial: true,
|
|
73
|
+
ignored: ignore
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
let timeout;
|
|
77
|
+
|
|
78
|
+
watcher.on('all', (_event, file) => {
|
|
79
|
+
|
|
80
|
+
clearTimeout(timeout);
|
|
81
|
+
timeout = setTimeout(() => {
|
|
82
|
+
|
|
83
|
+
console.log(`[build] change detected in ${file}, rebuilding payload...`);
|
|
84
|
+
writeFileSync(hotSource, gen());
|
|
85
|
+
|
|
86
|
+
if(typeof hotCallback === 'function') {
|
|
87
|
+
|
|
88
|
+
hotCallback(file);
|
|
89
|
+
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
console.log(`[build] ready.`);
|
|
93
|
+
|
|
94
|
+
}, 50);
|
|
95
|
+
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
} else {
|
|
99
|
+
|
|
100
|
+
buildCold();
|
|
101
|
+
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return true;
|
|
105
|
+
|
|
26
106
|
}
|
|
27
107
|
|
|
28
108
|
export default build;
|
package/src/clean/boolean.js
CHANGED
|
@@ -1,22 +1,32 @@
|
|
|
1
1
|
export default function cleanBoolean(x, {
|
|
2
|
-
|
|
2
|
+
required = false
|
|
3
3
|
} = {}){
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
5
|
+
try {
|
|
6
|
+
|
|
7
|
+
if(x !== true && x !== false){
|
|
8
|
+
|
|
9
|
+
throw {
|
|
10
|
+
message: `Invalid boolean value "${x}"`,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return x;
|
|
16
|
+
|
|
17
|
+
} catch (e) {
|
|
18
|
+
|
|
19
|
+
if(required) {
|
|
20
|
+
|
|
21
|
+
throw e;
|
|
22
|
+
|
|
23
|
+
} else {
|
|
24
|
+
|
|
25
|
+
return null;
|
|
26
|
+
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
}
|
|
30
|
+
|
|
21
31
|
}
|
|
22
32
|
|
package/src/clean/integer.js
CHANGED
|
@@ -1,74 +1,90 @@
|
|
|
1
1
|
export default function cleanInt(x, {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
required = false,
|
|
3
|
+
convertString = true,
|
|
4
|
+
min = false,
|
|
5
|
+
max = false,
|
|
6
6
|
} = {}){
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
+
|
|
18
|
+
throw {
|
|
19
|
+
message: 'string contains decimal',
|
|
20
|
+
value: x
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if(!isNaN(parseInt(x))){
|
|
26
|
+
|
|
27
|
+
x = parseInt(x);
|
|
28
|
+
|
|
29
|
+
} else {
|
|
30
|
+
|
|
31
|
+
throw {
|
|
32
|
+
message: 'string cannot be parsed to integer',
|
|
33
|
+
value: x
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
} else {
|
|
40
|
+
|
|
41
|
+
throw {
|
|
42
|
+
message: 'Input must be an integer',
|
|
43
|
+
value: x
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (min !== false && x < min) {
|
|
51
|
+
|
|
52
|
+
throw {
|
|
53
|
+
message: `Integer must be greater than or equal to ${min}`,
|
|
54
|
+
value: x,
|
|
55
|
+
min,
|
|
56
|
+
max
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (max !== false && x > max) {
|
|
62
|
+
|
|
63
|
+
throw {
|
|
64
|
+
message: `Integer must be less than or equal to ${max}`,
|
|
65
|
+
value: x,
|
|
66
|
+
min,
|
|
67
|
+
max
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return x;
|
|
73
|
+
|
|
74
|
+
} catch (e) {
|
|
75
|
+
|
|
76
|
+
if(required) {
|
|
77
|
+
|
|
78
|
+
throw e;
|
|
79
|
+
|
|
80
|
+
} else {
|
|
81
|
+
|
|
82
|
+
return null;
|
|
83
|
+
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
|
|
73
89
|
}
|
|
74
90
|
|
package/src/clean/number.js
CHANGED
|
@@ -1,93 +1,110 @@
|
|
|
1
1
|
export default function cleanNumber(x, {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
required = false,
|
|
3
|
+
convertString = true,
|
|
4
|
+
min = false,
|
|
5
|
+
max = false,
|
|
6
|
+
maxDecimal = false,
|
|
7
|
+
minDecimal = false,
|
|
8
8
|
} = {}){
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
+
|
|
19
|
+
x = parseFloat(x);
|
|
20
|
+
|
|
21
|
+
} catch(e){
|
|
22
|
+
|
|
23
|
+
throw {
|
|
24
|
+
message: 'Input cannot be parsed',
|
|
25
|
+
value: x
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
if(isNaN(x)){
|
|
32
|
+
|
|
33
|
+
throw {
|
|
34
|
+
message: 'Input cannot be parsed NaN',
|
|
35
|
+
value: x
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
} else {
|
|
41
|
+
|
|
42
|
+
throw {
|
|
43
|
+
message: 'Input must be a number',
|
|
44
|
+
value: x
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (min !== false && x < min) {
|
|
52
|
+
|
|
53
|
+
throw {
|
|
54
|
+
message: `Integer must be greater than or equal to ${min}`,
|
|
55
|
+
value: x,
|
|
56
|
+
min,
|
|
57
|
+
max
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (max !== false && x > max) {
|
|
63
|
+
|
|
64
|
+
throw {
|
|
65
|
+
message: `Integer must be less than or equal to ${max}`,
|
|
66
|
+
value: x,
|
|
67
|
+
min,
|
|
68
|
+
max
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (maxDecimal !== false && x.toString().split('.')[1]?.length > maxDecimal) {
|
|
74
|
+
|
|
75
|
+
throw {
|
|
76
|
+
message: `Number must have at most ${maxDecimal} decimal places`,
|
|
77
|
+
value: x,
|
|
78
|
+
maxDecimal
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (minDecimal !== false && x.toString().split('.')[1]?.length < minDecimal) {
|
|
84
|
+
|
|
85
|
+
throw {
|
|
86
|
+
message: `Number must have at least ${minDecimal} decimal places`,
|
|
87
|
+
value: x,
|
|
88
|
+
minDecimal
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return x;
|
|
86
94
|
|
|
87
|
-
|
|
95
|
+
} catch (e) {
|
|
96
|
+
|
|
97
|
+
if(required) {
|
|
98
|
+
|
|
99
|
+
throw e;
|
|
100
|
+
|
|
101
|
+
} else {
|
|
102
|
+
|
|
103
|
+
return null;
|
|
104
|
+
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
}
|
|
88
108
|
|
|
89
|
-
if(required) { throw e; } else { return null; }
|
|
90
|
-
|
|
91
|
-
}
|
|
92
109
|
}
|
|
93
110
|
|