@creejs/commons-lang 2.0.0 → 2.0.1
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/cjs/index-dev.cjs +1547 -0
- package/dist/cjs/index-dev.cjs.map +1 -0
- package/dist/cjs/index-min.cjs +2 -0
- package/dist/cjs/index-min.cjs.map +1 -0
- package/dist/esm/index-dev.js +1532 -0
- package/dist/esm/index-dev.js.map +1 -0
- package/dist/esm/index-min.js +2 -0
- package/dist/esm/index-min.js.map +1 -0
- package/dist/umd/index.dev.js +1553 -0
- package/dist/umd/index.dev.js.map +1 -0
- package/dist/umd/index.min.js +2 -0
- package/dist/umd/index.min.js.map +1 -0
- package/index.js +2 -2
- package/package.json +42 -7
- package/types/class-proxy-utils.d.ts +24 -0
- package/types/exec-utils.d.ts +10 -5
- package/types/index.d.ts +23 -7
- package/types/instance-proxy-utils.d.ts +17 -0
- package/types/lang-utils.d.ts +10 -5
- package/types/promise-utils.d.ts +31 -19
- package/types/string-utils.d.ts +21 -1
- package/types/type-assert.d.ts +53 -32
- package/types/type-utils.d.ts +69 -43
- package/lib/exec-utils.js +0 -58
- package/lib/index.js +0 -26
- package/lib/lang-utils.js +0 -110
- package/lib/promise-utils.js +0 -317
- package/lib/string-utils.js +0 -428
- package/lib/type-assert.js +0 -253
- package/lib/type-utils.js +0 -290
- package/types/lang.d.ts +0 -5
- package/types/type-asset.d.ts +0 -84
|
@@ -0,0 +1,1547 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @module LangUtils
|
|
7
|
+
* @description Language utility functions
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var LangUtils = {
|
|
11
|
+
constructorName,
|
|
12
|
+
defaults,
|
|
13
|
+
extend,
|
|
14
|
+
extends: extend,
|
|
15
|
+
equals,
|
|
16
|
+
isBrowser,
|
|
17
|
+
isNode
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Gets the constructor name of a value.
|
|
22
|
+
* @param {*} value - The value to check.
|
|
23
|
+
* @returns {string|undefined} The constructor name, or undefined if value has no constructor.
|
|
24
|
+
*/
|
|
25
|
+
function constructorName (value) {
|
|
26
|
+
return value?.constructor?.name
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Assigns default values from source objects to target object for undefined properties.
|
|
31
|
+
* @param {Object<string, any>} target - The target object to assign defaults to
|
|
32
|
+
* @param {...Object<string, any>} sources - Source objects containing default values
|
|
33
|
+
* @returns {Object<string, any>} The modified target object with defaults applied
|
|
34
|
+
* @throws {TypeError} If target is null or undefined
|
|
35
|
+
*/
|
|
36
|
+
function defaults (target, ...sources) {
|
|
37
|
+
if (target == null) {
|
|
38
|
+
throw new TypeError('"target" must not be null or undefined')
|
|
39
|
+
}
|
|
40
|
+
for (const source of sources) {
|
|
41
|
+
if (source == null) {
|
|
42
|
+
continue
|
|
43
|
+
}
|
|
44
|
+
for (const key in source) {
|
|
45
|
+
if (target[key] === undefined) {
|
|
46
|
+
target[key] = source[key];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return target
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Extends a target object with properties from one or more source objects.
|
|
55
|
+
* @param {Object<string, any>} target - The target object to extend (must not be null/undefined)
|
|
56
|
+
* @param {...Object<string, any>} sources - One or more source objects to copy properties from
|
|
57
|
+
* @returns {Object<string, any>} The modified target object
|
|
58
|
+
* @throws {TypeError} If target is null or undefined
|
|
59
|
+
*/
|
|
60
|
+
function extend (target, ...sources) {
|
|
61
|
+
if (target == null) {
|
|
62
|
+
throw new TypeError('"target" must not be null or undefined')
|
|
63
|
+
}
|
|
64
|
+
for (const source of sources) {
|
|
65
|
+
if (source == null) {
|
|
66
|
+
continue
|
|
67
|
+
}
|
|
68
|
+
for (const key in source) {
|
|
69
|
+
target[key] = source[key];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return target
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Compares two values for equality
|
|
77
|
+
* 1. First checks strict equality (===),
|
|
78
|
+
* 2. then checks if either value has an `equals` method and uses it.
|
|
79
|
+
* @param {*} value1 - First value to compare
|
|
80
|
+
* @param {*} value2 - Second value to compare
|
|
81
|
+
* @returns {boolean} True if values are equal, false otherwise
|
|
82
|
+
*/
|
|
83
|
+
function equals (value1, value2) {
|
|
84
|
+
if (value1 === value2) {
|
|
85
|
+
return true
|
|
86
|
+
}
|
|
87
|
+
if (typeof value1?.equals === 'function') {
|
|
88
|
+
return value1.equals(value2)
|
|
89
|
+
}
|
|
90
|
+
if (typeof value2?.equals === 'function') {
|
|
91
|
+
return value2.equals(value1)
|
|
92
|
+
}
|
|
93
|
+
return false
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Check if the current environment is a browser
|
|
98
|
+
* @returns {boolean}
|
|
99
|
+
*/
|
|
100
|
+
function isBrowser () {
|
|
101
|
+
return typeof window !== 'undefined' && typeof document !== 'undefined'
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Check if the current environment is a nodejs
|
|
106
|
+
* @returns {boolean}
|
|
107
|
+
*/
|
|
108
|
+
function isNode () {
|
|
109
|
+
return !isBrowser()
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* @module TypeUtils
|
|
114
|
+
* @description Utility functions for type checking and validation.
|
|
115
|
+
*/
|
|
116
|
+
var TypeUtils = {
|
|
117
|
+
isArray,
|
|
118
|
+
isBoolean,
|
|
119
|
+
isBuffer,
|
|
120
|
+
isFunction,
|
|
121
|
+
isInstance,
|
|
122
|
+
isIterable,
|
|
123
|
+
isDate,
|
|
124
|
+
isError,
|
|
125
|
+
isMap,
|
|
126
|
+
isWeakMap,
|
|
127
|
+
isNumber,
|
|
128
|
+
isPositive,
|
|
129
|
+
isNegative,
|
|
130
|
+
isNil,
|
|
131
|
+
isNullOrUndefined,
|
|
132
|
+
isNull,
|
|
133
|
+
isUndefined,
|
|
134
|
+
isPlainObject: isPlainObject$1,
|
|
135
|
+
isObject,
|
|
136
|
+
isPromise,
|
|
137
|
+
isRegExp,
|
|
138
|
+
isSet,
|
|
139
|
+
isWeakSet,
|
|
140
|
+
isStream,
|
|
141
|
+
isString,
|
|
142
|
+
isSymbol,
|
|
143
|
+
isPrimitive
|
|
144
|
+
};
|
|
145
|
+
/**
|
|
146
|
+
* Checks if the given value is an array.
|
|
147
|
+
* @param {*} value - The value to check.
|
|
148
|
+
* @returns {boolean} True if the value is an array, false otherwise.
|
|
149
|
+
*/
|
|
150
|
+
function isArray (value) {
|
|
151
|
+
return Array.isArray(value)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Checks if the given value is a boolean.
|
|
156
|
+
* @param {*} value - The value to check.
|
|
157
|
+
* @returns {boolean} True if the value is a boolean, false otherwise.
|
|
158
|
+
*/
|
|
159
|
+
function isBoolean (value) {
|
|
160
|
+
return typeof value === 'boolean'
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Checks if the given value is a Buffer.
|
|
165
|
+
* @param {*} value - The value to check.
|
|
166
|
+
* @returns {boolean} True if the value is a Buffer, false otherwise.
|
|
167
|
+
*/
|
|
168
|
+
function isBuffer (value) {
|
|
169
|
+
return value != null && Buffer.isBuffer(value)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Checks if the given value is a Date.
|
|
174
|
+
* @param {*} value - The value to check.
|
|
175
|
+
* @returns {boolean} True if the value is a Date, false otherwise.
|
|
176
|
+
*/
|
|
177
|
+
function isDate (value) {
|
|
178
|
+
return value != null && value instanceof Date
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Checks if the given value is an instance of Error.
|
|
183
|
+
* @param {*} value - The value to check.
|
|
184
|
+
* @returns {boolean} True if the value is an Error, false otherwise.
|
|
185
|
+
*/
|
|
186
|
+
function isError (value) {
|
|
187
|
+
return value != null && value instanceof Error
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Checks if the given value is a function.
|
|
192
|
+
* @param {*} value - The value to check.
|
|
193
|
+
* @returns {boolean} True if the value is a function, false otherwise.
|
|
194
|
+
*/
|
|
195
|
+
function isFunction (value) {
|
|
196
|
+
return typeof value === 'function'
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Checks if a value is a class instance (non-null and not a plain object).
|
|
201
|
+
* @param {*} value - The value to check.
|
|
202
|
+
* @returns {boolean} True if the value is a class instance, false otherwise.
|
|
203
|
+
*/
|
|
204
|
+
function isInstance (value) {
|
|
205
|
+
return value != null && typeof value === 'object' && !isPlainObject$1(value)
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Checks if a value is isIterable
|
|
210
|
+
* @param {*} value - The value to check.
|
|
211
|
+
* @returns {boolean} True if the value is isIterable, false otherwise.
|
|
212
|
+
*/
|
|
213
|
+
function isIterable (value) {
|
|
214
|
+
return value != null && typeof value[Symbol.iterator] === 'function'
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Checks if a value is Map
|
|
219
|
+
* @param {*} value - The value to check.
|
|
220
|
+
* @returns {boolean} True if the value is Map, otherwise false.
|
|
221
|
+
*/
|
|
222
|
+
function isMap (value) {
|
|
223
|
+
return value != null && typeof value === 'object' && value.constructor === Map
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Checks if a value is WeakMap
|
|
228
|
+
* @param {*} value - The value to check.
|
|
229
|
+
* @returns {boolean} True if the value is WeakMap, otherwise false.
|
|
230
|
+
*/
|
|
231
|
+
function isWeakMap (value) {
|
|
232
|
+
return value != null && typeof value === 'object' && value.constructor === WeakMap
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Checks if a value is null or undefined.
|
|
237
|
+
* 1. value == null
|
|
238
|
+
* 2. return true, if value is null or undefined
|
|
239
|
+
* @param {*} value - The value to check.
|
|
240
|
+
* @returns {boolean} True if the value is null or undefined, otherwise false.
|
|
241
|
+
*/
|
|
242
|
+
function isNil (value) {
|
|
243
|
+
return value == null
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Checks if a value is null or undefined.
|
|
248
|
+
* 1. same with isNil()
|
|
249
|
+
* @param {*} value - The value to check.
|
|
250
|
+
* @returns {boolean} True if the value is null or undefined, otherwise false.
|
|
251
|
+
*/
|
|
252
|
+
function isNullOrUndefined (value) {
|
|
253
|
+
return value == null
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* check that a value is a positive number.
|
|
258
|
+
* @param {number} value - The value to check.
|
|
259
|
+
* @returns {boolean}
|
|
260
|
+
*/
|
|
261
|
+
function isPositive (value) {
|
|
262
|
+
if (!isNumber(value)) {
|
|
263
|
+
return false
|
|
264
|
+
}
|
|
265
|
+
return value > 0
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* check that a value is a Negative number.
|
|
270
|
+
* @param {number} value - The value to check.
|
|
271
|
+
*/
|
|
272
|
+
function isNegative (value) {
|
|
273
|
+
if (!isNumber(value)) {
|
|
274
|
+
return false
|
|
275
|
+
}
|
|
276
|
+
return value < 0
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Checks if the given value is exactly null.
|
|
281
|
+
* @param {*} value - The value to check.
|
|
282
|
+
* @returns {boolean} True if the value is null, false otherwise.
|
|
283
|
+
*/
|
|
284
|
+
function isNull (value) {
|
|
285
|
+
return value === null
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Checks if a value is exactly undefined.
|
|
290
|
+
* @param {*} value - The value to check.
|
|
291
|
+
* @returns {boolean} True if the value is undefined, false otherwise.
|
|
292
|
+
*/
|
|
293
|
+
function isUndefined (value) {
|
|
294
|
+
return value === undefined
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Checks if a value is a number.
|
|
299
|
+
* @param {*} value - The value to check.
|
|
300
|
+
* @returns {boolean} True if the value is a number, false otherwise.
|
|
301
|
+
*/
|
|
302
|
+
function isNumber (value) {
|
|
303
|
+
return value != null && typeof value === 'number'
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Checks if a value is an object (and not null).
|
|
308
|
+
* @param {*} value - The value to check
|
|
309
|
+
* @returns {boolean} True if the value is an object (not null), false otherwise
|
|
310
|
+
*/
|
|
311
|
+
function isObject (value) {
|
|
312
|
+
return value != null && typeof value === 'object'
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Checks if a value is a plain object (created by the Object constructor).
|
|
317
|
+
* @param {*} value - The value to check.
|
|
318
|
+
* @returns {boolean} True if the value is a plain object, false otherwise.
|
|
319
|
+
*/
|
|
320
|
+
function isPlainObject$1 (value) {
|
|
321
|
+
return value !== null && typeof value === 'object' && (value.constructor === Object || value.constructor === undefined)
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* check if value is primitive: string, number, boolean
|
|
326
|
+
* 1. null/undefined returns false
|
|
327
|
+
* @param {*} value
|
|
328
|
+
* @returns {boolean}
|
|
329
|
+
*/
|
|
330
|
+
function isPrimitive (value) {
|
|
331
|
+
return value !== null && (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean')
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Checks if a value is a Promise.
|
|
336
|
+
* @param {*} value - The value to check.
|
|
337
|
+
* @returns {boolean} True if the value is a Promise, false otherwise.
|
|
338
|
+
*/
|
|
339
|
+
function isPromise (value) {
|
|
340
|
+
return value != null && typeof value.then === 'function'
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Checks if a RegExp
|
|
345
|
+
* @param {*} value - The value to check.
|
|
346
|
+
* @returns {boolean} True if the value is RegExp, otherwise false.
|
|
347
|
+
*/
|
|
348
|
+
function isRegExp (value) {
|
|
349
|
+
return value != null && typeof value === 'object' && value.constructor === RegExp
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Checks if a Set
|
|
354
|
+
* @param {*} value - The value to check.
|
|
355
|
+
* @returns {boolean} True if the value is Set, otherwise false.
|
|
356
|
+
*/
|
|
357
|
+
function isSet (value) {
|
|
358
|
+
return value != null && typeof value === 'object' && value.constructor === Set
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Checks if a WeakSet
|
|
363
|
+
* @param {*} value - The value to check.
|
|
364
|
+
* @returns {boolean} True if the value is WeakSet, otherwise false.
|
|
365
|
+
*/
|
|
366
|
+
function isWeakSet (value) {
|
|
367
|
+
return value != null && typeof value === 'object' && value.constructor === WeakSet
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Check if the value is a string
|
|
372
|
+
* @param {*} value
|
|
373
|
+
* @return {boolean}
|
|
374
|
+
*/
|
|
375
|
+
function isStream (value) {
|
|
376
|
+
return value != null && typeof value.pipe === 'function'
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Check if the value is a string
|
|
381
|
+
* @param {*} value
|
|
382
|
+
* @return {boolean}
|
|
383
|
+
*/
|
|
384
|
+
function isString (value) {
|
|
385
|
+
return value != null && typeof value === 'string'
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Checks if the given value is a Symbol.
|
|
390
|
+
* @param {*} value - The value to check.
|
|
391
|
+
* @returns {boolean} True if the value is a Symbol, false otherwise.
|
|
392
|
+
*/
|
|
393
|
+
function isSymbol (value) {
|
|
394
|
+
return value != null && typeof value === 'symbol'
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// 3rd
|
|
398
|
+
// internal
|
|
399
|
+
// owned
|
|
400
|
+
/**
|
|
401
|
+
* @module TypeAssert
|
|
402
|
+
* @description Type assertion utility functions for validating data types and throwing errors for invalid types.
|
|
403
|
+
*/
|
|
404
|
+
var TypeAssert = {
|
|
405
|
+
assertNumber,
|
|
406
|
+
assertPositive,
|
|
407
|
+
assertNegative,
|
|
408
|
+
assertBoolean,
|
|
409
|
+
assertObject,
|
|
410
|
+
assertPlainObject,
|
|
411
|
+
assertSymbol,
|
|
412
|
+
assertFunction,
|
|
413
|
+
assertInstance,
|
|
414
|
+
assertPromise,
|
|
415
|
+
assertNil,
|
|
416
|
+
assertNotNil,
|
|
417
|
+
assertNull,
|
|
418
|
+
assertNotNull,
|
|
419
|
+
assertUndefined,
|
|
420
|
+
assertString,
|
|
421
|
+
assertArray,
|
|
422
|
+
assertStringOrSymbol
|
|
423
|
+
};
|
|
424
|
+
/**
|
|
425
|
+
* if value is not Array, throw error
|
|
426
|
+
* @param {*} value
|
|
427
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
428
|
+
* @returns {void}
|
|
429
|
+
* @throws {Error}
|
|
430
|
+
*/
|
|
431
|
+
function assertArray (value, paramName) {
|
|
432
|
+
if (!Array.isArray(value)) {
|
|
433
|
+
throw new Error(`${paramName ? paramName + '' : ' '}Not Array: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* if value is not a string, throw error
|
|
438
|
+
* @param {*} value
|
|
439
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
440
|
+
* @returns {void}
|
|
441
|
+
* @throws {Error}
|
|
442
|
+
*/
|
|
443
|
+
function assertString (value, paramName) {
|
|
444
|
+
if (!isString(value)) {
|
|
445
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not String: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* if value is not a Number, throw error
|
|
450
|
+
* @param {*} value
|
|
451
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
452
|
+
* @returns {void}
|
|
453
|
+
* @throws {Error}
|
|
454
|
+
*/
|
|
455
|
+
function assertNumber (value, paramName) {
|
|
456
|
+
if (!isNumber(value)) {
|
|
457
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Number: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* Asserts that a value is a positive number.
|
|
463
|
+
* @param {number} value - The value to check.
|
|
464
|
+
* @param {string} [paramName] - Optional name of the parameter for error message.
|
|
465
|
+
* @throws {Error} If the value is not a number or is less than or equal to zero.
|
|
466
|
+
*/
|
|
467
|
+
function assertPositive (value, paramName) {
|
|
468
|
+
if (!isPositive(value)) {
|
|
469
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Positive: ${value}`)
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Asserts that a value is a Negative number.
|
|
475
|
+
* @param {number} value - The value to check.
|
|
476
|
+
* @param {string} [paramName] - Optional name of the parameter for error message.
|
|
477
|
+
* @throws {Error} If the value is not a number or is less than or equal to zero.
|
|
478
|
+
*/
|
|
479
|
+
function assertNegative (value, paramName) {
|
|
480
|
+
if (!isNegative(value)) {
|
|
481
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Negative: ${value}`)
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* if value is not a string, throw error
|
|
487
|
+
* @param {*} value
|
|
488
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
489
|
+
* @returns {void}
|
|
490
|
+
* @throws {Error}
|
|
491
|
+
*/
|
|
492
|
+
function assertBoolean (value, paramName) {
|
|
493
|
+
if (!isBoolean(value)) {
|
|
494
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Boolean: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* if value is not a Object, throw error
|
|
499
|
+
* @param {*} value
|
|
500
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
501
|
+
* @returns {void}
|
|
502
|
+
* @throws {Error}
|
|
503
|
+
*/
|
|
504
|
+
function assertObject (value, paramName) {
|
|
505
|
+
if (!isObject(value)) {
|
|
506
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Object: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* if value is not a PlainObject, throw error
|
|
511
|
+
* @param {*} value
|
|
512
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
513
|
+
* @returns {void}
|
|
514
|
+
* @throws {Error}
|
|
515
|
+
*/
|
|
516
|
+
function assertPlainObject (value, paramName) {
|
|
517
|
+
if (!isPlainObject$1(value)) {
|
|
518
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not PlainObject: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* if value is not a Symbol, throw error
|
|
523
|
+
* @param {*} value
|
|
524
|
+
* @param {string} [paramName] - The name of the parameter to check@param {string} [paramName] - The name of the parameter to check
|
|
525
|
+
* @returns {void}
|
|
526
|
+
* @throws {Error}
|
|
527
|
+
*/
|
|
528
|
+
function assertSymbol (value, paramName) {
|
|
529
|
+
if (!isSymbol(value)) {
|
|
530
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Symbol: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* if value is not a Function, throw error
|
|
535
|
+
* @param {*} value
|
|
536
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
537
|
+
* @returns {void}
|
|
538
|
+
* @throws {Error}
|
|
539
|
+
*/
|
|
540
|
+
function assertFunction (value, paramName) {
|
|
541
|
+
if (!isFunction(value)) {
|
|
542
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Function: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* if value is not a Class instance, throw error
|
|
547
|
+
* @param {*} value
|
|
548
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
549
|
+
* @returns {void}
|
|
550
|
+
* @throws {Error}
|
|
551
|
+
*/
|
|
552
|
+
function assertInstance (value, paramName) {
|
|
553
|
+
if (!isInstance(value)) {
|
|
554
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Class Instance: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* if value is not a string, throw error
|
|
559
|
+
* @param {*} value
|
|
560
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
561
|
+
* @returns {void}
|
|
562
|
+
* @throws {Error}
|
|
563
|
+
*/
|
|
564
|
+
function assertPromise (value, paramName) {
|
|
565
|
+
if (!isPromise(value)) {
|
|
566
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Promise: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* if value is not a Null or Undefined, throw error
|
|
571
|
+
* @param {*} value
|
|
572
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
573
|
+
* @returns {void}
|
|
574
|
+
* @throws {Error}
|
|
575
|
+
*/
|
|
576
|
+
function assertNil (value, paramName) {
|
|
577
|
+
if (!isNil(value)) {
|
|
578
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Neither Null nor Undefined: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* Asserts that the given value is not nil.
|
|
584
|
+
* @param {*} value - The value to check
|
|
585
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
586
|
+
* @throws {Error} Throws an error if the value is nil
|
|
587
|
+
*/
|
|
588
|
+
function assertNotNil (value, paramName) {
|
|
589
|
+
if (isNil(value)) {
|
|
590
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Should Not Nil`)
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* if value is not a Null, throw error
|
|
596
|
+
* @param {*} value
|
|
597
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
598
|
+
* @returns {void}
|
|
599
|
+
* @throws {Error}
|
|
600
|
+
*/
|
|
601
|
+
function assertNull (value, paramName) {
|
|
602
|
+
if (!isNull(value)) {
|
|
603
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Null: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
/**
|
|
608
|
+
* Asserts that the given value is not null.
|
|
609
|
+
* @param {*} value - The value to check
|
|
610
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
611
|
+
* @throws {Error} Throws an error if the value is null
|
|
612
|
+
*/
|
|
613
|
+
function assertNotNull (value, paramName) {
|
|
614
|
+
if (isNull(value)) {
|
|
615
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Should Not Null`)
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* if value is not a Undefined, throw error
|
|
620
|
+
* @param {*} value
|
|
621
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
622
|
+
* @returns {void}
|
|
623
|
+
* @throws {Error}
|
|
624
|
+
*/
|
|
625
|
+
function assertUndefined (value, paramName) {
|
|
626
|
+
if (!isUndefined(value)) {
|
|
627
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Undefined: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
/**
|
|
632
|
+
* Asserts that the given value is either a string or a symbol.
|
|
633
|
+
* @param {*} value - The value to check.
|
|
634
|
+
* @param {string} [paramName] - Optional parameter name for error message.
|
|
635
|
+
* @throws {Error} Throws an error if the value is not a string or symbol.
|
|
636
|
+
*/
|
|
637
|
+
function assertStringOrSymbol (value, paramName) {
|
|
638
|
+
if (!isString(value) && !isSymbol(value)) {
|
|
639
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not String or Symbol: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
// 3rd
|
|
644
|
+
// internal
|
|
645
|
+
// owned
|
|
646
|
+
|
|
647
|
+
/**
|
|
648
|
+
* @module StringUtils
|
|
649
|
+
* @description Utility functions for string manipulation, validation, and transformation.
|
|
650
|
+
*/
|
|
651
|
+
var StringUtils = {
|
|
652
|
+
isEmpty,
|
|
653
|
+
assertNotEmpty,
|
|
654
|
+
isBlank,
|
|
655
|
+
assertNotBlank,
|
|
656
|
+
capitalize,
|
|
657
|
+
decapitalize,
|
|
658
|
+
splitWithFixedLength,
|
|
659
|
+
split,
|
|
660
|
+
findMarkerPositions,
|
|
661
|
+
findMarkerPositionsRegex,
|
|
662
|
+
substringBefore,
|
|
663
|
+
substringBeforeLast,
|
|
664
|
+
substringAfter,
|
|
665
|
+
substringAfterLast,
|
|
666
|
+
substringBetween,
|
|
667
|
+
substringBetweenGreedy,
|
|
668
|
+
substringsBetween
|
|
669
|
+
};
|
|
670
|
+
|
|
671
|
+
/**
|
|
672
|
+
* Checks if a string is null, undefined, or length is 0.
|
|
673
|
+
* @param {string} str
|
|
674
|
+
* @returns {boolean}
|
|
675
|
+
* @throws {Error} If `str` is not null or undefined, and not a string.
|
|
676
|
+
*/
|
|
677
|
+
function isEmpty (str) {
|
|
678
|
+
if (str == null) {
|
|
679
|
+
return true
|
|
680
|
+
}
|
|
681
|
+
assertString(str);
|
|
682
|
+
return str.length === 0
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
/**
|
|
686
|
+
* Asserts that the given string is not empty.
|
|
687
|
+
* @param {string} str - The string to check.
|
|
688
|
+
* @throws {Error} Throws an error if the string is empty.
|
|
689
|
+
*/
|
|
690
|
+
function assertNotEmpty (str) {
|
|
691
|
+
if (isEmpty(str)) {
|
|
692
|
+
throw new Error(`Empty String: ${str}`)
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/**
|
|
697
|
+
* Checks if a string is null, undefined, or consists only of whitespace.
|
|
698
|
+
* @param {string} str - The string to check.
|
|
699
|
+
* @returns {boolean} True if the string is blank, false otherwise.
|
|
700
|
+
* @throws {Error} If `str` is not null or undefined, and not a string.
|
|
701
|
+
*/
|
|
702
|
+
function isBlank (str) {
|
|
703
|
+
if (str == null) {
|
|
704
|
+
return true
|
|
705
|
+
}
|
|
706
|
+
assertString(str);
|
|
707
|
+
return str.trim().length === 0
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
/**
|
|
711
|
+
* Asserts that the given string is not blank.
|
|
712
|
+
* @param {string} str - The string to check.
|
|
713
|
+
* @throws {Error} Throws an error if the string is blank.
|
|
714
|
+
*/
|
|
715
|
+
function assertNotBlank (str) {
|
|
716
|
+
if (isBlank(str)) {
|
|
717
|
+
throw new Error(`Blank String: ${str}`)
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
/**
|
|
722
|
+
* Capitalizes the first character of a string.
|
|
723
|
+
* @param {string} str - The string to capitalize.
|
|
724
|
+
* @returns {string} The capitalized string or original if unchanged.
|
|
725
|
+
*/
|
|
726
|
+
function capitalize (str) {
|
|
727
|
+
assertString(str);
|
|
728
|
+
if (str.length === 0) {
|
|
729
|
+
return str
|
|
730
|
+
}
|
|
731
|
+
const char0 = str.charAt(0);
|
|
732
|
+
const upperChar = char0.toUpperCase();
|
|
733
|
+
return char0 === upperChar ? str : upperChar + str.slice(1)
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
/**
|
|
737
|
+
* Converts the first character of a string to lowercase.
|
|
738
|
+
* If the string is null or empty, returns it unchanged.
|
|
739
|
+
* @param {string} str - The input string to decapitalize.
|
|
740
|
+
* @returns {string} The decapitalized string or original if unchanged.
|
|
741
|
+
*/
|
|
742
|
+
function decapitalize (str) {
|
|
743
|
+
assertString(str);
|
|
744
|
+
if (str.length === 0) {
|
|
745
|
+
return str
|
|
746
|
+
}
|
|
747
|
+
const char0 = str.charAt(0);
|
|
748
|
+
const lowerChar = char0.toLowerCase();
|
|
749
|
+
return char0 === lowerChar ? str : lowerChar + str.slice(1)
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
/**
|
|
753
|
+
* Splits a string into chunks of fixed length, padding the last chunk if needed.
|
|
754
|
+
* 1. if str is empty, returns an empty array "[]"
|
|
755
|
+
* 2. if length is less than string length, returns an array with the string padded with "padding" as the only element
|
|
756
|
+
* 3. the last chunk is padded with "padding" if needed
|
|
757
|
+
* @param {string} str - The string to split.
|
|
758
|
+
* @param {number} length - The desired length of each chunk.
|
|
759
|
+
* @param {string} [padding=' '] - The padding character for the last chunk.
|
|
760
|
+
* @returns {string[]} An array of string chunks with fixed length.
|
|
761
|
+
* @throws {Error} If `str` is not a string or `length` is not a number.
|
|
762
|
+
*/
|
|
763
|
+
function splitWithFixedLength (str, length, padding = ' ') {
|
|
764
|
+
assertString(str);
|
|
765
|
+
assertNumber(length);
|
|
766
|
+
assertString(padding);
|
|
767
|
+
if (str.length === 0) {
|
|
768
|
+
return []
|
|
769
|
+
}
|
|
770
|
+
if (length <= 0) {
|
|
771
|
+
throw new Error('length muse >=0')
|
|
772
|
+
}
|
|
773
|
+
if (str.length < length) {
|
|
774
|
+
return [str.padEnd(length, padding)]
|
|
775
|
+
}
|
|
776
|
+
const chunks = [];
|
|
777
|
+
for (let i = 0; i < str.length; i += length) {
|
|
778
|
+
const splitted = str.substring(i, i + length);
|
|
779
|
+
chunks.push(splitted.padEnd(length, padding));
|
|
780
|
+
}
|
|
781
|
+
return chunks
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
/**
|
|
785
|
+
* Splits a string into chunks using the specified markers.
|
|
786
|
+
* 1. If no markers are provided, defaults to comma (',').
|
|
787
|
+
* 2. null/undefined values are ignored
|
|
788
|
+
* 3. empty array "[]" returned, if string does not include any markers
|
|
789
|
+
* @param {string} str - The string to split.
|
|
790
|
+
* @param {...string} markers - The markers to split the string by.
|
|
791
|
+
* @returns {Array<string>} An array of string chunks.
|
|
792
|
+
* @throws {Error} If the input is not a string.
|
|
793
|
+
*/
|
|
794
|
+
function split (str, ...markers) {
|
|
795
|
+
assertString(str);
|
|
796
|
+
const strLength = str.length;
|
|
797
|
+
if (strLength === 0) {
|
|
798
|
+
return []
|
|
799
|
+
}
|
|
800
|
+
const workingMarkers = [...markers];
|
|
801
|
+
if (markers.length === 0) {
|
|
802
|
+
markers.push(',');
|
|
803
|
+
}
|
|
804
|
+
const markerPostionPairs = findMarkerPositionsRegex(str, ...workingMarkers);
|
|
805
|
+
if (markerPostionPairs.length === 0) {
|
|
806
|
+
return []
|
|
807
|
+
}
|
|
808
|
+
const chunks = [];
|
|
809
|
+
let chunk = '';
|
|
810
|
+
let startIndex = 0;
|
|
811
|
+
for (const { marker, index: endIndex } of markerPostionPairs) {
|
|
812
|
+
chunk = str.substring(startIndex, endIndex);
|
|
813
|
+
chunks.push(chunk);
|
|
814
|
+
startIndex = endIndex + marker.length;
|
|
815
|
+
}
|
|
816
|
+
// add rested into chunks
|
|
817
|
+
chunk = str.substring(startIndex);
|
|
818
|
+
chunks.push(chunk);
|
|
819
|
+
return chunks
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
/**
|
|
823
|
+
* Finds all positions of markers in a string.
|
|
824
|
+
* 1. use loop to iterate over markers
|
|
825
|
+
* 2. use sting.indexOf to find position of each marker
|
|
826
|
+
* @param {string} str - The input string to search in.
|
|
827
|
+
* @param {...string} markers - The markers to search for.
|
|
828
|
+
* @returns {Array<{marker: string, index: number}>} Array of objects containing marker and its index.
|
|
829
|
+
* @throws {Error} If `str` is not a string or no markers are provided.
|
|
830
|
+
*/
|
|
831
|
+
function findMarkerPositions (str, ...markers) {
|
|
832
|
+
assertString(str);
|
|
833
|
+
if (markers.length === 0) {
|
|
834
|
+
throw new Error('At least one marker must be provided')
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
const positions = [];
|
|
838
|
+
for (const marker of new Set(markers)) { // filter duplicated markers
|
|
839
|
+
if (isEmpty(marker)) { // 'abc'.indexOf('') === 0
|
|
840
|
+
continue
|
|
841
|
+
}
|
|
842
|
+
assertString(marker);
|
|
843
|
+
let index = str.indexOf(marker);
|
|
844
|
+
while (index !== -1) {
|
|
845
|
+
positions.push({ marker, index });
|
|
846
|
+
index = str.indexOf(marker, index + marker.length);
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
// Sort positions by index in ascending order
|
|
850
|
+
positions.sort((p1, p2) => p1.index - p2.index);
|
|
851
|
+
return positions
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
/**
|
|
855
|
+
* Finds all positions of markers in a string.
|
|
856
|
+
* 1. Finds all positions of markers in a string using regular expressions.
|
|
857
|
+
* 2. Each marker is included as a separate capture group in a single regex.
|
|
858
|
+
* @param {string} str - The input string to search in.
|
|
859
|
+
* @param {...string} markers - The markers to search for.
|
|
860
|
+
* @returns {Array<{marker: string, index: number}>} Array of objects containing marker and its index.
|
|
861
|
+
* @throws {Error} If `str` is not a string or no markers are provided.
|
|
862
|
+
*/
|
|
863
|
+
function findMarkerPositionsRegex (str, ...markers) {
|
|
864
|
+
assertString(str);
|
|
865
|
+
if (markers.length === 0) {
|
|
866
|
+
throw new Error('At least one marker must be provided')
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
// filter duplicated, empty markers
|
|
870
|
+
// Escape special regex characters in markers
|
|
871
|
+
const escapedMarkers = [...new Set(markers.filter(v => v != null))].map(marker => {
|
|
872
|
+
assertString(marker);
|
|
873
|
+
return marker.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
874
|
+
});
|
|
875
|
+
|
|
876
|
+
// Create regex pattern with each marker as a separate capture group
|
|
877
|
+
// Format: (marker1)|(marker2)|(marker3)...
|
|
878
|
+
const pattern = new RegExp(escapedMarkers.map(m => `(${m})`).join('|'), 'g');
|
|
879
|
+
|
|
880
|
+
const positions = [];
|
|
881
|
+
let match = null;
|
|
882
|
+
|
|
883
|
+
// Find all matches
|
|
884
|
+
while ((match = pattern.exec(str)) !== null) {
|
|
885
|
+
// Determine which marker was matched by checking which capture group has a value
|
|
886
|
+
for (let i = 1; i < match.length; i++) {
|
|
887
|
+
if (match[i]) {
|
|
888
|
+
positions.push({
|
|
889
|
+
marker: markers[i - 1], // Use the original marker, not the escaped version
|
|
890
|
+
index: match.index
|
|
891
|
+
});
|
|
892
|
+
break // Only one group will match for each exec call
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
// Avoid infinite loops with zero-width matches
|
|
897
|
+
if (match[0].length === 0) {
|
|
898
|
+
pattern.lastIndex++;
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
return positions
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
/**
|
|
906
|
+
* Returns the substring before the first occurrence of a marker.
|
|
907
|
+
* @param {string} str - The input string to search in.
|
|
908
|
+
* @param {string} marker - The string to search for.
|
|
909
|
+
* @returns {string | undefined} The substring before the marker, or undefined if marker not found.
|
|
910
|
+
* @throws {Error} If either input is not a string.
|
|
911
|
+
*/
|
|
912
|
+
function substringBefore (str, marker) {
|
|
913
|
+
assertString(str);
|
|
914
|
+
assertString(marker);
|
|
915
|
+
if (str.length === 0 || marker.length === 0) {
|
|
916
|
+
return
|
|
917
|
+
}
|
|
918
|
+
const index = str.indexOf(marker);
|
|
919
|
+
if (index === -1) {
|
|
920
|
+
return undefined
|
|
921
|
+
}
|
|
922
|
+
return str.substring(0, index)
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
/**
|
|
926
|
+
* Returns the substring before the last occurrence of a marker.
|
|
927
|
+
* @param {string} str
|
|
928
|
+
* @param {string} marker
|
|
929
|
+
* @returns {string | undefined}The substring before the last marker, or undefined if marker not found.
|
|
930
|
+
* @throws {Error} If either input is not a string.
|
|
931
|
+
*/
|
|
932
|
+
function substringBeforeLast (str, marker) {
|
|
933
|
+
assertString(str);
|
|
934
|
+
assertString(marker);
|
|
935
|
+
if (str.length === 0 || marker.length === 0) {
|
|
936
|
+
return
|
|
937
|
+
}
|
|
938
|
+
const index = str.lastIndexOf(marker);
|
|
939
|
+
if (index === -1) {
|
|
940
|
+
return
|
|
941
|
+
}
|
|
942
|
+
return str.substring(0, index)
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
/**
|
|
946
|
+
* Returns the substring after the first occurrence of the specified marker.
|
|
947
|
+
* If the marker is not found, returns an empty string.
|
|
948
|
+
*
|
|
949
|
+
* @param {string} str - The string to search in
|
|
950
|
+
* @param {string} marker - The string to search for
|
|
951
|
+
* @returns {string | undefined} The substring after the marker, or undefined if not found
|
|
952
|
+
*/
|
|
953
|
+
function substringAfter (str, marker) {
|
|
954
|
+
assertString(str);
|
|
955
|
+
assertString(marker);
|
|
956
|
+
if (str.length === 0 || marker.length === 0) {
|
|
957
|
+
return
|
|
958
|
+
}
|
|
959
|
+
const index = str.indexOf(marker);
|
|
960
|
+
if (index === -1) {
|
|
961
|
+
return
|
|
962
|
+
}
|
|
963
|
+
return str.substring(index + marker.length)
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
/**
|
|
967
|
+
* Returns the substring after the last occurrence of a marker in a string.
|
|
968
|
+
* @param {string} str - The input string to search in.
|
|
969
|
+
* @param {string} marker - The marker to search for.
|
|
970
|
+
* @returns {string|undefined} The substring after the last marker, or undefined if marker not found.
|
|
971
|
+
*/
|
|
972
|
+
function substringAfterLast (str, marker) {
|
|
973
|
+
assertString(str);
|
|
974
|
+
assertString(marker);
|
|
975
|
+
if (str.length === 0 || marker.length === 0) {
|
|
976
|
+
return
|
|
977
|
+
}
|
|
978
|
+
const index = str.lastIndexOf(marker);
|
|
979
|
+
if (index === -1) {
|
|
980
|
+
return
|
|
981
|
+
}
|
|
982
|
+
return str.substring(index + marker.length)
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
/**
|
|
986
|
+
* Extracts the substring between the specified start and end markers in a string.
|
|
987
|
+
* 1. NOT Greedy, substring between the FIRST startMarker and FIRST endMarker
|
|
988
|
+
* 2. undefined returned, if Not Found neither startMarker nor endMarker
|
|
989
|
+
* @param {string} str - The input string to search within
|
|
990
|
+
* @param {string} startMarker - The starting marker string
|
|
991
|
+
* @param {string} endMarker - The ending marker string
|
|
992
|
+
* @returns {string|undefined} The substring between markers, or undefined if markers not found
|
|
993
|
+
* @throws {Error} If any input is not a string.
|
|
994
|
+
*/
|
|
995
|
+
function substringBetween (str, startMarker, endMarker) {
|
|
996
|
+
assertNotEmpty(str);
|
|
997
|
+
assertNotEmpty(startMarker);
|
|
998
|
+
assertNotEmpty(endMarker);
|
|
999
|
+
const startIndex = str.indexOf(startMarker);
|
|
1000
|
+
if (startIndex === -1) {
|
|
1001
|
+
return
|
|
1002
|
+
}
|
|
1003
|
+
const endIndex = str.indexOf(endMarker, startIndex + startMarker.length);
|
|
1004
|
+
if (endIndex === -1) {
|
|
1005
|
+
return
|
|
1006
|
+
}
|
|
1007
|
+
return str.substring(startIndex + startMarker.length, endIndex)
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
/**
|
|
1011
|
+
* Extracts the substring between the first occurrence of `startMarker` and the last occurrence of `endMarker` in `str`.
|
|
1012
|
+
* 1. Greedy, substring between the FIRST startMarker and LAST endMarker
|
|
1013
|
+
* 2. undefined returned, if Not Found neither startMarker nor endMarker
|
|
1014
|
+
* @param {string} str - The input string to search.
|
|
1015
|
+
* @param {string} startMarker - The starting marker.
|
|
1016
|
+
* @param {string} endMarker - The ending marker.
|
|
1017
|
+
* @returns {string|undefined} The substring between markers, or `undefined` if markers are not found.
|
|
1018
|
+
* @throws {Error} If any input is not a string.
|
|
1019
|
+
*/
|
|
1020
|
+
function substringBetweenGreedy (str, startMarker, endMarker) {
|
|
1021
|
+
assertNotEmpty(str);
|
|
1022
|
+
assertNotEmpty(startMarker);
|
|
1023
|
+
assertNotEmpty(endMarker);
|
|
1024
|
+
const startIndex = str.indexOf(startMarker);
|
|
1025
|
+
if (startIndex === -1) {
|
|
1026
|
+
return
|
|
1027
|
+
}
|
|
1028
|
+
const endIndex = str.lastIndexOf(endMarker);
|
|
1029
|
+
if (endIndex === -1 || endIndex <= startIndex) {
|
|
1030
|
+
return
|
|
1031
|
+
}
|
|
1032
|
+
return str.substring(startIndex + startMarker.length, endIndex)
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
/**
|
|
1036
|
+
* Extracts all substrings between specified start and end markers in a string.
|
|
1037
|
+
* 1. NOT Greedy
|
|
1038
|
+
* @param {string} str - The input string to search within
|
|
1039
|
+
* @param {string} startMarker - The substring marking the start of extraction
|
|
1040
|
+
* @param {string} endMarker - The substring marking the end of extraction
|
|
1041
|
+
* @returns {string[]} Array of all found substrings between markers, empty array "[]" returned if not found
|
|
1042
|
+
* @throws {Error} If any input is not a string
|
|
1043
|
+
*/
|
|
1044
|
+
function substringsBetween (str, startMarker, endMarker) {
|
|
1045
|
+
assertNotEmpty(str);
|
|
1046
|
+
assertNotEmpty(startMarker);
|
|
1047
|
+
assertNotEmpty(endMarker);
|
|
1048
|
+
const substrings = [];
|
|
1049
|
+
let start = 0;
|
|
1050
|
+
while (true) {
|
|
1051
|
+
const index = str.indexOf(startMarker, start);
|
|
1052
|
+
if (index === -1) {
|
|
1053
|
+
break
|
|
1054
|
+
}
|
|
1055
|
+
const endIndex = str.indexOf(endMarker, index + startMarker.length);
|
|
1056
|
+
if (endIndex === -1) {
|
|
1057
|
+
break
|
|
1058
|
+
}
|
|
1059
|
+
substrings.push(str.substring(index + startMarker.length, endIndex));
|
|
1060
|
+
start = endIndex + endMarker.length;
|
|
1061
|
+
}
|
|
1062
|
+
return substrings
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
/**
|
|
1066
|
+
* Executes a task silently, suppressing any errors or rejections.
|
|
1067
|
+
* @param {Function} task - The export function to execute.
|
|
1068
|
+
* @returns {Promise<*>|*} The return value of the task, or a Promise if the task is asynchronous.
|
|
1069
|
+
*/
|
|
1070
|
+
function quiet (task) {
|
|
1071
|
+
assertFunction(task);
|
|
1072
|
+
try {
|
|
1073
|
+
const rtnVal = task();
|
|
1074
|
+
if (isPromise(rtnVal)) {
|
|
1075
|
+
return rtnVal.catch(() => {
|
|
1076
|
+
// do nothing
|
|
1077
|
+
})
|
|
1078
|
+
}
|
|
1079
|
+
return rtnVal
|
|
1080
|
+
} catch (e) {
|
|
1081
|
+
// do nothing
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
/**
|
|
1086
|
+
* Executes a task quietly, capturing any errors and passing them to the errorKeeper.
|
|
1087
|
+
* 1. Handles both synchronous and asynchronous (Promise) tasks.
|
|
1088
|
+
* @param {Function} task - The export function to execute.
|
|
1089
|
+
* @param {Error[]} errorKeeper - The array to store any caught errors.
|
|
1090
|
+
* @returns {Promise<*>|*} The return value of the task, or a Promise if the task is asynchronous.
|
|
1091
|
+
*/
|
|
1092
|
+
function quietKeepError (task, errorKeeper) {
|
|
1093
|
+
assertFunction(task);
|
|
1094
|
+
assertArray(errorKeeper);
|
|
1095
|
+
try {
|
|
1096
|
+
const rtnVal = task();
|
|
1097
|
+
if (isPromise(rtnVal)) {
|
|
1098
|
+
// @ts-ignore
|
|
1099
|
+
return rtnVal.catch(e => errorKeeper.push(e))
|
|
1100
|
+
}
|
|
1101
|
+
return rtnVal
|
|
1102
|
+
} catch (e) {
|
|
1103
|
+
// @ts-ignore
|
|
1104
|
+
errorKeeper.push(e);
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
var ExecUtils = {
|
|
1108
|
+
quiet,
|
|
1109
|
+
quietKeepError
|
|
1110
|
+
};
|
|
1111
|
+
|
|
1112
|
+
// @ts-nocheck
|
|
1113
|
+
|
|
1114
|
+
/**
|
|
1115
|
+
* @module PromiseUtils
|
|
1116
|
+
* @description Promise utility functions for enhanced promise handling, including timeout, delay, parallel execution, and series execution.
|
|
1117
|
+
*/
|
|
1118
|
+
var PromiseUtils = {
|
|
1119
|
+
defer,
|
|
1120
|
+
delay,
|
|
1121
|
+
timeout,
|
|
1122
|
+
allSettled,
|
|
1123
|
+
returnValuePromised,
|
|
1124
|
+
series,
|
|
1125
|
+
seriesAllSettled,
|
|
1126
|
+
parallel,
|
|
1127
|
+
parallelAllSettled
|
|
1128
|
+
};
|
|
1129
|
+
|
|
1130
|
+
/**
|
|
1131
|
+
* Creates a "Deferred" Object with Timeout support
|
|
1132
|
+
* 1. timeout=-1, it means no timeout check
|
|
1133
|
+
* @param {number} [timeout=-1] - Timeout duration in milliseconds
|
|
1134
|
+
* @param {string} [timeoutMessage]
|
|
1135
|
+
* @returns {{promise: Promise<*>, reject: function, resolve: function}}
|
|
1136
|
+
*/
|
|
1137
|
+
function defer (timeout = -1, timeoutMessage) {
|
|
1138
|
+
assertNumber(timeout);
|
|
1139
|
+
const rtnVal = {};
|
|
1140
|
+
|
|
1141
|
+
let timerHandler;
|
|
1142
|
+
if (timeout >= 0) {
|
|
1143
|
+
rtnVal.timerHandler = timerHandler = setTimeout(() => {
|
|
1144
|
+
clearTimeout(timerHandler); // must clear it
|
|
1145
|
+
rtnVal.timerCleared = true; // easy to check in test case
|
|
1146
|
+
rtnVal.reject(new Error(timeoutMessage ?? `Promise Timeout: ${timeout}ms`));
|
|
1147
|
+
}, timeout);
|
|
1148
|
+
rtnVal.timerHandler = timerHandler;
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
rtnVal.promise = new Promise((resolve, reject) => {
|
|
1152
|
+
rtnVal.resolve = (...args) => {
|
|
1153
|
+
if (timerHandler != null) {
|
|
1154
|
+
clearTimeout(timerHandler); // must clear it
|
|
1155
|
+
rtnVal.timerCleared = true; // easy to check in test case
|
|
1156
|
+
}
|
|
1157
|
+
rtnVal.resolved = true;
|
|
1158
|
+
resolve(...args);
|
|
1159
|
+
};
|
|
1160
|
+
|
|
1161
|
+
rtnVal.reject = (err) => {
|
|
1162
|
+
if (timerHandler != null) {
|
|
1163
|
+
clearTimeout(timerHandler); // must clear it
|
|
1164
|
+
rtnVal.timerCleared = true; // easy to check in test case
|
|
1165
|
+
}
|
|
1166
|
+
rtnVal.rejected = true;
|
|
1167
|
+
reject(err);
|
|
1168
|
+
};
|
|
1169
|
+
});
|
|
1170
|
+
rtnVal.promise.cancel = () => {
|
|
1171
|
+
if (timerHandler != null) {
|
|
1172
|
+
clearTimeout(timerHandler); // must clear it
|
|
1173
|
+
rtnVal.timerCleared = true; // easy to check in test case
|
|
1174
|
+
}
|
|
1175
|
+
rtnVal.rejected = true; // easy to check in test case
|
|
1176
|
+
rtnVal.canceled = rtnVal.promise.canceled = true; // easy to check in test case
|
|
1177
|
+
rtnVal.reject(new Error('Cancelled'));
|
|
1178
|
+
};
|
|
1179
|
+
return rtnVal
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
/**
|
|
1183
|
+
* Creates a timeout wrapper around a promise that rejects if the promise doesn't resolve within the given time.
|
|
1184
|
+
* @param {Promise<*>} promise - The promise to wrap with a timeout
|
|
1185
|
+
* @param {number} [time=1] - Timeout duration in milliseconds
|
|
1186
|
+
* @param {string} [message=`Promise Timeout: ${time}ms`] - Custom rejection message
|
|
1187
|
+
* @returns {Promise<*>} A new promise that either resolves with the original promise's value, rejects with original promise's error or timeout error
|
|
1188
|
+
* @throws {TypeError} If input is not a promise or timeout is not a number
|
|
1189
|
+
*/
|
|
1190
|
+
function timeout (promise, time, message) {
|
|
1191
|
+
assertPromise(promise);
|
|
1192
|
+
|
|
1193
|
+
time = time ?? 1;
|
|
1194
|
+
assertNumber(time);
|
|
1195
|
+
|
|
1196
|
+
const deferred = defer(time, message);
|
|
1197
|
+
const startTs = Date.now();
|
|
1198
|
+
promise.then((...args) => { // original promise settled, but timeout
|
|
1199
|
+
const elapsed = Date.now() - startTs;
|
|
1200
|
+
if (elapsed <= time) {
|
|
1201
|
+
deferred.resolve(...args);
|
|
1202
|
+
} else {
|
|
1203
|
+
deferred.reject(new Error(message ?? `Promise Timeout: ${time}ms`));
|
|
1204
|
+
}
|
|
1205
|
+
}).catch((err) => {
|
|
1206
|
+
// prevent double reject
|
|
1207
|
+
!deferred.resolved && !deferred.rejected && deferred.reject(err);
|
|
1208
|
+
});
|
|
1209
|
+
return deferred.promise
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1212
|
+
/**
|
|
1213
|
+
* Excutes All promises in parallel and returns an array of results.
|
|
1214
|
+
*
|
|
1215
|
+
* Why:
|
|
1216
|
+
* 1. Promise.allSettled() returns
|
|
1217
|
+
* * { status: 'fulfilled', value: any } when promise fulfills
|
|
1218
|
+
* * { status: 'rejected', reason: any } when promise rejects
|
|
1219
|
+
* 2. It's NOT convenient to use Promise.allSettled() to get the results of all promises.
|
|
1220
|
+
* * the data structure is not consistent when fullfilled or rejected
|
|
1221
|
+
* * have to check "string" type of status to know sucess or failure
|
|
1222
|
+
* @param {Promise} promises
|
|
1223
|
+
* @returns {Array<{ok: boolean, result: any}>}
|
|
1224
|
+
*/
|
|
1225
|
+
async function allSettled (promises) {
|
|
1226
|
+
assertArray(promises);
|
|
1227
|
+
const results = await Promise.allSettled(promises);
|
|
1228
|
+
const rtnVal = [];
|
|
1229
|
+
for (const result of results) {
|
|
1230
|
+
if (result.status === 'fulfilled') {
|
|
1231
|
+
rtnVal.push({ ok: true, result: result.value });
|
|
1232
|
+
}
|
|
1233
|
+
if (result.status === 'rejected') {
|
|
1234
|
+
rtnVal.push({ ok: false, result: result.reason });
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
return rtnVal
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
/**
|
|
1241
|
+
* Execute the task Function, and ensure it returns a Promise.
|
|
1242
|
+
* @param {function} task
|
|
1243
|
+
* @returns {Promise<*>}
|
|
1244
|
+
*/
|
|
1245
|
+
function returnValuePromised (task) {
|
|
1246
|
+
try {
|
|
1247
|
+
const taskRtnVal = task();
|
|
1248
|
+
if (isPromise(taskRtnVal)) {
|
|
1249
|
+
return taskRtnVal
|
|
1250
|
+
}
|
|
1251
|
+
return Promise.resolve(taskRtnVal)
|
|
1252
|
+
} catch (e) {
|
|
1253
|
+
return Promise.reject(e)
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
/**
|
|
1258
|
+
* Delays a promise by a specified time.
|
|
1259
|
+
* 1. delay(), wait 1ms
|
|
1260
|
+
* 2. delay(1000), wait 1000ms
|
|
1261
|
+
* 3. delay(promise), after promise settled, wait 1000ms
|
|
1262
|
+
* 4. delay(promise, 2000), after promise settled, wait 2000ms
|
|
1263
|
+
*
|
|
1264
|
+
* @param {Promise<*>|number|undefined} [promise] - The input promise to delay
|
|
1265
|
+
* @param {number|undefined} [ms] - Minimum delay in milliseconds (default: 1)
|
|
1266
|
+
* @returns {Promise} A new promise that settles after the delay period
|
|
1267
|
+
*/
|
|
1268
|
+
function delay (promise, ms) {
|
|
1269
|
+
if (isNumber(promise)) {
|
|
1270
|
+
ms = promise;
|
|
1271
|
+
promise = Promise.resolve();
|
|
1272
|
+
} else if (promise == null && ms == null) {
|
|
1273
|
+
ms = 1;
|
|
1274
|
+
promise = Promise.resolve();
|
|
1275
|
+
}
|
|
1276
|
+
promise != null && assertPromise(promise);
|
|
1277
|
+
ms = ms ?? 1000;
|
|
1278
|
+
assertNumber(ms);
|
|
1279
|
+
const deferred = defer();
|
|
1280
|
+
const startTs = Date.now();
|
|
1281
|
+
promise
|
|
1282
|
+
.then((...args) => {
|
|
1283
|
+
const escaped = Date.now() - startTs;
|
|
1284
|
+
if (escaped < ms) {
|
|
1285
|
+
setTimeout(() => deferred.resolve(...args), ms - escaped);
|
|
1286
|
+
} else {
|
|
1287
|
+
deferred.resolve(...args);
|
|
1288
|
+
}
|
|
1289
|
+
})
|
|
1290
|
+
.catch((err) => {
|
|
1291
|
+
const escaped = Date.now() - startTs;
|
|
1292
|
+
if (escaped < ms) {
|
|
1293
|
+
setTimeout(() => deferred.reject(err), ms - escaped);
|
|
1294
|
+
} else {
|
|
1295
|
+
deferred.reject(err);
|
|
1296
|
+
}
|
|
1297
|
+
});
|
|
1298
|
+
return deferred.promise
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
/**
|
|
1302
|
+
* Fast-Fail mode to execute Tasks(functions) in series (one after another) and returns their results in order.
|
|
1303
|
+
* 1. export function are executed one by one
|
|
1304
|
+
* 2. Fast Fail: if any tasks fail, the whole chain is rejected with the first error
|
|
1305
|
+
* 3. if an element is not function, rejects the whole chain with Error(Not Function)
|
|
1306
|
+
* @param {Function[]} promises
|
|
1307
|
+
* @returns {Promise<Array>} Promise that resolves with an array of results in the same order as input tasks
|
|
1308
|
+
*/
|
|
1309
|
+
async function series (tasks) {
|
|
1310
|
+
assertArray(tasks);
|
|
1311
|
+
const results = [];
|
|
1312
|
+
for (const task of tasks) {
|
|
1313
|
+
assertFunction(task);
|
|
1314
|
+
results.push(await task());
|
|
1315
|
+
}
|
|
1316
|
+
return results
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1319
|
+
/**
|
|
1320
|
+
* AllSettled Mode to execute Tasks(functions) in series (one after another) and returns their results in order.
|
|
1321
|
+
* 1. tasks are executed one by one
|
|
1322
|
+
* 2. Each result is an object with `ok` (boolean) and `result` (resolved value or error).
|
|
1323
|
+
* 3. if a task is not Function, rejects the whole chain with Error(Not Function)
|
|
1324
|
+
* @param {Function[]} tasks
|
|
1325
|
+
* @returns {Promise<Array<{ok: boolean, result: *}>>}
|
|
1326
|
+
*/
|
|
1327
|
+
async function seriesAllSettled (tasks) {
|
|
1328
|
+
assertArray(tasks);
|
|
1329
|
+
const results = [];
|
|
1330
|
+
for (const task of tasks) {
|
|
1331
|
+
assertFunction(task);
|
|
1332
|
+
try {
|
|
1333
|
+
results.push({ ok: true, result: await task() });
|
|
1334
|
+
} catch (err) {
|
|
1335
|
+
results.push({ ok: false, result: err });
|
|
1336
|
+
}
|
|
1337
|
+
}
|
|
1338
|
+
return results
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
/**
|
|
1342
|
+
* FastFail Mode to Execute tasks in parallel with a maximum concurrency limit
|
|
1343
|
+
* 1. tasks are executed in parallel with a maximum concurrency limit
|
|
1344
|
+
* 2. rejects whole chain with the first error, when first task fails
|
|
1345
|
+
* @param {Function[]} tasks
|
|
1346
|
+
* @param {number} [maxParallel=5]
|
|
1347
|
+
* @returns {Promise<Array>} Array of resolved values from all promises
|
|
1348
|
+
* @throws {TypeError} If input is not an array of export function or maxParallel is not a number
|
|
1349
|
+
*/
|
|
1350
|
+
async function parallel (tasks, maxParallel = 5) {
|
|
1351
|
+
assertArray(tasks);
|
|
1352
|
+
assertNumber(maxParallel);
|
|
1353
|
+
if (maxParallel <= 0) {
|
|
1354
|
+
throw new Error(`Invalid maxParallel: ${maxParallel}, should > 0`)
|
|
1355
|
+
}
|
|
1356
|
+
tasks.forEach((task) => assertFunction(task));
|
|
1357
|
+
const rtnVal = [];
|
|
1358
|
+
// once for all, run all tasks
|
|
1359
|
+
if (tasks.length <= maxParallel) {
|
|
1360
|
+
const resultsForBatch = await Promise.all(tasks.map(task => returnValuePromised(task)));
|
|
1361
|
+
rtnVal.push(...resultsForBatch);
|
|
1362
|
+
return rtnVal
|
|
1363
|
+
}
|
|
1364
|
+
// run group by MaxParallel
|
|
1365
|
+
const tasksToRun = [];
|
|
1366
|
+
for (const task of tasks) {
|
|
1367
|
+
assertFunction(task);
|
|
1368
|
+
tasksToRun.push(task);
|
|
1369
|
+
if (tasksToRun.length >= maxParallel) {
|
|
1370
|
+
const resultsForBatch = await Promise.all(tasksToRun.map(task => returnValuePromised(task)));
|
|
1371
|
+
rtnVal.push(...resultsForBatch);
|
|
1372
|
+
tasksToRun.length = 0;
|
|
1373
|
+
}
|
|
1374
|
+
}
|
|
1375
|
+
// Run all rested
|
|
1376
|
+
if (tasksToRun.length > 0 && tasksToRun.length < maxParallel) {
|
|
1377
|
+
const resultsForBatch = await Promise.all(tasksToRun.map(task => returnValuePromised(task)));
|
|
1378
|
+
rtnVal.push(...resultsForBatch);
|
|
1379
|
+
}
|
|
1380
|
+
return rtnVal
|
|
1381
|
+
}
|
|
1382
|
+
|
|
1383
|
+
/**
|
|
1384
|
+
* AllSettled Mode to execute tasks in parallel with a maximum concurrency limit
|
|
1385
|
+
* 1. tasks are executed in parallel with a maximum concurrency limit
|
|
1386
|
+
* 2. all tasks will be executed, even some of them failed.
|
|
1387
|
+
* @param {Function[]} tasks
|
|
1388
|
+
* @param {number} [maxParallel=5] - Maximum number of tasks to run in parallel
|
|
1389
|
+
* @returns {Promise<Array>} Array of resolved values from all promises
|
|
1390
|
+
* @throws {TypeError} If input is not an array of export function or maxParallel is not a number
|
|
1391
|
+
*/
|
|
1392
|
+
async function parallelAllSettled (tasks, maxParallel = 5) {
|
|
1393
|
+
assertArray(tasks);
|
|
1394
|
+
assertNumber(maxParallel);
|
|
1395
|
+
if (maxParallel <= 0) {
|
|
1396
|
+
throw new Error(`Invalid maxParallel: ${maxParallel}, should > 0`)
|
|
1397
|
+
}
|
|
1398
|
+
tasks.forEach((task) => assertFunction(task));
|
|
1399
|
+
const rtnVal = [];
|
|
1400
|
+
// once for all, run all promises
|
|
1401
|
+
if (tasks.length <= maxParallel) {
|
|
1402
|
+
const resultsForBatch = await allSettled(tasks.map(task => returnValuePromised(task)));
|
|
1403
|
+
rtnVal.push(...resultsForBatch);
|
|
1404
|
+
return rtnVal
|
|
1405
|
+
}
|
|
1406
|
+
// run group by MaxParallel
|
|
1407
|
+
const tasksToRun = [];
|
|
1408
|
+
for (const task of tasks) {
|
|
1409
|
+
assertFunction(task);
|
|
1410
|
+
tasksToRun.push(task);
|
|
1411
|
+
if (tasksToRun.length >= maxParallel) {
|
|
1412
|
+
const resultsForBatch = await allSettled(tasksToRun.map(task => returnValuePromised(task)));
|
|
1413
|
+
rtnVal.push(...resultsForBatch);
|
|
1414
|
+
tasksToRun.length = 0;
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
// Run all rested
|
|
1418
|
+
if (tasksToRun.length > 0 && tasksToRun.length < maxParallel) {
|
|
1419
|
+
const resultsForBatch = await allSettled(tasksToRun.map(task => returnValuePromised(task)));
|
|
1420
|
+
rtnVal.push(...resultsForBatch);
|
|
1421
|
+
}
|
|
1422
|
+
return rtnVal
|
|
1423
|
+
}
|
|
1424
|
+
|
|
1425
|
+
// owned
|
|
1426
|
+
|
|
1427
|
+
const { isPlainObject } = TypeUtils;
|
|
1428
|
+
|
|
1429
|
+
/**
|
|
1430
|
+
* @module ClassProxyUtils
|
|
1431
|
+
*/
|
|
1432
|
+
var ClassProxyUtils = {
|
|
1433
|
+
proxy: proxy$1,
|
|
1434
|
+
newProxyInstance
|
|
1435
|
+
};
|
|
1436
|
+
|
|
1437
|
+
/**
|
|
1438
|
+
* Creates a Proxy Class for given class.
|
|
1439
|
+
* @template {object} T
|
|
1440
|
+
* @param {Function} cls - The class to proxycls - The class to proxy
|
|
1441
|
+
* @param {ProxyHandler<T>} [propertyHandler = {}] - Proxy Property Handler
|
|
1442
|
+
* @param {boolean} [sealed=true] - Whether to seal the instance
|
|
1443
|
+
* @returns {Function} A proxied classA proxied instance of the class
|
|
1444
|
+
*/
|
|
1445
|
+
function proxy$1 (cls, propertyHandler, sealed = true) {
|
|
1446
|
+
if (typeof cls !== 'function') {
|
|
1447
|
+
throw new TypeError(`Not Class: type=${typeof cls}, value=${JSON.stringify(cls)}`)
|
|
1448
|
+
}
|
|
1449
|
+
if (propertyHandler != null) {
|
|
1450
|
+
if (!isPlainObject(propertyHandler)) {
|
|
1451
|
+
throw new TypeError(`Not PropertyHandler: type=${typeof propertyHandler}, value=${JSON.stringify(propertyHandler)}`)
|
|
1452
|
+
}
|
|
1453
|
+
const { get, set } = propertyHandler;
|
|
1454
|
+
if (get != null && typeof get !== 'function') {
|
|
1455
|
+
throw new TypeError(`Not PropertyHandler.get: type=${typeof get}, value=${JSON.stringify(get)}`)
|
|
1456
|
+
}
|
|
1457
|
+
if (set != null && typeof set !== 'function') {
|
|
1458
|
+
throw new TypeError(`Not PropertyHandler.set: type=${typeof set}, value=${JSON.stringify(set)}`)
|
|
1459
|
+
}
|
|
1460
|
+
}
|
|
1461
|
+
const construcHandler = {
|
|
1462
|
+
/**
|
|
1463
|
+
* Creates a proxied instance of a class, optionally sealing it.
|
|
1464
|
+
* @param {Function} constructor - The class constructor to instantiate.
|
|
1465
|
+
* @param {any[]} args - Arguments to pass to the constructor.
|
|
1466
|
+
* @param {Function} [newTarget] - The constructor that was originally called by `new`.
|
|
1467
|
+
* @returns {Proxy} A proxied instance of the constructed class.
|
|
1468
|
+
*/
|
|
1469
|
+
construct (constructor, args, newTarget) {
|
|
1470
|
+
const clsInstance = Reflect.construct(constructor, args);
|
|
1471
|
+
return new Proxy(sealed ? Object.preventExtensions(clsInstance) : clsInstance, propertyHandler ?? {})
|
|
1472
|
+
}
|
|
1473
|
+
};
|
|
1474
|
+
return new Proxy(cls, construcHandler)
|
|
1475
|
+
}
|
|
1476
|
+
|
|
1477
|
+
/**
|
|
1478
|
+
* Creates a proxied instance of a class with custom property handling.
|
|
1479
|
+
* @template {object} T
|
|
1480
|
+
* @param {Function} cls - The class to proxy
|
|
1481
|
+
* @param {any[]} args - Arguments to pass to the constructor
|
|
1482
|
+
* @param {ProxyHandler<T>} propertyHandler - Handler for property access/mutation
|
|
1483
|
+
* @param {boolean} [sealed=true] - Whether the proxy should be sealed
|
|
1484
|
+
* @returns {T} Proxied class instance
|
|
1485
|
+
*/
|
|
1486
|
+
function newProxyInstance (cls, args, propertyHandler, sealed = true) {
|
|
1487
|
+
const proxyCls = proxy$1(cls, propertyHandler, sealed);
|
|
1488
|
+
return Reflect.construct(proxyCls, args ?? [])
|
|
1489
|
+
}
|
|
1490
|
+
|
|
1491
|
+
/**
|
|
1492
|
+
* @module InstanceProxyUtils
|
|
1493
|
+
*/
|
|
1494
|
+
|
|
1495
|
+
/**
|
|
1496
|
+
* Creates a proxy wrapper around an object instance with optional property handler.
|
|
1497
|
+
* @template {object} T
|
|
1498
|
+
* @param {T} instance - The target object to proxy
|
|
1499
|
+
* @param {ProxyHandler<T>} [propertyHandler] - Optional proxy handler for property access
|
|
1500
|
+
* @param {boolean} [sealed=true] - Whether to prevent extensions on the target object
|
|
1501
|
+
* @returns {T} The proxied object instance
|
|
1502
|
+
* @throws {TypeError} If instance is not an object
|
|
1503
|
+
*/
|
|
1504
|
+
function proxy (instance, propertyHandler, sealed = true) {
|
|
1505
|
+
if (isNil(instance) || !isObject(instance) || isArray(instance)) {
|
|
1506
|
+
throw new TypeError(`Not Object: type=${typeof instance}, value=${JSON.stringify(instance)}`)
|
|
1507
|
+
}
|
|
1508
|
+
return new Proxy(sealed ? Object.preventExtensions(instance) : instance, propertyHandler ?? {})
|
|
1509
|
+
}
|
|
1510
|
+
|
|
1511
|
+
var InstanceProxyUtils = {
|
|
1512
|
+
proxy
|
|
1513
|
+
};
|
|
1514
|
+
|
|
1515
|
+
/**
|
|
1516
|
+
* @module Lang
|
|
1517
|
+
* @description Core language utilities for type checking, string manipulation, and common operations.
|
|
1518
|
+
*/
|
|
1519
|
+
|
|
1520
|
+
|
|
1521
|
+
var index = {
|
|
1522
|
+
LangUtils,
|
|
1523
|
+
StringUtils,
|
|
1524
|
+
TypeUtils,
|
|
1525
|
+
TypeAssert,
|
|
1526
|
+
ExecUtils,
|
|
1527
|
+
PromiseUtils,
|
|
1528
|
+
Lang: LangUtils,
|
|
1529
|
+
Type: TypeUtils,
|
|
1530
|
+
Exec: ExecUtils,
|
|
1531
|
+
ClassProxyUtils,
|
|
1532
|
+
InstanceProxyUtils
|
|
1533
|
+
};
|
|
1534
|
+
|
|
1535
|
+
exports.ClassProxyUtils = ClassProxyUtils;
|
|
1536
|
+
exports.Exec = ExecUtils;
|
|
1537
|
+
exports.ExecUtils = ExecUtils;
|
|
1538
|
+
exports.InstanceProxyUtils = InstanceProxyUtils;
|
|
1539
|
+
exports.Lang = LangUtils;
|
|
1540
|
+
exports.LangUtils = LangUtils;
|
|
1541
|
+
exports.PromiseUtils = PromiseUtils;
|
|
1542
|
+
exports.StringUtils = StringUtils;
|
|
1543
|
+
exports.Type = TypeUtils;
|
|
1544
|
+
exports.TypeAssert = TypeAssert;
|
|
1545
|
+
exports.TypeUtils = TypeUtils;
|
|
1546
|
+
exports.default = index;
|
|
1547
|
+
//# sourceMappingURL=index-dev.cjs.map
|