@creejs/commons-lang 1.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/README.md +2 -0
- package/index.js +2 -0
- package/lib/exec-utils.js +58 -0
- package/lib/index.js +26 -0
- package/lib/lang-utils.js +110 -0
- package/lib/promise-utils.js +317 -0
- package/lib/string-utils.js +428 -0
- package/lib/type-assert.js +253 -0
- package/lib/type-utils.js +290 -0
- package/package.json +31 -0
- package/types/exec-utils.d.ts +14 -0
- package/types/index.d.ts +7 -0
- package/types/lang-utils.d.ts +58 -0
- package/types/lang.d.ts +5 -0
- package/types/promise-utils.d.ts +94 -0
- package/types/string-utils.d.ts +152 -0
- package/types/type-assert.d.ts +91 -0
- package/types/type-asset.d.ts +84 -0
- package/types/type-utils.d.ts +158 -0
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @module TypeUtils
|
|
5
|
+
* @description Utility functions for type checking and validation.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Checks if the given value is an array.
|
|
10
|
+
* @param {*} value - The value to check.
|
|
11
|
+
* @returns {boolean} True if the value is an array, false otherwise.
|
|
12
|
+
*/
|
|
13
|
+
function isArray (value) {
|
|
14
|
+
return Array.isArray(value)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Checks if the given value is a boolean.
|
|
19
|
+
* @param {*} value - The value to check.
|
|
20
|
+
* @returns {boolean} True if the value is a boolean, false otherwise.
|
|
21
|
+
*/
|
|
22
|
+
function isBoolean (value) {
|
|
23
|
+
return typeof value === 'boolean'
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Checks if the given value is a Buffer.
|
|
28
|
+
* @param {*} value - The value to check.
|
|
29
|
+
* @returns {boolean} True if the value is a Buffer, false otherwise.
|
|
30
|
+
*/
|
|
31
|
+
function isBuffer (value) {
|
|
32
|
+
return value != null && Buffer.isBuffer(value)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Checks if the given value is a Date.
|
|
37
|
+
* @param {*} value - The value to check.
|
|
38
|
+
* @returns {boolean} True if the value is a Date, false otherwise.
|
|
39
|
+
*/
|
|
40
|
+
function isDate (value) {
|
|
41
|
+
return value != null && value instanceof Date
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Checks if the given value is an instance of Error.
|
|
46
|
+
* @param {*} value - The value to check.
|
|
47
|
+
* @returns {boolean} True if the value is an Error, false otherwise.
|
|
48
|
+
*/
|
|
49
|
+
function isError (value) {
|
|
50
|
+
return value != null && value instanceof Error
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Checks if the given value is a function.
|
|
55
|
+
* @param {*} value - The value to check.
|
|
56
|
+
* @returns {boolean} True if the value is a function, false otherwise.
|
|
57
|
+
*/
|
|
58
|
+
function isFunction (value) {
|
|
59
|
+
return typeof value === 'function'
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Checks if a value is a class instance (non-null and not a plain object).
|
|
64
|
+
* @param {*} value - The value to check.
|
|
65
|
+
* @returns {boolean} True if the value is a class instance, false otherwise.
|
|
66
|
+
*/
|
|
67
|
+
function isInstance (value) {
|
|
68
|
+
return value != null && typeof value === 'object' && !TypeUtils.isPlainObject(value)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Checks if a value is isIterable
|
|
73
|
+
* @param {*} value - The value to check.
|
|
74
|
+
* @returns {boolean} True if the value is isIterable, false otherwise.
|
|
75
|
+
*/
|
|
76
|
+
function isIterable (value) {
|
|
77
|
+
return value != null && typeof value[Symbol.iterator] === 'function'
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Checks if a value is Map
|
|
82
|
+
* @param {*} value - The value to check.
|
|
83
|
+
* @returns {boolean} True if the value is Map, otherwise false.
|
|
84
|
+
*/
|
|
85
|
+
function isMap (value) {
|
|
86
|
+
return value != null && typeof value === 'object' && value.constructor === Map
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Checks if a value is WeakMap
|
|
91
|
+
* @param {*} value - The value to check.
|
|
92
|
+
* @returns {boolean} True if the value is WeakMap, otherwise false.
|
|
93
|
+
*/
|
|
94
|
+
function isWeakMap (value) {
|
|
95
|
+
return value != null && typeof value === 'object' && value.constructor === WeakMap
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Checks if a value is null or undefined.
|
|
100
|
+
* 1. value == null
|
|
101
|
+
* 2. return true, if value is null or undefined
|
|
102
|
+
* @param {*} value - The value to check.
|
|
103
|
+
* @returns {boolean} True if the value is null or undefined, otherwise false.
|
|
104
|
+
*/
|
|
105
|
+
function isNil (value) {
|
|
106
|
+
return value == null
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Checks if a value is null or undefined.
|
|
111
|
+
* 1. same with isNil()
|
|
112
|
+
* @param {*} value - The value to check.
|
|
113
|
+
* @returns {boolean} True if the value is null or undefined, otherwise false.
|
|
114
|
+
*/
|
|
115
|
+
function isNullOrUndefined (value) {
|
|
116
|
+
return value == null
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* check that a value is a positive number.
|
|
121
|
+
* @param {number} value - The value to check.
|
|
122
|
+
* @returns {boolean}
|
|
123
|
+
*/
|
|
124
|
+
function isPositive (value) {
|
|
125
|
+
if (!isNumber(value)) {
|
|
126
|
+
return false
|
|
127
|
+
}
|
|
128
|
+
return value > 0
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* check that a value is a Negative number.
|
|
133
|
+
* @param {number} value - The value to check.
|
|
134
|
+
*/
|
|
135
|
+
function isNegative (value) {
|
|
136
|
+
if (!isNumber(value)) {
|
|
137
|
+
return false
|
|
138
|
+
}
|
|
139
|
+
return value < 0
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Checks if the given value is exactly null.
|
|
144
|
+
* @param {*} value - The value to check.
|
|
145
|
+
* @returns {boolean} True if the value is null, false otherwise.
|
|
146
|
+
*/
|
|
147
|
+
function isNull (value) {
|
|
148
|
+
return value === null
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Checks if a value is exactly undefined.
|
|
153
|
+
* @param {*} value - The value to check.
|
|
154
|
+
* @returns {boolean} True if the value is undefined, false otherwise.
|
|
155
|
+
*/
|
|
156
|
+
function isUndefined (value) {
|
|
157
|
+
return value === undefined
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Checks if a value is a number.
|
|
162
|
+
* @param {*} value - The value to check.
|
|
163
|
+
* @returns {boolean} True if the value is a number, false otherwise.
|
|
164
|
+
*/
|
|
165
|
+
function isNumber (value) {
|
|
166
|
+
return value != null && typeof value === 'number'
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Checks if a value is an object (and not null).
|
|
171
|
+
* @param {*} value - The value to check
|
|
172
|
+
* @returns {boolean} True if the value is an object (not null), false otherwise
|
|
173
|
+
*/
|
|
174
|
+
function isObject (value) {
|
|
175
|
+
return value != null && typeof value === 'object'
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Checks if a value is a plain object (created by the Object constructor).
|
|
180
|
+
* @param {*} value - The value to check.
|
|
181
|
+
* @returns {boolean} True if the value is a plain object, false otherwise.
|
|
182
|
+
*/
|
|
183
|
+
function isPlainObject (value) {
|
|
184
|
+
return value !== null && typeof value === 'object' && (value.constructor === Object || value.constructor === undefined)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* check if value is primitive: string, number, boolean
|
|
189
|
+
* 1. null/undefined returns false
|
|
190
|
+
* @param {*} value
|
|
191
|
+
* @returns {boolean}
|
|
192
|
+
*/
|
|
193
|
+
function isPrimitive (value) {
|
|
194
|
+
return value !== null && (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean')
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Checks if a value is a Promise.
|
|
199
|
+
* @param {*} value - The value to check.
|
|
200
|
+
* @returns {boolean} True if the value is a Promise, false otherwise.
|
|
201
|
+
*/
|
|
202
|
+
function isPromise (value) {
|
|
203
|
+
return value != null && typeof value.then === 'function'
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Checks if a RegExp
|
|
208
|
+
* @param {*} value - The value to check.
|
|
209
|
+
* @returns {boolean} True if the value is RegExp, otherwise false.
|
|
210
|
+
*/
|
|
211
|
+
function isRegExp (value) {
|
|
212
|
+
return value != null && typeof value === 'object' && value.constructor === RegExp
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Checks if a Set
|
|
217
|
+
* @param {*} value - The value to check.
|
|
218
|
+
* @returns {boolean} True if the value is Set, otherwise false.
|
|
219
|
+
*/
|
|
220
|
+
function isSet (value) {
|
|
221
|
+
return value != null && typeof value === 'object' && value.constructor === Set
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Checks if a WeakSet
|
|
226
|
+
* @param {*} value - The value to check.
|
|
227
|
+
* @returns {boolean} True if the value is WeakSet, otherwise false.
|
|
228
|
+
*/
|
|
229
|
+
function isWeakSet (value) {
|
|
230
|
+
return value != null && typeof value === 'object' && value.constructor === WeakSet
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Check if the value is a string
|
|
235
|
+
* @param {*} value
|
|
236
|
+
* @return {boolean}
|
|
237
|
+
*/
|
|
238
|
+
function isStream (value) {
|
|
239
|
+
return value != null && typeof value.pipe === 'function'
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Check if the value is a string
|
|
244
|
+
* @param {*} value
|
|
245
|
+
* @return {boolean}
|
|
246
|
+
*/
|
|
247
|
+
function isString (value) {
|
|
248
|
+
return value != null && typeof value === 'string'
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Checks if the given value is a Symbol.
|
|
253
|
+
* @param {*} value - The value to check.
|
|
254
|
+
* @returns {boolean} True if the value is a Symbol, false otherwise.
|
|
255
|
+
*/
|
|
256
|
+
function isSymbol (value) {
|
|
257
|
+
return value != null && typeof value === 'symbol'
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const TypeUtils = {
|
|
261
|
+
isArray,
|
|
262
|
+
isBoolean,
|
|
263
|
+
isBuffer,
|
|
264
|
+
isFunction,
|
|
265
|
+
isInstance,
|
|
266
|
+
isIterable,
|
|
267
|
+
isDate,
|
|
268
|
+
isError,
|
|
269
|
+
isMap,
|
|
270
|
+
isWeakMap,
|
|
271
|
+
isNumber,
|
|
272
|
+
isPositive,
|
|
273
|
+
isNegative,
|
|
274
|
+
isNil,
|
|
275
|
+
isNullOrUndefined,
|
|
276
|
+
isNull,
|
|
277
|
+
isUndefined,
|
|
278
|
+
isPlainObject,
|
|
279
|
+
isObject,
|
|
280
|
+
isPromise,
|
|
281
|
+
isRegExp,
|
|
282
|
+
isSet,
|
|
283
|
+
isWeakSet,
|
|
284
|
+
isStream,
|
|
285
|
+
isString,
|
|
286
|
+
isSymbol,
|
|
287
|
+
isPrimitive
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
module.exports = TypeUtils
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@creejs/commons-lang",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Commons Utils About Language",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"files": [
|
|
8
|
+
"index.js",
|
|
9
|
+
"lib/",
|
|
10
|
+
"types/",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
13
|
+
"publishConfig": {
|
|
14
|
+
"access": "public"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/frameworkee/commons.git"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"dts": "tsc",
|
|
22
|
+
"generate-docs": "jsdoc -c ./jsdoc.json"
|
|
23
|
+
},
|
|
24
|
+
"author": "rodney.vin@gmail.com",
|
|
25
|
+
"license": "Apache-2.0",
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"better-docs": "^2.7.3",
|
|
28
|
+
"jsdoc": "^4.0.4"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {}
|
|
31
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Executes a task silently, suppressing any errors or rejections.
|
|
3
|
+
* @param {Function} task - The function to execute.
|
|
4
|
+
* @returns {Promise<*>|*} The return value of the task, or a Promise if the task is asynchronous.
|
|
5
|
+
*/
|
|
6
|
+
export function quiet(task: Function): Promise<any> | any;
|
|
7
|
+
/**
|
|
8
|
+
* Executes a task quietly, capturing any errors and passing them to the errorKeeper.
|
|
9
|
+
* 1. Handles both synchronous and asynchronous (Promise) tasks.
|
|
10
|
+
* @param {Function} task - The function to execute.
|
|
11
|
+
* @param {Error[]} errorKeeper - The array to store any caught errors.
|
|
12
|
+
* @returns {Promise<*>|*} The return value of the task, or a Promise if the task is asynchronous.
|
|
13
|
+
*/
|
|
14
|
+
export function quietKeepError(task: Function, errorKeeper: Error[]): Promise<any> | any;
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import LangUtils = require("./lang-utils");
|
|
2
|
+
import StringUtils = require("./string-utils");
|
|
3
|
+
import TypeUtils = require("./type-utils");
|
|
4
|
+
import TypeAssert = require("./type-assert");
|
|
5
|
+
import ExecUtils = require("./exec-utils");
|
|
6
|
+
import PromiseUtils = require("./promise-utils");
|
|
7
|
+
export { LangUtils, StringUtils, TypeUtils, TypeAssert, ExecUtils, PromiseUtils, LangUtils as Lang, StringUtils as String, TypeUtils as Type, ExecUtils as Exec };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module LangUtils
|
|
3
|
+
* @description Language utility functions
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Gets the constructor name of a value.
|
|
7
|
+
* @param {*} value - The value to check.
|
|
8
|
+
* @returns {string|undefined} The constructor name, or undefined if value has no constructor.
|
|
9
|
+
*/
|
|
10
|
+
export function constructorName(value: any): string | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* Assigns default values from source objects to target object for undefined properties.
|
|
13
|
+
* @param {Object<string, any>} target - The target object to assign defaults to
|
|
14
|
+
* @param {...Object<string, any>} sources - Source objects containing default values
|
|
15
|
+
* @returns {Object<string, any>} The modified target object with defaults applied
|
|
16
|
+
* @throws {TypeError} If target is null or undefined
|
|
17
|
+
*/
|
|
18
|
+
export function defaults(target: {
|
|
19
|
+
[x: string]: any;
|
|
20
|
+
}, ...sources: {
|
|
21
|
+
[x: string]: any;
|
|
22
|
+
}[]): {
|
|
23
|
+
[x: string]: any;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Extends a target object with properties from one or more source objects.
|
|
27
|
+
* @param {Object<string, any>} target - The target object to extend (must not be null/undefined)
|
|
28
|
+
* @param {...Object<string, any>} sources - One or more source objects to copy properties from
|
|
29
|
+
* @returns {Object<string, any>} The modified target object
|
|
30
|
+
* @throws {TypeError} If target is null or undefined
|
|
31
|
+
*/
|
|
32
|
+
export function extend(target: {
|
|
33
|
+
[x: string]: any;
|
|
34
|
+
}, ...sources: {
|
|
35
|
+
[x: string]: any;
|
|
36
|
+
}[]): {
|
|
37
|
+
[x: string]: any;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Compares two values for equality
|
|
41
|
+
* 1. First checks strict equality (===),
|
|
42
|
+
* 2. then checks if either value has an `equals` method and uses it.
|
|
43
|
+
* @param {*} value1 - First value to compare
|
|
44
|
+
* @param {*} value2 - Second value to compare
|
|
45
|
+
* @returns {boolean} True if values are equal, false otherwise
|
|
46
|
+
*/
|
|
47
|
+
export function equals(value1: any, value2: any): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Check if the current environment is a browser
|
|
50
|
+
* @returns {boolean}
|
|
51
|
+
*/
|
|
52
|
+
export function isBrowser(): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Check if the current environment is a nodejs
|
|
55
|
+
* @returns {boolean}
|
|
56
|
+
*/
|
|
57
|
+
export function isNode(): boolean;
|
|
58
|
+
export { extend as extends };
|
package/types/lang.d.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a "Deferred" Object with Timeout support
|
|
3
|
+
* 1. timeout=-1, it means no timeout check
|
|
4
|
+
* @param {number} [timeout=-1] - Timeout duration in milliseconds
|
|
5
|
+
* @returns {{promise: Promise<*>, reject: function, resolve: function}}
|
|
6
|
+
*/
|
|
7
|
+
export function defer(timeout?: number, timeoutMessage: any): {
|
|
8
|
+
promise: Promise<any>;
|
|
9
|
+
reject: Function;
|
|
10
|
+
resolve: Function;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Delays the resolution or rejection of a promise by a specified time.
|
|
14
|
+
* Ensures the promise settles after at least the given milliseconds.
|
|
15
|
+
* If the original promise settles before the delay, waits for remaining time.
|
|
16
|
+
*
|
|
17
|
+
* @param {Promise} promise - The input promise to delay
|
|
18
|
+
* @param {number} [ms=1000] - Minimum delay in milliseconds (default: 1)
|
|
19
|
+
* @returns {Promise} A new promise that settles after the delay period
|
|
20
|
+
*/
|
|
21
|
+
export function delay(promise: Promise<any>, ms?: number): Promise<any>;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a timeout wrapper around a promise that rejects if the promise doesn't resolve within the given time.
|
|
24
|
+
* @param {Promise<*>} promise - The promise to wrap with a timeout
|
|
25
|
+
* @param {number} [time=1] - Timeout duration in milliseconds
|
|
26
|
+
* @param {string} [message=`Promise Timeout: ${time}ms`] - Custom rejection message
|
|
27
|
+
* @returns {Promise<*>} A new promise that either resolves with the original promise's value, rejects with original promise's error or timeout error
|
|
28
|
+
* @throws {TypeError} If input is not a promise or timeout is not a number
|
|
29
|
+
*/
|
|
30
|
+
export function timeout(promise: Promise<any>, time?: number, message?: string): Promise<any>;
|
|
31
|
+
/**
|
|
32
|
+
* Excutes All promises in parallel and returns an array of results.
|
|
33
|
+
*
|
|
34
|
+
* Why:
|
|
35
|
+
* 1. Promise.allSettled() returns
|
|
36
|
+
* * { status: 'fulfilled', value: any } when promise fulfills
|
|
37
|
+
* * { status: 'rejected', reason: any } when promise rejects
|
|
38
|
+
* 2. It's NOT convenient to use Promise.allSettled() to get the results of all promises.
|
|
39
|
+
* * the data structure is not consistent when fullfilled or rejected
|
|
40
|
+
* * have to check "string" type of status to know sucess or failure
|
|
41
|
+
* @param {Promise} promises
|
|
42
|
+
* @returns {Array<{ok: boolean, result: any}>}
|
|
43
|
+
*/
|
|
44
|
+
export function allSettled(promises: Promise<any>): Array<{
|
|
45
|
+
ok: boolean;
|
|
46
|
+
result: any;
|
|
47
|
+
}>;
|
|
48
|
+
/**
|
|
49
|
+
* Execute the task Function, and ensure it returns a Promise.
|
|
50
|
+
* @param {function} task
|
|
51
|
+
* @returns {Promise<*>}
|
|
52
|
+
*/
|
|
53
|
+
export function returnValuePromised(task: Function): Promise<any>;
|
|
54
|
+
/**
|
|
55
|
+
* Fast-Fail mode to execute Tasks(functions) in series (one after another) and returns their results in order.
|
|
56
|
+
* 1. function are executed one by one
|
|
57
|
+
* 2. Fast Fail: if any tasks fail, the whole chain is rejected with the first error
|
|
58
|
+
* 3. if an element is not function, rejects the whole chain with Error(Not Function)
|
|
59
|
+
* @param {Function[]} promises
|
|
60
|
+
* @returns {Promise<Array>} Promise that resolves with an array of results in the same order as input tasks
|
|
61
|
+
*/
|
|
62
|
+
export function series(tasks: any): Promise<any[]>;
|
|
63
|
+
/**
|
|
64
|
+
* AllSettled Mode to execute Tasks(functions) in series (one after another) and returns their results in order.
|
|
65
|
+
* 1. tasks are executed one by one
|
|
66
|
+
* 2. Each result is an object with `ok` (boolean) and `result` (resolved value or error).
|
|
67
|
+
* 3. if a task is not Function, rejects the whole chain with Error(Not Function)
|
|
68
|
+
* @param {Function[]} tasks
|
|
69
|
+
* @returns {Promise<Array<{ok: boolean, result: *}>>}
|
|
70
|
+
*/
|
|
71
|
+
export function seriesAllSettled(tasks: Function[]): Promise<Array<{
|
|
72
|
+
ok: boolean;
|
|
73
|
+
result: any;
|
|
74
|
+
}>>;
|
|
75
|
+
/**
|
|
76
|
+
* FastFail Mode to Execute tasks in parallel with a maximum concurrency limit
|
|
77
|
+
* 1. tasks are executed in parallel with a maximum concurrency limit
|
|
78
|
+
* 2. rejects whole chain with the first error, when first task fails
|
|
79
|
+
* @param {Function[]} tasks
|
|
80
|
+
* @param {number} [maxParallel=5]
|
|
81
|
+
* @returns {Promise<Array>} Array of resolved values from all promises
|
|
82
|
+
* @throws {TypeError} If input is not an array of function or maxParallel is not a number
|
|
83
|
+
*/
|
|
84
|
+
export function parallel(tasks: Function[], maxParallel?: number): Promise<any[]>;
|
|
85
|
+
/**
|
|
86
|
+
* AllSettled Mode to execute tasks in parallel with a maximum concurrency limit
|
|
87
|
+
* 1. tasks are executed in parallel with a maximum concurrency limit
|
|
88
|
+
* 2. all tasks will be executed, even some of them failed.
|
|
89
|
+
* @param {Function[]} tasks
|
|
90
|
+
* @param {number} [maxParallel=5] - Maximum number of tasks to run in parallel
|
|
91
|
+
* @returns {Promise<Array>} Array of resolved values from all promises
|
|
92
|
+
* @throws {TypeError} If input is not an array of function or maxParallel is not a number
|
|
93
|
+
*/
|
|
94
|
+
export function parallelAllSettled(tasks: Function[], maxParallel?: number): Promise<any[]>;
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if a string is null, undefined, or length is 0.
|
|
3
|
+
* @param {string} str
|
|
4
|
+
* @returns {boolean}
|
|
5
|
+
* @throws {Error} If `str` is not null or undefined, and not a string.
|
|
6
|
+
*/
|
|
7
|
+
export function isEmpty(str: string): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Asserts that the given string is not empty.
|
|
10
|
+
* @param {string} str - The string to check.
|
|
11
|
+
* @throws {Error} Throws an error if the string is empty.
|
|
12
|
+
*/
|
|
13
|
+
export function assertNotEmpty(str: string): void;
|
|
14
|
+
/**
|
|
15
|
+
* Checks if a string is null, undefined, or consists only of whitespace.
|
|
16
|
+
* @param {string} str - The string to check.
|
|
17
|
+
* @returns {boolean} True if the string is blank, false otherwise.
|
|
18
|
+
* @throws {Error} If `str` is not null or undefined, and not a string.
|
|
19
|
+
*/
|
|
20
|
+
export function isBlank(str: string): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Asserts that the given string is not blank.
|
|
23
|
+
* @param {string} str - The string to check.
|
|
24
|
+
* @throws {Error} Throws an error if the string is blank.
|
|
25
|
+
*/
|
|
26
|
+
export function assertNotBlank(str: string): void;
|
|
27
|
+
/**
|
|
28
|
+
* Capitalizes the first character of a string.
|
|
29
|
+
* @param {string} str - The string to capitalize.
|
|
30
|
+
* @returns {string} The capitalized string or original if unchanged.
|
|
31
|
+
*/
|
|
32
|
+
export function capitalize(str: string): string;
|
|
33
|
+
/**
|
|
34
|
+
* Converts the first character of a string to lowercase.
|
|
35
|
+
* If the string is null or empty, returns it unchanged.
|
|
36
|
+
* @param {string} str - The input string to decapitalize.
|
|
37
|
+
* @returns {string} The decapitalized string or original if unchanged.
|
|
38
|
+
*/
|
|
39
|
+
export function decapitalize(str: string): string;
|
|
40
|
+
/**
|
|
41
|
+
* Splits a string into chunks of fixed length, padding the last chunk if needed.
|
|
42
|
+
* 1. if str is empty, returns an empty array "[]"
|
|
43
|
+
* 2. if length is less than string length, returns an array with the string padded with "padding" as the only element
|
|
44
|
+
* 3. the last chunk is padded with "padding" if needed
|
|
45
|
+
* @param {string} str - The string to split.
|
|
46
|
+
* @param {number} length - The desired length of each chunk.
|
|
47
|
+
* @param {string} [padding=' '] - The padding character for the last chunk.
|
|
48
|
+
* @returns {string[]} An array of string chunks with fixed length.
|
|
49
|
+
* @throws {Error} If `str` is not a string or `length` is not a number.
|
|
50
|
+
*/
|
|
51
|
+
export function splitWithFixedLength(str: string, length: number, padding?: string): string[];
|
|
52
|
+
/**
|
|
53
|
+
* Splits a string into chunks using the specified markers.
|
|
54
|
+
* 1. If no markers are provided, defaults to comma (',').
|
|
55
|
+
* 2. null/undefined values are ignored
|
|
56
|
+
* 3. empty array "[]" returned, if string does not include any markers
|
|
57
|
+
* @param {string} str - The string to split.
|
|
58
|
+
* @param {...string} markers - The markers to split the string by.
|
|
59
|
+
* @returns {Array<string>} An array of string chunks.
|
|
60
|
+
* @throws {Error} If the input is not a string.
|
|
61
|
+
*/
|
|
62
|
+
export function split(str: string, ...markers: string[]): Array<string>;
|
|
63
|
+
/**
|
|
64
|
+
* Finds all positions of markers in a string.
|
|
65
|
+
* 1. use loop to iterate over markers
|
|
66
|
+
* 2. use sting.indexOf to find position of each marker
|
|
67
|
+
* @param {string} str - The input string to search in.
|
|
68
|
+
* @param {...string} markers - The markers to search for.
|
|
69
|
+
* @returns {Array<{marker: string, index: number}>} Array of objects containing marker and its index.
|
|
70
|
+
* @throws {Error} If `str` is not a string or no markers are provided.
|
|
71
|
+
*/
|
|
72
|
+
export function findMarkerPositions(str: string, ...markers: string[]): Array<{
|
|
73
|
+
marker: string;
|
|
74
|
+
index: number;
|
|
75
|
+
}>;
|
|
76
|
+
/**
|
|
77
|
+
* Finds all positions of markers in a string.
|
|
78
|
+
* 1. Finds all positions of markers in a string using regular expressions.
|
|
79
|
+
* 2. Each marker is included as a separate capture group in a single regex.
|
|
80
|
+
* @param {string} str - The input string to search in.
|
|
81
|
+
* @param {...string} markers - The markers to search for.
|
|
82
|
+
* @returns {Array<{marker: string, index: number}>} Array of objects containing marker and its index.
|
|
83
|
+
* @throws {Error} If `str` is not a string or no markers are provided.
|
|
84
|
+
*/
|
|
85
|
+
export function findMarkerPositionsRegex(str: string, ...markers: string[]): Array<{
|
|
86
|
+
marker: string;
|
|
87
|
+
index: number;
|
|
88
|
+
}>;
|
|
89
|
+
/**
|
|
90
|
+
* Returns the substring before the first occurrence of a marker.
|
|
91
|
+
* @param {string} str - The input string to search in.
|
|
92
|
+
* @param {string} marker - The string to search for.
|
|
93
|
+
* @returns {string | undefined} The substring before the marker, or undefined if marker not found.
|
|
94
|
+
* @throws {Error} If either input is not a string.
|
|
95
|
+
*/
|
|
96
|
+
export function substringBefore(str: string, marker: string): string | undefined;
|
|
97
|
+
/**
|
|
98
|
+
* Returns the substring before the last occurrence of a marker.
|
|
99
|
+
* @param {string} str
|
|
100
|
+
* @param {string} marker
|
|
101
|
+
* @returns {string | undefined}The substring before the last marker, or undefined if marker not found.
|
|
102
|
+
* @throws {Error} If either input is not a string.
|
|
103
|
+
*/
|
|
104
|
+
export function substringBeforeLast(str: string, marker: string): string | undefined;
|
|
105
|
+
/**
|
|
106
|
+
* Returns the substring after the first occurrence of the specified marker.
|
|
107
|
+
* If the marker is not found, returns an empty string.
|
|
108
|
+
*
|
|
109
|
+
* @param {string} str - The string to search in
|
|
110
|
+
* @param {string} marker - The string to search for
|
|
111
|
+
* @returns {string | undefined} The substring after the marker, or undefined if not found
|
|
112
|
+
*/
|
|
113
|
+
export function substringAfter(str: string, marker: string): string | undefined;
|
|
114
|
+
/**
|
|
115
|
+
* Returns the substring after the last occurrence of a marker in a string.
|
|
116
|
+
* @param {string} str - The input string to search in.
|
|
117
|
+
* @param {string} marker - The marker to search for.
|
|
118
|
+
* @returns {string|undefined} The substring after the last marker, or undefined if marker not found.
|
|
119
|
+
*/
|
|
120
|
+
export function substringAfterLast(str: string, marker: string): string | undefined;
|
|
121
|
+
/**
|
|
122
|
+
* Extracts the substring between the specified start and end markers in a string.
|
|
123
|
+
* 1. NOT Greedy, substring between the FIRST startMarker and FIRST endMarker
|
|
124
|
+
* 2. undefined returned, if Not Found neither startMarker nor endMarker
|
|
125
|
+
* @param {string} str - The input string to search within
|
|
126
|
+
* @param {string} startMarker - The starting marker string
|
|
127
|
+
* @param {string} endMarker - The ending marker string
|
|
128
|
+
* @returns {string|undefined} The substring between markers, or undefined if markers not found
|
|
129
|
+
* @throws {Error} If any input is not a string.
|
|
130
|
+
*/
|
|
131
|
+
export function substringBetween(str: string, startMarker: string, endMarker: string): string | undefined;
|
|
132
|
+
/**
|
|
133
|
+
* Extracts the substring between the first occurrence of `startMarker` and the last occurrence of `endMarker` in `str`.
|
|
134
|
+
* 1. Greedy, substring between the FIRST startMarker and LAST endMarker
|
|
135
|
+
* 2. undefined returned, if Not Found neither startMarker nor endMarker
|
|
136
|
+
* @param {string} str - The input string to search.
|
|
137
|
+
* @param {string} startMarker - The starting marker.
|
|
138
|
+
* @param {string} endMarker - The ending marker.
|
|
139
|
+
* @returns {string|undefined} The substring between markers, or `undefined` if markers are not found.
|
|
140
|
+
* @throws {Error} If any input is not a string.
|
|
141
|
+
*/
|
|
142
|
+
export function substringBetweenGreedy(str: string, startMarker: string, endMarker: string): string | undefined;
|
|
143
|
+
/**
|
|
144
|
+
* Extracts all substrings between specified start and end markers in a string.
|
|
145
|
+
* 1. NOT Greedy
|
|
146
|
+
* @param {string} str - The input string to search within
|
|
147
|
+
* @param {string} startMarker - The substring marking the start of extraction
|
|
148
|
+
* @param {string} endMarker - The substring marking the end of extraction
|
|
149
|
+
* @returns {string[]} Array of all found substrings between markers, empty array "[]" returned if not found
|
|
150
|
+
* @throws {Error} If any input is not a string
|
|
151
|
+
*/
|
|
152
|
+
export function substringsBetween(str: string, startMarker: string, endMarker: string): string[];
|