@lijuhong1981/jscheck 1.0.0
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/index.js +23 -0
- package/package.json +13 -0
- package/src/Check.js +235 -0
- package/src/isArray.js +5 -0
- package/src/isBoolean.js +5 -0
- package/src/isFunction.js +5 -0
- package/src/isInteger.js +5 -0
- package/src/isNumber.js +5 -0
- package/src/isObject.js +5 -0
- package/src/isString.js +5 -0
- package/src/isTypedArray.js +17 -0
- package/src/isValid.js +9 -0
package/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import Check from "./src/Check.js";
|
|
2
|
+
import isArray from "./src/isArray.js";
|
|
3
|
+
import isBoolean from "./src/isBoolean.js";
|
|
4
|
+
import isFunction from "./src/isFunction.js";
|
|
5
|
+
import isInteger from "./src/isInteger.js";
|
|
6
|
+
import isNumber from "./src/isNumber.js";
|
|
7
|
+
import isObject from "./src/isObject.js";
|
|
8
|
+
import isString from "./src/isString.js";
|
|
9
|
+
import isTypedArray from "./src/isTypedArray.js";
|
|
10
|
+
import isValid from "./src/isValid.js";
|
|
11
|
+
|
|
12
|
+
export default Check;
|
|
13
|
+
export {
|
|
14
|
+
isArray,
|
|
15
|
+
isBoolean,
|
|
16
|
+
isFunction,
|
|
17
|
+
isInteger,
|
|
18
|
+
isNumber,
|
|
19
|
+
isObject,
|
|
20
|
+
isString,
|
|
21
|
+
isTypedArray,
|
|
22
|
+
isValid,
|
|
23
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lijuhong1981/jscheck",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"module": "index.js",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
10
|
+
},
|
|
11
|
+
"author": "lijuhong1981",
|
|
12
|
+
"license": "ISC"
|
|
13
|
+
}
|
package/src/Check.js
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import isValid from "./isValid.js";
|
|
2
|
+
|
|
3
|
+
const Check = {};
|
|
4
|
+
|
|
5
|
+
Check.typeOf = {};
|
|
6
|
+
|
|
7
|
+
function getInvalidErrorMessage(name) {
|
|
8
|
+
return name + " is required, actual value was invalid.";
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function getFailedTypeErrorMessage(actual, expected, name) {
|
|
12
|
+
return (
|
|
13
|
+
"Expected " +
|
|
14
|
+
name +
|
|
15
|
+
" to be typeof " +
|
|
16
|
+
expected +
|
|
17
|
+
", actual typeof was " +
|
|
18
|
+
actual
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
Check.isValid = function (name, test) {
|
|
23
|
+
if (!isValid(test)) {
|
|
24
|
+
throw new Error(getInvalidErrorMessage(name));
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
Check.typeOf.func = function (name, test) {
|
|
29
|
+
if (typeof test !== "function") {
|
|
30
|
+
throw new Error(
|
|
31
|
+
getFailedTypeErrorMessage(typeof test, "function", name)
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
Check.typeOf.string = function (name, test) {
|
|
37
|
+
if (typeof test !== "string") {
|
|
38
|
+
throw new Error(
|
|
39
|
+
getFailedTypeErrorMessage(typeof test, "string", name)
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
Check.typeOf.number = function (name, test) {
|
|
45
|
+
if (typeof test !== "number") {
|
|
46
|
+
throw new Error(
|
|
47
|
+
getFailedTypeErrorMessage(typeof test, "number", name)
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
Check.typeOf.number.lessThan = function (name, test, limit) {
|
|
53
|
+
Check.typeOf.number(name, test);
|
|
54
|
+
if (test >= limit) {
|
|
55
|
+
throw new Error(
|
|
56
|
+
"Expected " +
|
|
57
|
+
name +
|
|
58
|
+
" to be less than " +
|
|
59
|
+
limit +
|
|
60
|
+
", actual value was " +
|
|
61
|
+
test
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
Check.typeOf.number.lessThanOrEquals = function (name, test, limit) {
|
|
67
|
+
Check.typeOf.number(name, test);
|
|
68
|
+
if (test > limit) {
|
|
69
|
+
throw new Error(
|
|
70
|
+
"Expected " +
|
|
71
|
+
name +
|
|
72
|
+
" to be less than or equal to " +
|
|
73
|
+
limit +
|
|
74
|
+
", actual value was " +
|
|
75
|
+
test
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
Check.typeOf.number.greaterThan = function (name, test, limit) {
|
|
81
|
+
Check.typeOf.number(name, test);
|
|
82
|
+
if (test <= limit) {
|
|
83
|
+
throw new Error(
|
|
84
|
+
"Expected " +
|
|
85
|
+
name +
|
|
86
|
+
" to be greater than " +
|
|
87
|
+
limit +
|
|
88
|
+
", actual value was " +
|
|
89
|
+
test
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
Check.typeOf.number.greaterThanOrEquals = function (name, test, limit) {
|
|
95
|
+
Check.typeOf.number(name, test);
|
|
96
|
+
if (test < limit) {
|
|
97
|
+
throw new Error(
|
|
98
|
+
"Expected " +
|
|
99
|
+
name +
|
|
100
|
+
" to be greater than or equal to" +
|
|
101
|
+
limit +
|
|
102
|
+
", actual value was " +
|
|
103
|
+
test
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
Check.typeOf.number.equals = function (name1, name2, test1, test2) {
|
|
109
|
+
Check.typeOf.number(name1, test1);
|
|
110
|
+
Check.typeOf.number(name2, test2);
|
|
111
|
+
if (test1 !== test2) {
|
|
112
|
+
throw new Error(
|
|
113
|
+
name1 +
|
|
114
|
+
" must be equal to " +
|
|
115
|
+
name2 +
|
|
116
|
+
", the actual values are " +
|
|
117
|
+
test1 +
|
|
118
|
+
" and " +
|
|
119
|
+
test2
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
Check.typeOf.object = function (name, test) {
|
|
125
|
+
if (typeof test !== "object") {
|
|
126
|
+
throw new Error(
|
|
127
|
+
getFailedTypeErrorMessage(typeof test, "object", name)
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
Check.typeOf.bool = function (name, test) {
|
|
133
|
+
if (typeof test !== "boolean") {
|
|
134
|
+
throw new Error(
|
|
135
|
+
getFailedTypeErrorMessage(typeof test, "boolean", name)
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
Check.typeOf.array = function (name, test) {
|
|
141
|
+
if (Array.isArray(test) === false) {
|
|
142
|
+
throw new Error(getFailedTypeErrorMessage(typeof test, 'array', name));
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
Check.typeOf.integer = function (name, test) {
|
|
147
|
+
if (Number.isSafeInteger(test) === false) {
|
|
148
|
+
throw new Error(getFailedTypeErrorMessage(typeof test, 'integer', name));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
Check.typeOf.integer.lessThan = function (name, test, limit) {
|
|
153
|
+
Check.typeOf.integer(name, test);
|
|
154
|
+
if (test >= limit) {
|
|
155
|
+
throw new Error('Expected ' +
|
|
156
|
+
name +
|
|
157
|
+
' to be less than ' +
|
|
158
|
+
limit +
|
|
159
|
+
', actual value was ' +
|
|
160
|
+
test);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
Check.typeOf.integer.lessThanOrEquals = function (name, test, limit) {
|
|
165
|
+
Check.typeOf.integer(name, test);
|
|
166
|
+
if (test > limit) {
|
|
167
|
+
throw new Error('Expected ' +
|
|
168
|
+
name +
|
|
169
|
+
' to be less than or equal to ' +
|
|
170
|
+
limit +
|
|
171
|
+
', actual value was ' +
|
|
172
|
+
test);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
Check.typeOf.integer.greaterThan = function (name, test, limit) {
|
|
177
|
+
Check.typeOf.integer(name, test);
|
|
178
|
+
if (test <= limit) {
|
|
179
|
+
throw new Error('Expected ' +
|
|
180
|
+
name +
|
|
181
|
+
' to be greater than ' +
|
|
182
|
+
limit +
|
|
183
|
+
', actual value was ' +
|
|
184
|
+
test);
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
Check.typeOf.integer.greaterThanOrEquals = function (name, test, limit) {
|
|
189
|
+
Check.typeOf.integer(name, test);
|
|
190
|
+
if (test < limit) {
|
|
191
|
+
throw new Error('Expected ' +
|
|
192
|
+
name +
|
|
193
|
+
' to be greater than or equal to' +
|
|
194
|
+
limit +
|
|
195
|
+
', actual value was ' +
|
|
196
|
+
test);
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
Check.typeOf.integer.equals = function (name1, name2, test1, test2) {
|
|
201
|
+
Check.typeOf.integer(name1, test1);
|
|
202
|
+
Check.typeOf.integer(name2, test2);
|
|
203
|
+
if (test1 !== test2) {
|
|
204
|
+
throw new Error(
|
|
205
|
+
name1 +
|
|
206
|
+
" must be equal to " +
|
|
207
|
+
name2 +
|
|
208
|
+
", the actual values are " +
|
|
209
|
+
test1 +
|
|
210
|
+
" and " +
|
|
211
|
+
test2
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
Check.instanceOf = function (name, test, target) {
|
|
217
|
+
if (test instanceof target === false) {
|
|
218
|
+
throw new Error(getFailedTypeErrorMessage(false, target.name, name));
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
Check.equals = function (name, test, target) {
|
|
223
|
+
Check.isValid(test);
|
|
224
|
+
Check.isValid(target);
|
|
225
|
+
if (test !== target) {
|
|
226
|
+
throw new Error('Expected ' +
|
|
227
|
+
name +
|
|
228
|
+
' to be equal ' +
|
|
229
|
+
target +
|
|
230
|
+
', actual value was ' +
|
|
231
|
+
test);
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
export default Check;
|
package/src/isArray.js
ADDED
package/src/isBoolean.js
ADDED
package/src/isInteger.js
ADDED
package/src/isNumber.js
ADDED
package/src/isObject.js
ADDED
package/src/isString.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
function isTypedArray(value) {
|
|
2
|
+
if (value instanceof Int8Array ||
|
|
3
|
+
value instanceof Uint8Array ||
|
|
4
|
+
value instanceof Uint8ClampedArray ||
|
|
5
|
+
value instanceof Int16Array ||
|
|
6
|
+
value instanceof Uint16Array ||
|
|
7
|
+
value instanceof Int32Array ||
|
|
8
|
+
value instanceof Uint32Array ||
|
|
9
|
+
value instanceof Float32Array ||
|
|
10
|
+
value instanceof Float64Array ||
|
|
11
|
+
value instanceof BigInt64Array ||
|
|
12
|
+
value instanceof BigUint64Array)
|
|
13
|
+
return true;
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default isTypedArray;
|