@jsnw/common-utils 1.0.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.cjs +228 -0
- package/dist/index.d.cts +136 -0
- package/dist/index.d.mts +136 -0
- package/dist/index.mjs +217 -0
- package/package.json +19 -25
- package/dist/cjs/chunk.js +0 -19
- package/dist/cjs/chunkStr.js +0 -17
- package/dist/cjs/createInFlightDeduper.js +0 -24
- package/dist/cjs/ignorePromise.js +0 -15
- package/dist/cjs/index.js +0 -25
- package/dist/cjs/isObjectEmpty.js +0 -16
- package/dist/cjs/monitor.js +0 -75
- package/dist/cjs/noop.js +0 -12
- package/dist/cjs/resultify.js +0 -23
- package/dist/cjs/wait.js +0 -34
- package/dist/esm/chunk.js +0 -15
- package/dist/esm/chunkStr.js +0 -14
- package/dist/esm/createInFlightDeduper.js +0 -21
- package/dist/esm/ignorePromise.js +0 -11
- package/dist/esm/index.js +0 -9
- package/dist/esm/isObjectEmpty.js +0 -13
- package/dist/esm/monitor.js +0 -71
- package/dist/esm/noop.js +0 -8
- package/dist/esm/resultify.js +0 -20
- package/dist/esm/wait.js +0 -30
- package/dist/types/chunk.d.ts +0 -9
- package/dist/types/chunkStr.d.ts +0 -8
- package/dist/types/createInFlightDeduper.d.ts +0 -10
- package/dist/types/ignorePromise.d.ts +0 -6
- package/dist/types/index.d.ts +0 -9
- package/dist/types/isObjectEmpty.d.ts +0 -5
- package/dist/types/monitor.d.ts +0 -40
- package/dist/types/noop.d.ts +0 -8
- package/dist/types/resultify.d.ts +0 -10
- package/dist/types/wait.d.ts +0 -8
package/README.md
CHANGED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/noop.ts
|
|
3
|
+
/**
|
|
4
|
+
* A no-operation function that performs no action.
|
|
5
|
+
* This function can be used as a placeholder or default value
|
|
6
|
+
* where a function is required but no operation is necessary.
|
|
7
|
+
*
|
|
8
|
+
* @returns {void}
|
|
9
|
+
*/
|
|
10
|
+
const noop = () => {};
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/ignorePromise.ts
|
|
13
|
+
/**
|
|
14
|
+
* Accepts a promise and ensures that its resolution or rejection is ignored.
|
|
15
|
+
*
|
|
16
|
+
* @param {Promise<any>} promise - The promise to be ignored.
|
|
17
|
+
*/
|
|
18
|
+
const ignorePromise = (promise) => {
|
|
19
|
+
promise.then(noop).catch(noop);
|
|
20
|
+
};
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/chunk.ts
|
|
23
|
+
/**
|
|
24
|
+
* Splits an array into smaller arrays of a specified size.
|
|
25
|
+
*
|
|
26
|
+
* @template T
|
|
27
|
+
* @param {T[]} arr - The array to be split into chunks.
|
|
28
|
+
* @param {number} chunk_size - The size of each chunk.
|
|
29
|
+
* @returns {T[][]} An array containing the chunks.
|
|
30
|
+
*/
|
|
31
|
+
const chunk = (arr, chunk_size) => {
|
|
32
|
+
if (chunk_size <= 0) throw new TypeError("Chunk size should be greater than 0");
|
|
33
|
+
return Array.from({ length: Math.ceil(arr.length / chunk_size) }, (_, i) => arr.slice(i * chunk_size, i * chunk_size + chunk_size));
|
|
34
|
+
};
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/chunkStr.ts
|
|
37
|
+
/**
|
|
38
|
+
* Splits a string into chunks of a specified size
|
|
39
|
+
*
|
|
40
|
+
* @param {string} str - The string to be split into chunks
|
|
41
|
+
* @param {number} chunkSize - The size of each chunk
|
|
42
|
+
* @returns {string[]} An array containing the chunks
|
|
43
|
+
*/
|
|
44
|
+
function chunkStr(str, chunkSize) {
|
|
45
|
+
if (chunkSize <= 0) throw new TypeError("Chunk size should be greater than 0");
|
|
46
|
+
return Array.from({ length: Math.ceil(str.length / chunkSize) }, (_, i) => str.slice(i * chunkSize, i * chunkSize + chunkSize));
|
|
47
|
+
}
|
|
48
|
+
//#endregion
|
|
49
|
+
//#region src/wait.ts
|
|
50
|
+
/**
|
|
51
|
+
* Waits asynchronously for the specified amount of milliseconds before resolving.
|
|
52
|
+
*
|
|
53
|
+
* @param {number} wait_ms - The number of milliseconds to wait. If less than 1, the promise resolves immediately.
|
|
54
|
+
* @param {AbortSignal} [signal]
|
|
55
|
+
* @returns {Promise<void>} A promise that resolves after the specified wait time.
|
|
56
|
+
*/
|
|
57
|
+
const wait = (wait_ms, signal) => {
|
|
58
|
+
if (wait_ms < 1) return Promise.resolve();
|
|
59
|
+
if (!Number.isFinite(wait_ms) && !signal) throw new TypeError("Invalid wait_ms: must be a finite number (or you should provide an AbortSignal as a second argument)");
|
|
60
|
+
if (signal?.aborted) return Promise.resolve();
|
|
61
|
+
if (!signal) return new Promise((resolve) => setTimeout(resolve, wait_ms));
|
|
62
|
+
return new Promise((resolve) => {
|
|
63
|
+
let timer_ref = null;
|
|
64
|
+
const on_abort = () => {
|
|
65
|
+
if (timer_ref) clearTimeout(timer_ref);
|
|
66
|
+
resolve();
|
|
67
|
+
};
|
|
68
|
+
signal.addEventListener("abort", on_abort, { once: true });
|
|
69
|
+
timer_ref = setTimeout(() => {
|
|
70
|
+
signal.removeEventListener("abort", on_abort);
|
|
71
|
+
resolve();
|
|
72
|
+
}, wait_ms);
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region src/isObjectEmpty.ts
|
|
77
|
+
/**
|
|
78
|
+
* @param {object} obj
|
|
79
|
+
* @returns {boolean}
|
|
80
|
+
*/
|
|
81
|
+
function isObjectEmpty(obj) {
|
|
82
|
+
if (Array.isArray(obj)) return obj.length === 0;
|
|
83
|
+
for (const key in obj) if (Object.prototype.hasOwnProperty.call(obj, key)) return false;
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
//#endregion
|
|
87
|
+
//#region src/createInFlightDeduper.ts
|
|
88
|
+
/**
|
|
89
|
+
* Creates a deduplicator for in-flight promises or sync functions.
|
|
90
|
+
* Prevents duplicate executions for the same key while a promise is pending.
|
|
91
|
+
* Clears the entry after settlement.
|
|
92
|
+
* @returns {InFlightDeduper}
|
|
93
|
+
*/
|
|
94
|
+
function createInFlightDeduper() {
|
|
95
|
+
const __promises = /* @__PURE__ */ new Map();
|
|
96
|
+
function use(key, fn) {
|
|
97
|
+
let promise = __promises.get(key);
|
|
98
|
+
if (promise) return promise;
|
|
99
|
+
promise = Promise.resolve(fn()).finally(() => {
|
|
100
|
+
__promises.delete(key);
|
|
101
|
+
});
|
|
102
|
+
__promises.set(key, promise);
|
|
103
|
+
return promise;
|
|
104
|
+
}
|
|
105
|
+
return { use };
|
|
106
|
+
}
|
|
107
|
+
//#endregion
|
|
108
|
+
//#region src/resultify.ts
|
|
109
|
+
/**
|
|
110
|
+
* Executes a function and returns its result as a tuple `[value, error]`.
|
|
111
|
+
* Works with both sync and async functions. Never throws.
|
|
112
|
+
*
|
|
113
|
+
* @template {() => any} F
|
|
114
|
+
* @template {Error} E
|
|
115
|
+
* @param {F} fn
|
|
116
|
+
*/
|
|
117
|
+
function resultify(fn) {
|
|
118
|
+
try {
|
|
119
|
+
const promiseOrResult = fn();
|
|
120
|
+
if (promiseOrResult instanceof Promise) return promiseOrResult.then((res) => [res, null]).catch((err) => [null, err]);
|
|
121
|
+
return [promiseOrResult, null];
|
|
122
|
+
} catch (e) {
|
|
123
|
+
return [null, e];
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
//#endregion
|
|
127
|
+
//#region src/monitor.ts
|
|
128
|
+
var Monitor = class Monitor {
|
|
129
|
+
marks = /* @__PURE__ */ new Map();
|
|
130
|
+
/**
|
|
131
|
+
* @template {(...args: any[]) => any} F
|
|
132
|
+
* @param {F} fn
|
|
133
|
+
* @returns {ReturnType<F> extends Promise<any> ? Promise<number> : number}
|
|
134
|
+
*/
|
|
135
|
+
static measureFunctionTime(fn) {
|
|
136
|
+
const timeStart = Date.now();
|
|
137
|
+
const promiseOrResult = fn();
|
|
138
|
+
if (promiseOrResult instanceof Promise) return Promise.resolve(promiseOrResult).then(() => Date.now() - timeStart);
|
|
139
|
+
return Date.now() - timeStart;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* @param {string} label
|
|
143
|
+
* @returns {Monitor}
|
|
144
|
+
*/
|
|
145
|
+
static autoStart(label) {
|
|
146
|
+
const instance = new Monitor();
|
|
147
|
+
instance.start(label);
|
|
148
|
+
return instance;
|
|
149
|
+
}
|
|
150
|
+
constructor() {}
|
|
151
|
+
/**
|
|
152
|
+
* @param {string} label
|
|
153
|
+
*/
|
|
154
|
+
start(label) {
|
|
155
|
+
if (!this.marks.has(label)) this.marks.set(label, [[Date.now()]]);
|
|
156
|
+
else return;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* @param {string} label
|
|
160
|
+
* @param {string} [description]
|
|
161
|
+
*/
|
|
162
|
+
mark(label, description) {
|
|
163
|
+
if (!this.marks.has(label)) throw new TypeError(`Label ${label} not found`);
|
|
164
|
+
this.marks.get(label).push([Date.now(), description]);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* @param {string} label
|
|
168
|
+
* @returns {MonitorResult}
|
|
169
|
+
*/
|
|
170
|
+
stop(label) {
|
|
171
|
+
const endTime = Date.now();
|
|
172
|
+
const marks = this.marks.get(label);
|
|
173
|
+
if (!marks) throw new TypeError(`Label ${label} not found`);
|
|
174
|
+
const [firstTs] = marks[0], processedMarks = [];
|
|
175
|
+
for (let i = 1; i < marks.length; i++) processedMarks.push({
|
|
176
|
+
ts: marks[i][0],
|
|
177
|
+
diff: marks[i][0] - firstTs,
|
|
178
|
+
description: marks[i][1]
|
|
179
|
+
});
|
|
180
|
+
this.marks.delete(label);
|
|
181
|
+
return {
|
|
182
|
+
marks: processedMarks,
|
|
183
|
+
startTs: firstTs,
|
|
184
|
+
stopTs: endTime,
|
|
185
|
+
totalTime: endTime - firstTs
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
//#endregion
|
|
190
|
+
//#region src/trySettlePromises.ts
|
|
191
|
+
const trySettlePromises = async (promises, onError) => {
|
|
192
|
+
onError = onError && typeof onError === "function" ? onError : noop;
|
|
193
|
+
const results = await Promise.allSettled(promises.map((v) => Array.isArray(v) ? v[0] : v));
|
|
194
|
+
for (let i = 0; i < results.length; i++) {
|
|
195
|
+
const res = results[i];
|
|
196
|
+
const entry = promises[i];
|
|
197
|
+
if (res.status === "rejected") try {
|
|
198
|
+
onError({
|
|
199
|
+
reason: res.reason,
|
|
200
|
+
message: Array.isArray(entry) ? entry[1] : void 0
|
|
201
|
+
}, i);
|
|
202
|
+
} catch (e) {}
|
|
203
|
+
}
|
|
204
|
+
return results;
|
|
205
|
+
};
|
|
206
|
+
//#endregion
|
|
207
|
+
//#region src/isAbortError.ts
|
|
208
|
+
const isAbortError = (e, signals) => {
|
|
209
|
+
if (!!e && typeof e === "object" && "name" in e && e.name === "AbortError") return true;
|
|
210
|
+
const filtered = (signals ?? []).filter((v) => v !== void 0 && v !== null);
|
|
211
|
+
for (const signal of filtered) {
|
|
212
|
+
if (!signal.aborted) continue;
|
|
213
|
+
if (e === signal.reason) return true;
|
|
214
|
+
}
|
|
215
|
+
return false;
|
|
216
|
+
};
|
|
217
|
+
//#endregion
|
|
218
|
+
exports.Monitor = Monitor;
|
|
219
|
+
exports.chunk = chunk;
|
|
220
|
+
exports.chunkStr = chunkStr;
|
|
221
|
+
exports.createInFlightDeduper = createInFlightDeduper;
|
|
222
|
+
exports.ignorePromise = ignorePromise;
|
|
223
|
+
exports.isAbortError = isAbortError;
|
|
224
|
+
exports.isObjectEmpty = isObjectEmpty;
|
|
225
|
+
exports.noop = noop;
|
|
226
|
+
exports.resultify = resultify;
|
|
227
|
+
exports.trySettlePromises = trySettlePromises;
|
|
228
|
+
exports.wait = wait;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
//#region src/noop.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* A no-operation function that performs no action.
|
|
4
|
+
* This function can be used as a placeholder or default value
|
|
5
|
+
* where a function is required but no operation is necessary.
|
|
6
|
+
*
|
|
7
|
+
* @returns {void}
|
|
8
|
+
*/
|
|
9
|
+
declare const noop: () => void;
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/ignorePromise.d.ts
|
|
12
|
+
/**
|
|
13
|
+
* Accepts a promise and ensures that its resolution or rejection is ignored.
|
|
14
|
+
*
|
|
15
|
+
* @param {Promise<any>} promise - The promise to be ignored.
|
|
16
|
+
*/
|
|
17
|
+
declare const ignorePromise: (promise: Promise<any>) => void;
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/chunk.d.ts
|
|
20
|
+
/**
|
|
21
|
+
* Splits an array into smaller arrays of a specified size.
|
|
22
|
+
*
|
|
23
|
+
* @template T
|
|
24
|
+
* @param {T[]} arr - The array to be split into chunks.
|
|
25
|
+
* @param {number} chunk_size - The size of each chunk.
|
|
26
|
+
* @returns {T[][]} An array containing the chunks.
|
|
27
|
+
*/
|
|
28
|
+
declare const chunk: <T extends any>(arr: T[], chunk_size: number) => T[][];
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/chunkStr.d.ts
|
|
31
|
+
/**
|
|
32
|
+
* Splits a string into chunks of a specified size
|
|
33
|
+
*
|
|
34
|
+
* @param {string} str - The string to be split into chunks
|
|
35
|
+
* @param {number} chunkSize - The size of each chunk
|
|
36
|
+
* @returns {string[]} An array containing the chunks
|
|
37
|
+
*/
|
|
38
|
+
declare function chunkStr(str: string, chunkSize: number): string[];
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/wait.d.ts
|
|
41
|
+
/**
|
|
42
|
+
* Waits asynchronously for the specified amount of milliseconds before resolving.
|
|
43
|
+
*
|
|
44
|
+
* @param {number} wait_ms - The number of milliseconds to wait. If less than 1, the promise resolves immediately.
|
|
45
|
+
* @param {AbortSignal} [signal]
|
|
46
|
+
* @returns {Promise<void>} A promise that resolves after the specified wait time.
|
|
47
|
+
*/
|
|
48
|
+
declare const wait: (wait_ms: number, signal?: AbortSignal) => Promise<void>;
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/isObjectEmpty.d.ts
|
|
51
|
+
/**
|
|
52
|
+
* @param {object} obj
|
|
53
|
+
* @returns {boolean}
|
|
54
|
+
*/
|
|
55
|
+
declare function isObjectEmpty(obj: object): boolean;
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region src/createInFlightDeduper.d.ts
|
|
58
|
+
type InFlightDeduper = {
|
|
59
|
+
use: <F extends () => any>(key: string, fn: F) => Promise<Awaited<ReturnType<F>>>;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Creates a deduplicator for in-flight promises or sync functions.
|
|
63
|
+
* Prevents duplicate executions for the same key while a promise is pending.
|
|
64
|
+
* Clears the entry after settlement.
|
|
65
|
+
* @returns {InFlightDeduper}
|
|
66
|
+
*/
|
|
67
|
+
declare function createInFlightDeduper(): InFlightDeduper;
|
|
68
|
+
//#endregion
|
|
69
|
+
//#region src/resultify.d.ts
|
|
70
|
+
type ErrorResult<R, E> = readonly [R, null] | readonly [null, E];
|
|
71
|
+
/**
|
|
72
|
+
* Executes a function and returns its result as a tuple `[value, error]`.
|
|
73
|
+
* Works with both sync and async functions. Never throws.
|
|
74
|
+
*
|
|
75
|
+
* @template {() => any} F
|
|
76
|
+
* @template {Error} E
|
|
77
|
+
* @param {F} fn
|
|
78
|
+
*/
|
|
79
|
+
declare function resultify<F extends () => any, E = Error>(fn: F): ReturnType<F> extends Promise<infer R> ? Promise<ErrorResult<R, E>> : ErrorResult<ReturnType<F>, E>;
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region src/monitor.d.ts
|
|
82
|
+
type MonitorMark = {
|
|
83
|
+
ts: number;
|
|
84
|
+
diff: number;
|
|
85
|
+
description?: string;
|
|
86
|
+
};
|
|
87
|
+
type MonitorResult = {
|
|
88
|
+
marks: MonitorMark[];
|
|
89
|
+
startTs: number;
|
|
90
|
+
stopTs: number;
|
|
91
|
+
totalTime: number;
|
|
92
|
+
};
|
|
93
|
+
declare class Monitor {
|
|
94
|
+
private readonly marks;
|
|
95
|
+
/**
|
|
96
|
+
* @template {(...args: any[]) => any} F
|
|
97
|
+
* @param {F} fn
|
|
98
|
+
* @returns {ReturnType<F> extends Promise<any> ? Promise<number> : number}
|
|
99
|
+
*/
|
|
100
|
+
static measureFunctionTime<F extends (...args: any[]) => any>(fn: F): ReturnType<F> extends Promise<any> ? Promise<number> : number;
|
|
101
|
+
/**
|
|
102
|
+
* @param {string} label
|
|
103
|
+
* @returns {Monitor}
|
|
104
|
+
*/
|
|
105
|
+
static autoStart(label: string): Monitor;
|
|
106
|
+
constructor();
|
|
107
|
+
/**
|
|
108
|
+
* @param {string} label
|
|
109
|
+
*/
|
|
110
|
+
start(label: string): void;
|
|
111
|
+
/**
|
|
112
|
+
* @param {string} label
|
|
113
|
+
* @param {string} [description]
|
|
114
|
+
*/
|
|
115
|
+
mark(label: string, description?: string): void;
|
|
116
|
+
/**
|
|
117
|
+
* @param {string} label
|
|
118
|
+
* @returns {MonitorResult}
|
|
119
|
+
*/
|
|
120
|
+
stop(label: string): MonitorResult;
|
|
121
|
+
}
|
|
122
|
+
//#endregion
|
|
123
|
+
//#region src/trySettlePromises.d.ts
|
|
124
|
+
type DescribedPromise<T extends any> = [Promise<T>, string];
|
|
125
|
+
type AwaitedTuple<T extends (Promise<any> | DescribedPromise<any>)[]> = { [K in keyof T]: T[K] extends DescribedPromise<any> ? PromiseSettledResult<Awaited<T[K][0]>> : T[K] extends Promise<any> ? PromiseSettledResult<Awaited<T[K]>> : never; };
|
|
126
|
+
type ErrorDetails = {
|
|
127
|
+
reason: unknown;
|
|
128
|
+
message?: string;
|
|
129
|
+
};
|
|
130
|
+
type ErrorCallback = (details: ErrorDetails, index: number) => void;
|
|
131
|
+
declare const trySettlePromises: <T extends (Promise<any> | DescribedPromise<any>)[]>(promises: [...T], onError?: ErrorCallback) => Promise<AwaitedTuple<T>>;
|
|
132
|
+
//#endregion
|
|
133
|
+
//#region src/isAbortError.d.ts
|
|
134
|
+
declare const isAbortError: (e: unknown, signals?: (AbortSignal | undefined | null)[]) => boolean;
|
|
135
|
+
//#endregion
|
|
136
|
+
export { ErrorResult, InFlightDeduper, Monitor, MonitorMark, MonitorResult, chunk, chunkStr, createInFlightDeduper, ignorePromise, isAbortError, isObjectEmpty, noop, resultify, trySettlePromises, wait };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
//#region src/noop.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* A no-operation function that performs no action.
|
|
4
|
+
* This function can be used as a placeholder or default value
|
|
5
|
+
* where a function is required but no operation is necessary.
|
|
6
|
+
*
|
|
7
|
+
* @returns {void}
|
|
8
|
+
*/
|
|
9
|
+
declare const noop: () => void;
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/ignorePromise.d.ts
|
|
12
|
+
/**
|
|
13
|
+
* Accepts a promise and ensures that its resolution or rejection is ignored.
|
|
14
|
+
*
|
|
15
|
+
* @param {Promise<any>} promise - The promise to be ignored.
|
|
16
|
+
*/
|
|
17
|
+
declare const ignorePromise: (promise: Promise<any>) => void;
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/chunk.d.ts
|
|
20
|
+
/**
|
|
21
|
+
* Splits an array into smaller arrays of a specified size.
|
|
22
|
+
*
|
|
23
|
+
* @template T
|
|
24
|
+
* @param {T[]} arr - The array to be split into chunks.
|
|
25
|
+
* @param {number} chunk_size - The size of each chunk.
|
|
26
|
+
* @returns {T[][]} An array containing the chunks.
|
|
27
|
+
*/
|
|
28
|
+
declare const chunk: <T extends any>(arr: T[], chunk_size: number) => T[][];
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/chunkStr.d.ts
|
|
31
|
+
/**
|
|
32
|
+
* Splits a string into chunks of a specified size
|
|
33
|
+
*
|
|
34
|
+
* @param {string} str - The string to be split into chunks
|
|
35
|
+
* @param {number} chunkSize - The size of each chunk
|
|
36
|
+
* @returns {string[]} An array containing the chunks
|
|
37
|
+
*/
|
|
38
|
+
declare function chunkStr(str: string, chunkSize: number): string[];
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/wait.d.ts
|
|
41
|
+
/**
|
|
42
|
+
* Waits asynchronously for the specified amount of milliseconds before resolving.
|
|
43
|
+
*
|
|
44
|
+
* @param {number} wait_ms - The number of milliseconds to wait. If less than 1, the promise resolves immediately.
|
|
45
|
+
* @param {AbortSignal} [signal]
|
|
46
|
+
* @returns {Promise<void>} A promise that resolves after the specified wait time.
|
|
47
|
+
*/
|
|
48
|
+
declare const wait: (wait_ms: number, signal?: AbortSignal) => Promise<void>;
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/isObjectEmpty.d.ts
|
|
51
|
+
/**
|
|
52
|
+
* @param {object} obj
|
|
53
|
+
* @returns {boolean}
|
|
54
|
+
*/
|
|
55
|
+
declare function isObjectEmpty(obj: object): boolean;
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region src/createInFlightDeduper.d.ts
|
|
58
|
+
type InFlightDeduper = {
|
|
59
|
+
use: <F extends () => any>(key: string, fn: F) => Promise<Awaited<ReturnType<F>>>;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Creates a deduplicator for in-flight promises or sync functions.
|
|
63
|
+
* Prevents duplicate executions for the same key while a promise is pending.
|
|
64
|
+
* Clears the entry after settlement.
|
|
65
|
+
* @returns {InFlightDeduper}
|
|
66
|
+
*/
|
|
67
|
+
declare function createInFlightDeduper(): InFlightDeduper;
|
|
68
|
+
//#endregion
|
|
69
|
+
//#region src/resultify.d.ts
|
|
70
|
+
type ErrorResult<R, E> = readonly [R, null] | readonly [null, E];
|
|
71
|
+
/**
|
|
72
|
+
* Executes a function and returns its result as a tuple `[value, error]`.
|
|
73
|
+
* Works with both sync and async functions. Never throws.
|
|
74
|
+
*
|
|
75
|
+
* @template {() => any} F
|
|
76
|
+
* @template {Error} E
|
|
77
|
+
* @param {F} fn
|
|
78
|
+
*/
|
|
79
|
+
declare function resultify<F extends () => any, E = Error>(fn: F): ReturnType<F> extends Promise<infer R> ? Promise<ErrorResult<R, E>> : ErrorResult<ReturnType<F>, E>;
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region src/monitor.d.ts
|
|
82
|
+
type MonitorMark = {
|
|
83
|
+
ts: number;
|
|
84
|
+
diff: number;
|
|
85
|
+
description?: string;
|
|
86
|
+
};
|
|
87
|
+
type MonitorResult = {
|
|
88
|
+
marks: MonitorMark[];
|
|
89
|
+
startTs: number;
|
|
90
|
+
stopTs: number;
|
|
91
|
+
totalTime: number;
|
|
92
|
+
};
|
|
93
|
+
declare class Monitor {
|
|
94
|
+
private readonly marks;
|
|
95
|
+
/**
|
|
96
|
+
* @template {(...args: any[]) => any} F
|
|
97
|
+
* @param {F} fn
|
|
98
|
+
* @returns {ReturnType<F> extends Promise<any> ? Promise<number> : number}
|
|
99
|
+
*/
|
|
100
|
+
static measureFunctionTime<F extends (...args: any[]) => any>(fn: F): ReturnType<F> extends Promise<any> ? Promise<number> : number;
|
|
101
|
+
/**
|
|
102
|
+
* @param {string} label
|
|
103
|
+
* @returns {Monitor}
|
|
104
|
+
*/
|
|
105
|
+
static autoStart(label: string): Monitor;
|
|
106
|
+
constructor();
|
|
107
|
+
/**
|
|
108
|
+
* @param {string} label
|
|
109
|
+
*/
|
|
110
|
+
start(label: string): void;
|
|
111
|
+
/**
|
|
112
|
+
* @param {string} label
|
|
113
|
+
* @param {string} [description]
|
|
114
|
+
*/
|
|
115
|
+
mark(label: string, description?: string): void;
|
|
116
|
+
/**
|
|
117
|
+
* @param {string} label
|
|
118
|
+
* @returns {MonitorResult}
|
|
119
|
+
*/
|
|
120
|
+
stop(label: string): MonitorResult;
|
|
121
|
+
}
|
|
122
|
+
//#endregion
|
|
123
|
+
//#region src/trySettlePromises.d.ts
|
|
124
|
+
type DescribedPromise<T extends any> = [Promise<T>, string];
|
|
125
|
+
type AwaitedTuple<T extends (Promise<any> | DescribedPromise<any>)[]> = { [K in keyof T]: T[K] extends DescribedPromise<any> ? PromiseSettledResult<Awaited<T[K][0]>> : T[K] extends Promise<any> ? PromiseSettledResult<Awaited<T[K]>> : never; };
|
|
126
|
+
type ErrorDetails = {
|
|
127
|
+
reason: unknown;
|
|
128
|
+
message?: string;
|
|
129
|
+
};
|
|
130
|
+
type ErrorCallback = (details: ErrorDetails, index: number) => void;
|
|
131
|
+
declare const trySettlePromises: <T extends (Promise<any> | DescribedPromise<any>)[]>(promises: [...T], onError?: ErrorCallback) => Promise<AwaitedTuple<T>>;
|
|
132
|
+
//#endregion
|
|
133
|
+
//#region src/isAbortError.d.ts
|
|
134
|
+
declare const isAbortError: (e: unknown, signals?: (AbortSignal | undefined | null)[]) => boolean;
|
|
135
|
+
//#endregion
|
|
136
|
+
export { ErrorResult, InFlightDeduper, Monitor, MonitorMark, MonitorResult, chunk, chunkStr, createInFlightDeduper, ignorePromise, isAbortError, isObjectEmpty, noop, resultify, trySettlePromises, wait };
|