tiny-essentials 1.9.1 → 1.10.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/dist/TinyBasicsEs.js +155 -98
- package/dist/TinyBasicsEs.min.js +1 -1
- package/dist/TinyEssentials.js +605 -174
- package/dist/TinyEssentials.min.js +1 -1
- package/dist/TinyRateLimiter.js +450 -76
- package/dist/TinyRateLimiter.min.js +1 -1
- package/dist/legacy/firebase/mySQL.cjs +1 -1
- package/dist/v1/basics/objFilter.cjs +155 -98
- package/dist/v1/basics/objFilter.d.mts +16 -26
- package/dist/v1/basics/objFilter.mjs +146 -77
- package/dist/v1/libs/TinyRateLimiter.cjs +450 -76
- package/dist/v1/libs/TinyRateLimiter.d.mts +186 -31
- package/dist/v1/libs/TinyRateLimiter.mjs +420 -75
- package/docs/basics/objFilter.md +7 -0
- package/docs/libs/TinyRateLimiter.md +167 -15
- package/package.json +1 -1
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
var buffer = require('buffer');
|
|
4
4
|
|
|
5
|
+
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
|
|
6
|
+
|
|
5
7
|
/**
|
|
6
8
|
* An object containing type validation functions and their evaluation order.
|
|
7
9
|
*
|
|
@@ -11,111 +13,27 @@ var buffer = require('buffer');
|
|
|
11
13
|
* The `order` array defines the priority in which types should be checked,
|
|
12
14
|
* which can be useful for functions that infer types in a consistent manner.
|
|
13
15
|
*
|
|
14
|
-
* @typedef {Object} TypeValidator
|
|
15
|
-
* @property {Object.<string, (val: any) => boolean>} items - A dictionary of type validation functions.
|
|
16
|
-
* @property {string[]} order - The order in which types should be evaluated.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Validates values against specific types using predefined functions.
|
|
21
|
-
*
|
|
22
|
-
* @type {TypeValidator}
|
|
23
16
|
*/
|
|
24
17
|
const typeValidator = {
|
|
25
|
-
items: {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
/** @param {*} val @returns {val is boolean} */
|
|
33
|
-
boolean: (val) => typeof val === 'boolean',
|
|
34
|
-
|
|
35
|
-
/** @param {*} val @returns {val is number} */
|
|
36
|
-
number: (val) => typeof val === 'number' && !isNaN(val),
|
|
37
|
-
|
|
38
|
-
/** @param {*} val @returns {val is bigint} */
|
|
39
|
-
bigint: (val) => typeof val === 'bigint',
|
|
40
|
-
|
|
41
|
-
/** @param {*} val @returns {val is string} */
|
|
42
|
-
string: (val) => typeof val === 'string',
|
|
43
|
-
|
|
44
|
-
/** @param {*} val @returns {val is symbol} */
|
|
45
|
-
symbol: (val) => typeof val === 'symbol',
|
|
46
|
-
|
|
47
|
-
/** @param {*} val @returns {val is Function} */
|
|
48
|
-
function: (val) => typeof val === 'function',
|
|
49
|
-
|
|
50
|
-
/** @param {*} val @returns {val is Array} */
|
|
51
|
-
array: (val) => Array.isArray(val),
|
|
52
|
-
|
|
53
|
-
/** @param {*} val @returns {val is Date} */
|
|
54
|
-
date: (val) => val instanceof Date,
|
|
55
|
-
|
|
56
|
-
/** @param {*} val @returns {val is RegExp} */
|
|
57
|
-
regexp: (val) => val instanceof RegExp,
|
|
58
|
-
|
|
59
|
-
/** @param {*} val @returns {val is Map} */
|
|
60
|
-
map: (val) => val instanceof Map,
|
|
61
|
-
|
|
62
|
-
/** @param {*} val @returns {val is Set} */
|
|
63
|
-
set: (val) => val instanceof Set,
|
|
64
|
-
|
|
65
|
-
/** @param {*} val @returns {val is WeakMap} */
|
|
66
|
-
weakmap: (val) => val instanceof WeakMap,
|
|
67
|
-
|
|
68
|
-
/** @param {*} val @returns {val is WeakSet} */
|
|
69
|
-
weakset: (val) => val instanceof WeakSet,
|
|
70
|
-
|
|
71
|
-
/** @param {*} val @returns {val is Promise} */
|
|
72
|
-
promise: (val) => val instanceof Promise,
|
|
73
|
-
|
|
74
|
-
/** @param {*} val @returns {val is Buffer} */
|
|
75
|
-
buffer: (val) => typeof buffer.Buffer !== 'undefined' && buffer.Buffer.isBuffer(val),
|
|
76
|
-
|
|
77
|
-
/** @param {*} val @returns {val is File} */
|
|
78
|
-
file: (val) => typeof File !== 'undefined' && val instanceof File,
|
|
79
|
-
|
|
80
|
-
/** @param {*} val @returns {val is HTMLElement} */
|
|
81
|
-
htmlelement: (val) => typeof HTMLElement !== 'undefined' && val instanceof HTMLElement,
|
|
82
|
-
|
|
83
|
-
/** @param {*} val @returns {val is object} */
|
|
84
|
-
object: (val) => typeof val === 'object' && val !== null && !Array.isArray(val),
|
|
85
|
-
},
|
|
86
|
-
|
|
87
|
-
/** Evaluation order of the type checkers. */
|
|
88
|
-
order: [
|
|
89
|
-
'undefined',
|
|
90
|
-
'null',
|
|
91
|
-
'boolean',
|
|
92
|
-
'number',
|
|
93
|
-
'bigint',
|
|
94
|
-
'string',
|
|
95
|
-
'symbol',
|
|
96
|
-
'function',
|
|
97
|
-
'array',
|
|
98
|
-
'buffer',
|
|
99
|
-
'file',
|
|
100
|
-
'date',
|
|
101
|
-
'regexp',
|
|
102
|
-
'map',
|
|
103
|
-
'set',
|
|
104
|
-
'weakmap',
|
|
105
|
-
'weakset',
|
|
106
|
-
'promise',
|
|
107
|
-
'htmlelement',
|
|
108
|
-
'object',
|
|
109
|
-
],
|
|
18
|
+
items: {},
|
|
19
|
+
/**
|
|
20
|
+
* Evaluation order of the type checkers.
|
|
21
|
+
* @type {string[]}
|
|
22
|
+
* */
|
|
23
|
+
order: [],
|
|
110
24
|
};
|
|
111
25
|
|
|
26
|
+
/** @typedef {Object.<string, (val: any) => *>} ExtendObjType */
|
|
27
|
+
/** @typedef {Array<[string, (val: any) => *]>} ExtendObjTypeArray */
|
|
28
|
+
|
|
112
29
|
/**
|
|
113
30
|
* Adds new type checkers to the typeValidator without overwriting existing ones.
|
|
114
31
|
*
|
|
115
|
-
*
|
|
32
|
+
* Accepts either an object with named functions or an array of [key, fn] arrays.
|
|
116
33
|
* If no index is provided, the type is inserted just before 'object' (if it exists), or at the end.
|
|
117
34
|
*
|
|
118
|
-
* @param {
|
|
35
|
+
* @param {ExtendObjType|ExtendObjTypeArray} newItems
|
|
36
|
+
* - New type validators to be added.
|
|
119
37
|
* @param {number} [index] - Optional. Position at which to insert each new type. Ignored if the type already exists.
|
|
120
38
|
* @returns {string[]} - A list of successfully added type names.
|
|
121
39
|
*
|
|
@@ -123,12 +41,20 @@ const typeValidator = {
|
|
|
123
41
|
* extendObjType({
|
|
124
42
|
* htmlElement2: val => typeof HTMLElement !== 'undefined' && val instanceof HTMLElement
|
|
125
43
|
* });
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* extendObjType([
|
|
47
|
+
* ['alpha', val => typeof val === 'string'],
|
|
48
|
+
* ['beta', val => Array.isArray(val)]
|
|
49
|
+
* ]);
|
|
126
50
|
*/
|
|
127
51
|
function extendObjType(newItems, index) {
|
|
128
52
|
const added = [];
|
|
129
53
|
|
|
130
|
-
|
|
54
|
+
const entries = Array.isArray(newItems) ? newItems : Object.entries(newItems);
|
|
55
|
+
for (const [key, fn] of entries) {
|
|
131
56
|
if (!typeValidator.items.hasOwnProperty(key)) {
|
|
57
|
+
// @ts-ignore
|
|
132
58
|
typeValidator.items[key] = fn;
|
|
133
59
|
|
|
134
60
|
let insertAt = typeof index === 'number' ? index : -1; // Default to -1 if index isn't provided
|
|
@@ -206,9 +132,12 @@ function cloneObjTypeOrder() {
|
|
|
206
132
|
*/
|
|
207
133
|
const getType = (val) => {
|
|
208
134
|
if (val === null) return 'null';
|
|
209
|
-
|
|
135
|
+
// @ts-ignore
|
|
136
|
+
for (const name of typeValidator.order) {
|
|
137
|
+
// @ts-ignore
|
|
210
138
|
if (typeof typeValidator.items[name] !== 'function' || typeValidator.items[name](val))
|
|
211
139
|
return name;
|
|
140
|
+
}
|
|
212
141
|
return 'unknown';
|
|
213
142
|
};
|
|
214
143
|
|
|
@@ -243,7 +172,9 @@ function checkObj(obj) {
|
|
|
243
172
|
/** @type {{ valid:*; type: string | null }} */
|
|
244
173
|
const data = { valid: null, type: null };
|
|
245
174
|
for (const name of typeValidator.order) {
|
|
175
|
+
// @ts-ignore
|
|
246
176
|
if (typeof typeValidator.items[name] === 'function') {
|
|
177
|
+
// @ts-ignore
|
|
247
178
|
const result = typeValidator.items[name](obj);
|
|
248
179
|
if (result) {
|
|
249
180
|
data.valid = result;
|
|
@@ -283,6 +214,132 @@ function countObj(obj) {
|
|
|
283
214
|
return 0;
|
|
284
215
|
}
|
|
285
216
|
|
|
217
|
+
// Insert obj types
|
|
218
|
+
|
|
219
|
+
extendObjType([
|
|
220
|
+
[
|
|
221
|
+
'undefined',
|
|
222
|
+
/** @param {*} val @returns {val is undefined} */
|
|
223
|
+
(val) => typeof val === 'undefined',
|
|
224
|
+
],
|
|
225
|
+
[
|
|
226
|
+
'null',
|
|
227
|
+
/** @param {*} val @returns {val is null} */
|
|
228
|
+
(val) => val === null,
|
|
229
|
+
],
|
|
230
|
+
[
|
|
231
|
+
'boolean',
|
|
232
|
+
/** @param {*} val @returns {val is boolean} */
|
|
233
|
+
(val) => typeof val === 'boolean',
|
|
234
|
+
],
|
|
235
|
+
[
|
|
236
|
+
'number',
|
|
237
|
+
/** @param {*} val @returns {val is number} */
|
|
238
|
+
(val) => typeof val === 'number' && !Number.isNaN(val),
|
|
239
|
+
],
|
|
240
|
+
[
|
|
241
|
+
'bigint',
|
|
242
|
+
/** @param {*} val @returns {val is bigint} */
|
|
243
|
+
(val) => typeof val === 'bigint',
|
|
244
|
+
],
|
|
245
|
+
[
|
|
246
|
+
'string',
|
|
247
|
+
/** @param {*} val @returns {val is string} */
|
|
248
|
+
(val) => typeof val === 'string',
|
|
249
|
+
],
|
|
250
|
+
[
|
|
251
|
+
'symbol',
|
|
252
|
+
/** @param {*} val @returns {val is symbol} */
|
|
253
|
+
(val) => typeof val === 'symbol',
|
|
254
|
+
],
|
|
255
|
+
[
|
|
256
|
+
'function',
|
|
257
|
+
/** @param {*} val @returns {val is Function} */
|
|
258
|
+
(val) => typeof val === 'function',
|
|
259
|
+
],
|
|
260
|
+
[
|
|
261
|
+
'array',
|
|
262
|
+
/** @param {*} val @returns {val is any[]} */
|
|
263
|
+
(val) => Array.isArray(val),
|
|
264
|
+
],
|
|
265
|
+
]);
|
|
266
|
+
|
|
267
|
+
if (!isBrowser) {
|
|
268
|
+
extendObjType([
|
|
269
|
+
[
|
|
270
|
+
'buffer',
|
|
271
|
+
/** @param {*} val @returns {val is Buffer} */
|
|
272
|
+
(val) => typeof buffer.Buffer !== 'undefined' && buffer.Buffer.isBuffer(val),
|
|
273
|
+
],
|
|
274
|
+
]);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (isBrowser) {
|
|
278
|
+
extendObjType([
|
|
279
|
+
[
|
|
280
|
+
'file',
|
|
281
|
+
/** @param {*} val @returns {val is File} */
|
|
282
|
+
(val) => typeof File !== 'undefined' && val instanceof File,
|
|
283
|
+
],
|
|
284
|
+
]);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
extendObjType([
|
|
288
|
+
[
|
|
289
|
+
'date',
|
|
290
|
+
/** @param {*} val @returns {val is Date} */
|
|
291
|
+
(val) => val instanceof Date,
|
|
292
|
+
],
|
|
293
|
+
[
|
|
294
|
+
'regexp',
|
|
295
|
+
/** @param {*} val @returns {val is RegExp} */
|
|
296
|
+
(val) => val instanceof RegExp,
|
|
297
|
+
],
|
|
298
|
+
[
|
|
299
|
+
'map',
|
|
300
|
+
/** @param {*} val @returns {val is Map<any, any>} */
|
|
301
|
+
(val) => val instanceof Map,
|
|
302
|
+
],
|
|
303
|
+
[
|
|
304
|
+
'set',
|
|
305
|
+
/** @param {*} val @returns {val is Set<any>} */
|
|
306
|
+
(val) => val instanceof Set,
|
|
307
|
+
],
|
|
308
|
+
[
|
|
309
|
+
'weakmap',
|
|
310
|
+
/** @param {*} val @returns {val is WeakMap<any, any>} */
|
|
311
|
+
(val) => val instanceof WeakMap,
|
|
312
|
+
],
|
|
313
|
+
[
|
|
314
|
+
'weakset',
|
|
315
|
+
/** @param {*} val @returns {val is WeakSet<any>} */
|
|
316
|
+
(val) => val instanceof WeakSet,
|
|
317
|
+
],
|
|
318
|
+
[
|
|
319
|
+
'promise',
|
|
320
|
+
/** @param {*} val @returns {val is Promise<any>} */
|
|
321
|
+
(val) => val instanceof Promise,
|
|
322
|
+
],
|
|
323
|
+
]);
|
|
324
|
+
|
|
325
|
+
if (isBrowser) {
|
|
326
|
+
extendObjType([
|
|
327
|
+
[
|
|
328
|
+
'htmlelement',
|
|
329
|
+
/** @param {*} val @returns {val is HTMLElement} */
|
|
330
|
+
(val) => typeof HTMLElement !== 'undefined' && val instanceof HTMLElement,
|
|
331
|
+
],
|
|
332
|
+
]);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
extendObjType([
|
|
336
|
+
[
|
|
337
|
+
'object',
|
|
338
|
+
/** @param {*} val @returns {val is object} */
|
|
339
|
+
(val) => typeof val === 'object' && val !== null && !Array.isArray(val),
|
|
340
|
+
],
|
|
341
|
+
]);
|
|
342
|
+
|
|
286
343
|
exports.checkObj = checkObj;
|
|
287
344
|
exports.cloneObjTypeOrder = cloneObjTypeOrder;
|
|
288
345
|
exports.countObj = countObj;
|
|
@@ -1,10 +1,13 @@
|
|
|
1
|
+
/** @typedef {Object.<string, (val: any) => *>} ExtendObjType */
|
|
2
|
+
/** @typedef {Array<[string, (val: any) => *]>} ExtendObjTypeArray */
|
|
1
3
|
/**
|
|
2
4
|
* Adds new type checkers to the typeValidator without overwriting existing ones.
|
|
3
5
|
*
|
|
4
|
-
*
|
|
6
|
+
* Accepts either an object with named functions or an array of [key, fn] arrays.
|
|
5
7
|
* If no index is provided, the type is inserted just before 'object' (if it exists), or at the end.
|
|
6
8
|
*
|
|
7
|
-
* @param {
|
|
9
|
+
* @param {ExtendObjType|ExtendObjTypeArray} newItems
|
|
10
|
+
* - New type validators to be added.
|
|
8
11
|
* @param {number} [index] - Optional. Position at which to insert each new type. Ignored if the type already exists.
|
|
9
12
|
* @returns {string[]} - A list of successfully added type names.
|
|
10
13
|
*
|
|
@@ -12,10 +15,14 @@
|
|
|
12
15
|
* extendObjType({
|
|
13
16
|
* htmlElement2: val => typeof HTMLElement !== 'undefined' && val instanceof HTMLElement
|
|
14
17
|
* });
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* extendObjType([
|
|
21
|
+
* ['alpha', val => typeof val === 'string'],
|
|
22
|
+
* ['beta', val => Array.isArray(val)]
|
|
23
|
+
* ]);
|
|
15
24
|
*/
|
|
16
|
-
export function extendObjType(newItems:
|
|
17
|
-
[x: string]: (val: any) => boolean;
|
|
18
|
-
}, index?: number): string[];
|
|
25
|
+
export function extendObjType(newItems: ExtendObjType | ExtendObjTypeArray, index?: number): string[];
|
|
19
26
|
/**
|
|
20
27
|
* Reorders the typeValidator.order array according to a custom new order.
|
|
21
28
|
* All values in the new order must already exist in the current order.
|
|
@@ -67,7 +74,7 @@ export function checkObj(obj: any): {
|
|
|
67
74
|
* It returns a new object where the keys are the same and the values are the cloned functions.
|
|
68
75
|
*/
|
|
69
76
|
export function getCheckObj(): {
|
|
70
|
-
[k: string]:
|
|
77
|
+
[k: string]: any;
|
|
71
78
|
};
|
|
72
79
|
/**
|
|
73
80
|
* Counts the number of elements in an array or the number of properties in an object.
|
|
@@ -81,25 +88,8 @@ export function getCheckObj(): {
|
|
|
81
88
|
* countObj('not an object'); // 0
|
|
82
89
|
*/
|
|
83
90
|
export function countObj(obj: Array<any> | Record<string | number, any>): number;
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
*
|
|
87
|
-
* Each item in `typeValidator.items` is a function that receives any value
|
|
88
|
-
* and returns a boolean indicating whether the value matches the corresponding type.
|
|
89
|
-
*
|
|
90
|
-
* The `order` array defines the priority in which types should be checked,
|
|
91
|
-
* which can be useful for functions that infer types in a consistent manner.
|
|
92
|
-
*/
|
|
93
|
-
export type TypeValidator = {
|
|
94
|
-
/**
|
|
95
|
-
* - A dictionary of type validation functions.
|
|
96
|
-
*/
|
|
97
|
-
items: {
|
|
98
|
-
[x: string]: (val: any) => boolean;
|
|
99
|
-
};
|
|
100
|
-
/**
|
|
101
|
-
* - The order in which types should be evaluated.
|
|
102
|
-
*/
|
|
103
|
-
order: string[];
|
|
91
|
+
export type ExtendObjType = {
|
|
92
|
+
[x: string]: (val: any) => any;
|
|
104
93
|
};
|
|
94
|
+
export type ExtendObjTypeArray = Array<[string, (val: any) => any]>;
|
|
105
95
|
//# sourceMappingURL=objFilter.d.mts.map
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Buffer } from 'buffer';
|
|
2
|
+
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
|
|
2
3
|
/**
|
|
3
4
|
* An object containing type validation functions and their evaluation order.
|
|
4
5
|
*
|
|
@@ -8,89 +9,25 @@ import { Buffer } from 'buffer';
|
|
|
8
9
|
* The `order` array defines the priority in which types should be checked,
|
|
9
10
|
* which can be useful for functions that infer types in a consistent manner.
|
|
10
11
|
*
|
|
11
|
-
* @typedef {Object} TypeValidator
|
|
12
|
-
* @property {Object.<string, (val: any) => boolean>} items - A dictionary of type validation functions.
|
|
13
|
-
* @property {string[]} order - The order in which types should be evaluated.
|
|
14
|
-
*/
|
|
15
|
-
/**
|
|
16
|
-
* Validates values against specific types using predefined functions.
|
|
17
|
-
*
|
|
18
|
-
* @type {TypeValidator}
|
|
19
12
|
*/
|
|
20
13
|
const typeValidator = {
|
|
21
|
-
items: {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
boolean: (val) => typeof val === 'boolean',
|
|
28
|
-
/** @param {*} val @returns {val is number} */
|
|
29
|
-
number: (val) => typeof val === 'number' && !isNaN(val),
|
|
30
|
-
/** @param {*} val @returns {val is bigint} */
|
|
31
|
-
bigint: (val) => typeof val === 'bigint',
|
|
32
|
-
/** @param {*} val @returns {val is string} */
|
|
33
|
-
string: (val) => typeof val === 'string',
|
|
34
|
-
/** @param {*} val @returns {val is symbol} */
|
|
35
|
-
symbol: (val) => typeof val === 'symbol',
|
|
36
|
-
/** @param {*} val @returns {val is Function} */
|
|
37
|
-
function: (val) => typeof val === 'function',
|
|
38
|
-
/** @param {*} val @returns {val is Array} */
|
|
39
|
-
array: (val) => Array.isArray(val),
|
|
40
|
-
/** @param {*} val @returns {val is Date} */
|
|
41
|
-
date: (val) => val instanceof Date,
|
|
42
|
-
/** @param {*} val @returns {val is RegExp} */
|
|
43
|
-
regexp: (val) => val instanceof RegExp,
|
|
44
|
-
/** @param {*} val @returns {val is Map} */
|
|
45
|
-
map: (val) => val instanceof Map,
|
|
46
|
-
/** @param {*} val @returns {val is Set} */
|
|
47
|
-
set: (val) => val instanceof Set,
|
|
48
|
-
/** @param {*} val @returns {val is WeakMap} */
|
|
49
|
-
weakmap: (val) => val instanceof WeakMap,
|
|
50
|
-
/** @param {*} val @returns {val is WeakSet} */
|
|
51
|
-
weakset: (val) => val instanceof WeakSet,
|
|
52
|
-
/** @param {*} val @returns {val is Promise} */
|
|
53
|
-
promise: (val) => val instanceof Promise,
|
|
54
|
-
/** @param {*} val @returns {val is Buffer} */
|
|
55
|
-
buffer: (val) => typeof Buffer !== 'undefined' && Buffer.isBuffer(val),
|
|
56
|
-
/** @param {*} val @returns {val is File} */
|
|
57
|
-
file: (val) => typeof File !== 'undefined' && val instanceof File,
|
|
58
|
-
/** @param {*} val @returns {val is HTMLElement} */
|
|
59
|
-
htmlelement: (val) => typeof HTMLElement !== 'undefined' && val instanceof HTMLElement,
|
|
60
|
-
/** @param {*} val @returns {val is object} */
|
|
61
|
-
object: (val) => typeof val === 'object' && val !== null && !Array.isArray(val),
|
|
62
|
-
},
|
|
63
|
-
/** Evaluation order of the type checkers. */
|
|
64
|
-
order: [
|
|
65
|
-
'undefined',
|
|
66
|
-
'null',
|
|
67
|
-
'boolean',
|
|
68
|
-
'number',
|
|
69
|
-
'bigint',
|
|
70
|
-
'string',
|
|
71
|
-
'symbol',
|
|
72
|
-
'function',
|
|
73
|
-
'array',
|
|
74
|
-
'buffer',
|
|
75
|
-
'file',
|
|
76
|
-
'date',
|
|
77
|
-
'regexp',
|
|
78
|
-
'map',
|
|
79
|
-
'set',
|
|
80
|
-
'weakmap',
|
|
81
|
-
'weakset',
|
|
82
|
-
'promise',
|
|
83
|
-
'htmlelement',
|
|
84
|
-
'object',
|
|
85
|
-
],
|
|
14
|
+
items: {},
|
|
15
|
+
/**
|
|
16
|
+
* Evaluation order of the type checkers.
|
|
17
|
+
* @type {string[]}
|
|
18
|
+
* */
|
|
19
|
+
order: [],
|
|
86
20
|
};
|
|
21
|
+
/** @typedef {Object.<string, (val: any) => *>} ExtendObjType */
|
|
22
|
+
/** @typedef {Array<[string, (val: any) => *]>} ExtendObjTypeArray */
|
|
87
23
|
/**
|
|
88
24
|
* Adds new type checkers to the typeValidator without overwriting existing ones.
|
|
89
25
|
*
|
|
90
|
-
*
|
|
26
|
+
* Accepts either an object with named functions or an array of [key, fn] arrays.
|
|
91
27
|
* If no index is provided, the type is inserted just before 'object' (if it exists), or at the end.
|
|
92
28
|
*
|
|
93
|
-
* @param {
|
|
29
|
+
* @param {ExtendObjType|ExtendObjTypeArray} newItems
|
|
30
|
+
* - New type validators to be added.
|
|
94
31
|
* @param {number} [index] - Optional. Position at which to insert each new type. Ignored if the type already exists.
|
|
95
32
|
* @returns {string[]} - A list of successfully added type names.
|
|
96
33
|
*
|
|
@@ -98,11 +35,19 @@ const typeValidator = {
|
|
|
98
35
|
* extendObjType({
|
|
99
36
|
* htmlElement2: val => typeof HTMLElement !== 'undefined' && val instanceof HTMLElement
|
|
100
37
|
* });
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* extendObjType([
|
|
41
|
+
* ['alpha', val => typeof val === 'string'],
|
|
42
|
+
* ['beta', val => Array.isArray(val)]
|
|
43
|
+
* ]);
|
|
101
44
|
*/
|
|
102
45
|
export function extendObjType(newItems, index) {
|
|
103
46
|
const added = [];
|
|
104
|
-
|
|
47
|
+
const entries = Array.isArray(newItems) ? newItems : Object.entries(newItems);
|
|
48
|
+
for (const [key, fn] of entries) {
|
|
105
49
|
if (!typeValidator.items.hasOwnProperty(key)) {
|
|
50
|
+
// @ts-ignore
|
|
106
51
|
typeValidator.items[key] = fn;
|
|
107
52
|
let insertAt = typeof index === 'number' ? index : -1; // Default to -1 if index isn't provided
|
|
108
53
|
// Default to before 'object', or to the end
|
|
@@ -171,9 +116,12 @@ export function cloneObjTypeOrder() {
|
|
|
171
116
|
const getType = (val) => {
|
|
172
117
|
if (val === null)
|
|
173
118
|
return 'null';
|
|
174
|
-
|
|
119
|
+
// @ts-ignore
|
|
120
|
+
for (const name of typeValidator.order) {
|
|
121
|
+
// @ts-ignore
|
|
175
122
|
if (typeof typeValidator.items[name] !== 'function' || typeValidator.items[name](val))
|
|
176
123
|
return name;
|
|
124
|
+
}
|
|
177
125
|
return 'unknown';
|
|
178
126
|
};
|
|
179
127
|
/**
|
|
@@ -208,7 +156,9 @@ export function checkObj(obj) {
|
|
|
208
156
|
/** @type {{ valid:*; type: string | null }} */
|
|
209
157
|
const data = { valid: null, type: null };
|
|
210
158
|
for (const name of typeValidator.order) {
|
|
159
|
+
// @ts-ignore
|
|
211
160
|
if (typeof typeValidator.items[name] === 'function') {
|
|
161
|
+
// @ts-ignore
|
|
212
162
|
const result = typeValidator.items[name](obj);
|
|
213
163
|
if (result) {
|
|
214
164
|
data.valid = result;
|
|
@@ -247,3 +197,122 @@ export function countObj(obj) {
|
|
|
247
197
|
// Nothing
|
|
248
198
|
return 0;
|
|
249
199
|
}
|
|
200
|
+
// Insert obj types
|
|
201
|
+
extendObjType([
|
|
202
|
+
[
|
|
203
|
+
'undefined',
|
|
204
|
+
/** @param {*} val @returns {val is undefined} */
|
|
205
|
+
(val) => typeof val === 'undefined',
|
|
206
|
+
],
|
|
207
|
+
[
|
|
208
|
+
'null',
|
|
209
|
+
/** @param {*} val @returns {val is null} */
|
|
210
|
+
(val) => val === null,
|
|
211
|
+
],
|
|
212
|
+
[
|
|
213
|
+
'boolean',
|
|
214
|
+
/** @param {*} val @returns {val is boolean} */
|
|
215
|
+
(val) => typeof val === 'boolean',
|
|
216
|
+
],
|
|
217
|
+
[
|
|
218
|
+
'number',
|
|
219
|
+
/** @param {*} val @returns {val is number} */
|
|
220
|
+
(val) => typeof val === 'number' && !Number.isNaN(val),
|
|
221
|
+
],
|
|
222
|
+
[
|
|
223
|
+
'bigint',
|
|
224
|
+
/** @param {*} val @returns {val is bigint} */
|
|
225
|
+
(val) => typeof val === 'bigint',
|
|
226
|
+
],
|
|
227
|
+
[
|
|
228
|
+
'string',
|
|
229
|
+
/** @param {*} val @returns {val is string} */
|
|
230
|
+
(val) => typeof val === 'string',
|
|
231
|
+
],
|
|
232
|
+
[
|
|
233
|
+
'symbol',
|
|
234
|
+
/** @param {*} val @returns {val is symbol} */
|
|
235
|
+
(val) => typeof val === 'symbol',
|
|
236
|
+
],
|
|
237
|
+
[
|
|
238
|
+
'function',
|
|
239
|
+
/** @param {*} val @returns {val is Function} */
|
|
240
|
+
(val) => typeof val === 'function',
|
|
241
|
+
],
|
|
242
|
+
[
|
|
243
|
+
'array',
|
|
244
|
+
/** @param {*} val @returns {val is any[]} */
|
|
245
|
+
(val) => Array.isArray(val),
|
|
246
|
+
],
|
|
247
|
+
]);
|
|
248
|
+
if (!isBrowser) {
|
|
249
|
+
extendObjType([
|
|
250
|
+
[
|
|
251
|
+
'buffer',
|
|
252
|
+
/** @param {*} val @returns {val is Buffer} */
|
|
253
|
+
(val) => typeof Buffer !== 'undefined' && Buffer.isBuffer(val),
|
|
254
|
+
],
|
|
255
|
+
]);
|
|
256
|
+
}
|
|
257
|
+
if (isBrowser) {
|
|
258
|
+
extendObjType([
|
|
259
|
+
[
|
|
260
|
+
'file',
|
|
261
|
+
/** @param {*} val @returns {val is File} */
|
|
262
|
+
(val) => typeof File !== 'undefined' && val instanceof File,
|
|
263
|
+
],
|
|
264
|
+
]);
|
|
265
|
+
}
|
|
266
|
+
extendObjType([
|
|
267
|
+
[
|
|
268
|
+
'date',
|
|
269
|
+
/** @param {*} val @returns {val is Date} */
|
|
270
|
+
(val) => val instanceof Date,
|
|
271
|
+
],
|
|
272
|
+
[
|
|
273
|
+
'regexp',
|
|
274
|
+
/** @param {*} val @returns {val is RegExp} */
|
|
275
|
+
(val) => val instanceof RegExp,
|
|
276
|
+
],
|
|
277
|
+
[
|
|
278
|
+
'map',
|
|
279
|
+
/** @param {*} val @returns {val is Map<any, any>} */
|
|
280
|
+
(val) => val instanceof Map,
|
|
281
|
+
],
|
|
282
|
+
[
|
|
283
|
+
'set',
|
|
284
|
+
/** @param {*} val @returns {val is Set<any>} */
|
|
285
|
+
(val) => val instanceof Set,
|
|
286
|
+
],
|
|
287
|
+
[
|
|
288
|
+
'weakmap',
|
|
289
|
+
/** @param {*} val @returns {val is WeakMap<any, any>} */
|
|
290
|
+
(val) => val instanceof WeakMap,
|
|
291
|
+
],
|
|
292
|
+
[
|
|
293
|
+
'weakset',
|
|
294
|
+
/** @param {*} val @returns {val is WeakSet<any>} */
|
|
295
|
+
(val) => val instanceof WeakSet,
|
|
296
|
+
],
|
|
297
|
+
[
|
|
298
|
+
'promise',
|
|
299
|
+
/** @param {*} val @returns {val is Promise<any>} */
|
|
300
|
+
(val) => val instanceof Promise,
|
|
301
|
+
],
|
|
302
|
+
]);
|
|
303
|
+
if (isBrowser) {
|
|
304
|
+
extendObjType([
|
|
305
|
+
[
|
|
306
|
+
'htmlelement',
|
|
307
|
+
/** @param {*} val @returns {val is HTMLElement} */
|
|
308
|
+
(val) => typeof HTMLElement !== 'undefined' && val instanceof HTMLElement,
|
|
309
|
+
],
|
|
310
|
+
]);
|
|
311
|
+
}
|
|
312
|
+
extendObjType([
|
|
313
|
+
[
|
|
314
|
+
'object',
|
|
315
|
+
/** @param {*} val @returns {val is object} */
|
|
316
|
+
(val) => typeof val === 'object' && val !== null && !Array.isArray(val),
|
|
317
|
+
],
|
|
318
|
+
]);
|