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