@shirudo/result 0.0.6 → 1.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/LICENSE +21 -0
- package/README.md +137 -296
- package/dist/collections.cjs +12 -0
- package/dist/collections.d.cts +3 -0
- package/dist/collections.d.mts +3 -0
- package/dist/collections.mjs +4 -0
- package/dist/errors-2WOswg7r.mjs +95 -0
- package/dist/errors-2WOswg7r.mjs.map +1 -0
- package/dist/errors-BFjY06EV.d.cts +55 -0
- package/dist/errors-BFjY06EV.d.cts.map +1 -0
- package/dist/errors-C5qGMRiU.d.mts +55 -0
- package/dist/errors-C5qGMRiU.d.mts.map +1 -0
- package/dist/errors-D2EMzJQl.cjs +209 -0
- package/dist/errors-D2EMzJQl.cjs.map +1 -0
- package/dist/errors.cjs +21 -0
- package/dist/errors.d.cts +2 -0
- package/dist/errors.d.mts +2 -0
- package/dist/errors.mjs +3 -0
- package/dist/flatten-B8bN6fiI.d.mts +71 -0
- package/dist/flatten-B8bN6fiI.d.mts.map +1 -0
- package/dist/flatten-C6Y9hx79.mjs +147 -0
- package/dist/flatten-C6Y9hx79.mjs.map +1 -0
- package/dist/flatten-DK7eJKPx.d.cts +71 -0
- package/dist/flatten-DK7eJKPx.d.cts.map +1 -0
- package/dist/flatten-Df9U40nO.cjs +182 -0
- package/dist/flatten-Df9U40nO.cjs.map +1 -0
- package/dist/index.cjs +113 -1018
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +12 -546
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +12 -546
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +25 -935
- package/dist/index.mjs.map +1 -1
- package/dist/mapOrElse-B0_4r6Jn.mjs +96 -0
- package/dist/mapOrElse-B0_4r6Jn.mjs.map +1 -0
- package/dist/mapOrElse-B5gx9x2E.d.mts +64 -0
- package/dist/mapOrElse-B5gx9x2E.d.mts.map +1 -0
- package/dist/mapOrElse-DIe7LkYV.d.cts +64 -0
- package/dist/mapOrElse-DIe7LkYV.d.cts.map +1 -0
- package/dist/mapOrElse-H-GvAUka.cjs +137 -0
- package/dist/mapOrElse-H-GvAUka.cjs.map +1 -0
- package/dist/operators.cjs +33 -0
- package/dist/operators.d.cts +3 -0
- package/dist/operators.d.mts +3 -0
- package/dist/operators.mjs +4 -0
- package/dist/result-CY-KIivk.cjs +1100 -0
- package/dist/result-CY-KIivk.cjs.map +1 -0
- package/dist/result-DKKaNdOx.mjs +861 -0
- package/dist/result-DKKaNdOx.mjs.map +1 -0
- package/dist/sequence-Br6tAIIu.d.cts +419 -0
- package/dist/sequence-Br6tAIIu.d.cts.map +1 -0
- package/dist/sequence-C0jlR3AY.d.mts +419 -0
- package/dist/sequence-C0jlR3AY.d.mts.map +1 -0
- package/package.json +44 -10
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { W as Awaitable, s as Result } from "./sequence-Br6tAIIu.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/core/sequenceRecord.d.ts
|
|
4
|
+
type OkValueOf$2<R$1> = R$1 extends Result<infer T, any> ? T : never;
|
|
5
|
+
type ErrValueOf$2<R$1> = R$1 extends Result<any, infer E> ? E : never;
|
|
6
|
+
/**
|
|
7
|
+
* Like `sequence`, but for Records/Objects.
|
|
8
|
+
* Short-circuits on the first Err.
|
|
9
|
+
*/
|
|
10
|
+
declare function sequenceRecord<const R$1 extends { readonly [K in keyof R$1]: Result<any, any> }>(record: R$1): Result<{ [K in keyof R$1]: OkValueOf$2<R$1[K]> }, ErrValueOf$2<R$1[keyof R$1]>>;
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/core/collectFirstOkAsync.d.ts
|
|
13
|
+
type CollectFirstOkAsyncInput$1 = Promise<Result<any, any>> | (() => Awaitable<Result<any, any>>);
|
|
14
|
+
type ResolvedResult$1<I> = I extends (() => infer R) ? Awaited<R> : I extends Promise<infer R> ? R : never;
|
|
15
|
+
type OkValueOfInput$1<I> = ResolvedResult$1<I> extends Result<infer T, any> ? T : never;
|
|
16
|
+
type ErrValueOfInput$1<I> = ResolvedResult$1<I> extends Result<any, infer E> ? E : never;
|
|
17
|
+
/**
|
|
18
|
+
* Async version of collectFirstOk.
|
|
19
|
+
*
|
|
20
|
+
* - Takes either already started Promises or "Thunks" (`() => Awaitable<Result<...>>`).
|
|
21
|
+
* - Processes inputs strictly sequentially (like `for ... of` + `await`).
|
|
22
|
+
* - Returns the first `Ok` and collects all errors if no `Ok` is found.
|
|
23
|
+
*/
|
|
24
|
+
declare function collectFirstOkAsync<const Inputs extends readonly CollectFirstOkAsyncInput$1[]>(inputs: Inputs): Promise<Result<OkValueOfInput$1<Inputs[number]>, ErrValueOfInput$1<Inputs[number]>[]>>;
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/core/collectFirstOkParallelAsync.d.ts
|
|
27
|
+
type CollectFirstOkAsyncInput = Promise<Result<any, any>> | (() => Awaitable<Result<any, any>>);
|
|
28
|
+
type ResolvedResult<I> = I extends (() => infer R) ? Awaited<R> : I extends Promise<infer R> ? R : never;
|
|
29
|
+
type OkValueOfInput<I> = ResolvedResult<I> extends Result<infer T, any> ? T : never;
|
|
30
|
+
type ErrValueOfInput<I> = ResolvedResult<I> extends Result<any, infer E> ? E : never;
|
|
31
|
+
/**
|
|
32
|
+
* Parallel version of `collectFirstOkAsync`.
|
|
33
|
+
*
|
|
34
|
+
* - Starts all inputs immediately (Promises or Thunks).
|
|
35
|
+
* - Returns the first `Ok` as soon as it is available.
|
|
36
|
+
* - If no `Ok` is found, returns an `Err` with all error values (in input order).
|
|
37
|
+
* - Rejections are treated as `ErrValue` (`caught as ErrValue`).
|
|
38
|
+
* - If multiple inputs provide an `Ok`, the one that completes first wins.
|
|
39
|
+
* In case of simultaneous completion, the first observed result wins.
|
|
40
|
+
* - If no `Ok` arrives and at least one input never settles, the Promise remains pending.
|
|
41
|
+
*/
|
|
42
|
+
declare function collectFirstOkParallelAsync<const Inputs extends readonly CollectFirstOkAsyncInput[]>(inputs: Inputs): Promise<Result<OkValueOfInput<Inputs[number]>, ErrValueOfInput<Inputs[number]>[]>>;
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/core/collectAllErrors.d.ts
|
|
45
|
+
type OkValueOf$1<R$1> = R$1 extends Result<infer T, any> ? T : never;
|
|
46
|
+
type ErrValueOf$1<R$1> = R$1 extends Result<any, infer E> ? E : never;
|
|
47
|
+
type CollectionValues<Results extends readonly Result<any, any>[]> = { -readonly [K in keyof Results]: OkValueOf$1<Results[K]> };
|
|
48
|
+
/**
|
|
49
|
+
* Combines a list of Results.
|
|
50
|
+
* Return Ok(values) only if all are Ok, otherwise Err([errors]).
|
|
51
|
+
*/
|
|
52
|
+
declare function collectAllErrors<const Results extends readonly Result<any, any>[]>(results: Results): Result<CollectionValues<Results>, Array<ErrValueOf$1<Results[number]>>>;
|
|
53
|
+
//#endregion
|
|
54
|
+
//#region src/core/partition.d.ts
|
|
55
|
+
type OkValueOf<R$1> = R$1 extends Result<infer T, any> ? T : never;
|
|
56
|
+
type ErrValueOf<R$1> = R$1 extends Result<any, infer E> ? E : never;
|
|
57
|
+
/**
|
|
58
|
+
* Partitions Results into Ok values and Err errors.
|
|
59
|
+
*/
|
|
60
|
+
declare function partition<const Results extends readonly Result<any, any>[]>(results: Results): [oks: Array<OkValueOf<Results[number]>>, errs: Array<ErrValueOf<Results[number]>>];
|
|
61
|
+
//#endregion
|
|
62
|
+
//#region src/core/flatten.d.ts
|
|
63
|
+
/**
|
|
64
|
+
* Flattens a nested Result.
|
|
65
|
+
* Result<Result<T, E>, E> → Result<T, E>
|
|
66
|
+
* Corresponds to Rust `flatten`.
|
|
67
|
+
*/
|
|
68
|
+
declare function flatten<T, E>(result: Result<Result<T, E>, E>): Result<T, E>;
|
|
69
|
+
//#endregion
|
|
70
|
+
export { collectFirstOkAsync as a, collectFirstOkParallelAsync as i, partition as n, sequenceRecord as o, collectAllErrors as r, flatten as t };
|
|
71
|
+
//# sourceMappingURL=flatten-DK7eJKPx.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flatten-DK7eJKPx.d.cts","names":[],"sources":["../src/core/sequenceRecord.ts","../src/core/collectFirstOkAsync.ts","../src/core/collectFirstOkParallelAsync.ts","../src/core/collectAllErrors.ts","../src/core/partition.ts","../src/core/flatten.ts"],"sourcesContent":[],"mappings":";;;KAKK,mBAAe,YAAU;KACzB,oBAAgB,YAAU;AANQ;AAKH;AAOpC;;AAA0E,iBAA1D,cAA0D,CAAA,kBAAA,iBAC9D,MAD0D,GAC1D,GAD8D,MAC9D,CAAA,GAAA,EAAA,GAAA,CAAA,EACY,CAAA,CAAA,MAAA,EADZ,GACY,CAAA,EAArB,MAAqB,CAAA,QAAc,MAAd,GAAc,GAAV,WAAU,CAAA,GAAA,CAAE,CAAF,CAAA,CAAA,EAAE,EAAO,YAAP,CAAkB,GAAlB,CAAA,MAA0B,GAA1B,CAAA,CAAA,CAAA;;;KCTnC,0BAAA,GACC,QAAQ,2BACD,UAAU;ADPgB,KCSlC,gBDJS,CAAA,CAAA,CAAA,GCIW,CDJX,UAAM,GAAU,GAAA,KAAM,EAAA,ICIe,ODJf,CCIuB,CDJvB,CAAA,GCI4B,CDJ5B,SCIsC,ODJtC,CAAA,KAAA,EAAA,CAAA,GAAA,CAAA,GAAA,KAAA;AAAA,KCK/B,gBDJU,CAAA,CAAA,CAAA,GCIU,gBDJM,CCIS,CDJT,CAAA,SCIoB,MDJd,CAAA,KAAA,EAAA,EAAA,GAAA,CAAA,GAAA,CAAA,GAAA,KAAA;AAMrC,KCDK,iBDCW,CAAc,CAAA,CAAA,GCDJ,gBDCI,CCDW,CDCX,CAAA,SCDsB,MDCtB,CAAA,GAAA,EAAA,KAAA,EAAA,CAAA,GAAA,CAAA,GAAA,KAAA;;;;;;;;AAE4B,iBCMpC,mBDNoC,CAAA,qBAAA,SCMc,0BDNd,EAAA,CAAA,CAAA,MAAA,ECO9C,MDP8C,CAAA,ECQvD,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AAA,OAAA,EGCzB,OHDyB,CAAA,EGEnC,MHFmC,CGE5B,gBHF4B,CGEX,OHFW,CAAA,EGED,KHFC,CGEK,YHFL,CGEgB,OHFhB,CAAA,MAAA,CAAA,CAAA,CAAA,CAAA;;;KIXjC,iBAAe,YAAU;KACzB,kBAAgB,YAAU;AJJQ;AAKH;AAOpC;AAAsE,iBIHtD,SJGsD,CAAA,sBAAA,SIHb,MJGa,CAAA,GAAA,EAAA,GAAA,CAAA,EAAA,CAAA,CAAA,OAAA,EIFzD,OJEyD,CAAA,EAAA,CAAA,GAAA,EID7D,KJC6D,CIDvD,SJCuD,CID7C,OJC6C,CAAA,MAAA,CAAA,CAAA,CAAA,EAAA,IAAA,EIDpB,KJCoB,CIDd,UJCc,CIDH,OJCG,CAAA,MAAA,CAAA,CAAA,CAAA,CAAA;;;;;AAZ/B;AAKH;AAOpC;AAAsE,iBKJtD,OLIsD,CAAA,CAAA,EAAA,CAAA,CAAA,CAAA,MAAA,EKJhC,MLIgC,CKJzB,MLIyB,CKJlB,CLIkB,EKJf,CLIe,CAAA,EKJX,CLIW,CAAA,CAAA,EKJN,MLIM,CKJC,CLID,EKJI,CLIJ,CAAA"}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
const require_result = require('./result-CY-KIivk.cjs');
|
|
2
|
+
const require_errors = require('./errors-D2EMzJQl.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/core/sequenceRecord.ts
|
|
5
|
+
/**
|
|
6
|
+
* Like `sequence`, but for Records/Objects.
|
|
7
|
+
* Short-circuits on the first Err.
|
|
8
|
+
*/
|
|
9
|
+
function sequenceRecord(record) {
|
|
10
|
+
const out = {};
|
|
11
|
+
for (const key of Reflect.ownKeys(record)) {
|
|
12
|
+
const result = record[key];
|
|
13
|
+
if (!require_result.isResult(result)) throw new require_errors.InvalidResultStateError("sequenceRecord");
|
|
14
|
+
if (result.isOk()) {
|
|
15
|
+
out[key] = result.value;
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
if (result.isErr()) return result;
|
|
19
|
+
throw new require_errors.InvalidResultStateError("sequenceRecord");
|
|
20
|
+
}
|
|
21
|
+
return require_result.ok(out);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/core/collectFirstOkAsync.ts
|
|
26
|
+
/**
|
|
27
|
+
* Async version of collectFirstOk.
|
|
28
|
+
*
|
|
29
|
+
* - Takes either already started Promises or "Thunks" (`() => Awaitable<Result<...>>`).
|
|
30
|
+
* - Processes inputs strictly sequentially (like `for ... of` + `await`).
|
|
31
|
+
* - Returns the first `Ok` and collects all errors if no `Ok` is found.
|
|
32
|
+
*/
|
|
33
|
+
async function collectFirstOkAsync(inputs) {
|
|
34
|
+
const errors = [];
|
|
35
|
+
for (const input of inputs) {
|
|
36
|
+
const pendingResult = typeof input === "function" ? input() : input;
|
|
37
|
+
let result;
|
|
38
|
+
try {
|
|
39
|
+
result = await pendingResult;
|
|
40
|
+
} catch (error) {
|
|
41
|
+
errors.push(error);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (result.isOk()) return require_result.ok(result.value);
|
|
45
|
+
if (result.isErr()) {
|
|
46
|
+
errors.push(result.error);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
throw new require_errors.InvalidResultStateError("collectFirstOkAsync");
|
|
50
|
+
}
|
|
51
|
+
return require_result.err(errors);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/core/collectFirstOkParallelAsync.ts
|
|
56
|
+
/**
|
|
57
|
+
* Parallel version of `collectFirstOkAsync`.
|
|
58
|
+
*
|
|
59
|
+
* - Starts all inputs immediately (Promises or Thunks).
|
|
60
|
+
* - Returns the first `Ok` as soon as it is available.
|
|
61
|
+
* - If no `Ok` is found, returns an `Err` with all error values (in input order).
|
|
62
|
+
* - Rejections are treated as `ErrValue` (`caught as ErrValue`).
|
|
63
|
+
* - If multiple inputs provide an `Ok`, the one that completes first wins.
|
|
64
|
+
* In case of simultaneous completion, the first observed result wins.
|
|
65
|
+
* - If no `Ok` arrives and at least one input never settles, the Promise remains pending.
|
|
66
|
+
*/
|
|
67
|
+
async function collectFirstOkParallelAsync(inputs) {
|
|
68
|
+
if (inputs.length === 0) return require_result.err([]);
|
|
69
|
+
const started = inputs.map((input) => typeof input === "function" ? Promise.resolve().then(input) : input);
|
|
70
|
+
const firstOk = new Promise((resolve) => {
|
|
71
|
+
for (const promise of started) promise.then((result) => {
|
|
72
|
+
if (result.isOk()) resolve(require_result.ok(result.value));
|
|
73
|
+
}).catch(() => {});
|
|
74
|
+
});
|
|
75
|
+
const allErrors = Promise.allSettled(started).then((settled) => {
|
|
76
|
+
const errors = [];
|
|
77
|
+
for (const entry of settled) if (entry.status === "fulfilled") {
|
|
78
|
+
const result = entry.value;
|
|
79
|
+
if (result.isErr()) errors.push(result.error);
|
|
80
|
+
else if (!result.isOk()) throw new require_errors.InvalidResultStateError("collectFirstOkParallelAsync");
|
|
81
|
+
} else errors.push(entry.reason);
|
|
82
|
+
return require_result.err(errors);
|
|
83
|
+
});
|
|
84
|
+
return Promise.race([firstOk, allErrors]);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/core/collectAllErrors.ts
|
|
89
|
+
/**
|
|
90
|
+
* Combines a list of Results.
|
|
91
|
+
* Return Ok(values) only if all are Ok, otherwise Err([errors]).
|
|
92
|
+
*/
|
|
93
|
+
function collectAllErrors(results) {
|
|
94
|
+
const values = [];
|
|
95
|
+
const errors = [];
|
|
96
|
+
for (const result of results) {
|
|
97
|
+
if (result.isOk()) {
|
|
98
|
+
values.push(result.value);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (result.isErr()) {
|
|
102
|
+
errors.push(result.error);
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
throw new require_errors.InvalidResultStateError("collectAllErrors");
|
|
106
|
+
}
|
|
107
|
+
return errors.length === 0 ? require_result.ok(values) : require_result.err(errors);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
//#endregion
|
|
111
|
+
//#region src/core/partition.ts
|
|
112
|
+
/**
|
|
113
|
+
* Partitions Results into Ok values and Err errors.
|
|
114
|
+
*/
|
|
115
|
+
function partition(results) {
|
|
116
|
+
const oks = [];
|
|
117
|
+
const errs = [];
|
|
118
|
+
for (const result of results) {
|
|
119
|
+
if (result.isOk()) {
|
|
120
|
+
oks.push(result.value);
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (result.isErr()) {
|
|
124
|
+
errs.push(result.error);
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
throw new require_errors.InvalidResultStateError("partition");
|
|
128
|
+
}
|
|
129
|
+
return [oks, errs];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
//#endregion
|
|
133
|
+
//#region src/core/flatten.ts
|
|
134
|
+
/**
|
|
135
|
+
* Flattens a nested Result.
|
|
136
|
+
* Result<Result<T, E>, E> → Result<T, E>
|
|
137
|
+
* Corresponds to Rust `flatten`.
|
|
138
|
+
*/
|
|
139
|
+
function flatten(result) {
|
|
140
|
+
if (result.isOk()) return result.value;
|
|
141
|
+
if (result.isErr()) return result;
|
|
142
|
+
throw new require_errors.InvalidResultStateError("flatten");
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
//#endregion
|
|
146
|
+
Object.defineProperty(exports, 'collectAllErrors', {
|
|
147
|
+
enumerable: true,
|
|
148
|
+
get: function () {
|
|
149
|
+
return collectAllErrors;
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
Object.defineProperty(exports, 'collectFirstOkAsync', {
|
|
153
|
+
enumerable: true,
|
|
154
|
+
get: function () {
|
|
155
|
+
return collectFirstOkAsync;
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
Object.defineProperty(exports, 'collectFirstOkParallelAsync', {
|
|
159
|
+
enumerable: true,
|
|
160
|
+
get: function () {
|
|
161
|
+
return collectFirstOkParallelAsync;
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
Object.defineProperty(exports, 'flatten', {
|
|
165
|
+
enumerable: true,
|
|
166
|
+
get: function () {
|
|
167
|
+
return flatten;
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
Object.defineProperty(exports, 'partition', {
|
|
171
|
+
enumerable: true,
|
|
172
|
+
get: function () {
|
|
173
|
+
return partition;
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
Object.defineProperty(exports, 'sequenceRecord', {
|
|
177
|
+
enumerable: true,
|
|
178
|
+
get: function () {
|
|
179
|
+
return sequenceRecord;
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
//# sourceMappingURL=flatten-Df9U40nO.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flatten-Df9U40nO.cjs","names":["out: Partial<Out>","isResult","InvalidResultStateError","ok","errors: Array<ErrValueOfInput<Inputs[number]>>","result: Result<any, any>","ok","InvalidResultStateError","err","err","ok","errors: ErrValue[]","InvalidResultStateError","values: Array<OkValueOf<Results[number]>>","errors: Array<ErrValueOf<Results[number]>>","InvalidResultStateError","ok","err","oks: Array<OkValueOf<Results[number]>>","errs: Array<ErrValueOf<Results[number]>>","InvalidResultStateError","InvalidResultStateError"],"sources":["../src/core/sequenceRecord.ts","../src/core/collectFirstOkAsync.ts","../src/core/collectFirstOkParallelAsync.ts","../src/core/collectAllErrors.ts","../src/core/partition.ts","../src/core/flatten.ts"],"sourcesContent":["import type { Result } from './result';\nimport { ok } from './result';\nimport { InvalidResultStateError } from '../errors';\nimport { isResult } from './isResult';\n\ntype OkValueOf<R> = R extends Result<infer T, any> ? T : never;\ntype ErrValueOf<R> = R extends Result<any, infer E> ? E : never;\n\n/**\n * Like `sequence`, but for Records/Objects.\n * Short-circuits on the first Err.\n */\nexport function sequenceRecord<const R extends { readonly [K in keyof R]: Result<any, any> }>(\n record: R\n): Result<{ [K in keyof R]: OkValueOf<R[K]> }, ErrValueOf<R[keyof R]>> {\n type Out = { [K in keyof R]: OkValueOf<R[K]> };\n type E = ErrValueOf<R[keyof R]>;\n\n const out: Partial<Out> = {};\n\n for (const key of Reflect.ownKeys(record) as Array<keyof R>) {\n const result = record[key];\n if (!isResult(result)) throw new InvalidResultStateError('sequenceRecord');\n if (result.isOk()) {\n out[key] = result.value as Out[typeof key];\n continue;\n }\n if (result.isErr()) return result as unknown as Result<Out, E>;\n throw new InvalidResultStateError('sequenceRecord');\n }\n\n return ok<Out, E>(out as Out);\n}\n","import type { Result } from './result';\nimport { ok, err } from './result';\nimport type { Awaitable } from './pipeable';\nimport { InvalidResultStateError } from '../errors';\n\ntype CollectFirstOkAsyncInput =\n | Promise<Result<any, any>>\n | (() => Awaitable<Result<any, any>>);\n\ntype ResolvedResult<I> = I extends () => infer R ? Awaited<R> : I extends Promise<infer R> ? R : never;\ntype OkValueOfInput<I> = ResolvedResult<I> extends Result<infer T, any> ? T : never;\ntype ErrValueOfInput<I> = ResolvedResult<I> extends Result<any, infer E> ? E : never;\n\n/**\n * Async version of collectFirstOk.\n *\n * - Takes either already started Promises or \"Thunks\" (`() => Awaitable<Result<...>>`).\n * - Processes inputs strictly sequentially (like `for ... of` + `await`).\n * - Returns the first `Ok` and collects all errors if no `Ok` is found.\n */\nexport async function collectFirstOkAsync<const Inputs extends readonly CollectFirstOkAsyncInput[]>(\n inputs: Inputs\n): Promise<Result<OkValueOfInput<Inputs[number]>, ErrValueOfInput<Inputs[number]>[]>> {\n const errors: Array<ErrValueOfInput<Inputs[number]>> = [];\n\n for (const input of inputs) {\n const pendingResult = typeof input === 'function' ? input() : input;\n let result: Result<any, any>;\n try {\n result = await pendingResult;\n } catch (error) {\n errors.push(error as ErrValueOfInput<Inputs[number]>);\n continue;\n }\n\n if (result.isOk()) {\n return ok<OkValueOfInput<Inputs[number]>, ErrValueOfInput<Inputs[number]>[]>(\n result.value as OkValueOfInput<Inputs[number]>\n );\n }\n if (result.isErr()) {\n errors.push(result.error as ErrValueOfInput<Inputs[number]>);\n continue;\n }\n throw new InvalidResultStateError('collectFirstOkAsync');\n }\n\n return err<ErrValueOfInput<Inputs[number]>[], OkValueOfInput<Inputs[number]>>(errors);\n}\n","import type { Awaitable } from './pipeable';\nimport type { Result } from './result';\nimport { err, ok } from './result';\nimport { InvalidResultStateError } from '../errors';\n\ntype CollectFirstOkAsyncInput = Promise<Result<any, any>> | (() => Awaitable<Result<any, any>>);\n\ntype ResolvedResult<I> = I extends () => infer R ? Awaited<R> : I extends Promise<infer R> ? R : never;\ntype OkValueOfInput<I> = ResolvedResult<I> extends Result<infer T, any> ? T : never;\ntype ErrValueOfInput<I> = ResolvedResult<I> extends Result<any, infer E> ? E : never;\n\n/**\n * Parallel version of `collectFirstOkAsync`.\n *\n * - Starts all inputs immediately (Promises or Thunks).\n * - Returns the first `Ok` as soon as it is available.\n * - If no `Ok` is found, returns an `Err` with all error values (in input order).\n * - Rejections are treated as `ErrValue` (`caught as ErrValue`).\n * - If multiple inputs provide an `Ok`, the one that completes first wins.\n * In case of simultaneous completion, the first observed result wins.\n * - If no `Ok` arrives and at least one input never settles, the Promise remains pending.\n */\nexport async function collectFirstOkParallelAsync<const Inputs extends readonly CollectFirstOkAsyncInput[]>(\n inputs: Inputs\n): Promise<Result<OkValueOfInput<Inputs[number]>, ErrValueOfInput<Inputs[number]>[]>> {\n type OkValue = OkValueOfInput<Inputs[number]>;\n type ErrValue = ErrValueOfInput<Inputs[number]>;\n\n if (inputs.length === 0) {\n return err<ErrValue[], OkValue>([]);\n }\n\n const started = inputs.map((input) =>\n typeof input === 'function' ? Promise.resolve().then(input) : input\n );\n\n const firstOk = new Promise<Result<OkValue, ErrValue[]>>((resolve) => {\n for (const promise of started) {\n promise\n .then((result) => {\n if (result.isOk()) {\n resolve(ok<OkValue, ErrValue[]>(result.value as OkValue));\n }\n })\n .catch(() => {\n // Ignored here; handled in allSettled below.\n });\n }\n });\n\n const allErrors = Promise.allSettled(started).then((settled) => {\n const errors: ErrValue[] = [];\n for (const entry of settled) {\n if (entry.status === 'fulfilled') {\n const result = entry.value;\n if (result.isErr()) {\n errors.push(result.error as ErrValue);\n } else if (!result.isOk()) {\n throw new InvalidResultStateError('collectFirstOkParallelAsync');\n }\n } else {\n errors.push(entry.reason as ErrValue);\n }\n }\n return err<ErrValue[], OkValue>(errors);\n });\n\n return Promise.race([firstOk, allErrors]);\n}\n","import type { Result } from './result';\nimport { ok, err } from './result';\nimport { InvalidResultStateError } from '../errors';\n\ntype OkValueOf<R> = R extends Result<infer T, any> ? T : never;\ntype ErrValueOf<R> = R extends Result<any, infer E> ? E : never;\ntype CollectionValues<Results extends readonly Result<any, any>[]> = {\n -readonly [K in keyof Results]: OkValueOf<Results[K]>;\n};\n\n/**\n * Combines a list of Results.\n * Return Ok(values) only if all are Ok, otherwise Err([errors]).\n */\nexport function collectAllErrors<const Results extends readonly Result<any, any>[]>(\n results: Results\n): Result<CollectionValues<Results>, Array<ErrValueOf<Results[number]>>> {\n const values: Array<OkValueOf<Results[number]>> = [];\n const errors: Array<ErrValueOf<Results[number]>> = [];\n\n for (const result of results) {\n if (result.isOk()) {\n values.push(result.value as OkValueOf<Results[number]>);\n continue;\n }\n if (result.isErr()) {\n errors.push(result.error as ErrValueOf<Results[number]>);\n continue;\n }\n throw new InvalidResultStateError('collectAllErrors');\n }\n\n return errors.length === 0\n ? ok<CollectionValues<Results>, Array<ErrValueOf<Results[number]>>>(values as CollectionValues<Results>)\n : err<Array<ErrValueOf<Results[number]>>, CollectionValues<Results>>(errors);\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\ntype OkValueOf<R> = R extends Result<infer T, any> ? T : never;\ntype ErrValueOf<R> = R extends Result<any, infer E> ? E : never;\n\n/**\n * Partitions Results into Ok values and Err errors.\n */\nexport function partition<const Results extends readonly Result<any, any>[]>(\n results: Results\n): [oks: Array<OkValueOf<Results[number]>>, errs: Array<ErrValueOf<Results[number]>>] {\n const oks: Array<OkValueOf<Results[number]>> = [];\n const errs: Array<ErrValueOf<Results[number]>> = [];\n\n for (const result of results) {\n if (result.isOk()) {\n oks.push(result.value as OkValueOf<Results[number]>);\n continue;\n }\n if (result.isErr()) {\n errs.push(result.error as ErrValueOf<Results[number]>);\n continue;\n }\n throw new InvalidResultStateError('partition');\n }\n\n return [oks, errs];\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Flattens a nested Result.\n * Result<Result<T, E>, E> → Result<T, E>\n * Corresponds to Rust `flatten`.\n */\nexport function flatten<T, E>(result: Result<Result<T, E>, E>): Result<T, E> {\n if (result.isOk()) {\n return result.value;\n }\n if (result.isErr()) return result as unknown as Result<T, E>;\n throw new InvalidResultStateError('flatten');\n}\n"],"mappings":";;;;;;;;AAYA,SAAgB,eACZ,QACmE;CAInE,MAAMA,MAAoB,EAAE;AAE5B,MAAK,MAAM,OAAO,QAAQ,QAAQ,OAAO,EAAoB;EACzD,MAAM,SAAS,OAAO;AACtB,MAAI,CAACC,wBAAS,OAAO,CAAE,OAAM,IAAIC,uCAAwB,iBAAiB;AAC1E,MAAI,OAAO,MAAM,EAAE;AACf,OAAI,OAAO,OAAO;AAClB;;AAEJ,MAAI,OAAO,OAAO,CAAE,QAAO;AAC3B,QAAM,IAAIA,uCAAwB,iBAAiB;;AAGvD,QAAOC,kBAAW,IAAW;;;;;;;;;;;;ACXjC,eAAsB,oBAClB,QACkF;CAClF,MAAMC,SAAiD,EAAE;AAEzD,MAAK,MAAM,SAAS,QAAQ;EACxB,MAAM,gBAAgB,OAAO,UAAU,aAAa,OAAO,GAAG;EAC9D,IAAIC;AACJ,MAAI;AACA,YAAS,MAAM;WACV,OAAO;AACZ,UAAO,KAAK,MAAyC;AACrD;;AAGJ,MAAI,OAAO,MAAM,CACb,QAAOC,kBACH,OAAO,MACV;AAEL,MAAI,OAAO,OAAO,EAAE;AAChB,UAAO,KAAK,OAAO,MAAyC;AAC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