@lijuhong1981/jscheck 1.1.3 → 1.1.4

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 (2) hide show
  1. package/package.json +22 -22
  2. package/src/Check.js +224 -38
package/package.json CHANGED
@@ -1,22 +1,22 @@
1
- {
2
- "name": "@lijuhong1981/jscheck",
3
- "version": "1.1.3",
4
- "description": "> TODO: description",
5
- "author": "lijuhong1981 <lijuhong@hotmail.com>",
6
- "homepage": "https://github.com/lijuhong1981/jslibs#readme",
7
- "license": "ISC",
8
- "type": "module",
9
- "main": "index.js",
10
- "module": "index.js",
11
- "publishConfig": {
12
- "access": "public"
13
- },
14
- "repository": {
15
- "type": "git",
16
- "url": "git+https://github.com/lijuhong1981/jslibs.git"
17
- },
18
- "bugs": {
19
- "url": "https://github.com/lijuhong1981/jslibs/issues"
20
- },
21
- "gitHead": "11a1d49d8302089d97ed1c37cdbfa8a231d3ef0c"
22
- }
1
+ {
2
+ "name": "@lijuhong1981/jscheck",
3
+ "version": "1.1.4",
4
+ "description": "> TODO: description",
5
+ "author": "lijuhong1981 <lijuhong@hotmail.com>",
6
+ "homepage": "https://github.com/lijuhong1981/jslibs#readme",
7
+ "license": "ISC",
8
+ "type": "module",
9
+ "main": "index.js",
10
+ "module": "index.js",
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/lijuhong1981/jslibs.git"
17
+ },
18
+ "bugs": {
19
+ "url": "https://github.com/lijuhong1981/jslibs/issues"
20
+ },
21
+ "gitHead": "0aa8a09a9c5bc3ad3fd3710892adf1efb223e5a6"
22
+ }
package/src/Check.js CHANGED
@@ -1,17 +1,24 @@
1
1
  import isDefined from "./isDefined.js";
2
2
  import isValid from "./isValid.js";
3
3
 
4
+ /**
5
+ * 检查工具对象
6
+ * @namespace Check
7
+ */
4
8
  const Check = {};
5
-
9
+ /**
10
+ * 检查类型
11
+ * @namespace Check.typeOf
12
+ */
6
13
  Check.typeOf = {};
7
14
 
8
15
  function getUndefinedErrorMessage(name) {
9
16
  return name + " is required, actual value was undefined.";
10
- }
17
+ };
11
18
 
12
19
  function getInvalidErrorMessage(name) {
13
20
  return name + " is required, actual value was invalid.";
14
- }
21
+ };
15
22
 
16
23
  function getFailedTypeErrorMessage(actual, expected, name) {
17
24
  return (
@@ -22,28 +29,63 @@ function getFailedTypeErrorMessage(actual, expected, name) {
22
29
  ", actual typeof was " +
23
30
  actual
24
31
  );
25
- }
26
-
32
+ };
33
+ /**
34
+ * 检查值是否定义
35
+ * @param {string} name 值的名称
36
+ * @param {any} test 需要检查的值
37
+ * @throws {Error} 如果值未定义,则抛出错误
38
+ * @static
39
+ * @memberof Check
40
+ */
27
41
  Check.defined = function (name, test) {
28
42
  if (!isDefined(test)) {
29
43
  throw new Error(getUndefinedErrorMessage(name));
30
44
  }
31
45
  };
32
-
46
+ /**
47
+ * 检查值是否有效
48
+ * @param {string} name 值的名称
49
+ * @param {any} test 需要检查的值
50
+ * @throws {Error} 如果值无效,则抛出错误
51
+ * @static
52
+ * @memberof Check
53
+ */
33
54
  Check.valid = function (name, test) {
34
55
  if (!isValid(test)) {
35
56
  throw new Error(getInvalidErrorMessage(name));
36
57
  }
37
58
  };
38
-
39
- Check.typeOf.func = function (name, test) {
59
+ /**
60
+ * 检查值是否为函数
61
+ * @param {string} name 值的名称
62
+ * @param {any} test 需要检查的值
63
+ * @throws {Error} 如果值不是函数,则抛出错误
64
+ * @static
65
+ * @memberof Check.typeOf
66
+ */
67
+ Check.typeOf.function = function (name, test) {
40
68
  if (typeof test !== "function") {
41
69
  throw new Error(
42
70
  getFailedTypeErrorMessage(typeof test, "function", name)
43
71
  );
44
72
  }
45
73
  };
46
-
74
+ /**
75
+ * 检查值是否为函数
76
+ * @see Check.typeOf.function
77
+ * @static
78
+ * @memberof Check.typeOf
79
+ */
80
+ Check.typeOf.func = Check.typeOf.function;
81
+ /**
82
+ * 检查值是否为字符串
83
+ * @param {string} name 值的名称
84
+ * @param {any} test 需要检查的值
85
+ * @throws {Error} 如果值不是字符串,则抛出错误
86
+ * @static
87
+ * @memberof Check.typeOf
88
+ */
47
89
  Check.typeOf.string = function (name, test) {
48
90
  if (typeof test !== "string") {
49
91
  throw new Error(
@@ -51,7 +93,14 @@ Check.typeOf.string = function (name, test) {
51
93
  );
52
94
  }
53
95
  };
54
-
96
+ /**
97
+ * 检查值是否为数字
98
+ * @param {string} name 值的名称
99
+ * @param {any} test 需要检查的值
100
+ * @throws {Error} 如果值不是数字,则抛出错误
101
+ * @static
102
+ * @memberof Check.typeOf
103
+ */
55
104
  Check.typeOf.number = function (name, test) {
56
105
  if (typeof test !== "number") {
57
106
  throw new Error(
@@ -59,7 +108,15 @@ Check.typeOf.number = function (name, test) {
59
108
  );
60
109
  }
61
110
  };
62
-
111
+ /**
112
+ * 检查值是否为数字且小于指定值
113
+ * @param {string} name 值的名称
114
+ * @param {any} test 需要检查的值
115
+ * @param {number} limit 指定值
116
+ * @throws {Error} 如果值小于指定值,则抛出错误
117
+ * @static
118
+ * @memberof Check.typeOf.number
119
+ */
63
120
  Check.typeOf.number.lessThan = function (name, test, limit) {
64
121
  Check.typeOf.number(name, test);
65
122
  if (test >= limit) {
@@ -73,7 +130,15 @@ Check.typeOf.number.lessThan = function (name, test, limit) {
73
130
  );
74
131
  }
75
132
  };
76
-
133
+ /**
134
+ * 检查值是否为数字且小于或等于指定值
135
+ * @param {string} name 值的名称
136
+ * @param {any} test 需要检查的值
137
+ * @param {number} limit 指定值
138
+ * @throws {Error} 如果值小于或等于指定值,则抛出错误
139
+ * @static
140
+ * @memberof Check.typeOf.number
141
+ */
77
142
  Check.typeOf.number.lessThanOrEquals = function (name, test, limit) {
78
143
  Check.typeOf.number(name, test);
79
144
  if (test > limit) {
@@ -87,7 +152,15 @@ Check.typeOf.number.lessThanOrEquals = function (name, test, limit) {
87
152
  );
88
153
  }
89
154
  };
90
-
155
+ /**
156
+ * 检查值是否为数字且大于指定值
157
+ * @param {string} name 值的名称
158
+ * @param {any} test 需要检查的值
159
+ * @param {number} limit 指定值
160
+ * @throws {Error} 如果值大于指定值,则抛出错误
161
+ * @static
162
+ * @memberof Check.typeOf.number
163
+ */
91
164
  Check.typeOf.number.greaterThan = function (name, test, limit) {
92
165
  Check.typeOf.number(name, test);
93
166
  if (test <= limit) {
@@ -101,7 +174,15 @@ Check.typeOf.number.greaterThan = function (name, test, limit) {
101
174
  );
102
175
  }
103
176
  };
104
-
177
+ /**
178
+ * 检查值是否为数字且大于或等于指定值
179
+ * @param {string} name 值的名称
180
+ * @param {any} test 需要检查的值
181
+ * @param {number} limit 指定值
182
+ * @throws {Error} 如果值大于或等于指定值,则抛出错误
183
+ * @static
184
+ * @memberof Check.typeOf.number
185
+ */
105
186
  Check.typeOf.number.greaterThanOrEquals = function (name, test, limit) {
106
187
  Check.typeOf.number(name, test);
107
188
  if (test < limit) {
@@ -115,7 +196,16 @@ Check.typeOf.number.greaterThanOrEquals = function (name, test, limit) {
115
196
  );
116
197
  }
117
198
  };
118
-
199
+ /**
200
+ * 检查两个值是否为数字且相等
201
+ * @param {string} name1 值1的名称
202
+ * @param {string} name2 值2的名称
203
+ * @param {number} test1 需要检查的值1
204
+ * @param {number} test2 需要检查的值2
205
+ * @throws {Error} 如果两个数值不相等,则抛出错误
206
+ * @static
207
+ * @memberof Check.typeOf.number
208
+ */
119
209
  Check.typeOf.number.equals = function (name1, name2, test1, test2) {
120
210
  Check.typeOf.number(name1, test1);
121
211
  Check.typeOf.number(name2, test2);
@@ -131,7 +221,14 @@ Check.typeOf.number.equals = function (name1, name2, test1, test2) {
131
221
  );
132
222
  }
133
223
  };
134
-
224
+ /**
225
+ * 检查值是否为对象
226
+ * @param {string} name 值的名称
227
+ * @param {any} test 需要检查的值
228
+ * @throws {Error} 如果值不是对象,则抛出错误
229
+ * @static
230
+ * @memberof Check.typeOf
231
+ */
135
232
  Check.typeOf.object = function (name, test) {
136
233
  if (typeof test !== "object") {
137
234
  throw new Error(
@@ -139,27 +236,63 @@ Check.typeOf.object = function (name, test) {
139
236
  );
140
237
  }
141
238
  };
142
-
143
- Check.typeOf.bool = function (name, test) {
239
+ /**
240
+ * 检查值是否为布尔值
241
+ * @param {string} name 值的名称
242
+ * @param {any} test 需要检查的值
243
+ * @throws {Error} 如果值不是布尔值,则抛出错误
244
+ * @static
245
+ * @memberof Check.typeOf
246
+ */
247
+ Check.typeOf.boolean = function (name, test) {
144
248
  if (typeof test !== "boolean") {
145
249
  throw new Error(
146
250
  getFailedTypeErrorMessage(typeof test, "boolean", name)
147
251
  );
148
252
  }
149
253
  };
150
-
254
+ /**
255
+ * 检查值是否为布尔值
256
+ * @see Check.typeOf.boolean
257
+ * @static
258
+ * @memberof Check.typeOf
259
+ */
260
+ Check.typeOf.bool = Check.typeOf.boolean;
261
+ /**
262
+ * 检查值是否为数组
263
+ * @param {string} name 值的名称
264
+ * @param {any} test 需要检查的值
265
+ * @throws {Error} 如果值不是数组,则抛出错误
266
+ * @static
267
+ * @memberof Check.typeOf
268
+ */
151
269
  Check.typeOf.array = function (name, test) {
152
270
  if (Array.isArray(test) === false) {
153
271
  throw new Error(getFailedTypeErrorMessage(typeof test, 'array', name));
154
272
  }
155
273
  };
156
-
274
+ /**
275
+ * 检查值是否为整数
276
+ * @param {string} name 值的名称
277
+ * @param {any} test 需要检查的值
278
+ * @throws {Error} 如果值不是整数,则抛出错误
279
+ * @static
280
+ * @memberof Check.typeOf
281
+ */
157
282
  Check.typeOf.integer = function (name, test) {
158
283
  if (Number.isSafeInteger(test) === false) {
159
284
  throw new Error(getFailedTypeErrorMessage(typeof test, 'integer', name));
160
285
  }
161
286
  }
162
-
287
+ /**
288
+ * 检查值是否为整数且小于指定值
289
+ * @param {string} name 值的名称
290
+ * @param {any} test 需要检查的值
291
+ * @param {number} limit 指定值
292
+ * @throws {Error} 如果值小于指定值,则抛出错误
293
+ * @static
294
+ * @memberof Check.typeOf.integer
295
+ */
163
296
  Check.typeOf.integer.lessThan = function (name, test, limit) {
164
297
  Check.typeOf.integer(name, test);
165
298
  if (test >= limit) {
@@ -171,7 +304,15 @@ Check.typeOf.integer.lessThan = function (name, test, limit) {
171
304
  test);
172
305
  }
173
306
  };
174
-
307
+ /**
308
+ * 检查值是否为整数且小于或等于指定值
309
+ * @param {string} name 值的名称
310
+ * @param {any} test 需要检查的值
311
+ * @param {number} limit 指定值
312
+ * @throws {Error} 如果值小于或等于指定值,则抛出错误
313
+ * @static
314
+ * @memberof Check.typeOf.integer
315
+ */
175
316
  Check.typeOf.integer.lessThanOrEquals = function (name, test, limit) {
176
317
  Check.typeOf.integer(name, test);
177
318
  if (test > limit) {
@@ -183,7 +324,15 @@ Check.typeOf.integer.lessThanOrEquals = function (name, test, limit) {
183
324
  test);
184
325
  }
185
326
  };
186
-
327
+ /**
328
+ * 检查值是否为整数且大于指定值
329
+ * @param {string} name 值的名称
330
+ * @param {any} test 需要检查的值
331
+ * @param {number} limit 指定值
332
+ * @throws {Error} 如果值大于指定值,则抛出错误
333
+ * @static
334
+ * @memberof Check.typeOf.integer
335
+ */
187
336
  Check.typeOf.integer.greaterThan = function (name, test, limit) {
188
337
  Check.typeOf.integer(name, test);
189
338
  if (test <= limit) {
@@ -195,7 +344,15 @@ Check.typeOf.integer.greaterThan = function (name, test, limit) {
195
344
  test);
196
345
  }
197
346
  };
198
-
347
+ /**
348
+ * 检查值是否为整数且大于或等于指定值
349
+ * @param {string} name 值的名称
350
+ * @param {any} test 需要检查的值
351
+ * @param {number} limit 指定值
352
+ * @throws {Error} 如果值大于或等于指定值,则抛出错误
353
+ * @static
354
+ * @memberof Check.typeOf.integer
355
+ */
199
356
  Check.typeOf.integer.greaterThanOrEquals = function (name, test, limit) {
200
357
  Check.typeOf.integer(name, test);
201
358
  if (test < limit) {
@@ -207,7 +364,16 @@ Check.typeOf.integer.greaterThanOrEquals = function (name, test, limit) {
207
364
  test);
208
365
  }
209
366
  };
210
-
367
+ /**
368
+ * 检查两个值是否为整数且相等
369
+ * @param {string} name1 值1的名称
370
+ * @param {string} name2 值2的名称
371
+ * @param {number} test1 需要检查的值1
372
+ * @param {number} test2 需要检查的值2
373
+ * @throws {Error} 如果两个整数值不相等,则抛出错误
374
+ * @static
375
+ * @memberof Check.typeOf.integer
376
+ */
211
377
  Check.typeOf.integer.equals = function (name1, name2, test1, test2) {
212
378
  Check.typeOf.integer(name1, test1);
213
379
  Check.typeOf.integer(name2, test2);
@@ -223,23 +389,43 @@ Check.typeOf.integer.equals = function (name1, name2, test1, test2) {
223
389
  );
224
390
  }
225
391
  };
226
-
227
- Check.instanceOf = function (name, test, target) {
392
+ /**
393
+ * 检查值是否为指定类的实例
394
+ * @param {string} name 值的名称
395
+ * @param {any} test 需要检查的值
396
+ * @param {Function} target 指定类
397
+ * @throws {Error} 如果值不是指定类的实例,则抛出错误
398
+ * @static
399
+ * @memberof Check.typeOf
400
+ */
401
+ Check.typeOf.instanceOf = function (name, test, target) {
228
402
  if (test instanceof target === false) {
229
403
  throw new Error(getFailedTypeErrorMessage(false, target.name, name));
230
404
  }
231
405
  };
232
-
233
- Check.equals = function (name, test, target) {
234
- Check.isValid(test);
235
- Check.isValid(target);
236
- if (test !== target) {
237
- throw new Error('Expected ' +
238
- name +
239
- ' to be equal ' +
240
- target +
241
- ', actual value was ' +
242
- test);
406
+ /**
407
+ * 检查两个值是否已定义且相等
408
+ * @param {string} name1 值1的名称
409
+ * @param {string} name2 值2的名称
410
+ * @param {any} test1 需要检查的值1
411
+ * @param {any} test2 需要检查的值2
412
+ * @throws {Error} 如果两个值不相等,则抛出错误
413
+ * @static
414
+ * @memberof Check.typeOf
415
+ */
416
+ Check.typeOf.equals = function (name1, name2, test1, test2) {
417
+ Check.defined(name1, test1);
418
+ Check.defined(name2, test2);
419
+ if (test1 !== test2) {
420
+ throw new Error(
421
+ name1 +
422
+ " must be equal to " +
423
+ name2 +
424
+ ", the actual values are " +
425
+ test1 +
426
+ " and " +
427
+ test2
428
+ );
243
429
  }
244
430
  };
245
431