@ls-stack/utils 1.13.2 → 2.0.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/dist/arrayUtils.cjs +91 -0
- package/dist/arrayUtils.d.cts +51 -0
- package/dist/arrayUtils.js +1 -2
- package/dist/assertions.cjs +99 -0
- package/dist/assertions.d.cts +17 -0
- package/dist/assertions.js +1 -2
- package/dist/castValues.cjs +41 -0
- package/dist/castValues.d.cts +4 -0
- package/dist/castValues.js +0 -1
- package/dist/{chunk-FYMTZQ42.js → chunk-4UGSP3L3.js} +0 -1
- package/dist/{chunk-HLFWWIDK.js → chunk-5DZT3Z5Z.js} +0 -1
- package/dist/{chunk-W7X6NSGR.js → chunk-AZBBTE33.js} +0 -1
- package/dist/{chunk-MMKMF4GG.js → chunk-IATIXMCE.js} +0 -1
- package/dist/chunk-JOBPQAI3.js +147 -0
- package/dist/{chunk-M74KKYE4.js → chunk-KCOXGSRA.js} +0 -1
- package/dist/{chunk-NKCC3375.js → chunk-NWXBMMHO.js} +0 -1
- package/dist/{chunk-SNAAKCTH.js → chunk-RVVH3MDA.js} +0 -1
- package/dist/{chunk-4QK76IRX.js → chunk-T5WDDPFI.js} +1 -6
- package/dist/{chunk-GLZ5MZC5.js → chunk-V2L472LS.js} +0 -30
- package/dist/consoleFmt.cjs +84 -0
- package/dist/consoleFmt.d.cts +48 -0
- package/dist/consoleFmt.js +0 -1
- package/dist/conversions.cjs +44 -0
- package/dist/conversions.d.cts +3 -0
- package/dist/conversions.js +1 -2
- package/dist/createThrottleController.cjs +183 -0
- package/dist/createThrottleController.d.cts +16 -0
- package/dist/createThrottleController.js +2 -3
- package/dist/debounce.cjs +125 -0
- package/dist/debounce.d.cts +45 -0
- package/dist/debounce.js +0 -1
- package/dist/dedent.cjs +57 -0
- package/dist/dedent.d.cts +4 -0
- package/dist/dedent.js +0 -1
- package/dist/deepEqual.cjs +88 -0
- package/dist/deepEqual.d.cts +4 -0
- package/dist/deepEqual.js +1 -2
- package/dist/enhancedMap.cjs +128 -0
- package/dist/enhancedMap.d.cts +20 -0
- package/dist/enhancedMap.d.ts +0 -2
- package/dist/enhancedMap.js +2 -3
- package/dist/exhaustiveMatch.cjs +55 -0
- package/dist/exhaustiveMatch.d.cts +6 -0
- package/dist/exhaustiveMatch.js +0 -1
- package/dist/interpolate.cjs +88 -0
- package/dist/interpolate.d.cts +11 -0
- package/dist/interpolate.js +2 -3
- package/dist/levenshtein.cjs +180 -0
- package/dist/levenshtein.d.cts +5 -0
- package/dist/levenshtein.js +0 -1
- package/dist/main.cjs +4 -0
- package/dist/main.d.cts +2 -0
- package/dist/main.d.ts +1 -1
- package/dist/main.js +0 -1
- package/dist/mathUtils.cjs +50 -0
- package/dist/mathUtils.d.cts +6 -0
- package/dist/mathUtils.js +1 -2
- package/dist/objUtils.cjs +67 -0
- package/dist/objUtils.d.cts +7 -0
- package/dist/objUtils.js +1 -2
- package/dist/parallelAsyncCalls.cjs +265 -0
- package/dist/{parallelAsyncResultCalls.d.ts → parallelAsyncCalls.d.cts} +7 -7
- package/dist/parallelAsyncCalls.d.ts +76 -0
- package/dist/{parallelAsyncResultCalls.js → parallelAsyncCalls.js} +7 -9
- package/dist/promiseUtils.cjs +38 -0
- package/dist/promiseUtils.d.cts +8 -0
- package/dist/promiseUtils.js +0 -1
- package/dist/rsResult.cjs +179 -0
- package/dist/rsResult.d.cts +88 -0
- package/dist/rsResult.d.ts +33 -63
- package/dist/rsResult.js +6 -10
- package/dist/runShellCmd.cjs +127 -0
- package/dist/runShellCmd.d.cts +23 -0
- package/dist/runShellCmd.d.ts +2 -1
- package/dist/runShellCmd.js +9 -4
- package/dist/shallowEqual.cjs +88 -0
- package/dist/shallowEqual.d.cts +4 -0
- package/dist/shallowEqual.js +0 -1
- package/dist/sleep.cjs +32 -0
- package/dist/sleep.d.cts +3 -0
- package/dist/sleep.js +1 -2
- package/dist/stringUtils.cjs +66 -0
- package/dist/stringUtils.d.cts +16 -0
- package/dist/stringUtils.d.ts +1 -3
- package/dist/stringUtils.js +1 -4
- package/dist/testUtils.cjs +357 -0
- package/dist/testUtils.d.cts +38 -0
- package/dist/testUtils.js +5 -6
- package/dist/typingFnUtils.cjs +55 -0
- package/dist/typingFnUtils.d.cts +15 -0
- package/dist/typingFnUtils.js +0 -1
- package/dist/typingTestUtils.cjs +43 -0
- package/dist/typingTestUtils.d.cts +12 -0
- package/dist/typingTestUtils.js +0 -1
- package/dist/typingUtils.cjs +18 -0
- package/dist/typingUtils.d.cts +11 -0
- package/dist/typingUtils.js +0 -1
- package/dist/yamlStringify.cjs +346 -0
- package/dist/yamlStringify.d.cts +8 -0
- package/dist/yamlStringify.js +3 -4
- package/package.json +64 -34
- package/dist/arrayUtils.js.map +0 -1
- package/dist/assertions.js.map +0 -1
- package/dist/castValues.js.map +0 -1
- package/dist/chunk-4QK76IRX.js.map +0 -1
- package/dist/chunk-FYMTZQ42.js.map +0 -1
- package/dist/chunk-GLZ5MZC5.js.map +0 -1
- package/dist/chunk-HLFWWIDK.js.map +0 -1
- package/dist/chunk-I7ZDKDUM.js +0 -217
- package/dist/chunk-I7ZDKDUM.js.map +0 -1
- package/dist/chunk-M74KKYE4.js.map +0 -1
- package/dist/chunk-MMKMF4GG.js.map +0 -1
- package/dist/chunk-NKCC3375.js.map +0 -1
- package/dist/chunk-SNAAKCTH.js.map +0 -1
- package/dist/chunk-W7X6NSGR.js.map +0 -1
- package/dist/consoleFmt.js.map +0 -1
- package/dist/conversions.js.map +0 -1
- package/dist/createThrottleController.js.map +0 -1
- package/dist/debounce.js.map +0 -1
- package/dist/dedent.js.map +0 -1
- package/dist/deepEqual.js.map +0 -1
- package/dist/enhancedMap.js.map +0 -1
- package/dist/exhaustiveMatch.js.map +0 -1
- package/dist/interpolate.js.map +0 -1
- package/dist/levenshtein.js.map +0 -1
- package/dist/main.js.map +0 -1
- package/dist/mathUtils.js.map +0 -1
- package/dist/objUtils.js.map +0 -1
- package/dist/parallelAsyncResultCalls.js.map +0 -1
- package/dist/promiseUtils.js.map +0 -1
- package/dist/rsResult.js.map +0 -1
- package/dist/runShellCmd.js.map +0 -1
- package/dist/shallowEqual.js.map +0 -1
- package/dist/sleep.js.map +0 -1
- package/dist/stringUtils.js.map +0 -1
- package/dist/testUtils.js.map +0 -1
- package/dist/typingFnUtils.js.map +0 -1
- package/dist/typingTestUtils.js.map +0 -1
- package/dist/typingUtils.js.map +0 -1
- package/dist/yamlStringify.js.map +0 -1
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/objUtils.ts
|
|
21
|
+
var objUtils_exports = {};
|
|
22
|
+
__export(objUtils_exports, {
|
|
23
|
+
mapArrayToObject: () => mapArrayToObject,
|
|
24
|
+
mapObjectToObject: () => mapObjectToObject,
|
|
25
|
+
objectTypedEntries: () => objectTypedEntries,
|
|
26
|
+
omit: () => omit,
|
|
27
|
+
pick: () => pick
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(objUtils_exports);
|
|
30
|
+
function objectTypedEntries(obj) {
|
|
31
|
+
return Object.entries(obj);
|
|
32
|
+
}
|
|
33
|
+
function pick(obj, keys, rename) {
|
|
34
|
+
const result = {};
|
|
35
|
+
if (!obj) {
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
for (const key of keys) {
|
|
39
|
+
result[rename?.[key] || key] = obj[key];
|
|
40
|
+
}
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
function mapArrayToObject(array, mapper) {
|
|
44
|
+
return Object.fromEntries(array.map(mapper));
|
|
45
|
+
}
|
|
46
|
+
function mapObjectToObject(obj, mapper) {
|
|
47
|
+
return Object.fromEntries(
|
|
48
|
+
objectTypedEntries(obj).map(([key, value]) => mapper(key, value))
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
function omit(obj, keys) {
|
|
52
|
+
const result = {};
|
|
53
|
+
for (const key of Object.keys(obj)) {
|
|
54
|
+
if (!keys.includes(key)) {
|
|
55
|
+
result[key] = obj[key];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
61
|
+
0 && (module.exports = {
|
|
62
|
+
mapArrayToObject,
|
|
63
|
+
mapObjectToObject,
|
|
64
|
+
objectTypedEntries,
|
|
65
|
+
omit,
|
|
66
|
+
pick
|
|
67
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
declare function objectTypedEntries<T extends Record<string, unknown>>(obj: T): [Extract<keyof T, string>, T[keyof T]][];
|
|
2
|
+
declare function pick<T, K extends keyof T>(obj: T | undefined, keys: K[], rename?: Partial<Record<K, string>>): Record<string, unknown>;
|
|
3
|
+
declare function mapArrayToObject<T, K extends string, O>(array: T[], mapper: (item: T, index: number) => [K, O]): Record<K, O>;
|
|
4
|
+
declare function mapObjectToObject<I extends Record<string | number | symbol, unknown>, K extends string | number | symbol, O>(obj: I, mapper: (key: keyof I, value: I[keyof I]) => [K, O]): Record<K, O>;
|
|
5
|
+
declare function omit<T extends Record<string, unknown>, K extends keyof T>(obj: T, keys: K[]): Omit<T, K>;
|
|
6
|
+
|
|
7
|
+
export { mapArrayToObject, mapObjectToObject, objectTypedEntries, omit, pick };
|
package/dist/objUtils.js
CHANGED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/parallelAsyncCalls.ts
|
|
21
|
+
var parallelAsyncCalls_exports = {};
|
|
22
|
+
__export(parallelAsyncCalls_exports, {
|
|
23
|
+
parallelAsyncCalls: () => parallelAsyncCalls
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(parallelAsyncCalls_exports);
|
|
26
|
+
|
|
27
|
+
// src/assertions.ts
|
|
28
|
+
function invariant(condition, errorMsg = "Invariant violation") {
|
|
29
|
+
if (!condition) {
|
|
30
|
+
throw new Error(`Invariant violation: ${errorMsg}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function isObject(value) {
|
|
34
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/rsResult.ts
|
|
38
|
+
function okUnwrapOr() {
|
|
39
|
+
return this.value;
|
|
40
|
+
}
|
|
41
|
+
function ok(value = void 0) {
|
|
42
|
+
return {
|
|
43
|
+
ok: true,
|
|
44
|
+
error: false,
|
|
45
|
+
value,
|
|
46
|
+
unwrapOrNull: okUnwrapOr,
|
|
47
|
+
unwrapOr: okUnwrapOr,
|
|
48
|
+
unwrap: okUnwrapOr
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function err(error) {
|
|
52
|
+
return {
|
|
53
|
+
ok: false,
|
|
54
|
+
error,
|
|
55
|
+
unwrapOrNull: () => null,
|
|
56
|
+
unwrapOr: (defaultValue) => defaultValue,
|
|
57
|
+
errorResult() {
|
|
58
|
+
return err(error);
|
|
59
|
+
},
|
|
60
|
+
unwrap: () => {
|
|
61
|
+
if (error instanceof Error) {
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
if (Array.isArray(error)) {
|
|
65
|
+
throw new Error(error.join("\n"), { cause: error });
|
|
66
|
+
}
|
|
67
|
+
throw new Error(
|
|
68
|
+
isObject(error) && "message" in error && error.message ? String(error.message) : "Unwrap failed",
|
|
69
|
+
{ cause: error }
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function unknownToError(error) {
|
|
75
|
+
return err(normalizeError(error));
|
|
76
|
+
}
|
|
77
|
+
async function unwrap(result) {
|
|
78
|
+
const unwrapped = await result;
|
|
79
|
+
if (unwrapped.ok) {
|
|
80
|
+
return unwrapped.value;
|
|
81
|
+
}
|
|
82
|
+
throw unwrapped.error;
|
|
83
|
+
}
|
|
84
|
+
function asyncMap(resultPromise) {
|
|
85
|
+
return {
|
|
86
|
+
err: async (mapFn) => {
|
|
87
|
+
const result = await resultPromise;
|
|
88
|
+
return result.ok ? ok(result.value) : err(mapFn(result.error));
|
|
89
|
+
},
|
|
90
|
+
ok: async (mapFn) => {
|
|
91
|
+
const result = await resultPromise;
|
|
92
|
+
return result.ok ? ok(mapFn(result.value)) : err(result.error);
|
|
93
|
+
},
|
|
94
|
+
okAndErr: async ({
|
|
95
|
+
ok: mapFn,
|
|
96
|
+
err: mapErrFn
|
|
97
|
+
}) => {
|
|
98
|
+
const result = await resultPromise;
|
|
99
|
+
return result.ok ? ok(mapFn(result.value)) : err(mapErrFn(result.error));
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function syncMap(result) {
|
|
104
|
+
return {
|
|
105
|
+
err: (mapFn) => {
|
|
106
|
+
return result.ok ? ok(result.value) : err(mapFn(result.error));
|
|
107
|
+
},
|
|
108
|
+
ok: (mapFn) => {
|
|
109
|
+
return result.ok ? ok(mapFn(result.value)) : err(result.error);
|
|
110
|
+
},
|
|
111
|
+
okAndErr: ({
|
|
112
|
+
ok: mapFn,
|
|
113
|
+
err: mapErrFn
|
|
114
|
+
}) => {
|
|
115
|
+
return result.ok ? ok(mapFn(result.value)) : err(mapErrFn(result.error));
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
var Result = {
|
|
120
|
+
ok,
|
|
121
|
+
err,
|
|
122
|
+
unknownToError,
|
|
123
|
+
unwrap,
|
|
124
|
+
asyncMap,
|
|
125
|
+
map: syncMap
|
|
126
|
+
};
|
|
127
|
+
function normalizeError(error) {
|
|
128
|
+
if (error instanceof Error) return error;
|
|
129
|
+
if (typeof error === "string") {
|
|
130
|
+
return new Error(error);
|
|
131
|
+
}
|
|
132
|
+
if (isObject(error)) {
|
|
133
|
+
return new Error(
|
|
134
|
+
"message" in error && error.message ? String(error.message) : safeJsonStringify(error) ?? "unknown",
|
|
135
|
+
{ cause: error }
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
return new Error(safeJsonStringify(error) ?? "unknown", { cause: error });
|
|
139
|
+
}
|
|
140
|
+
function safeJsonStringify(value) {
|
|
141
|
+
try {
|
|
142
|
+
return JSON.stringify(value);
|
|
143
|
+
} catch (_) {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// src/sleep.ts
|
|
149
|
+
function sleep(ms) {
|
|
150
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// src/parallelAsyncCalls.ts
|
|
154
|
+
var ParallelAsyncResultCalls = class {
|
|
155
|
+
pendingCalls = [];
|
|
156
|
+
alreadyRun = false;
|
|
157
|
+
constructor() {
|
|
158
|
+
}
|
|
159
|
+
add(call) {
|
|
160
|
+
this.pendingCalls.push(
|
|
161
|
+
isObject(call) ? call : { metadata: void 0, fn: call }
|
|
162
|
+
);
|
|
163
|
+
return this;
|
|
164
|
+
}
|
|
165
|
+
/** adds calls return tuples with inferred results */
|
|
166
|
+
addTuple(...calls) {
|
|
167
|
+
for (const call of calls) {
|
|
168
|
+
this.pendingCalls.push(
|
|
169
|
+
isObject(call) ? call : { metadata: void 0, fn: call }
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
return {
|
|
173
|
+
runAll: this.runAll.bind(this),
|
|
174
|
+
runAllSettled: this.runAllSettled.bind(this)
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
async runAllSettled({ delayStart } = {}) {
|
|
178
|
+
invariant(!this.alreadyRun, "Already run");
|
|
179
|
+
const asyncResults = await Promise.allSettled(
|
|
180
|
+
this.pendingCalls.map(async (call, i) => {
|
|
181
|
+
try {
|
|
182
|
+
if (delayStart) {
|
|
183
|
+
await sleep(delayStart(i));
|
|
184
|
+
}
|
|
185
|
+
const result = await call.fn();
|
|
186
|
+
return { result, callMetadata: call.metadata };
|
|
187
|
+
} catch (exception) {
|
|
188
|
+
return {
|
|
189
|
+
result: Result.unknownToError(exception),
|
|
190
|
+
callMetadata: call.metadata
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
})
|
|
194
|
+
);
|
|
195
|
+
const failed = [];
|
|
196
|
+
const succeeded = [];
|
|
197
|
+
const results = [];
|
|
198
|
+
for (const asyncResult of asyncResults) {
|
|
199
|
+
invariant(asyncResult.status === "fulfilled");
|
|
200
|
+
const { result, callMetadata } = asyncResult.value;
|
|
201
|
+
if (result.ok) {
|
|
202
|
+
const success = { value: result.value, metadata: callMetadata };
|
|
203
|
+
results.push(success);
|
|
204
|
+
succeeded.push(success);
|
|
205
|
+
} else {
|
|
206
|
+
const fail = { metadata: callMetadata, error: result.error };
|
|
207
|
+
results.push(fail);
|
|
208
|
+
failed.push(fail);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
const allFailed = failed.length === this.pendingCalls.length;
|
|
212
|
+
this.alreadyRun = true;
|
|
213
|
+
this.pendingCalls = [];
|
|
214
|
+
return {
|
|
215
|
+
failed,
|
|
216
|
+
succeeded,
|
|
217
|
+
allFailed,
|
|
218
|
+
results
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
async runAll({
|
|
222
|
+
delayStart
|
|
223
|
+
} = {}) {
|
|
224
|
+
invariant(!this.alreadyRun, "Already run");
|
|
225
|
+
try {
|
|
226
|
+
const asyncResults = await Promise.all(
|
|
227
|
+
this.pendingCalls.map(async (call, i) => {
|
|
228
|
+
try {
|
|
229
|
+
if (delayStart) {
|
|
230
|
+
await sleep(delayStart(i));
|
|
231
|
+
}
|
|
232
|
+
const result = await call.fn();
|
|
233
|
+
if (!result.ok) {
|
|
234
|
+
throw result.error;
|
|
235
|
+
}
|
|
236
|
+
return {
|
|
237
|
+
value: result.value,
|
|
238
|
+
metadata: call.metadata
|
|
239
|
+
};
|
|
240
|
+
} catch (exception) {
|
|
241
|
+
throw {
|
|
242
|
+
metadata: call.metadata,
|
|
243
|
+
error: normalizeError(exception)
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
})
|
|
247
|
+
);
|
|
248
|
+
return Result.ok(asyncResults);
|
|
249
|
+
} catch (exception) {
|
|
250
|
+
return Result.err(
|
|
251
|
+
exception
|
|
252
|
+
);
|
|
253
|
+
} finally {
|
|
254
|
+
this.alreadyRun = true;
|
|
255
|
+
this.pendingCalls = [];
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
function parallelAsyncCalls() {
|
|
260
|
+
return new ParallelAsyncResultCalls();
|
|
261
|
+
}
|
|
262
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
263
|
+
0 && (module.exports = {
|
|
264
|
+
parallelAsyncCalls
|
|
265
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Result
|
|
1
|
+
import { Result } from './rsResult.cjs';
|
|
2
2
|
|
|
3
3
|
type ValidMetadata = string | number | boolean | Record<string, unknown>;
|
|
4
4
|
type TupleRunAllSuccess<T> = T extends () => Promise<Result<infer V>> ? Succeeded<V, undefined> : T extends {
|
|
@@ -7,12 +7,12 @@ type TupleRunAllSuccess<T> = T extends () => Promise<Result<infer V>> ? Succeede
|
|
|
7
7
|
} ? Succeeded<V, M> : never;
|
|
8
8
|
type TupleRunAllFailed<T> = T extends () => Promise<Result<any>> ? {
|
|
9
9
|
metadata: undefined;
|
|
10
|
-
error:
|
|
10
|
+
error: Error;
|
|
11
11
|
} : T extends {
|
|
12
12
|
metadata: infer M extends ValidMetadata;
|
|
13
13
|
} ? {
|
|
14
14
|
metadata: M;
|
|
15
|
-
error:
|
|
15
|
+
error: Error;
|
|
16
16
|
} : never;
|
|
17
17
|
type TupleRunAllSettled<T> = T extends () => Promise<Result<infer V>> ? Succeeded<V, undefined> | Failed<undefined> : T extends {
|
|
18
18
|
metadata: infer M;
|
|
@@ -23,7 +23,7 @@ type RunProps = {
|
|
|
23
23
|
};
|
|
24
24
|
type Failed<M> = {
|
|
25
25
|
metadata: M;
|
|
26
|
-
error:
|
|
26
|
+
error: Error;
|
|
27
27
|
};
|
|
28
28
|
type Succeeded<R, M> = {
|
|
29
29
|
value: R;
|
|
@@ -62,7 +62,7 @@ declare class ParallelAsyncResultCalls<M extends ValidMetadata | undefined = und
|
|
|
62
62
|
delayStart?: (index: number) => number;
|
|
63
63
|
}): Promise<Result<Succeeded<R, M>[], {
|
|
64
64
|
metadata: M;
|
|
65
|
-
error:
|
|
65
|
+
error: Error;
|
|
66
66
|
}>>;
|
|
67
67
|
}
|
|
68
68
|
/**
|
|
@@ -71,6 +71,6 @@ declare class ParallelAsyncResultCalls<M extends ValidMetadata | undefined = und
|
|
|
71
71
|
* @template R - The type of the result value.
|
|
72
72
|
* @template M - The type of the call metadata.
|
|
73
73
|
*/
|
|
74
|
-
declare function
|
|
74
|
+
declare function parallelAsyncCalls<M extends ValidMetadata | undefined = undefined, R = unknown>(): ParallelAsyncResultCalls<M, R>;
|
|
75
75
|
|
|
76
|
-
export {
|
|
76
|
+
export { parallelAsyncCalls };
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Result } from './rsResult.js';
|
|
2
|
+
|
|
3
|
+
type ValidMetadata = string | number | boolean | Record<string, unknown>;
|
|
4
|
+
type TupleRunAllSuccess<T> = T extends () => Promise<Result<infer V>> ? Succeeded<V, undefined> : T extends {
|
|
5
|
+
metadata: infer M;
|
|
6
|
+
fn: () => Promise<Result<infer V>>;
|
|
7
|
+
} ? Succeeded<V, M> : never;
|
|
8
|
+
type TupleRunAllFailed<T> = T extends () => Promise<Result<any>> ? {
|
|
9
|
+
metadata: undefined;
|
|
10
|
+
error: Error;
|
|
11
|
+
} : T extends {
|
|
12
|
+
metadata: infer M extends ValidMetadata;
|
|
13
|
+
} ? {
|
|
14
|
+
metadata: M;
|
|
15
|
+
error: Error;
|
|
16
|
+
} : never;
|
|
17
|
+
type TupleRunAllSettled<T> = T extends () => Promise<Result<infer V>> ? Succeeded<V, undefined> | Failed<undefined> : T extends {
|
|
18
|
+
metadata: infer M;
|
|
19
|
+
fn: () => Promise<Result<infer V>>;
|
|
20
|
+
} ? Succeeded<V, M> | Failed<M> : never;
|
|
21
|
+
type RunProps = {
|
|
22
|
+
delayStart?: (index: number) => number;
|
|
23
|
+
};
|
|
24
|
+
type Failed<M> = {
|
|
25
|
+
metadata: M;
|
|
26
|
+
error: Error;
|
|
27
|
+
};
|
|
28
|
+
type Succeeded<R, M> = {
|
|
29
|
+
value: R;
|
|
30
|
+
metadata: M;
|
|
31
|
+
};
|
|
32
|
+
declare class ParallelAsyncResultCalls<M extends ValidMetadata | undefined = undefined, R = unknown> {
|
|
33
|
+
private pendingCalls;
|
|
34
|
+
alreadyRun: boolean;
|
|
35
|
+
constructor();
|
|
36
|
+
add(call: M extends undefined ? () => Promise<Result<R>> : {
|
|
37
|
+
metadata: M;
|
|
38
|
+
fn: () => Promise<Result<R>>;
|
|
39
|
+
}): this;
|
|
40
|
+
/** adds calls return tuples with inferred results */
|
|
41
|
+
addTuple<T extends (M extends undefined ? () => Promise<Result<R>> : {
|
|
42
|
+
metadata: M;
|
|
43
|
+
fn: () => Promise<Result<R>>;
|
|
44
|
+
})[]>(...calls: T): {
|
|
45
|
+
runAllSettled: (props?: RunProps) => Promise<{
|
|
46
|
+
results: {
|
|
47
|
+
[I in keyof T]: TupleRunAllSettled<T[I]>;
|
|
48
|
+
};
|
|
49
|
+
allFailed: boolean;
|
|
50
|
+
}>;
|
|
51
|
+
runAll: (props?: RunProps) => Promise<Result<{
|
|
52
|
+
[I in keyof T]: TupleRunAllSuccess<T[I]>;
|
|
53
|
+
}, TupleRunAllFailed<T[number]>>>;
|
|
54
|
+
};
|
|
55
|
+
runAllSettled({ delayStart }?: RunProps): Promise<{
|
|
56
|
+
failed: Failed<M>[];
|
|
57
|
+
succeeded: Succeeded<R, M>[];
|
|
58
|
+
allFailed: boolean;
|
|
59
|
+
results: (Failed<M> | Succeeded<R, M>)[];
|
|
60
|
+
}>;
|
|
61
|
+
runAll({ delayStart, }?: {
|
|
62
|
+
delayStart?: (index: number) => number;
|
|
63
|
+
}): Promise<Result<Succeeded<R, M>[], {
|
|
64
|
+
metadata: M;
|
|
65
|
+
error: Error;
|
|
66
|
+
}>>;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Executes multiple asynchronous calls in parallel and collects the results in a easier to use format.
|
|
70
|
+
*
|
|
71
|
+
* @template R - The type of the result value.
|
|
72
|
+
* @template M - The type of the call metadata.
|
|
73
|
+
*/
|
|
74
|
+
declare function parallelAsyncCalls<M extends ValidMetadata | undefined = undefined, R = unknown>(): ParallelAsyncResultCalls<M, R>;
|
|
75
|
+
|
|
76
|
+
export { parallelAsyncCalls };
|
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Result,
|
|
3
3
|
normalizeError
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-JOBPQAI3.js";
|
|
5
5
|
import {
|
|
6
6
|
sleep
|
|
7
|
-
} from "./chunk-
|
|
8
|
-
import "./chunk-GLZ5MZC5.js";
|
|
7
|
+
} from "./chunk-5DZT3Z5Z.js";
|
|
9
8
|
import {
|
|
10
9
|
invariant,
|
|
11
10
|
isObject
|
|
12
|
-
} from "./chunk-
|
|
11
|
+
} from "./chunk-4UGSP3L3.js";
|
|
13
12
|
|
|
14
|
-
// src/
|
|
13
|
+
// src/parallelAsyncCalls.ts
|
|
15
14
|
var ParallelAsyncResultCalls = class {
|
|
16
15
|
pendingCalls = [];
|
|
17
16
|
alreadyRun = false;
|
|
@@ -47,7 +46,7 @@ var ParallelAsyncResultCalls = class {
|
|
|
47
46
|
return { result, callMetadata: call.metadata };
|
|
48
47
|
} catch (exception) {
|
|
49
48
|
return {
|
|
50
|
-
result: Result.
|
|
49
|
+
result: Result.unknownToError(exception),
|
|
51
50
|
callMetadata: call.metadata
|
|
52
51
|
};
|
|
53
52
|
}
|
|
@@ -117,10 +116,9 @@ var ParallelAsyncResultCalls = class {
|
|
|
117
116
|
}
|
|
118
117
|
}
|
|
119
118
|
};
|
|
120
|
-
function
|
|
119
|
+
function parallelAsyncCalls() {
|
|
121
120
|
return new ParallelAsyncResultCalls();
|
|
122
121
|
}
|
|
123
122
|
export {
|
|
124
|
-
|
|
123
|
+
parallelAsyncCalls
|
|
125
124
|
};
|
|
126
|
-
//# sourceMappingURL=parallelAsyncResultCalls.js.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/promiseUtils.ts
|
|
21
|
+
var promiseUtils_exports = {};
|
|
22
|
+
__export(promiseUtils_exports, {
|
|
23
|
+
defer: () => defer
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(promiseUtils_exports);
|
|
26
|
+
function defer() {
|
|
27
|
+
let resolve;
|
|
28
|
+
let reject;
|
|
29
|
+
const promise = new Promise((_resolve, _reject) => {
|
|
30
|
+
resolve = _resolve;
|
|
31
|
+
reject = _reject;
|
|
32
|
+
});
|
|
33
|
+
return { resolve, reject, promise };
|
|
34
|
+
}
|
|
35
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
36
|
+
0 && (module.exports = {
|
|
37
|
+
defer
|
|
38
|
+
});
|
package/dist/promiseUtils.js
CHANGED