@creejs/commons-lang 2.1.19 → 2.1.21
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 +349 -58
- package/dist/cjs/index-dev.cjs.map +1 -1
- package/dist/cjs/index-min.cjs +1 -1
- package/dist/cjs/index-min.cjs.map +1 -1
- package/dist/esm/index-dev.js +348 -59
- package/dist/esm/index-dev.js.map +1 -1
- package/dist/esm/index-min.js +1 -1
- package/dist/esm/index-min.js.map +1 -1
- package/dist/umd/index.dev.js +349 -58
- package/dist/umd/index.dev.js.map +1 -1
- package/dist/umd/index.min.js +1 -1
- package/dist/umd/index.min.js.map +1 -1
- package/package.json +1 -1
- package/types/_error.d.ts +30 -0
- package/types/aggregated-error.d.ts +34 -0
- package/types/array-utils.d.ts +8 -0
- package/types/index.d.ts +5 -1
- package/types/promise-utils.d.ts +49 -11
- package/types/string-utils.d.ts +8 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export default class _Error extends Error {
|
|
2
|
+
/**
|
|
3
|
+
* Creates and returns a 404 Not Found error instance with the given message.
|
|
4
|
+
* @param {string} message - The error message to include.
|
|
5
|
+
* @returns {_Error} A new Error instance with status code 404.
|
|
6
|
+
*/
|
|
7
|
+
static notFound(message: string): _Error;
|
|
8
|
+
static notImpled(): _Error;
|
|
9
|
+
/**
|
|
10
|
+
* Creates and returns a new Error instance for not supported operations.
|
|
11
|
+
* @param {string} message - The error message to include.
|
|
12
|
+
* @returns {_Error} A new Error instance with "Not Supported:" prefix and 505 status code.
|
|
13
|
+
*/
|
|
14
|
+
static notSupported(message: string): _Error;
|
|
15
|
+
/**
|
|
16
|
+
* Checks if the given error is a "Not Supported" error.
|
|
17
|
+
* @param {Error|_Error|{[key:string]:any}|unknown} err - The error to check
|
|
18
|
+
* @returns {boolean} True if the error is a Not Supported error (code 505), false otherwise
|
|
19
|
+
*/
|
|
20
|
+
static isNotSupported(err: Error | _Error | {
|
|
21
|
+
[key: string]: any;
|
|
22
|
+
} | unknown): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Error constructor with custom message and code.
|
|
25
|
+
* @param {string} message - Error message
|
|
26
|
+
* @param {number|string} code - Error code
|
|
27
|
+
*/
|
|
28
|
+
constructor(message: string, code: number | string);
|
|
29
|
+
code: string | number;
|
|
30
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export default class AggregatedError extends Error {
|
|
2
|
+
/**
|
|
3
|
+
* Checks if the given error is an AggregatedErrorLike
|
|
4
|
+
* 1. have "message:string" property
|
|
5
|
+
* 2. have "errors:Error[]" property
|
|
6
|
+
* @param {any} err - The error to check
|
|
7
|
+
* @returns {boolean} True if the error is an AggregatedError, false otherwise
|
|
8
|
+
*/
|
|
9
|
+
static isAggregatedErrorLike(err: any): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Checks if the given error is an instance of AggregatedError.
|
|
12
|
+
* @param {Error} err - The error to check.
|
|
13
|
+
* @returns {boolean} True if the error is an AggregatedError, false otherwise.
|
|
14
|
+
*/
|
|
15
|
+
static isAggregatedError(err: Error): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Error constructor with custom message and code.
|
|
18
|
+
* @param {string} message - Error message
|
|
19
|
+
* @param {any[]} [errors] - Errors
|
|
20
|
+
*/
|
|
21
|
+
constructor(message: string, errors?: any[]);
|
|
22
|
+
errors: any[];
|
|
23
|
+
/**
|
|
24
|
+
* Adds an error to the errors collection.
|
|
25
|
+
* @param {any} err - The error object to be added.
|
|
26
|
+
*/
|
|
27
|
+
addError(err: any): void;
|
|
28
|
+
/**
|
|
29
|
+
* Removes a specific error from the errors array.
|
|
30
|
+
* @param {Error} err - The error instance to remove.
|
|
31
|
+
* @returns {boolean} True if the error was found and removed, false otherwise.
|
|
32
|
+
*/
|
|
33
|
+
removeError(err: Error): boolean;
|
|
34
|
+
}
|
package/types/array-utils.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
declare namespace _default {
|
|
2
2
|
export { first };
|
|
3
|
+
export { chunk };
|
|
3
4
|
export { last };
|
|
4
5
|
export { equals };
|
|
5
6
|
export { equalsIgnoreOrder };
|
|
@@ -13,6 +14,13 @@ export default _default;
|
|
|
13
14
|
* @throws {Error} if arr is not an array
|
|
14
15
|
*/
|
|
15
16
|
export function first(arr: any[], defaultValue?: any): any;
|
|
17
|
+
/**
|
|
18
|
+
* Splits an array into chunks of the specified size.
|
|
19
|
+
* @param {any[]} array - The array to be chunked.
|
|
20
|
+
* @param {number} size - The size of each chunk.
|
|
21
|
+
* @returns {any[]} An array of arrays containing the chunks.
|
|
22
|
+
*/
|
|
23
|
+
export function chunk(array: any[], size: number): any[];
|
|
16
24
|
/**
|
|
17
25
|
* Gets the last element of an array
|
|
18
26
|
* @param {any[]} arr - The input array
|
package/types/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
declare namespace _default {
|
|
2
|
+
export { _Error };
|
|
3
|
+
export { AggregatedError };
|
|
2
4
|
export { LangUtils };
|
|
3
5
|
export { StringUtils };
|
|
4
6
|
export { TypeUtils };
|
|
@@ -17,6 +19,8 @@ declare namespace _default {
|
|
|
17
19
|
export { ArrayUtils };
|
|
18
20
|
}
|
|
19
21
|
export default _default;
|
|
22
|
+
import _Error from './_error.js';
|
|
23
|
+
import AggregatedError from './aggregated-error.js';
|
|
20
24
|
import LangUtils from './lang-utils.js';
|
|
21
25
|
import StringUtils from './string-utils.js';
|
|
22
26
|
import TypeUtils from './type-utils.js';
|
|
@@ -30,4 +34,4 @@ import TypedArrayUtils from './typed-array-utils.js';
|
|
|
30
34
|
import ArrayBufferUtils from './array-buffer-utils.js';
|
|
31
35
|
import TimeUtils from './time-utils.js';
|
|
32
36
|
import ArrayUtils from './array-utils.js';
|
|
33
|
-
export { LangUtils, StringUtils, TypeUtils, TypeAssert, ExecUtils, PromiseUtils, LangUtils as Lang, TypeUtils as Type, ExecUtils as Exec, ClassProxyUtils, InstanceProxyUtils, ReflectUtils, TypedArrayUtils, ArrayBufferUtils, TimeUtils, ArrayUtils };
|
|
37
|
+
export { _Error, AggregatedError, LangUtils, StringUtils, TypeUtils, TypeAssert, ExecUtils, PromiseUtils, LangUtils as Lang, TypeUtils as Type, ExecUtils as Exec, ClassProxyUtils, InstanceProxyUtils, ReflectUtils, TypedArrayUtils, ArrayBufferUtils, TimeUtils, ArrayUtils };
|
package/types/promise-utils.d.ts
CHANGED
|
@@ -51,14 +51,34 @@ export function returnValuePromised(task: Function): Promise<any>;
|
|
|
51
51
|
*/
|
|
52
52
|
export function delay(promise?: Promise<any> | number | undefined, ms?: number | undefined): Promise<any>;
|
|
53
53
|
/**
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
54
|
+
* 1. run all tasks
|
|
55
|
+
* 2. any Task succeed, return its result
|
|
56
|
+
* * resolve with the result.
|
|
57
|
+
* * the others tasks will run to its end, and results will be dropped.
|
|
58
|
+
* 3. If all tasks fail, rejects with an array of errors. the array length is same as the input tasks
|
|
59
|
+
* @param {Array<Promise<any>|Function>} tasks - Array of promises or async functions to execute
|
|
60
|
+
* @returns {Promise<any>} A promise that resolves with the result of the first successful task
|
|
61
|
+
*/
|
|
62
|
+
export function any(tasks: Array<Promise<any> | Function>): Promise<any>;
|
|
63
|
+
/**
|
|
64
|
+
* Execute Tasks(functions) in series (one after another) and returns their results in order.
|
|
65
|
+
* 1. Tasks are executed one by one
|
|
66
|
+
* * if task is a function, execute it.
|
|
67
|
+
* * if task is a promise, wait for it to settle.
|
|
68
|
+
* 2. Fast Fail: if any tasks fail, the whole chain is rejected with the first error
|
|
69
|
+
* 3. if an element is not function, rejects the whole chain with Error(Not Function)
|
|
70
|
+
* 4. All Tasks run successfully, Return Results Array, it's length is same as the input tasks
|
|
71
|
+
* @param {Array<Promise<any>|Function>} tasks
|
|
72
|
+
* @returns {Promise<any[]>} Promise that resolves with an array of results in the same order as input tasks
|
|
73
|
+
*/
|
|
74
|
+
export function series(tasks: Array<Promise<any> | Function>): Promise<any[]>;
|
|
75
|
+
/**
|
|
76
|
+
* Resolves with the first successfully completed task from the given array of tasks.
|
|
77
|
+
* If all tasks fail, rejects with an array of errors.
|
|
78
|
+
* @param {Array<Promise<any>|Function>} tasks - Array of promises or async functions to execute
|
|
79
|
+
* @returns {Promise<any>} A promise that resolves with the result of the first successful task
|
|
80
|
+
*/
|
|
81
|
+
export function seriesAny(tasks: Array<Promise<any> | Function>): Promise<any>;
|
|
62
82
|
/**
|
|
63
83
|
* AllSettled Mode to execute Tasks(functions) in series (one after another) and returns their results in order.
|
|
64
84
|
* 1. tasks are executed one by one
|
|
@@ -87,10 +107,24 @@ export function parallel(tasks: Function[], maxParallel?: number): Promise<any[]
|
|
|
87
107
|
* 2. all tasks will be executed, even some of them failed.
|
|
88
108
|
* @param {Function[]} tasks
|
|
89
109
|
* @param {number} [maxParallel=5] - Maximum number of tasks to run in parallel
|
|
90
|
-
* @returns {Promise<any[]>}
|
|
110
|
+
* @returns {Promise<{ok: boolean, result: any}[]>}
|
|
91
111
|
* @throws {TypeError} If input is not an array of export function or maxParallel is not a number
|
|
92
112
|
*/
|
|
93
|
-
export function parallelAllSettled(tasks: Function[], maxParallel?: number): Promise<
|
|
113
|
+
export function parallelAllSettled(tasks: Function[], maxParallel?: number): Promise<{
|
|
114
|
+
ok: boolean;
|
|
115
|
+
result: any;
|
|
116
|
+
}[]>;
|
|
117
|
+
/**
|
|
118
|
+
* Executes multiple async tasks in parallel with limited concurrency,
|
|
119
|
+
* 1. resolving when any task completes successfully.
|
|
120
|
+
* 2. Maybe multiple tasks are executed as a bulk block, and all of them resolved.
|
|
121
|
+
* * only the first fulfilled value is returned
|
|
122
|
+
* * other results are dropped
|
|
123
|
+
* @param {Array<Function|Promise<any>>} tasks - Array of async functions to execute
|
|
124
|
+
* @param {number} [maxParallel=5] - Maximum number of tasks to run in parallel
|
|
125
|
+
* @returns {Promise<any>} Resolves with the result of the first successfully completed task
|
|
126
|
+
*/
|
|
127
|
+
export function parallelAny(tasks: Array<Function | Promise<any>>, maxParallel?: number): Promise<any>;
|
|
94
128
|
/**
|
|
95
129
|
* Creates a "Waiter" Object
|
|
96
130
|
* 1. wait the specified time
|
|
@@ -100,6 +134,7 @@ export function parallelAllSettled(tasks: Function[], maxParallel?: number): Pro
|
|
|
100
134
|
*/
|
|
101
135
|
export function wait(waitTime: number): Waiter;
|
|
102
136
|
declare namespace _default {
|
|
137
|
+
export { any };
|
|
103
138
|
export { defer };
|
|
104
139
|
export { delay };
|
|
105
140
|
export { timeout };
|
|
@@ -108,6 +143,7 @@ declare namespace _default {
|
|
|
108
143
|
export { series };
|
|
109
144
|
export { seriesAllSettled };
|
|
110
145
|
export { parallel };
|
|
146
|
+
export { parallelAny };
|
|
111
147
|
export { parallelAllSettled };
|
|
112
148
|
export { wait };
|
|
113
149
|
}
|
|
@@ -116,6 +152,7 @@ export type Deferred = {
|
|
|
116
152
|
promise: Promise<any>;
|
|
117
153
|
timerHandler: NodeJS.Timeout;
|
|
118
154
|
timerCleared: boolean;
|
|
155
|
+
pending: boolean;
|
|
119
156
|
resolved: boolean;
|
|
120
157
|
rejected: boolean;
|
|
121
158
|
canceled: boolean;
|
|
@@ -125,5 +162,6 @@ export type Deferred = {
|
|
|
125
162
|
export type Waiter = {
|
|
126
163
|
promise: Promise<any>;
|
|
127
164
|
timerHandler: NodeJS.Timeout;
|
|
128
|
-
|
|
165
|
+
_resolve: (...args: any[]) => void;
|
|
166
|
+
wakeup: () => void;
|
|
129
167
|
};
|
package/types/string-utils.d.ts
CHANGED
|
@@ -150,6 +150,13 @@ export function substringBetweenGreedy(str: string, startMarker: string, endMark
|
|
|
150
150
|
* @throws {Error} If any input is not a string
|
|
151
151
|
*/
|
|
152
152
|
export function substringsBetween(str: string, startMarker: string, endMarker: string): string[];
|
|
153
|
+
/**
|
|
154
|
+
* Safely converts a value to its string representation.
|
|
155
|
+
* Attempts to use JSON.stringify first, falls back to toString() if stringify fails.
|
|
156
|
+
* @param {*} value - The value to convert to string
|
|
157
|
+
* @returns {string} The string representation of the value
|
|
158
|
+
*/
|
|
159
|
+
export function safeToString(value: any): string;
|
|
153
160
|
declare namespace _default {
|
|
154
161
|
export { isEmpty };
|
|
155
162
|
export { assertNotEmpty };
|
|
@@ -168,5 +175,6 @@ declare namespace _default {
|
|
|
168
175
|
export { substringBetween };
|
|
169
176
|
export { substringBetweenGreedy };
|
|
170
177
|
export { substringsBetween };
|
|
178
|
+
export { safeToString };
|
|
171
179
|
}
|
|
172
180
|
export default _default;
|