@shirudo/result 0.0.6 → 1.0.1
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 +136 -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
package/dist/index.mjs
CHANGED
|
@@ -1,679 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
for (const op of ops) ret = op(ret);
|
|
6
|
-
return ret;
|
|
7
|
-
}
|
|
8
|
-
async pipeAsync(...ops) {
|
|
9
|
-
let ret = this;
|
|
10
|
-
for (const op of ops) ret = await op(ret);
|
|
11
|
-
return ret;
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
//#endregion
|
|
16
|
-
//#region src/errors.ts
|
|
17
|
-
const ERR_INVALID_STATE = "ERR_INVALID_STATE";
|
|
18
|
-
const ERR_TASK_YIELD_NOT_RESULT = "ERR_TASK_YIELD_NOT_RESULT";
|
|
19
|
-
const ERR_MATCH_ON_OK = "ERR_MATCH_ON_OK";
|
|
20
|
-
const ERR_UNWRAP_ON_ERR = "ERR_UNWRAP_ON_ERR";
|
|
21
|
-
const ERR_UNWRAP_ERR_ON_OK = "ERR_UNWRAP_ERR_ON_OK";
|
|
22
|
-
const ERR_EXPECT_OK = "ERR_EXPECT_OK";
|
|
23
|
-
const ERR_EXPECT_ERR = "ERR_EXPECT_ERR";
|
|
24
|
-
const formatResultErrorMessage = (code, message, context) => {
|
|
25
|
-
if (context) return `${code}: ${message} (context: ${context})`;
|
|
26
|
-
return `${code}: ${message}`;
|
|
27
|
-
};
|
|
28
|
-
var ResultError = class extends Error {
|
|
29
|
-
code;
|
|
30
|
-
context;
|
|
31
|
-
constructor(message, code, context) {
|
|
32
|
-
super(formatResultErrorMessage(code, message, context));
|
|
33
|
-
this.code = code;
|
|
34
|
-
this.context = context;
|
|
35
|
-
this.name = new.target.name;
|
|
36
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
var ResultTypeError = class extends TypeError {
|
|
40
|
-
code;
|
|
41
|
-
context;
|
|
42
|
-
constructor(message, code, context) {
|
|
43
|
-
super(formatResultErrorMessage(code, message, context));
|
|
44
|
-
this.code = code;
|
|
45
|
-
this.context = context;
|
|
46
|
-
this.name = new.target.name;
|
|
47
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
const INVALID_RESULT_STATE_MESSAGE = "Unreachable: Result is neither Ok nor Err";
|
|
51
|
-
var InvalidResultStateError = class extends ResultError {
|
|
52
|
-
constructor(context) {
|
|
53
|
-
super(INVALID_RESULT_STATE_MESSAGE, ERR_INVALID_STATE, context);
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
var TaskYieldNotResultError = class extends ResultTypeError {
|
|
57
|
-
yieldedValue;
|
|
58
|
-
constructor(yieldedValue) {
|
|
59
|
-
super("task() expected yielded values to be Result. Use `yield*` on a Result.", ERR_TASK_YIELD_NOT_RESULT);
|
|
60
|
-
this.yieldedValue = yieldedValue;
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
var MatchOnOkError = class extends ResultTypeError {
|
|
64
|
-
constructor() {
|
|
65
|
-
super("match() can only be called on Err results. Use `if (result.isErr()) { ... }` first.", ERR_MATCH_ON_OK);
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
var UnwrapOnErrError = class extends ResultTypeError {
|
|
69
|
-
errorValue;
|
|
70
|
-
constructor(errorValue) {
|
|
71
|
-
super(`Called unwrap() on Err: ${String(errorValue)}`, ERR_UNWRAP_ON_ERR);
|
|
72
|
-
this.errorValue = errorValue;
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
var UnwrapErrOnOkError = class extends ResultTypeError {
|
|
76
|
-
okValue;
|
|
77
|
-
constructor(okValue) {
|
|
78
|
-
super(`Called unwrapErr() on Ok: ${String(okValue)}`, ERR_UNWRAP_ERR_ON_OK);
|
|
79
|
-
this.okValue = okValue;
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
var ExpectOkError = class extends ResultError {
|
|
83
|
-
expectedMessage;
|
|
84
|
-
constructor(expectedMessage) {
|
|
85
|
-
super(expectedMessage, ERR_EXPECT_OK);
|
|
86
|
-
this.expectedMessage = expectedMessage;
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
var ExpectErrError = class extends ResultError {
|
|
90
|
-
expectedMessage;
|
|
91
|
-
constructor(expectedMessage) {
|
|
92
|
-
super(expectedMessage, ERR_EXPECT_ERR);
|
|
93
|
-
this.expectedMessage = expectedMessage;
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
//#endregion
|
|
98
|
-
//#region src/core/matcher.ts
|
|
99
|
-
/**
|
|
100
|
-
* Matcher for Err values (returns an arbitrary return type, e.g. string messages).
|
|
101
|
-
*
|
|
102
|
-
* - `.when(Ctor, handler)` matches via `instanceof`
|
|
103
|
-
* - `.whenGuard(guard, handler)` matches via Type-Guard
|
|
104
|
-
* - `.run()` is only allowed if all error cases have been handled (`E` has been reduced to `never`)
|
|
105
|
-
*/
|
|
106
|
-
var ErrorMatchBuilder = class ErrorMatchBuilder {
|
|
107
|
-
#error;
|
|
108
|
-
#matched;
|
|
109
|
-
#value;
|
|
110
|
-
constructor(error, matched = false, value) {
|
|
111
|
-
this.#error = error;
|
|
112
|
-
this.#matched = matched;
|
|
113
|
-
this.#value = value;
|
|
114
|
-
Object.freeze(this);
|
|
115
|
-
}
|
|
116
|
-
when(ctor, handler) {
|
|
117
|
-
if (this.#matched) return this;
|
|
118
|
-
if (this.#error instanceof ctor) return new ErrorMatchBuilder(this.#error, true, handler(this.#error));
|
|
119
|
-
return this;
|
|
120
|
-
}
|
|
121
|
-
whenGuard(guard, handler) {
|
|
122
|
-
if (this.#matched) return this;
|
|
123
|
-
const error = this.#error;
|
|
124
|
-
if (guard(error)) return new ErrorMatchBuilder(this.#error, true, handler(error));
|
|
125
|
-
return this;
|
|
126
|
-
}
|
|
127
|
-
otherwise(handler) {
|
|
128
|
-
if (this.#matched) return this.#value;
|
|
129
|
-
return handler(this.#error);
|
|
130
|
-
}
|
|
131
|
-
run() {
|
|
132
|
-
if (this.#matched) return this.#value;
|
|
133
|
-
throw this.#error;
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
function isResult$1(value) {
|
|
137
|
-
return typeof value === "object" && value !== null && "isOk" in value && typeof value.isOk === "function" && "isErr" in value && typeof value.isErr === "function";
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Matcher for `Result` Errors, which returns a `Result` again.
|
|
141
|
-
*
|
|
142
|
-
* Handlers may:
|
|
143
|
-
* - return a `Result` (is returned directly)
|
|
144
|
-
* - return an Error value (is automatically wrapped into `Err(error)`)
|
|
145
|
-
*/
|
|
146
|
-
var ErrMatchBuilder = class ErrMatchBuilder {
|
|
147
|
-
#makeErr;
|
|
148
|
-
#error;
|
|
149
|
-
#resolved;
|
|
150
|
-
constructor(makeErr, error, resolved) {
|
|
151
|
-
this.#makeErr = makeErr;
|
|
152
|
-
this.#error = error;
|
|
153
|
-
this.#resolved = resolved;
|
|
154
|
-
Object.freeze(this);
|
|
155
|
-
}
|
|
156
|
-
static fromResult(result, makeErr) {
|
|
157
|
-
if (result.isOk()) return new ErrMatchBuilder(makeErr, void 0, result);
|
|
158
|
-
if (result.isErr()) return new ErrMatchBuilder(makeErr, result.error);
|
|
159
|
-
throw new InvalidResultStateError("ErrMatchBuilder.fromResult");
|
|
160
|
-
}
|
|
161
|
-
when(ctor, handler) {
|
|
162
|
-
if (this.#resolved) return this;
|
|
163
|
-
if (this.#error instanceof ctor) {
|
|
164
|
-
const out = handler(this.#error);
|
|
165
|
-
const resolved = isResult$1(out) ? out : this.#makeErr(out);
|
|
166
|
-
return new ErrMatchBuilder(this.#makeErr, this.#error, resolved);
|
|
167
|
-
}
|
|
168
|
-
return this;
|
|
169
|
-
}
|
|
170
|
-
whenGuard(guard, handler) {
|
|
171
|
-
if (this.#resolved) return this;
|
|
172
|
-
const error = this.#error;
|
|
173
|
-
if (guard(error)) {
|
|
174
|
-
const out = handler(error);
|
|
175
|
-
const resolved = isResult$1(out) ? out : this.#makeErr(out);
|
|
176
|
-
return new ErrMatchBuilder(this.#makeErr, this.#error, resolved);
|
|
177
|
-
}
|
|
178
|
-
return this;
|
|
179
|
-
}
|
|
180
|
-
otherwise(handler) {
|
|
181
|
-
if (this.#resolved) return this.#resolved;
|
|
182
|
-
const out = handler(this.#error);
|
|
183
|
-
return isResult$1(out) ? out : this.#makeErr(out);
|
|
184
|
-
}
|
|
185
|
-
run() {
|
|
186
|
-
if (this.#resolved) return this.#resolved;
|
|
187
|
-
throw this.#error;
|
|
188
|
-
}
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
//#endregion
|
|
192
|
-
//#region src/core/map.ts
|
|
193
|
-
/**
|
|
194
|
-
* Transforms the value (Ok case).
|
|
195
|
-
* Corresponds to Rust `map`.
|
|
196
|
-
*/
|
|
197
|
-
function map(project) {
|
|
198
|
-
return (source) => {
|
|
199
|
-
if (source.isOk()) return ok(project(source.value));
|
|
200
|
-
return source;
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
//#endregion
|
|
205
|
-
//#region src/core/mapErr.ts
|
|
206
|
-
/**
|
|
207
|
-
* Transforms the error (Err case).
|
|
208
|
-
* Corresponds to Rust `map_err`.
|
|
209
|
-
*/
|
|
210
|
-
function mapErr(project) {
|
|
211
|
-
return (source) => {
|
|
212
|
-
if (source.isErr()) return err(project(source.error));
|
|
213
|
-
return source;
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
//#endregion
|
|
218
|
-
//#region src/core/mapBoth.ts
|
|
219
|
-
/**
|
|
220
|
-
* Transforms both the Ok value and the Err error.
|
|
221
|
-
* Corresponds to FP `bimap` / `mapBoth`.
|
|
222
|
-
*/
|
|
223
|
-
function mapBoth(mapOk, mapErr$1) {
|
|
224
|
-
return (source) => {
|
|
225
|
-
if (source.isOk()) return ok(mapOk(source.value));
|
|
226
|
-
if (source.isErr()) return err(mapErr$1(source.error));
|
|
227
|
-
throw new InvalidResultStateError("mapBoth");
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* Alias for `mapBoth`.
|
|
232
|
-
*/
|
|
233
|
-
const bimap = mapBoth;
|
|
234
|
-
|
|
235
|
-
//#endregion
|
|
236
|
-
//#region src/core/flatMap.ts
|
|
237
|
-
/**
|
|
238
|
-
* Chains another operation that returns a Result.
|
|
239
|
-
* Corresponds to Rust `and_then` or JS `flatMap`.
|
|
240
|
-
*/
|
|
241
|
-
function flatMap(project) {
|
|
242
|
-
return (source) => {
|
|
243
|
-
if (source.isOk()) return project(source.value);
|
|
244
|
-
return source;
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
//#endregion
|
|
249
|
-
//#region src/core/zip.ts
|
|
250
|
-
function zipImpl(left, right) {
|
|
251
|
-
if (left.isErr()) return left;
|
|
252
|
-
if (right.isErr()) return right;
|
|
253
|
-
if (left.isOk() && right.isOk()) return ok([left.value, right.value]);
|
|
254
|
-
throw new InvalidResultStateError("zip");
|
|
255
|
-
}
|
|
256
|
-
function zip(...args) {
|
|
257
|
-
if (args.length === 1) {
|
|
258
|
-
const right$1 = args[0];
|
|
259
|
-
return (left$1) => zipImpl(left$1, right$1);
|
|
260
|
-
}
|
|
261
|
-
const [left, right] = args;
|
|
262
|
-
return zipImpl(left, right);
|
|
263
|
-
}
|
|
264
|
-
function combineImpl(left, right) {
|
|
265
|
-
if (left.isOk() && right.isOk()) return ok([left.value, right.value]);
|
|
266
|
-
const errors = [];
|
|
267
|
-
if (left.isErr()) errors.push(left.error);
|
|
268
|
-
if (right.isErr()) errors.push(right.error);
|
|
269
|
-
return err(errors);
|
|
270
|
-
}
|
|
271
|
-
function combine(...args) {
|
|
272
|
-
if (args.length === 1) {
|
|
273
|
-
const right$1 = args[0];
|
|
274
|
-
return (left$1) => combineImpl(left$1, right$1);
|
|
275
|
-
}
|
|
276
|
-
const [left, right] = args;
|
|
277
|
-
return combineImpl(left, right);
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
//#endregion
|
|
281
|
-
//#region src/core/tap.ts
|
|
282
|
-
/**
|
|
283
|
-
* Executes a side effect (logging, debugging) without changing the Result.
|
|
284
|
-
* Corresponds to Rust `inspect` / `inspect_err`.
|
|
285
|
-
*/
|
|
286
|
-
function tap(observer) {
|
|
287
|
-
return (source) => {
|
|
288
|
-
if (source.isOk() && observer.ok) observer.ok(source.value);
|
|
289
|
-
if (source.isErr() && observer.err) observer.err(source.error);
|
|
290
|
-
return source;
|
|
291
|
-
};
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
//#endregion
|
|
295
|
-
//#region src/core/filter.ts
|
|
296
|
-
/**
|
|
297
|
-
* Checks a condition. If false, the Result becomes Err.
|
|
298
|
-
* Corresponds to Rust `filter` (partially).
|
|
299
|
-
*/
|
|
300
|
-
function filter(predicate, errorFn) {
|
|
301
|
-
return (source) => {
|
|
302
|
-
if (source.isOk()) {
|
|
303
|
-
if (predicate(source.value)) return source;
|
|
304
|
-
return err(errorFn());
|
|
305
|
-
}
|
|
306
|
-
return source;
|
|
307
|
-
};
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
//#endregion
|
|
311
|
-
//#region src/core/match.ts
|
|
312
|
-
/**
|
|
313
|
-
* Resolves the Result. The end of the pipe.
|
|
314
|
-
* Corresponds to Rust `match`.
|
|
315
|
-
*/
|
|
316
|
-
function match(handlers) {
|
|
317
|
-
return (source) => {
|
|
318
|
-
if (source.isOk()) return handlers.ok(source.value);
|
|
319
|
-
if (source.isErr()) return handlers.err(source.error);
|
|
320
|
-
throw new InvalidResultStateError("match");
|
|
321
|
-
};
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
//#endregion
|
|
325
|
-
//#region src/core/recover.ts
|
|
326
|
-
/**
|
|
327
|
-
* Recover: converts Err to Ok(defaultValue).
|
|
328
|
-
* Result is guaranteed to be Ok → Error type becomes `never`.
|
|
329
|
-
*/
|
|
330
|
-
function recover(defaultValue) {
|
|
331
|
-
return (source) => {
|
|
332
|
-
if (source.isOk()) return source;
|
|
333
|
-
if (source.isErr()) return ok(defaultValue);
|
|
334
|
-
throw new InvalidResultStateError("recover");
|
|
335
|
-
};
|
|
336
|
-
}
|
|
337
|
-
/**
|
|
338
|
-
* Like `recover`, but calculates the default value based on the error.
|
|
339
|
-
*/
|
|
340
|
-
function recoverWith(fn) {
|
|
341
|
-
return (source) => {
|
|
342
|
-
if (source.isOk()) return source;
|
|
343
|
-
if (source.isErr()) return ok(fn(source.error));
|
|
344
|
-
throw new InvalidResultStateError("recoverWith");
|
|
345
|
-
};
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
//#endregion
|
|
349
|
-
//#region src/core/swap.ts
|
|
350
|
-
/**
|
|
351
|
-
* Swaps Ok and Err.
|
|
352
|
-
* Result<T, E> → Result<E, T>
|
|
353
|
-
*/
|
|
354
|
-
function swap(result) {
|
|
355
|
-
if (result.isOk()) return err(result.value);
|
|
356
|
-
if (result.isErr()) return ok(result.error);
|
|
357
|
-
throw new InvalidResultStateError("swap");
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
//#endregion
|
|
361
|
-
//#region src/core/tryCatch.ts
|
|
362
|
-
/**
|
|
363
|
-
* Executes a function and catches exceptions.
|
|
364
|
-
* Converts exceptions into Result<E>.
|
|
365
|
-
* Corresponds to Rust `Result::from` for fallible operations.
|
|
366
|
-
*/
|
|
367
|
-
function tryCatch(fn, errorMapper) {
|
|
368
|
-
return (source) => {
|
|
369
|
-
if (source.isErr()) return source;
|
|
370
|
-
try {
|
|
371
|
-
return ok(fn());
|
|
372
|
-
} catch (error) {
|
|
373
|
-
return err(errorMapper ? errorMapper(error) : error);
|
|
374
|
-
}
|
|
375
|
-
};
|
|
376
|
-
}
|
|
1
|
+
import { A as flatMap, B as isResult, C as tryCatch, D as match, E as recoverWith, F as combine, I as zip, L as all, M as mapBoth, N as mapErr, O as filter, P as map, R as sequence, S as tryMap, T as recover, _ as tapAsync, a as fromNullable, b as mapAsync, c as ok, d as tryAsync, f as tryFn, g as filterAsync, h as matchAsync, i as err, j as bimap, k as tap, l as okIf, m as tryCatchAsync, n as Ok, o as fromPromise, p as tryMapAsync, r as Result, s as fromThrowable, t as Err, u as okIfLazy, v as flatMapAsync, w as swap, x as collectFirstOk, y as mapErrAsync, z as matchTag } from "./result-DKKaNdOx.mjs";
|
|
2
|
+
import { _ as TaskYieldNotResultError, a as ERR_MATCH_ERR_HANDLER_NOT_RESULT, c as ERR_UNWRAP_ERR_ON_OK, d as ExpectOkError, f as InvalidResultStateError, g as ResultTypeError, h as ResultError, i as ERR_INVALID_STATE, l as ERR_UNWRAP_ON_ERR, m as MatchOnOkError, n as ERR_EXPECT_OK, o as ERR_MATCH_ON_OK, p as MatchErrHandlerNotResultError, r as ERR_INVALID_RESULT_STATE, s as ERR_TASK_YIELD_NOT_RESULT, t as ERR_EXPECT_ERR, u as ExpectErrError, v as UnwrapErrOnOkError, y as UnwrapOnErrError } from "./errors-2WOswg7r.mjs";
|
|
3
|
+
import { a as and, i as or, n as mapOr, o as foldAsync, r as orElse, s as fold, t as mapOrElse } from "./mapOrElse-B0_4r6Jn.mjs";
|
|
4
|
+
import { a as collectFirstOkAsync, i as collectFirstOkParallelAsync, n as partition, o as sequenceRecord, r as collectAllErrors, t as flatten } from "./flatten-C6Y9hx79.mjs";
|
|
377
5
|
|
|
378
|
-
//#endregion
|
|
379
|
-
//#region src/core/tryMap.ts
|
|
380
|
-
/**
|
|
381
|
-
* Like `map`, but catches exceptions and converts them to Err.
|
|
382
|
-
*/
|
|
383
|
-
function tryMap(project, errorMapper) {
|
|
384
|
-
return (source) => {
|
|
385
|
-
if (source.isErr()) return source;
|
|
386
|
-
try {
|
|
387
|
-
if (source.isOk()) return ok(project(source.value));
|
|
388
|
-
throw new InvalidResultStateError("tryMap");
|
|
389
|
-
} catch (error) {
|
|
390
|
-
return err(errorMapper ? errorMapper(error) : error);
|
|
391
|
-
}
|
|
392
|
-
};
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
//#endregion
|
|
396
|
-
//#region src/core/collectFirstOk.ts
|
|
397
|
-
/**
|
|
398
|
-
* Parse a set of `Result`s, short-circuits when an input value is `Ok`.
|
|
399
|
-
* If no `Ok` is found, returns an `Err` containing the collected error values.
|
|
400
|
-
* Useful for "try multiple approaches until one works" patterns.
|
|
401
|
-
*/
|
|
402
|
-
function collectFirstOk(results) {
|
|
403
|
-
const errors = [];
|
|
404
|
-
for (const result of results) {
|
|
405
|
-
if (result.isOk()) return ok(result.value);
|
|
406
|
-
if (result.isErr()) {
|
|
407
|
-
errors.push(result.error);
|
|
408
|
-
continue;
|
|
409
|
-
}
|
|
410
|
-
throw new InvalidResultStateError("collectFirstOk");
|
|
411
|
-
}
|
|
412
|
-
return err(errors);
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
//#endregion
|
|
416
|
-
//#region src/core/mapAsync.ts
|
|
417
|
-
/**
|
|
418
|
-
* Async version of map.
|
|
419
|
-
*/
|
|
420
|
-
function mapAsync(project) {
|
|
421
|
-
return async (source) => {
|
|
422
|
-
if (source.isOk()) return ok(await project(source.value));
|
|
423
|
-
return source;
|
|
424
|
-
};
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
//#endregion
|
|
428
|
-
//#region src/core/mapErrAsync.ts
|
|
429
|
-
/**
|
|
430
|
-
* Async version of mapErr.
|
|
431
|
-
*/
|
|
432
|
-
function mapErrAsync(project) {
|
|
433
|
-
return async (source) => {
|
|
434
|
-
if (source.isErr()) return err(await project(source.error));
|
|
435
|
-
return source;
|
|
436
|
-
};
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
//#endregion
|
|
440
|
-
//#region src/core/flatMapAsync.ts
|
|
441
|
-
/**
|
|
442
|
-
* Async version of flatMap.
|
|
443
|
-
*/
|
|
444
|
-
function flatMapAsync(project) {
|
|
445
|
-
return async (source) => {
|
|
446
|
-
if (source.isOk()) return await project(source.value);
|
|
447
|
-
return source;
|
|
448
|
-
};
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
//#endregion
|
|
452
|
-
//#region src/core/tapAsync.ts
|
|
453
|
-
/**
|
|
454
|
-
* Async version of tap.
|
|
455
|
-
*/
|
|
456
|
-
function tapAsync(observer) {
|
|
457
|
-
return async (source) => {
|
|
458
|
-
if (source.isOk() && observer.ok) await observer.ok(source.value);
|
|
459
|
-
if (source.isErr() && observer.err) await observer.err(source.error);
|
|
460
|
-
return source;
|
|
461
|
-
};
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
//#endregion
|
|
465
|
-
//#region src/core/filterAsync.ts
|
|
466
|
-
/**
|
|
467
|
-
* Async version of filter.
|
|
468
|
-
*/
|
|
469
|
-
function filterAsync(predicate, errorFn) {
|
|
470
|
-
return async (source) => {
|
|
471
|
-
if (source.isOk()) {
|
|
472
|
-
if (await predicate(source.value)) return source;
|
|
473
|
-
return err(await errorFn());
|
|
474
|
-
}
|
|
475
|
-
return source;
|
|
476
|
-
};
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
//#endregion
|
|
480
|
-
//#region src/core/matchAsync.ts
|
|
481
|
-
/**
|
|
482
|
-
* Async version of match.
|
|
483
|
-
*/
|
|
484
|
-
function matchAsync(handlers) {
|
|
485
|
-
return async (source) => {
|
|
486
|
-
if (source.isOk()) return await handlers.ok(source.value);
|
|
487
|
-
if (source.isErr()) return await handlers.err(source.error);
|
|
488
|
-
throw new InvalidResultStateError("matchAsync");
|
|
489
|
-
};
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
//#endregion
|
|
493
|
-
//#region src/core/tryCatchAsync.ts
|
|
494
|
-
/**
|
|
495
|
-
* Async version of tryCatch.
|
|
496
|
-
*/
|
|
497
|
-
function tryCatchAsync(fn, errorMapper) {
|
|
498
|
-
return async (source) => {
|
|
499
|
-
if (source.isErr()) return source;
|
|
500
|
-
try {
|
|
501
|
-
return ok(await fn());
|
|
502
|
-
} catch (error) {
|
|
503
|
-
return err(errorMapper ? errorMapper(error) : error);
|
|
504
|
-
}
|
|
505
|
-
};
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
//#endregion
|
|
509
|
-
//#region src/core/tryMapAsync.ts
|
|
510
|
-
/**
|
|
511
|
-
* Async version of tryMap.
|
|
512
|
-
*/
|
|
513
|
-
function tryMapAsync(project, errorMapper) {
|
|
514
|
-
return async (source) => {
|
|
515
|
-
if (source.isErr()) return source;
|
|
516
|
-
try {
|
|
517
|
-
if (source.isOk()) return ok(await project(source.value));
|
|
518
|
-
throw new InvalidResultStateError("tryMapAsync");
|
|
519
|
-
} catch (error) {
|
|
520
|
-
return err(errorMapper ? errorMapper(error) : error);
|
|
521
|
-
}
|
|
522
|
-
};
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
//#endregion
|
|
526
|
-
//#region src/core/result.ts
|
|
527
|
-
var ResultBase = class extends Pipeable {
|
|
528
|
-
isOk() {
|
|
529
|
-
return this._tag === "Ok";
|
|
530
|
-
}
|
|
531
|
-
isErr() {
|
|
532
|
-
return this._tag === "Err";
|
|
533
|
-
}
|
|
534
|
-
unwrapOr(defaultValue) {
|
|
535
|
-
return this._tag === "Ok" ? this.value : defaultValue;
|
|
536
|
-
}
|
|
537
|
-
/**
|
|
538
|
-
* Folds the Result into a single value by applying one of two functions.
|
|
539
|
-
*/
|
|
540
|
-
fold(onOk, onErr) {
|
|
541
|
-
if (this._tag === "Ok") return onOk(this.value);
|
|
542
|
-
if (this._tag === "Err") return onErr(this.error);
|
|
543
|
-
throw new InvalidResultStateError("Result.fold");
|
|
544
|
-
}
|
|
545
|
-
/**
|
|
546
|
-
* Enables `yield* result` in generators (Do-notation).
|
|
547
|
-
*
|
|
548
|
-
* The iterator yields the `Result` itself; the runner (see `task`) decides:
|
|
549
|
-
* - Ok → sends back the Ok value (`next(value)`), `yield*` yields `T`
|
|
550
|
-
* - Err → aborts and returns the Err Result
|
|
551
|
-
*/
|
|
552
|
-
*[Symbol.iterator]() {
|
|
553
|
-
return yield this;
|
|
554
|
-
}
|
|
555
|
-
/**
|
|
556
|
-
* Matches on the Err value via `.when(...)` chain.
|
|
557
|
-
*
|
|
558
|
-
* Note: for type safety reasons, `.match()` can only be called on a Result already narrowed to `Err`,
|
|
559
|
-
* e.g. inside `if (result.isErr()) { ... }`.
|
|
560
|
-
*/
|
|
561
|
-
match() {
|
|
562
|
-
if (this._tag === "Err") return new ErrorMatchBuilder(this.error);
|
|
563
|
-
throw new MatchOnOkError();
|
|
564
|
-
}
|
|
565
|
-
/**
|
|
566
|
-
* Matches on the Err value, but normalizes every branch to a `Result`:
|
|
567
|
-
* - Handlers may return a `Result` (is returned directly)
|
|
568
|
-
* - or an Error value (is wrapped into `Err(error)`)
|
|
569
|
-
*/
|
|
570
|
-
matchErr() {
|
|
571
|
-
const makeErr = (error) => err(error);
|
|
572
|
-
return ErrMatchBuilder.fromResult(this, makeErr);
|
|
573
|
-
}
|
|
574
|
-
/**
|
|
575
|
-
* Serializes the Result into a simple object format.
|
|
576
|
-
* Preserves the original types.
|
|
577
|
-
*/
|
|
578
|
-
serialize() {
|
|
579
|
-
if (this._tag === "Ok") return {
|
|
580
|
-
isSuccess: true,
|
|
581
|
-
data: this.value
|
|
582
|
-
};
|
|
583
|
-
return {
|
|
584
|
-
isSuccess: false,
|
|
585
|
-
error: this.error
|
|
586
|
-
};
|
|
587
|
-
}
|
|
588
|
-
/**
|
|
589
|
-
* Serializes the Result into a user-friendly format.
|
|
590
|
-
* Converts Errors to readable strings.
|
|
591
|
-
*/
|
|
592
|
-
toUserFriendly() {
|
|
593
|
-
if (this._tag === "Ok") return {
|
|
594
|
-
isSuccess: true,
|
|
595
|
-
data: this.value
|
|
596
|
-
};
|
|
597
|
-
const error = this.error;
|
|
598
|
-
return {
|
|
599
|
-
isSuccess: false,
|
|
600
|
-
error: error && typeof error === "object" && "message" in error ? error.message : String(error)
|
|
601
|
-
};
|
|
602
|
-
}
|
|
603
|
-
};
|
|
604
|
-
var Ok = class extends ResultBase {
|
|
605
|
-
_tag = "Ok";
|
|
606
|
-
value;
|
|
607
|
-
error = void 0;
|
|
608
|
-
constructor(value) {
|
|
609
|
-
super();
|
|
610
|
-
this.value = value;
|
|
611
|
-
Object.freeze(this);
|
|
612
|
-
}
|
|
613
|
-
};
|
|
614
|
-
var Err = class extends ResultBase {
|
|
615
|
-
_tag = "Err";
|
|
616
|
-
value = void 0;
|
|
617
|
-
error;
|
|
618
|
-
constructor(error) {
|
|
619
|
-
super();
|
|
620
|
-
this.error = error;
|
|
621
|
-
Object.freeze(this);
|
|
622
|
-
}
|
|
623
|
-
};
|
|
624
|
-
function ok(value) {
|
|
625
|
-
return new Ok(value);
|
|
626
|
-
}
|
|
627
|
-
function err(error) {
|
|
628
|
-
return new Err(error);
|
|
629
|
-
}
|
|
630
|
-
function okIf(condition, okValue, errValue) {
|
|
631
|
-
return condition ? ok(okValue) : err(errValue);
|
|
632
|
-
}
|
|
633
|
-
function okIfLazy(condition, okFn, errFn) {
|
|
634
|
-
return condition ? ok(okFn()) : err(errFn());
|
|
635
|
-
}
|
|
636
|
-
function fromNullable(value, error) {
|
|
637
|
-
if (value === null || value === void 0) return err(error);
|
|
638
|
-
return ok(value);
|
|
639
|
-
}
|
|
640
|
-
async function fromPromise(promise, errorMapper) {
|
|
641
|
-
try {
|
|
642
|
-
return ok(await promise);
|
|
643
|
-
} catch (error) {
|
|
644
|
-
try {
|
|
645
|
-
return err(errorMapper ? errorMapper(error) : error);
|
|
646
|
-
} catch (mapperError) {
|
|
647
|
-
return err(mapperError);
|
|
648
|
-
}
|
|
649
|
-
}
|
|
650
|
-
}
|
|
651
|
-
function tryFn(fn) {
|
|
652
|
-
try {
|
|
653
|
-
return ok(fn());
|
|
654
|
-
} catch (error) {
|
|
655
|
-
return err(error);
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
const Result = {
|
|
659
|
-
ok,
|
|
660
|
-
err,
|
|
661
|
-
fromNullable,
|
|
662
|
-
fromPromise,
|
|
663
|
-
try: tryFn
|
|
664
|
-
};
|
|
665
|
-
|
|
666
|
-
//#endregion
|
|
667
|
-
//#region src/core/isResult.ts
|
|
668
|
-
/**
|
|
669
|
-
* Checks whether a value is a Result.
|
|
670
|
-
* Pure function alternative for runtime checks.
|
|
671
|
-
*/
|
|
672
|
-
function isResult(value) {
|
|
673
|
-
return typeof value === "object" && value !== null && "isOk" in value && typeof value.isOk === "function" && "isErr" in value && typeof value.isErr === "function";
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
//#endregion
|
|
677
6
|
//#region src/gen.ts
|
|
678
7
|
async function task(makeGenerator, onThrow) {
|
|
679
8
|
const iterator = makeGenerator();
|
|
@@ -714,38 +43,6 @@ async function task(makeGenerator, onThrow) {
|
|
|
714
43
|
}
|
|
715
44
|
const gen = task;
|
|
716
45
|
|
|
717
|
-
//#endregion
|
|
718
|
-
//#region src/core/fold.ts
|
|
719
|
-
/**
|
|
720
|
-
* Folds the Result into a single value by applying one of two functions.
|
|
721
|
-
* The end of the pipe.
|
|
722
|
-
*
|
|
723
|
-
* This is the pipe operator equivalent of the `.fold()` instance method.
|
|
724
|
-
*/
|
|
725
|
-
function fold(handlers) {
|
|
726
|
-
return (source) => {
|
|
727
|
-
if (source.isOk()) return handlers.ok(source.value);
|
|
728
|
-
if (source.isErr()) return handlers.err(source.error);
|
|
729
|
-
throw new InvalidResultStateError("fold");
|
|
730
|
-
};
|
|
731
|
-
}
|
|
732
|
-
|
|
733
|
-
//#endregion
|
|
734
|
-
//#region src/core/foldAsync.ts
|
|
735
|
-
/**
|
|
736
|
-
* Async version of `fold`. Folds the Result into a single value asynchronously.
|
|
737
|
-
* The end of the pipe.
|
|
738
|
-
*
|
|
739
|
-
* This is the async pipe operator equivalent of the `.fold()` instance method.
|
|
740
|
-
*/
|
|
741
|
-
function foldAsync(handlers) {
|
|
742
|
-
return async (source) => {
|
|
743
|
-
if (source.isOk()) return await handlers.ok(source.value);
|
|
744
|
-
if (source.isErr()) return await handlers.err(source.error);
|
|
745
|
-
throw new InvalidResultStateError("foldAsync");
|
|
746
|
-
};
|
|
747
|
-
}
|
|
748
|
-
|
|
749
46
|
//#endregion
|
|
750
47
|
//#region src/core/unwrap.ts
|
|
751
48
|
/**
|
|
@@ -766,7 +63,8 @@ function unwrap(result) {
|
|
|
766
63
|
*/
|
|
767
64
|
function unwrapOr(result, defaultValue) {
|
|
768
65
|
if (result.isOk()) return result.value;
|
|
769
|
-
return defaultValue;
|
|
66
|
+
if (result.isErr()) return defaultValue;
|
|
67
|
+
throw new InvalidResultStateError("unwrapOr");
|
|
770
68
|
}
|
|
771
69
|
|
|
772
70
|
//#endregion
|
|
@@ -823,7 +121,8 @@ function unwrapErr(result) {
|
|
|
823
121
|
*/
|
|
824
122
|
function expectResult(result, message) {
|
|
825
123
|
if (result.isOk()) return result.value;
|
|
826
|
-
throw new ExpectOkError(message);
|
|
124
|
+
if (result.isErr()) throw new ExpectOkError(message);
|
|
125
|
+
throw new InvalidResultStateError("expectResult");
|
|
827
126
|
}
|
|
828
127
|
|
|
829
128
|
//#endregion
|
|
@@ -834,224 +133,8 @@ function expectResult(result, message) {
|
|
|
834
133
|
*/
|
|
835
134
|
function expectErr(result, message) {
|
|
836
135
|
if (result.isErr()) return result.error;
|
|
837
|
-
throw new ExpectErrError(message);
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
//#endregion
|
|
841
|
-
//#region src/core/and.ts
|
|
842
|
-
/**
|
|
843
|
-
* Combines two Results. Returns the second one only if the first is Ok.
|
|
844
|
-
* Corresponds to Rust `and`.
|
|
845
|
-
*/
|
|
846
|
-
function and(result, other) {
|
|
847
|
-
return result.isOk() ? other : result;
|
|
848
|
-
}
|
|
849
|
-
|
|
850
|
-
//#endregion
|
|
851
|
-
//#region src/core/or.ts
|
|
852
|
-
/**
|
|
853
|
-
* Fallback to another Result if the first is Err.
|
|
854
|
-
* Corresponds to Rust `or`.
|
|
855
|
-
*/
|
|
856
|
-
function or(result, other) {
|
|
857
|
-
return result.isOk() ? result : other;
|
|
858
|
-
}
|
|
859
|
-
|
|
860
|
-
//#endregion
|
|
861
|
-
//#region src/core/orElse.ts
|
|
862
|
-
/**
|
|
863
|
-
* Fallback with a function that returns a Result.
|
|
864
|
-
* Corresponds to Rust `or_else`.
|
|
865
|
-
*/
|
|
866
|
-
function orElse(result, fn) {
|
|
867
|
-
if (result.isOk()) return result;
|
|
868
|
-
if (result.isErr()) return fn(result.error);
|
|
869
|
-
throw new InvalidResultStateError("orElse");
|
|
870
|
-
}
|
|
871
|
-
|
|
872
|
-
//#endregion
|
|
873
|
-
//#region src/core/mapOr.ts
|
|
874
|
-
/**
|
|
875
|
-
* Transforms the value or returns a default value.
|
|
876
|
-
* Corresponds to Rust `map_or`.
|
|
877
|
-
*/
|
|
878
|
-
function mapOr(result, defaultValue, fn) {
|
|
879
|
-
if (result.isOk()) return fn(result.value);
|
|
880
|
-
return defaultValue;
|
|
881
|
-
}
|
|
882
|
-
|
|
883
|
-
//#endregion
|
|
884
|
-
//#region src/core/mapOrElse.ts
|
|
885
|
-
/**
|
|
886
|
-
* Transforms the value or calculates a default value using a function.
|
|
887
|
-
* Corresponds to Rust `map_or_else`.
|
|
888
|
-
*/
|
|
889
|
-
function mapOrElse(result, defaultFn, fn) {
|
|
890
|
-
if (result.isOk()) return fn(result.value);
|
|
891
|
-
if (result.isErr()) return defaultFn(result.error);
|
|
892
|
-
throw new InvalidResultStateError("mapOrElse");
|
|
893
|
-
}
|
|
894
|
-
|
|
895
|
-
//#endregion
|
|
896
|
-
//#region src/core/sequence.ts
|
|
897
|
-
/**
|
|
898
|
-
* Combines a list of Results into a single Result of a list.
|
|
899
|
-
* Short-circuits on the first Err.
|
|
900
|
-
* Analogous to Rust `collect::<Result<Vec<_>, _>>()`.
|
|
901
|
-
*/
|
|
902
|
-
function sequence(results) {
|
|
903
|
-
const values = [];
|
|
904
|
-
for (const result of results) {
|
|
905
|
-
if (result.isOk()) {
|
|
906
|
-
values.push(result.value);
|
|
907
|
-
continue;
|
|
908
|
-
}
|
|
909
|
-
if (result.isErr()) return result;
|
|
910
|
-
throw new InvalidResultStateError("sequence");
|
|
911
|
-
}
|
|
912
|
-
return ok(values);
|
|
913
|
-
}
|
|
914
|
-
/**
|
|
915
|
-
* Alias for `sequence`.
|
|
916
|
-
*/
|
|
917
|
-
function all(results) {
|
|
918
|
-
return sequence(results);
|
|
919
|
-
}
|
|
920
|
-
|
|
921
|
-
//#endregion
|
|
922
|
-
//#region src/core/sequenceRecord.ts
|
|
923
|
-
/**
|
|
924
|
-
* Like `sequence`, but for Records/Objects.
|
|
925
|
-
* Short-circuits on the first Err.
|
|
926
|
-
*/
|
|
927
|
-
function sequenceRecord(record) {
|
|
928
|
-
const out = {};
|
|
929
|
-
for (const key of Object.keys(record)) {
|
|
930
|
-
const result = record[key];
|
|
931
|
-
if (!result) continue;
|
|
932
|
-
if (result.isOk()) {
|
|
933
|
-
out[key] = result.value;
|
|
934
|
-
continue;
|
|
935
|
-
}
|
|
936
|
-
if (result.isErr()) return result;
|
|
937
|
-
throw new InvalidResultStateError("sequenceRecord");
|
|
938
|
-
}
|
|
939
|
-
return ok(out);
|
|
940
|
-
}
|
|
941
|
-
|
|
942
|
-
//#endregion
|
|
943
|
-
//#region src/core/collectFirstOkAsync.ts
|
|
944
|
-
/**
|
|
945
|
-
* Async version of collectFirstOk.
|
|
946
|
-
*
|
|
947
|
-
* - Takes either already started Promises or "Thunks" (`() => Awaitable<Result<...>>`).
|
|
948
|
-
* - Processes inputs strictly sequentially (like `for ... of` + `await`).
|
|
949
|
-
* - Returns the first `Ok` and collects all errors if no `Ok` is found.
|
|
950
|
-
*/
|
|
951
|
-
async function collectFirstOkAsync(inputs) {
|
|
952
|
-
const errors = [];
|
|
953
|
-
for (const input of inputs) try {
|
|
954
|
-
const result = await (typeof input === "function" ? input() : input);
|
|
955
|
-
if (result.isOk()) return ok(result.value);
|
|
956
|
-
if (result.isErr()) {
|
|
957
|
-
errors.push(result.error);
|
|
958
|
-
continue;
|
|
959
|
-
}
|
|
960
|
-
throw new InvalidResultStateError("collectFirstOkAsync");
|
|
961
|
-
} catch (error) {
|
|
962
|
-
errors.push(error);
|
|
963
|
-
}
|
|
964
|
-
return err(errors);
|
|
965
|
-
}
|
|
966
|
-
|
|
967
|
-
//#endregion
|
|
968
|
-
//#region src/core/collectFirstOkParallelAsync.ts
|
|
969
|
-
/**
|
|
970
|
-
* Parallel version of `collectFirstOkAsync`.
|
|
971
|
-
*
|
|
972
|
-
* - Starts all inputs immediately (Promises or Thunks).
|
|
973
|
-
* - Returns the first `Ok` as soon as it is available.
|
|
974
|
-
* - If no `Ok` is found, returns an `Err` with all error values (in input order).
|
|
975
|
-
* - Rejections are treated as `ErrValue` (`caught as ErrValue`).
|
|
976
|
-
* - If multiple inputs provide an `Ok`, the one that completes first wins.
|
|
977
|
-
* In case of simultaneous completion, the first observed result wins.
|
|
978
|
-
* - If no `Ok` arrives and at least one input never settles, the Promise remains pending.
|
|
979
|
-
*/
|
|
980
|
-
async function collectFirstOkParallelAsync(inputs) {
|
|
981
|
-
if (inputs.length === 0) return err([]);
|
|
982
|
-
const started = inputs.map((input) => typeof input === "function" ? Promise.resolve().then(input) : input);
|
|
983
|
-
const firstOk = new Promise((resolve) => {
|
|
984
|
-
for (const promise of started) promise.then((result) => {
|
|
985
|
-
if (result.isOk()) resolve(ok(result.value));
|
|
986
|
-
}).catch(() => {});
|
|
987
|
-
});
|
|
988
|
-
const allErrors = Promise.allSettled(started).then((settled) => {
|
|
989
|
-
const errors = [];
|
|
990
|
-
for (const entry of settled) if (entry.status === "fulfilled") {
|
|
991
|
-
const result = entry.value;
|
|
992
|
-
if (result.isErr()) errors.push(result.error);
|
|
993
|
-
else if (!result.isOk()) errors.push(new InvalidResultStateError("collectFirstOkParallelAsync"));
|
|
994
|
-
} else errors.push(entry.reason);
|
|
995
|
-
return err(errors);
|
|
996
|
-
});
|
|
997
|
-
return Promise.race([firstOk, allErrors]);
|
|
998
|
-
}
|
|
999
|
-
|
|
1000
|
-
//#endregion
|
|
1001
|
-
//#region src/core/collectAllErrors.ts
|
|
1002
|
-
/**
|
|
1003
|
-
* Combines a list of Results.
|
|
1004
|
-
* Return Ok(values) only if all are Ok, otherwise Err([errors]).
|
|
1005
|
-
*/
|
|
1006
|
-
function collectAllErrors(results) {
|
|
1007
|
-
const values = [];
|
|
1008
|
-
const errors = [];
|
|
1009
|
-
for (const result of results) {
|
|
1010
|
-
if (result.isOk()) {
|
|
1011
|
-
values.push(result.value);
|
|
1012
|
-
continue;
|
|
1013
|
-
}
|
|
1014
|
-
if (result.isErr()) {
|
|
1015
|
-
errors.push(result.error);
|
|
1016
|
-
continue;
|
|
1017
|
-
}
|
|
1018
|
-
throw new InvalidResultStateError("collectAllErrors");
|
|
1019
|
-
}
|
|
1020
|
-
return errors.length === 0 ? ok(values) : err(errors);
|
|
1021
|
-
}
|
|
1022
|
-
|
|
1023
|
-
//#endregion
|
|
1024
|
-
//#region src/core/partition.ts
|
|
1025
|
-
/**
|
|
1026
|
-
* Partitions Results into Ok values and Err errors.
|
|
1027
|
-
*/
|
|
1028
|
-
function partition(results) {
|
|
1029
|
-
const oks = [];
|
|
1030
|
-
const errs = [];
|
|
1031
|
-
for (const result of results) {
|
|
1032
|
-
if (result.isOk()) {
|
|
1033
|
-
oks.push(result.value);
|
|
1034
|
-
continue;
|
|
1035
|
-
}
|
|
1036
|
-
if (result.isErr()) {
|
|
1037
|
-
errs.push(result.error);
|
|
1038
|
-
continue;
|
|
1039
|
-
}
|
|
1040
|
-
throw new InvalidResultStateError("partition");
|
|
1041
|
-
}
|
|
1042
|
-
return [oks, errs];
|
|
1043
|
-
}
|
|
1044
|
-
|
|
1045
|
-
//#endregion
|
|
1046
|
-
//#region src/core/flatten.ts
|
|
1047
|
-
/**
|
|
1048
|
-
* Flattens a nested Result.
|
|
1049
|
-
* Result<Result<T, E>, E> → Result<T, E>
|
|
1050
|
-
* Corresponds to Rust `flatten`.
|
|
1051
|
-
*/
|
|
1052
|
-
function flatten(result) {
|
|
1053
|
-
if (result.isOk()) return result.value;
|
|
1054
|
-
return result;
|
|
136
|
+
if (result.isOk()) throw new ExpectErrError(message);
|
|
137
|
+
throw new InvalidResultStateError("expectErr");
|
|
1055
138
|
}
|
|
1056
139
|
|
|
1057
140
|
//#endregion
|
|
@@ -1074,7 +157,8 @@ function toPromise(result) {
|
|
|
1074
157
|
*/
|
|
1075
158
|
function toNullable(result) {
|
|
1076
159
|
if (result.isOk()) return result.value;
|
|
1077
|
-
return null;
|
|
160
|
+
if (result.isErr()) return null;
|
|
161
|
+
throw new InvalidResultStateError("toNullable");
|
|
1078
162
|
}
|
|
1079
163
|
|
|
1080
164
|
//#endregion
|
|
@@ -1084,7 +168,9 @@ function toNullable(result) {
|
|
|
1084
168
|
* Pure function alternative to the instance method.
|
|
1085
169
|
*/
|
|
1086
170
|
function isOk(result) {
|
|
1087
|
-
|
|
171
|
+
if (result.isOk()) return true;
|
|
172
|
+
if (result.isErr()) return false;
|
|
173
|
+
throw new InvalidResultStateError("isOk");
|
|
1088
174
|
}
|
|
1089
175
|
|
|
1090
176
|
//#endregion
|
|
@@ -1094,7 +180,9 @@ function isOk(result) {
|
|
|
1094
180
|
* Pure function alternative to the instance method.
|
|
1095
181
|
*/
|
|
1096
182
|
function isErr(result) {
|
|
1097
|
-
|
|
183
|
+
if (result.isErr()) return true;
|
|
184
|
+
if (result.isOk()) return false;
|
|
185
|
+
throw new InvalidResultStateError("isErr");
|
|
1098
186
|
}
|
|
1099
187
|
|
|
1100
188
|
//#endregion
|
|
@@ -1104,8 +192,9 @@ function isErr(result) {
|
|
|
1104
192
|
* Corresponds to Rust `contains`.
|
|
1105
193
|
*/
|
|
1106
194
|
function contains(result, value) {
|
|
1107
|
-
if (
|
|
1108
|
-
|
|
195
|
+
if (result.isOk()) return result.value === value;
|
|
196
|
+
if (result.isErr()) return false;
|
|
197
|
+
throw new InvalidResultStateError("contains");
|
|
1109
198
|
}
|
|
1110
199
|
|
|
1111
200
|
//#endregion
|
|
@@ -1115,10 +204,11 @@ function contains(result, value) {
|
|
|
1115
204
|
* Analogous to `contains` for the Err case.
|
|
1116
205
|
*/
|
|
1117
206
|
function containsErr(result, error) {
|
|
1118
|
-
if (
|
|
1119
|
-
|
|
207
|
+
if (result.isErr()) return result.error === error;
|
|
208
|
+
if (result.isOk()) return false;
|
|
209
|
+
throw new InvalidResultStateError("containsErr");
|
|
1120
210
|
}
|
|
1121
211
|
|
|
1122
212
|
//#endregion
|
|
1123
|
-
export { ERR_EXPECT_ERR, ERR_EXPECT_OK, ERR_INVALID_STATE, ERR_MATCH_ON_OK, ERR_TASK_YIELD_NOT_RESULT, ERR_UNWRAP_ERR_ON_OK, ERR_UNWRAP_ON_ERR, Err, ExpectErrError, ExpectOkError, InvalidResultStateError, MatchOnOkError, Ok, Result, ResultError, ResultTypeError, TaskYieldNotResultError, UnwrapErrOnOkError, UnwrapOnErrError, all, and, bimap, collectAllErrors, collectFirstOk, collectFirstOkAsync, collectFirstOkParallelAsync, combine, contains, containsErr, err, expectErr, expectResult, filter, filterAsync, flatMap, flatMapAsync, flatten, fold, foldAsync, fromNullable, fromPromise, gen, isErr, isOk, isResult, map, mapAsync, mapBoth, mapErr, mapErrAsync, mapOr, mapOrElse, match, matchAsync, ok, okIf, okIfLazy, or, orElse, partition, recover, recoverWith, sequence, sequenceRecord, swap, tap, tapAsync, task, toNullable, toPromise, tryCatch, tryCatchAsync, tryFn, tryMap, tryMapAsync, unwrap, unwrapErr, unwrapOr, unwrapOrDefault, unwrapOrElse, unwrapOrThrow, zip };
|
|
213
|
+
export { ERR_EXPECT_ERR, ERR_EXPECT_OK, ERR_INVALID_RESULT_STATE, ERR_INVALID_STATE, ERR_MATCH_ERR_HANDLER_NOT_RESULT, ERR_MATCH_ON_OK, ERR_TASK_YIELD_NOT_RESULT, ERR_UNWRAP_ERR_ON_OK, ERR_UNWRAP_ON_ERR, Err, ExpectErrError, ExpectOkError, InvalidResultStateError, MatchErrHandlerNotResultError, MatchOnOkError, Ok, Result, ResultError, ResultTypeError, TaskYieldNotResultError, UnwrapErrOnOkError, UnwrapOnErrError, all, and, bimap, collectAllErrors, collectFirstOk, collectFirstOkAsync, collectFirstOkParallelAsync, combine, contains, containsErr, err, expectErr, expectResult, filter, filterAsync, flatMap, flatMapAsync, flatten, fold, foldAsync, fromNullable, fromPromise, fromThrowable, gen, isErr, isOk, isResult, map, mapAsync, mapBoth, mapErr, mapErrAsync, mapOr, mapOrElse, match, matchAsync, matchTag, ok, okIf, okIfLazy, or, orElse, partition, recover, recoverWith, sequence, sequenceRecord, swap, tap, tapAsync, task, toNullable, toPromise, tryAsync, tryCatch, tryCatchAsync, tryFn, tryMap, tryMapAsync, unwrap, unwrapErr, unwrapOr, unwrapOrDefault, unwrapOrElse, unwrapOrThrow, zip };
|
|
1124
214
|
//# sourceMappingURL=index.mjs.map
|