@creejs/commons-lang 2.1.11 → 2.1.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@creejs/commons-lang",
3
- "version": "2.1.11",
3
+ "version": "2.1.13",
4
4
  "description": "Commons Utils About Language, used inside creejs",
5
5
  "keywords": [
6
6
  "commons",
@@ -0,0 +1,39 @@
1
+ declare namespace _default {
2
+ export { first };
3
+ export { last };
4
+ export { equals };
5
+ export { equalsIgnoreOrder };
6
+ }
7
+ export default _default;
8
+ /**
9
+ * Gets the first element of an array
10
+ * @param {any[]} arr - The input array
11
+ * @param {any} [defaultValue] - The value to return if the array is empty or first element is undefined/null.
12
+ * @returns {any} The first element of the array, or the defaultValue if the array is empty or not an array.
13
+ * @throws {Error} if arr is not an array
14
+ */
15
+ export function first(arr: any[], defaultValue?: any): any;
16
+ /**
17
+ * Gets the last element of an array
18
+ * @param {any[]} arr - The input array
19
+ * @param {any} [defaultValue] - The value to return if the array is empty or last element is undefined/null
20
+ * @returns {any} The last element of the array or the defaultValue
21
+ * @throws {Error} if arr is not an array
22
+ */
23
+ export function last(arr: any[], defaultValue?: any): any;
24
+ /**
25
+ * Checks if two arrays are equal by order
26
+ * @param {any[]} arr1 - first array to compare
27
+ * @param {any[]} arr2 - Second array to compare
28
+ * @param {(a:any, b:any) => 0|1|-1} [compareFn]
29
+ * @returns {boolean} True if arrays have same elements (order-independent), false otherwise
30
+ */
31
+ export function equals(arr1: any[], arr2: any[], compareFn?: (a: any, b: any) => 0 | 1 | -1): boolean;
32
+ /**
33
+ * Checks if two arrays are equal ignoring element order
34
+ * @param {any[]} arr1 - first array to compare
35
+ * @param {any[]} arr2 - Second array to compare
36
+ * @param {(a:any, b:any) => 0|1|-1} [compareFn]
37
+ * @returns {boolean} True if arrays have same elements (order-independent), false otherwise
38
+ */
39
+ export function equalsIgnoreOrder(arr1: any[], arr2: any[], compareFn?: (a: any, b: any) => 0 | 1 | -1): boolean;
package/types/index.d.ts CHANGED
@@ -14,6 +14,7 @@ declare namespace _default {
14
14
  export { TypedArrayUtils };
15
15
  export { ArrayBufferUtils };
16
16
  export { TimeUtils };
17
+ export { ArrayUtils };
17
18
  }
18
19
  export default _default;
19
20
  import LangUtils from './lang-utils.js';
@@ -28,4 +29,5 @@ import ReflectUtils from './reflect-utils.js';
28
29
  import TypedArrayUtils from './typed-array-utils.js';
29
30
  import ArrayBufferUtils from './array-buffer-utils.js';
30
31
  import TimeUtils from './time-utils.js';
31
- export { LangUtils, StringUtils, TypeUtils, TypeAssert, ExecUtils, PromiseUtils, LangUtils as Lang, TypeUtils as Type, ExecUtils as Exec, ClassProxyUtils, InstanceProxyUtils, ReflectUtils, TypedArrayUtils, ArrayBufferUtils, TimeUtils };
32
+ 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 };
@@ -3,13 +3,9 @@
3
3
  * 1. timeout=-1, it means no timeout check
4
4
  * @param {number} [timeout=-1] - Timeout duration in milliseconds
5
5
  * @param {string} [timeoutMessage]
6
- * @returns {{promise: Promise<*>, reject: function, resolve: function}}
6
+ * @returns {Deferred}
7
7
  */
8
- export function defer(timeout?: number, timeoutMessage?: string): {
9
- promise: Promise<any>;
10
- reject: Function;
11
- resolve: Function;
12
- };
8
+ export function defer(timeout?: number, timeoutMessage?: string): Deferred;
13
9
  /**
14
10
  * Creates a timeout wrapper around a promise that rejects if the promise doesn't resolve within the given time.
15
11
  * @param {Promise<*>} promise - The promise to wrap with a timeout
@@ -29,13 +25,13 @@ export function timeout(promise: Promise<any>, time?: number, message?: string):
29
25
  * 2. It's NOT convenient to use Promise.allSettled() to get the results of all promises.
30
26
  * * the data structure is not consistent when fullfilled or rejected
31
27
  * * have to check "string" type of status to know sucess or failure
32
- * @param {Promise} promises
33
- * @returns {Array<{ok: boolean, result: any}>}
28
+ * @param {Promise<*>[]} promises
29
+ * @returns {Promise<{ok: boolean, result: any}[]>}
34
30
  */
35
- export function allSettled(promises: Promise<any>): Array<{
31
+ export function allSettled(promises: Promise<any>[]): Promise<{
36
32
  ok: boolean;
37
33
  result: any;
38
- }>;
34
+ }[]>;
39
35
  /**
40
36
  * Execute the task Function, and ensure it returns a Promise.
41
37
  * @param {function} task
@@ -51,7 +47,7 @@ export function returnValuePromised(task: Function): Promise<any>;
51
47
  *
52
48
  * @param {Promise<*>|number|undefined} [promise] - The input promise to delay
53
49
  * @param {number|undefined} [ms] - Minimum delay in milliseconds (default: 1)
54
- * @returns {Promise} A new promise that settles after the delay period
50
+ * @returns {Promise<*>} A new promise that settles after the delay period
55
51
  */
56
52
  export function delay(promise?: Promise<any> | number | undefined, ms?: number | undefined): Promise<any>;
57
53
  /**
@@ -59,10 +55,10 @@ export function delay(promise?: Promise<any> | number | undefined, ms?: number |
59
55
  * 1. export function are executed one by one
60
56
  * 2. Fast Fail: if any tasks fail, the whole chain is rejected with the first error
61
57
  * 3. if an element is not function, rejects the whole chain with Error(Not Function)
62
- * @param {Function[]} promises
63
- * @returns {Promise<Array>} Promise that resolves with an array of results in the same order as input tasks
58
+ * @param {Function[]} tasks
59
+ * @returns {Promise<any[]>} Promise that resolves with an array of results in the same order as input tasks
64
60
  */
65
- export function series(tasks: any): Promise<any[]>;
61
+ export function series(tasks: Function[]): Promise<any[]>;
66
62
  /**
67
63
  * AllSettled Mode to execute Tasks(functions) in series (one after another) and returns their results in order.
68
64
  * 1. tasks are executed one by one
@@ -81,7 +77,7 @@ export function seriesAllSettled(tasks: Function[]): Promise<Array<{
81
77
  * 2. rejects whole chain with the first error, when first task fails
82
78
  * @param {Function[]} tasks
83
79
  * @param {number} [maxParallel=5]
84
- * @returns {Promise<Array>} Array of resolved values from all promises
80
+ * @returns {Promise<any[]>} Array of resolved values from all promises
85
81
  * @throws {TypeError} If input is not an array of export function or maxParallel is not a number
86
82
  */
87
83
  export function parallel(tasks: Function[], maxParallel?: number): Promise<any[]>;
@@ -91,7 +87,7 @@ export function parallel(tasks: Function[], maxParallel?: number): Promise<any[]
91
87
  * 2. all tasks will be executed, even some of them failed.
92
88
  * @param {Function[]} tasks
93
89
  * @param {number} [maxParallel=5] - Maximum number of tasks to run in parallel
94
- * @returns {Promise<Array>} Array of resolved values from all promises
90
+ * @returns {Promise<any[]>} Array of resolved values from all promises
95
91
  * @throws {TypeError} If input is not an array of export function or maxParallel is not a number
96
92
  */
97
93
  export function parallelAllSettled(tasks: Function[], maxParallel?: number): Promise<any[]>;
@@ -107,3 +103,13 @@ declare namespace _default {
107
103
  export { parallelAllSettled };
108
104
  }
109
105
  export default _default;
106
+ export type Deferred = {
107
+ promise: Promise<any>;
108
+ timerHandler: NodeJS.Timeout;
109
+ timerCleared: boolean;
110
+ resolved: boolean;
111
+ rejected: boolean;
112
+ canceled: boolean;
113
+ reject: (reason: Error) => void;
114
+ resolve: (...args: any[]) => void;
115
+ };
@@ -1,10 +1,46 @@
1
1
  declare namespace _default {
2
+ export { s2ns };
3
+ export { ms2ns };
2
4
  export { timestamp64 };
5
+ export { lapseNano };
6
+ export { lapseMillis };
7
+ export { timeoutNano };
8
+ export { timeoutMillis };
3
9
  }
4
10
  export default _default;
11
+ export const s2ns: 1000000000;
12
+ export const ms2ns: 1000000;
5
13
  /**
6
14
  * Gets the current timestamp in nanoseconds (if running in Node.js) or milliseconds (in browser).
7
15
  * Uses `process.hrtime.bigint()` for high-resolution timestamps in Node.js, falls back to `Date.now()` in browsers.
8
16
  * @returns {bigint} Current timestamp as a BigInt
9
17
  */
10
18
  export function timestamp64(): bigint;
19
+ /**
20
+ * Calculates the time elapsed in nanoseconds between the given timestamp and now.
21
+ * @param {bigint} start - start timestamp64, in nanoseconds.
22
+ * @param {bigint} [end] - end timestamp64, in nanoseconds. If not provided, uses current timestamp64.
23
+ * @returns {bigint} The elapsed time in nanoseconds (current timestamp64 - ts64).
24
+ */
25
+ export function lapseNano(start: bigint, end?: bigint): bigint;
26
+ /**
27
+ * Calculates the time elapsed in milliseconds between the given timestamp and now.
28
+ * @param {bigint} start - start The timestamp in 64-bit format.
29
+ * @param {bigint} [end] - end timestamp64, in nanoseconds. If not provided, uses current timestamp64.
30
+ * @returns {bigint} The elapsed time in milliseconds.
31
+ */
32
+ export function lapseMillis(start: bigint, end?: bigint): bigint;
33
+ /**
34
+ * compare current timestamp64 against the given ts64, and check if the elapsed time exceeds the specified timeout.
35
+ * @param {bigint} nanoTimestamp64 - The timestamp to compare against (in nanoseconds).
36
+ * @param {bigint|number} nanoTimeout - The timeout threshold (in nanoseconds).
37
+ * @returns {boolean} True if elapsed time exceeds timeout, false otherwise.
38
+ */
39
+ export function timeoutNano(nanoTimestamp64: bigint, nanoTimeout: bigint | number): boolean;
40
+ /**
41
+ * compare current timestamp64 against the given ts64, and check if the elapsed time exceeds the specified timeout.
42
+ * @param {bigint} nanoTimestamp64 - The timestamp to compare against (in nanoseconds).
43
+ * @param {bigint|number} millisTimeout - The timeout threshold (in milliseconds).
44
+ * @returns {boolean} True if elapsed time exceeds timeout, false otherwise.
45
+ */
46
+ export function timeoutMillis(nanoTimestamp64: bigint, millisTimeout: bigint | number): boolean;