@awesomeness-js/utils 1.0.24 → 1.0.25
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/build/build.js +11 -11
- package/build/postBuild.js +8 -8
- package/eslint.config.js +90 -0
- package/index.js +77 -74
- package/package.json +25 -25
- package/schemas/schema1.js +5 -1
- package/schemas/schema2.js +5 -1
- package/schemas.js +2 -2
- package/src/build.js +12 -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 +55 -41
- 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/thingType.js +62 -24
- package/src/toPennies.js +23 -5
- package/src/utils/buildExportsTree.js +45 -17
- package/src/utils/buildFileDataList.js +32 -17
- package/src/utils/clean.js +205 -120
- package/src/utils/extractJSDocComment.js +14 -7
- package/src/utils/generateFile.js +20 -18
- package/src/utils/generateFlatExportLines.js +34 -17
- package/src/utils/generateImportStatements.js +15 -7
- package/src/utils/generateNamedExports.js +20 -9
- package/src/utils/generateNamespaceCode.js +45 -24
- package/src/utils/generateNamespaceExportLines.js +16 -7
- package/src/utils/shouldIgnore.js +57 -37
- 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/md5.test.js +7 -2
- package/tests/uuid.test.js +10 -4
- package/tests/validateSchema.test.js +21 -9
- package/tsconfig.json +20 -16
- package/types/collectImports.d.ts +6 -0
- package/types/index.d.ts +3 -0
package/build/build.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import build from '../src/build.js';
|
|
2
2
|
|
|
3
3
|
build({
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
4
|
+
src: './src',
|
|
5
|
+
dest: './index.js',
|
|
6
|
+
dts: false,
|
|
7
|
+
ignore: [
|
|
8
|
+
'ignoreMe.js',
|
|
9
|
+
'ignoreFolder/*',
|
|
10
|
+
//'namespaceExample/*',
|
|
11
|
+
],
|
|
12
12
|
});
|
|
13
13
|
|
|
14
14
|
build({
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
src: './schemas',
|
|
16
|
+
dest: './schemas.js',
|
|
17
|
+
dts: false
|
|
18
18
|
});
|
package/build/postBuild.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import build from '../src/build.js';
|
|
2
2
|
build({
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
src: './src',
|
|
4
|
+
dest: './types/index.d.ts',
|
|
5
|
+
dts: true,
|
|
6
|
+
ignore: [
|
|
7
|
+
'ignoreMe.js',
|
|
8
|
+
'ignoreFolder/*',
|
|
9
|
+
//'namespaceExample/*',
|
|
10
|
+
],
|
|
11
11
|
});
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
export default [
|
|
2
|
+
// Here is where we fix "padding-line-between-statements"
|
|
3
|
+
{
|
|
4
|
+
rules: {
|
|
5
|
+
'padding-line-between-statements': [
|
|
6
|
+
'error',
|
|
7
|
+
// Insert a blank line **before** any block-like statement
|
|
8
|
+
{
|
|
9
|
+
blankLine: 'always',
|
|
10
|
+
prev: '*',
|
|
11
|
+
next: 'block-like'
|
|
12
|
+
},
|
|
13
|
+
// Insert a blank line **after** any block-like statement
|
|
14
|
+
{
|
|
15
|
+
blankLine: 'always',
|
|
16
|
+
prev: 'block-like',
|
|
17
|
+
next: '*'
|
|
18
|
+
},
|
|
19
|
+
// Keep the rest of your rules here as needed...
|
|
20
|
+
{
|
|
21
|
+
blankLine: 'always',
|
|
22
|
+
prev: '*',
|
|
23
|
+
next: 'function'
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
blankLine: 'always',
|
|
27
|
+
prev: 'function',
|
|
28
|
+
next: '*'
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
blankLine: 'always',
|
|
32
|
+
prev: '*',
|
|
33
|
+
next: 'throw'
|
|
34
|
+
}
|
|
35
|
+
],
|
|
36
|
+
'padded-blocks': [
|
|
37
|
+
'error',
|
|
38
|
+
{
|
|
39
|
+
blocks: 'always',
|
|
40
|
+
classes: 'always',
|
|
41
|
+
switches: 'always'
|
|
42
|
+
}
|
|
43
|
+
],
|
|
44
|
+
|
|
45
|
+
"object-curly-newline": [
|
|
46
|
+
"error",
|
|
47
|
+
{
|
|
48
|
+
ObjectExpression: {
|
|
49
|
+
minProperties: 2, // If object has 2+ props, break into multiple lines
|
|
50
|
+
multiline: true,
|
|
51
|
+
consistent: true
|
|
52
|
+
},
|
|
53
|
+
// You can also configure ImportDeclaration, ExportDeclaration, etc.
|
|
54
|
+
ObjectPattern: {
|
|
55
|
+
multiline: true,
|
|
56
|
+
minProperties: 2,
|
|
57
|
+
consistent: true
|
|
58
|
+
},
|
|
59
|
+
ImportDeclaration: {
|
|
60
|
+
multiline: true,
|
|
61
|
+
minProperties: 2
|
|
62
|
+
},
|
|
63
|
+
ExportDeclaration: {
|
|
64
|
+
multiline: true,
|
|
65
|
+
minProperties: 2
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
],
|
|
69
|
+
|
|
70
|
+
// 2) If the object is multiline, put each property on its own line
|
|
71
|
+
"object-property-newline": [
|
|
72
|
+
"error",
|
|
73
|
+
{
|
|
74
|
+
allowAllPropertiesOnSameLine: false
|
|
75
|
+
}
|
|
76
|
+
],
|
|
77
|
+
|
|
78
|
+
indent: [ "error", "tab", { "SwitchCase": 1 } ],
|
|
79
|
+
semi: [ "error", "always" ],
|
|
80
|
+
"arrow-parens": [ "error", "always" ],
|
|
81
|
+
"object-curly-spacing": [ "error", "always" ],
|
|
82
|
+
"array-bracket-spacing": [ "error", "always" ],
|
|
83
|
+
'lines-between-class-members': [ 'error', 'always' ],
|
|
84
|
+
'newline-after-var': [ 'error', 'always' ],
|
|
85
|
+
'newline-before-return': 'error',
|
|
86
|
+
'brace-style': [ 'error', '1tbs', { allowSingleLine: false } ],
|
|
87
|
+
"no-unreachable": "off"
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
];
|
package/index.js
CHANGED
|
@@ -12,6 +12,7 @@ import _clean_object from './src/clean/object.js';
|
|
|
12
12
|
import _clean_string from './src/clean/string.js';
|
|
13
13
|
import _clean_timestamp from './src/clean/timestamp.js';
|
|
14
14
|
import _clean_uuid from './src/clean/uuid.js';
|
|
15
|
+
import _collectImports from './src/collectImports.js';
|
|
15
16
|
import _combineFiles from './src/combineFiles.js';
|
|
16
17
|
import _convertBytes from './src/convertBytes.js';
|
|
17
18
|
import _decrypt from './src/decrypt.js';
|
|
@@ -43,6 +44,7 @@ import _uuid from './src/uuid.js';
|
|
|
43
44
|
import _validateSchema from './src/validateSchema.js';
|
|
44
45
|
|
|
45
46
|
export { _build as build };
|
|
47
|
+
export { _collectImports as collectImports };
|
|
46
48
|
export { _combineFiles as combineFiles };
|
|
47
49
|
export { _convertBytes as convertBytes };
|
|
48
50
|
export { _decrypt as decrypt };
|
|
@@ -60,78 +62,79 @@ export { _uuid as uuid };
|
|
|
60
62
|
export { _validateSchema as validateSchema };
|
|
61
63
|
|
|
62
64
|
export default {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Builds a file from the specified source directory and writes it to the destination file.
|
|
67
|
+
*
|
|
68
|
+
* @param {Object} options - The options for the build process.
|
|
69
|
+
* @param {string} [options.src='./src'] - The source directory to build from.
|
|
70
|
+
* @param {string} [options.dest='./index.js'] - The destination file to write the built content to.
|
|
71
|
+
* @param {boolean} [options.exportRoots=true] - Whether to export root files.
|
|
72
|
+
* @param {string[]} [options.ignore=[]] - An array of file patterns to ignore.
|
|
73
|
+
* @param {boolean} [options.includeComments=true] - Whether to include comments in the generated file.
|
|
74
|
+
* @param {boolean} [options.dts=false] - Whether to generate TypeScript declaration files.
|
|
75
|
+
* @returns {Promise<boolean>} A promise that resolves to true when the build is complete.
|
|
76
|
+
*/
|
|
77
|
+
build: _build,
|
|
78
|
+
collectImports: _collectImports,
|
|
79
|
+
combineFiles: _combineFiles,
|
|
80
|
+
/**
|
|
81
|
+
* Converts a given number of bytes into a more readable string format with appropriate units.
|
|
82
|
+
*
|
|
83
|
+
* @param {number} bytes - The number of bytes to convert.
|
|
84
|
+
* @param {number} [precision=2] - The number of decimal places to include in the result.
|
|
85
|
+
* @returns {string} The converted bytes in a string format with appropriate units.
|
|
86
|
+
*/
|
|
87
|
+
convertBytes: _convertBytes,
|
|
88
|
+
decrypt: _decrypt,
|
|
89
|
+
/**
|
|
90
|
+
* Iterates over elements of an array or properties of an object, invoking a callback for each element/property.
|
|
91
|
+
* The iteration stops if the callback returns `false`.
|
|
92
|
+
*
|
|
93
|
+
* @example each({ a: 1, b: 2 }, (value, key) => { console.log(value, key); });
|
|
94
|
+
* @param {Object|Array} objectOrArray - The object or array to iterate over.
|
|
95
|
+
* @param {Function} callback - The function to invoke per iteration. It is invoked with two arguments: (value, key/index).
|
|
96
|
+
* @returns {void}
|
|
97
|
+
*/
|
|
98
|
+
each: _each,
|
|
99
|
+
eachAsync: _eachAsync,
|
|
100
|
+
encrypt: _encrypt,
|
|
101
|
+
getAllFiles: _getAllFiles,
|
|
102
|
+
ignoreMe: _ignoreMe,
|
|
103
|
+
isUUID: _isUUID,
|
|
104
|
+
md5: _md5,
|
|
105
|
+
setLocalEnvs: _setLocalEnvs,
|
|
106
|
+
thingType: _thingType,
|
|
107
|
+
toPennies: _toPennies,
|
|
108
|
+
uuid: _uuid,
|
|
109
|
+
validateSchema: _validateSchema,
|
|
110
|
+
clean: {
|
|
111
|
+
array: _clean_array,
|
|
112
|
+
boolean: _clean_boolean,
|
|
113
|
+
integer: _clean_integer,
|
|
114
|
+
number: _clean_number,
|
|
115
|
+
object: _clean_object,
|
|
116
|
+
string: _clean_string,
|
|
117
|
+
timestamp: _clean_timestamp,
|
|
118
|
+
uuid: _clean_uuid,
|
|
119
|
+
},
|
|
120
|
+
ignoreFolder: {
|
|
121
|
+
ignoreMe: _ignoreFolder_ignoreMe,
|
|
122
|
+
},
|
|
123
|
+
password: {
|
|
124
|
+
check: _password_check,
|
|
125
|
+
hash: _password_hash,
|
|
126
|
+
},
|
|
127
|
+
utils: {
|
|
128
|
+
buildExportsTree: _utils_buildExportsTree,
|
|
129
|
+
buildFileDataList: _utils_buildFileDataList,
|
|
130
|
+
clean: _utils_clean,
|
|
131
|
+
extractJSDocComment: _utils_extractJSDocComment,
|
|
132
|
+
generateFile: _utils_generateFile,
|
|
133
|
+
generateFlatExportLines: _utils_generateFlatExportLines,
|
|
134
|
+
generateImportStatements: _utils_generateImportStatements,
|
|
135
|
+
generateNamedExports: _utils_generateNamedExports,
|
|
136
|
+
generateNamespaceCode: _utils_generateNamespaceCode,
|
|
137
|
+
generateNamespaceExportLines: _utils_generateNamespaceExportLines,
|
|
138
|
+
shouldIgnore: _utils_shouldIgnore,
|
|
139
|
+
},
|
|
137
140
|
};
|
package/package.json
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
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.0.25",
|
|
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
|
+
}
|
|
26
|
+
}
|
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
|
@@ -14,15 +14,19 @@ import { writeFileSync } from 'fs';
|
|
|
14
14
|
import generateFile from './utils/generateFile.js';
|
|
15
15
|
|
|
16
16
|
async function build({
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
src = './src',
|
|
18
|
+
dest = './index.js',
|
|
19
|
+
exportRoots = true,
|
|
20
|
+
ignore = [],
|
|
21
|
+
includeComments = true,
|
|
22
|
+
dts = false,
|
|
23
|
+
useTabs = true
|
|
23
24
|
} = {}) {
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
|
|
26
|
+
writeFileSync(dest, generateFile(src, exportRoots, ignore, includeComments, dts, useTabs));
|
|
27
|
+
|
|
28
|
+
return true;
|
|
29
|
+
|
|
26
30
|
}
|
|
27
31
|
|
|
28
32
|
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
|
|