@shirudo/result 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -1
- package/dist/collections.cjs +4 -3
- package/dist/collections.d.cts +2 -2
- package/dist/collections.d.mts +2 -2
- package/dist/collections.mjs +2 -2
- package/dist/errors.cjs +131 -20
- package/dist/errors.cjs.map +1 -0
- package/dist/errors.d.cts +63 -2
- package/dist/errors.d.cts.map +1 -0
- package/dist/errors.d.mts +63 -2
- package/dist/errors.d.mts.map +1 -0
- package/dist/errors.mjs +109 -2
- package/dist/errors.mjs.map +1 -0
- package/dist/{flatten-Df9U40nO.cjs → flatten-B_XIkaOK.cjs} +29 -3
- package/dist/flatten-B_XIkaOK.cjs.map +1 -0
- package/dist/flatten-C7D6Kttf.d.mts +81 -0
- package/dist/flatten-C7D6Kttf.d.mts.map +1 -0
- package/dist/{flatten-C6Y9hx79.mjs → flatten-ChTL5BfA.mjs} +24 -4
- package/dist/flatten-ChTL5BfA.mjs.map +1 -0
- package/dist/flatten-D0K8UcQH.d.cts +81 -0
- package/dist/flatten-D0K8UcQH.d.cts.map +1 -0
- package/dist/index.cjs +90 -40
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +78 -58
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +78 -58
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +58 -12
- package/dist/index.mjs.map +1 -1
- package/dist/operators.cjs +31 -30
- package/dist/operators.d.cts +2 -2
- package/dist/operators.d.mts +2 -2
- package/dist/operators.mjs +2 -2
- package/dist/{result-CY-KIivk.cjs → result-BBOGxvSH.cjs} +86 -503
- package/dist/result-BBOGxvSH.cjs.map +1 -0
- package/dist/{result-DKKaNdOx.mjs → result-q9Na2bGa.mjs} +87 -372
- package/dist/result-q9Na2bGa.mjs.map +1 -0
- package/dist/sequence-DDwePRLd.d.cts +283 -0
- package/dist/sequence-DDwePRLd.d.cts.map +1 -0
- package/dist/sequence-DDwePRLd.d.mts +283 -0
- package/dist/sequence-DDwePRLd.d.mts.map +1 -0
- package/dist/swap-BnyMBdD_.cjs +552 -0
- package/dist/swap-BnyMBdD_.cjs.map +1 -0
- package/dist/swap-CFD2g1cB.mjs +385 -0
- package/dist/swap-CFD2g1cB.mjs.map +1 -0
- package/dist/swap-CrHQZIax.d.cts +212 -0
- package/dist/swap-CrHQZIax.d.cts.map +1 -0
- package/dist/swap-DT4LdRFV.d.mts +212 -0
- package/dist/swap-DT4LdRFV.d.mts.map +1 -0
- package/package.json +9 -8
- package/dist/errors-2WOswg7r.mjs +0 -95
- package/dist/errors-2WOswg7r.mjs.map +0 -1
- package/dist/errors-BFjY06EV.d.cts +0 -55
- package/dist/errors-BFjY06EV.d.cts.map +0 -1
- package/dist/errors-C5qGMRiU.d.mts +0 -55
- package/dist/errors-C5qGMRiU.d.mts.map +0 -1
- package/dist/errors-D2EMzJQl.cjs +0 -209
- package/dist/errors-D2EMzJQl.cjs.map +0 -1
- package/dist/flatten-B8bN6fiI.d.mts +0 -71
- package/dist/flatten-B8bN6fiI.d.mts.map +0 -1
- package/dist/flatten-C6Y9hx79.mjs.map +0 -1
- package/dist/flatten-DK7eJKPx.d.cts +0 -71
- package/dist/flatten-DK7eJKPx.d.cts.map +0 -1
- package/dist/flatten-Df9U40nO.cjs.map +0 -1
- package/dist/mapOrElse-B0_4r6Jn.mjs +0 -96
- package/dist/mapOrElse-B0_4r6Jn.mjs.map +0 -1
- package/dist/mapOrElse-B5gx9x2E.d.mts +0 -64
- package/dist/mapOrElse-B5gx9x2E.d.mts.map +0 -1
- package/dist/mapOrElse-DIe7LkYV.d.cts +0 -64
- package/dist/mapOrElse-DIe7LkYV.d.cts.map +0 -1
- package/dist/mapOrElse-H-GvAUka.cjs +0 -137
- package/dist/mapOrElse-H-GvAUka.cjs.map +0 -1
- package/dist/result-CY-KIivk.cjs.map +0 -1
- package/dist/result-DKKaNdOx.mjs.map +0 -1
- package/dist/sequence-Br6tAIIu.d.cts +0 -419
- package/dist/sequence-Br6tAIIu.d.cts.map +0 -1
- package/dist/sequence-C0jlR3AY.d.mts +0 -419
- package/dist/sequence-C0jlR3AY.d.mts.map +0 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ExpectErrError, ExpectOkError, InvalidResultStateError, MatchErrHandlerNotResultError, MatchOnOkError, MatchTagMissingHandlerError, UnwrapErrOnOkError, UnwrapOnErrError } from "./errors.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/core/pipeable.ts
|
|
4
4
|
var Pipeable = class {
|
|
@@ -48,8 +48,9 @@ function matchTag(result, key, handlers) {
|
|
|
48
48
|
if (result.isOk()) throw new MatchOnOkError("matchTag");
|
|
49
49
|
if (result.isErr()) {
|
|
50
50
|
const error = result.error;
|
|
51
|
-
const
|
|
52
|
-
|
|
51
|
+
const tag = error[key];
|
|
52
|
+
const handler = Object.hasOwn(handlers, tag) ? handlers[tag] : void 0;
|
|
53
|
+
if (typeof handler !== "function") throw new MatchTagMissingHandlerError(tag);
|
|
53
54
|
return handler(error);
|
|
54
55
|
}
|
|
55
56
|
throw new InvalidResultStateError("matchTag");
|
|
@@ -99,18 +100,19 @@ var ErrorMatchBuilder = class ErrorMatchBuilder {
|
|
|
99
100
|
var AsyncErrorMatchBuilder = class AsyncErrorMatchBuilder {
|
|
100
101
|
#error;
|
|
101
102
|
#matched;
|
|
102
|
-
#
|
|
103
|
-
|
|
103
|
+
#handler;
|
|
104
|
+
#memo;
|
|
105
|
+
constructor(error, matched = false, handler) {
|
|
104
106
|
this.#error = error;
|
|
105
107
|
this.#matched = matched;
|
|
106
|
-
this.#
|
|
108
|
+
this.#handler = handler;
|
|
107
109
|
Object.freeze(this);
|
|
108
110
|
}
|
|
109
111
|
when(ctor, handler) {
|
|
110
112
|
if (this.#matched) return this;
|
|
111
113
|
if (this.#error instanceof ctor) {
|
|
112
|
-
const
|
|
113
|
-
return new AsyncErrorMatchBuilder(this.#error, true,
|
|
114
|
+
const run = () => handler(this.#error);
|
|
115
|
+
return new AsyncErrorMatchBuilder(this.#error, true, run);
|
|
114
116
|
}
|
|
115
117
|
return this;
|
|
116
118
|
}
|
|
@@ -118,27 +120,31 @@ var AsyncErrorMatchBuilder = class AsyncErrorMatchBuilder {
|
|
|
118
120
|
if (this.#matched) return this;
|
|
119
121
|
const error = this.#error;
|
|
120
122
|
if (guard(error)) {
|
|
121
|
-
const
|
|
122
|
-
return new AsyncErrorMatchBuilder(this.#error, true,
|
|
123
|
+
const run = () => handler(error);
|
|
124
|
+
return new AsyncErrorMatchBuilder(this.#error, true, run);
|
|
123
125
|
}
|
|
124
126
|
return this;
|
|
125
127
|
}
|
|
126
128
|
whenTag(key, tag, handler) {
|
|
127
129
|
if (this.#matched) return this;
|
|
128
130
|
if (matchesTag(this.#error, key, tag)) {
|
|
129
|
-
const
|
|
130
|
-
return new AsyncErrorMatchBuilder(this.#error, true,
|
|
131
|
+
const run = () => handler(this.#error);
|
|
132
|
+
return new AsyncErrorMatchBuilder(this.#error, true, run);
|
|
131
133
|
}
|
|
132
134
|
return this;
|
|
133
135
|
}
|
|
134
136
|
async otherwise(handler) {
|
|
135
|
-
if (this.#matched) return await this.#
|
|
137
|
+
if (this.#matched) return await this.#invoke();
|
|
136
138
|
return await handler(this.#error);
|
|
137
139
|
}
|
|
138
140
|
async run() {
|
|
139
|
-
if (this.#matched) return await this.#
|
|
141
|
+
if (this.#matched) return await this.#invoke();
|
|
140
142
|
throw this.#error;
|
|
141
143
|
}
|
|
144
|
+
#invoke() {
|
|
145
|
+
this.#memo ??= Promise.resolve().then(this.#handler);
|
|
146
|
+
return this.#memo;
|
|
147
|
+
}
|
|
142
148
|
};
|
|
143
149
|
const expectResultReturn = (value, handlerName) => {
|
|
144
150
|
if (isResult(value)) return value;
|
|
@@ -199,50 +205,55 @@ var ErrMatchBuilder = class ErrMatchBuilder {
|
|
|
199
205
|
};
|
|
200
206
|
var AsyncErrMatchBuilder = class AsyncErrMatchBuilder {
|
|
201
207
|
#error;
|
|
202
|
-
#
|
|
203
|
-
|
|
208
|
+
#resolve;
|
|
209
|
+
#memo;
|
|
210
|
+
constructor(error, resolve) {
|
|
204
211
|
this.#error = error;
|
|
205
|
-
this.#
|
|
212
|
+
this.#resolve = resolve;
|
|
206
213
|
Object.freeze(this);
|
|
207
214
|
}
|
|
208
215
|
static fromResult(result) {
|
|
209
|
-
if (result.isOk()) return new AsyncErrMatchBuilder(void 0,
|
|
216
|
+
if (result.isOk()) return new AsyncErrMatchBuilder(void 0, () => result);
|
|
210
217
|
if (result.isErr()) return new AsyncErrMatchBuilder(result.error);
|
|
211
218
|
throw new InvalidResultStateError("AsyncErrMatchBuilder.fromResult");
|
|
212
219
|
}
|
|
213
220
|
when(ctor, handler) {
|
|
214
|
-
if (this.#
|
|
221
|
+
if (this.#resolve) return this;
|
|
215
222
|
if (this.#error instanceof ctor) {
|
|
216
|
-
const
|
|
217
|
-
return new AsyncErrMatchBuilder(this.#error,
|
|
223
|
+
const resolve = async () => expectResultReturn(await handler(this.#error), "when");
|
|
224
|
+
return new AsyncErrMatchBuilder(this.#error, resolve);
|
|
218
225
|
}
|
|
219
226
|
return this;
|
|
220
227
|
}
|
|
221
228
|
whenGuard(guard, handler) {
|
|
222
|
-
if (this.#
|
|
229
|
+
if (this.#resolve) return this;
|
|
223
230
|
const error = this.#error;
|
|
224
231
|
if (guard(error)) {
|
|
225
|
-
const
|
|
226
|
-
return new AsyncErrMatchBuilder(this.#error,
|
|
232
|
+
const resolve = async () => expectResultReturn(await handler(error), "whenGuard");
|
|
233
|
+
return new AsyncErrMatchBuilder(this.#error, resolve);
|
|
227
234
|
}
|
|
228
235
|
return this;
|
|
229
236
|
}
|
|
230
237
|
whenTag(key, tag, handler) {
|
|
231
|
-
if (this.#
|
|
238
|
+
if (this.#resolve) return this;
|
|
232
239
|
if (matchesTag(this.#error, key, tag)) {
|
|
233
|
-
const
|
|
234
|
-
return new AsyncErrMatchBuilder(this.#error,
|
|
240
|
+
const resolve = async () => expectResultReturn(await handler(this.#error), "whenTag");
|
|
241
|
+
return new AsyncErrMatchBuilder(this.#error, resolve);
|
|
235
242
|
}
|
|
236
243
|
return this;
|
|
237
244
|
}
|
|
238
245
|
async otherwise(handler) {
|
|
239
|
-
if (this.#
|
|
246
|
+
if (this.#resolve) return await this.#invoke();
|
|
240
247
|
return expectResultReturn(await handler(this.#error), "otherwise");
|
|
241
248
|
}
|
|
242
249
|
async run() {
|
|
243
|
-
if (this.#
|
|
250
|
+
if (this.#resolve) return await this.#invoke();
|
|
244
251
|
throw this.#error;
|
|
245
252
|
}
|
|
253
|
+
#invoke() {
|
|
254
|
+
this.#memo ??= Promise.resolve().then(this.#resolve);
|
|
255
|
+
return this.#memo;
|
|
256
|
+
}
|
|
246
257
|
};
|
|
247
258
|
|
|
248
259
|
//#endregion
|
|
@@ -281,8 +292,8 @@ function zipImpl(left, right) {
|
|
|
281
292
|
}
|
|
282
293
|
function zip(...args) {
|
|
283
294
|
if (args.length === 1) {
|
|
284
|
-
const right
|
|
285
|
-
return (left
|
|
295
|
+
const right = args[0];
|
|
296
|
+
return (left) => zipImpl(left, right);
|
|
286
297
|
}
|
|
287
298
|
const [left, right] = args;
|
|
288
299
|
return zipImpl(left, right);
|
|
@@ -298,347 +309,16 @@ function combineImpl(left, right) {
|
|
|
298
309
|
}
|
|
299
310
|
function combine(...args) {
|
|
300
311
|
if (args.length === 1) {
|
|
301
|
-
const right
|
|
302
|
-
return (left
|
|
312
|
+
const right = args[0];
|
|
313
|
+
return (left) => combineImpl(left, right);
|
|
303
314
|
}
|
|
304
315
|
const [left, right] = args;
|
|
305
316
|
return combineImpl(left, right);
|
|
306
317
|
}
|
|
307
318
|
|
|
308
|
-
//#endregion
|
|
309
|
-
//#region src/core/map.ts
|
|
310
|
-
/**
|
|
311
|
-
* Transforms the value (Ok case).
|
|
312
|
-
* Corresponds to Rust `map`.
|
|
313
|
-
*/
|
|
314
|
-
function map(project) {
|
|
315
|
-
return (source) => {
|
|
316
|
-
if (source.isOk()) return ok(project(source.value));
|
|
317
|
-
if (source.isErr()) return source;
|
|
318
|
-
throw new InvalidResultStateError("map");
|
|
319
|
-
};
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
//#endregion
|
|
323
|
-
//#region src/core/mapErr.ts
|
|
324
|
-
/**
|
|
325
|
-
* Transforms the error (Err case).
|
|
326
|
-
* Corresponds to Rust `map_err`.
|
|
327
|
-
*/
|
|
328
|
-
function mapErr(project) {
|
|
329
|
-
return (source) => {
|
|
330
|
-
if (source.isErr()) return err(project(source.error));
|
|
331
|
-
if (source.isOk()) return source;
|
|
332
|
-
throw new InvalidResultStateError("mapErr");
|
|
333
|
-
};
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
//#endregion
|
|
337
|
-
//#region src/core/mapBoth.ts
|
|
338
|
-
/**
|
|
339
|
-
* Transforms both the Ok value and the Err error.
|
|
340
|
-
* Corresponds to FP `bimap` / `mapBoth`.
|
|
341
|
-
*/
|
|
342
|
-
function mapBoth(mapOk, mapErr$1) {
|
|
343
|
-
return (source) => {
|
|
344
|
-
if (source.isOk()) return ok(mapOk(source.value));
|
|
345
|
-
if (source.isErr()) return err(mapErr$1(source.error));
|
|
346
|
-
throw new InvalidResultStateError("mapBoth");
|
|
347
|
-
};
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* Alias for `mapBoth`.
|
|
351
|
-
*/
|
|
352
|
-
const bimap = mapBoth;
|
|
353
|
-
|
|
354
|
-
//#endregion
|
|
355
|
-
//#region src/core/flatMap.ts
|
|
356
|
-
/**
|
|
357
|
-
* Chains another operation that returns a Result.
|
|
358
|
-
* Corresponds to Rust `and_then` or JS `flatMap`.
|
|
359
|
-
*/
|
|
360
|
-
function flatMap(project) {
|
|
361
|
-
return (source) => {
|
|
362
|
-
if (source.isOk()) return project(source.value);
|
|
363
|
-
if (source.isErr()) return source;
|
|
364
|
-
throw new InvalidResultStateError("flatMap");
|
|
365
|
-
};
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
//#endregion
|
|
369
|
-
//#region src/core/tap.ts
|
|
370
|
-
/**
|
|
371
|
-
* Executes a side effect (logging, debugging) without changing the Result.
|
|
372
|
-
* Corresponds to Rust `inspect` / `inspect_err`.
|
|
373
|
-
*/
|
|
374
|
-
function tap(observer) {
|
|
375
|
-
return (source) => {
|
|
376
|
-
if (source.isOk()) {
|
|
377
|
-
if (observer.ok) observer.ok(source.value);
|
|
378
|
-
return source;
|
|
379
|
-
}
|
|
380
|
-
if (source.isErr()) {
|
|
381
|
-
if (observer.err) observer.err(source.error);
|
|
382
|
-
return source;
|
|
383
|
-
}
|
|
384
|
-
throw new InvalidResultStateError("tap");
|
|
385
|
-
};
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
//#endregion
|
|
389
|
-
//#region src/core/filter.ts
|
|
390
|
-
/**
|
|
391
|
-
* Checks a condition. If false, the Result becomes Err.
|
|
392
|
-
* Corresponds to Rust `filter` (partially).
|
|
393
|
-
*/
|
|
394
|
-
function filter(predicate, errorFn) {
|
|
395
|
-
return (source) => {
|
|
396
|
-
if (source.isOk()) {
|
|
397
|
-
if (predicate(source.value)) return source;
|
|
398
|
-
return err(errorFn());
|
|
399
|
-
}
|
|
400
|
-
if (source.isErr()) return source;
|
|
401
|
-
throw new InvalidResultStateError("filter");
|
|
402
|
-
};
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
//#endregion
|
|
406
|
-
//#region src/core/match.ts
|
|
407
|
-
/**
|
|
408
|
-
* Resolves the Result. The end of the pipe.
|
|
409
|
-
* Corresponds to Rust `match`.
|
|
410
|
-
*/
|
|
411
|
-
function match(handlers) {
|
|
412
|
-
return (source) => {
|
|
413
|
-
if (source.isOk()) return handlers.ok(source.value);
|
|
414
|
-
if (source.isErr()) return handlers.err(source.error);
|
|
415
|
-
throw new InvalidResultStateError("match");
|
|
416
|
-
};
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
//#endregion
|
|
420
|
-
//#region src/core/recover.ts
|
|
421
|
-
/**
|
|
422
|
-
* Recover: converts Err to Ok(defaultValue).
|
|
423
|
-
* Result is guaranteed to be Ok → Error type becomes `never`.
|
|
424
|
-
*/
|
|
425
|
-
function recover(defaultValue) {
|
|
426
|
-
return (source) => {
|
|
427
|
-
if (source.isOk()) return source;
|
|
428
|
-
if (source.isErr()) return ok(defaultValue);
|
|
429
|
-
throw new InvalidResultStateError("recover");
|
|
430
|
-
};
|
|
431
|
-
}
|
|
432
|
-
/**
|
|
433
|
-
* Like `recover`, but calculates the default value based on the error.
|
|
434
|
-
*/
|
|
435
|
-
function recoverWith(fn) {
|
|
436
|
-
return (source) => {
|
|
437
|
-
if (source.isOk()) return source;
|
|
438
|
-
if (source.isErr()) return ok(fn(source.error));
|
|
439
|
-
throw new InvalidResultStateError("recoverWith");
|
|
440
|
-
};
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
//#endregion
|
|
444
|
-
//#region src/core/swap.ts
|
|
445
|
-
/**
|
|
446
|
-
* Swaps Ok and Err.
|
|
447
|
-
* Result<T, E> → Result<E, T>
|
|
448
|
-
*/
|
|
449
|
-
function swap(result) {
|
|
450
|
-
if (result.isOk()) return err(result.value);
|
|
451
|
-
if (result.isErr()) return ok(result.error);
|
|
452
|
-
throw new InvalidResultStateError("swap");
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
//#endregion
|
|
456
|
-
//#region src/core/tryCatch.ts
|
|
457
|
-
/**
|
|
458
|
-
* Executes a function and catches exceptions.
|
|
459
|
-
* Converts exceptions into Result<E>.
|
|
460
|
-
* Corresponds to Rust `Result::from` for fallible operations.
|
|
461
|
-
*/
|
|
462
|
-
function tryCatch(fn, errorMapper) {
|
|
463
|
-
return (source) => {
|
|
464
|
-
if (source.isErr()) return source;
|
|
465
|
-
try {
|
|
466
|
-
return ok(fn());
|
|
467
|
-
} catch (error) {
|
|
468
|
-
return err(errorMapper ? errorMapper(error) : error);
|
|
469
|
-
}
|
|
470
|
-
};
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
//#endregion
|
|
474
|
-
//#region src/core/tryMap.ts
|
|
475
|
-
/**
|
|
476
|
-
* Like `map`, but catches exceptions and converts them to Err.
|
|
477
|
-
*/
|
|
478
|
-
function tryMap(project, errorMapper) {
|
|
479
|
-
return (source) => {
|
|
480
|
-
if (source.isErr()) return source;
|
|
481
|
-
if (!source.isOk()) throw new InvalidResultStateError("tryMap");
|
|
482
|
-
try {
|
|
483
|
-
return ok(project(source.value));
|
|
484
|
-
} catch (error) {
|
|
485
|
-
return err(errorMapper ? errorMapper(error) : error);
|
|
486
|
-
}
|
|
487
|
-
};
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
//#endregion
|
|
491
|
-
//#region src/core/collectFirstOk.ts
|
|
492
|
-
/**
|
|
493
|
-
* Parse a set of `Result`s, short-circuits when an input value is `Ok`.
|
|
494
|
-
* If no `Ok` is found, returns an `Err` containing the collected error values.
|
|
495
|
-
* Useful for "try multiple approaches until one works" patterns.
|
|
496
|
-
*/
|
|
497
|
-
function collectFirstOk(results) {
|
|
498
|
-
const errors = [];
|
|
499
|
-
for (const result of results) {
|
|
500
|
-
if (result.isOk()) return ok(result.value);
|
|
501
|
-
if (result.isErr()) {
|
|
502
|
-
errors.push(result.error);
|
|
503
|
-
continue;
|
|
504
|
-
}
|
|
505
|
-
throw new InvalidResultStateError("collectFirstOk");
|
|
506
|
-
}
|
|
507
|
-
return err(errors);
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
//#endregion
|
|
511
|
-
//#region src/core/mapAsync.ts
|
|
512
|
-
/**
|
|
513
|
-
* Async version of map.
|
|
514
|
-
*/
|
|
515
|
-
function mapAsync(project) {
|
|
516
|
-
return async (source) => {
|
|
517
|
-
if (source.isOk()) return ok(await project(source.value));
|
|
518
|
-
if (source.isErr()) return source;
|
|
519
|
-
throw new InvalidResultStateError("mapAsync");
|
|
520
|
-
};
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
//#endregion
|
|
524
|
-
//#region src/core/mapErrAsync.ts
|
|
525
|
-
/**
|
|
526
|
-
* Async version of mapErr.
|
|
527
|
-
*/
|
|
528
|
-
function mapErrAsync(project) {
|
|
529
|
-
return async (source) => {
|
|
530
|
-
if (source.isErr()) return err(await project(source.error));
|
|
531
|
-
if (source.isOk()) return source;
|
|
532
|
-
throw new InvalidResultStateError("mapErrAsync");
|
|
533
|
-
};
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
//#endregion
|
|
537
|
-
//#region src/core/flatMapAsync.ts
|
|
538
|
-
/**
|
|
539
|
-
* Async version of flatMap.
|
|
540
|
-
*/
|
|
541
|
-
function flatMapAsync(project) {
|
|
542
|
-
return async (source) => {
|
|
543
|
-
if (source.isOk()) return await project(source.value);
|
|
544
|
-
if (source.isErr()) return source;
|
|
545
|
-
throw new InvalidResultStateError("flatMapAsync");
|
|
546
|
-
};
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
//#endregion
|
|
550
|
-
//#region src/core/tapAsync.ts
|
|
551
|
-
/**
|
|
552
|
-
* Async version of tap.
|
|
553
|
-
*/
|
|
554
|
-
function tapAsync(observer) {
|
|
555
|
-
return async (source) => {
|
|
556
|
-
if (source.isOk()) {
|
|
557
|
-
if (observer.ok) await observer.ok(source.value);
|
|
558
|
-
return source;
|
|
559
|
-
}
|
|
560
|
-
if (source.isErr()) {
|
|
561
|
-
if (observer.err) await observer.err(source.error);
|
|
562
|
-
return source;
|
|
563
|
-
}
|
|
564
|
-
throw new InvalidResultStateError("tapAsync");
|
|
565
|
-
};
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
//#endregion
|
|
569
|
-
//#region src/core/filterAsync.ts
|
|
570
|
-
/**
|
|
571
|
-
* Async version of filter.
|
|
572
|
-
*/
|
|
573
|
-
function filterAsync(predicate, errorFn) {
|
|
574
|
-
return async (source) => {
|
|
575
|
-
if (source.isOk()) {
|
|
576
|
-
if (await predicate(source.value)) return source;
|
|
577
|
-
return err(await errorFn());
|
|
578
|
-
}
|
|
579
|
-
if (source.isErr()) return source;
|
|
580
|
-
throw new InvalidResultStateError("filterAsync");
|
|
581
|
-
};
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
//#endregion
|
|
585
|
-
//#region src/core/matchAsync.ts
|
|
586
|
-
/**
|
|
587
|
-
* Async version of match.
|
|
588
|
-
*/
|
|
589
|
-
function matchAsync(handlers) {
|
|
590
|
-
return async (source) => {
|
|
591
|
-
if (source.isOk()) return await handlers.ok(source.value);
|
|
592
|
-
if (source.isErr()) return await handlers.err(source.error);
|
|
593
|
-
throw new InvalidResultStateError("matchAsync");
|
|
594
|
-
};
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
//#endregion
|
|
598
|
-
//#region src/core/tryCatchAsync.ts
|
|
599
|
-
/**
|
|
600
|
-
* Async version of tryCatch.
|
|
601
|
-
*/
|
|
602
|
-
function tryCatchAsync(fn, errorMapper) {
|
|
603
|
-
return async (source) => {
|
|
604
|
-
if (source.isErr()) return source;
|
|
605
|
-
try {
|
|
606
|
-
return ok(await fn());
|
|
607
|
-
} catch (error) {
|
|
608
|
-
return err(errorMapper ? errorMapper(error) : error);
|
|
609
|
-
}
|
|
610
|
-
};
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
//#endregion
|
|
614
|
-
//#region src/core/tryMapAsync.ts
|
|
615
|
-
/**
|
|
616
|
-
* Async version of tryMap.
|
|
617
|
-
*/
|
|
618
|
-
function tryMapAsync(project, errorMapper) {
|
|
619
|
-
return async (source) => {
|
|
620
|
-
if (source.isErr()) return source;
|
|
621
|
-
if (!source.isOk()) throw new InvalidResultStateError("tryMapAsync");
|
|
622
|
-
try {
|
|
623
|
-
return ok(await project(source.value));
|
|
624
|
-
} catch (error) {
|
|
625
|
-
return err(errorMapper ? errorMapper(error) : error);
|
|
626
|
-
}
|
|
627
|
-
};
|
|
628
|
-
}
|
|
629
|
-
|
|
630
319
|
//#endregion
|
|
631
320
|
//#region src/core/result.ts
|
|
632
321
|
var ResultBase = class extends Pipeable {
|
|
633
|
-
constructor() {
|
|
634
|
-
super();
|
|
635
|
-
Object.defineProperty(this, RESULT_BRAND, {
|
|
636
|
-
value: true,
|
|
637
|
-
enumerable: false,
|
|
638
|
-
configurable: false,
|
|
639
|
-
writable: false
|
|
640
|
-
});
|
|
641
|
-
}
|
|
642
322
|
isOk() {
|
|
643
323
|
if (this._tag === "Ok") return true;
|
|
644
324
|
if (this._tag === "Err") return false;
|
|
@@ -676,12 +356,12 @@ var ResultBase = class extends Pipeable {
|
|
|
676
356
|
}
|
|
677
357
|
expect(message) {
|
|
678
358
|
if (this._tag === "Ok") return this.value;
|
|
679
|
-
if (this._tag === "Err") throw new ExpectOkError(message);
|
|
359
|
+
if (this._tag === "Err") throw new ExpectOkError(message, this.error);
|
|
680
360
|
throw new InvalidResultStateError("Result.expect");
|
|
681
361
|
}
|
|
682
362
|
expectErr(message) {
|
|
683
363
|
if (this._tag === "Err") return this.error;
|
|
684
|
-
if (this._tag === "Ok") throw new ExpectErrError(message);
|
|
364
|
+
if (this._tag === "Ok") throw new ExpectErrError(message, this.value);
|
|
685
365
|
throw new InvalidResultStateError("Result.expectErr");
|
|
686
366
|
}
|
|
687
367
|
toPromise() {
|
|
@@ -723,12 +403,17 @@ var ResultBase = class extends Pipeable {
|
|
|
723
403
|
throw new InvalidResultStateError("Result.matchErrorAsync");
|
|
724
404
|
}
|
|
725
405
|
/**
|
|
726
|
-
* Matches on the Err value via `.when(...)` chain.
|
|
406
|
+
* Matches on the **Err** value via a `.when(...)` chain.
|
|
727
407
|
*
|
|
728
|
-
*
|
|
729
|
-
*
|
|
408
|
+
* Not the same as the `match({ ok, err })` pipe operator: that operator resolves
|
|
409
|
+
* **both** branches (Ok and Err) via callbacks, whereas this method is **Err-only**
|
|
410
|
+
* and returns an {@link ErrorMatchBuilder}.
|
|
730
411
|
*
|
|
731
|
-
*
|
|
412
|
+
* Note: for type-safety reasons, `.match()` can only be called on a Result already
|
|
413
|
+
* narrowed to `Err`, e.g. inside `if (result.isErr()) { ... }`.
|
|
414
|
+
*
|
|
415
|
+
* @deprecated Use `.matchError()` for clearer Err-only semantics. For Ok+Err handling,
|
|
416
|
+
* use the `match({ ok, err })` pipe operator instead.
|
|
732
417
|
*/
|
|
733
418
|
match() {
|
|
734
419
|
if (this._tag === "Err") return new ErrorMatchBuilder(this.error);
|
|
@@ -747,8 +432,32 @@ var ResultBase = class extends Pipeable {
|
|
|
747
432
|
return AsyncErrMatchBuilder.fromResult(this);
|
|
748
433
|
}
|
|
749
434
|
/**
|
|
435
|
+
* Serializes the Result into the discriminated shape
|
|
436
|
+
* `{ _tag: 'Ok', value }` / `{ _tag: 'Err', error }`.
|
|
437
|
+
*
|
|
438
|
+
* Unlike {@link serialize}, `Ok(undefined)` stays unambiguous, and the
|
|
439
|
+
* shape round-trips through `fromSerialized(...)`. It also matches what
|
|
440
|
+
* `JSON.stringify(result)` produces for a Result instance.
|
|
441
|
+
*/
|
|
442
|
+
toSerialized() {
|
|
443
|
+
if (this._tag === "Ok") return {
|
|
444
|
+
_tag: "Ok",
|
|
445
|
+
value: this.value
|
|
446
|
+
};
|
|
447
|
+
if (this._tag === "Err") return {
|
|
448
|
+
_tag: "Err",
|
|
449
|
+
error: this.error
|
|
450
|
+
};
|
|
451
|
+
throw new InvalidResultStateError("Result.toSerialized");
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
750
454
|
* Serializes the Result into a simple object format.
|
|
751
455
|
* Preserves the original types.
|
|
456
|
+
*
|
|
457
|
+
* @deprecated Use {@link toSerialized} instead: with this format,
|
|
458
|
+
* `Ok(undefined)` is indistinguishable from a missing `data` field, and
|
|
459
|
+
* there is no way to rebuild a Result from it. `toSerialized()`
|
|
460
|
+
* round-trips via `fromSerialized(...)`.
|
|
752
461
|
*/
|
|
753
462
|
serialize() {
|
|
754
463
|
if (this._tag === "Ok") return {
|
|
@@ -778,6 +487,12 @@ var ResultBase = class extends Pipeable {
|
|
|
778
487
|
};
|
|
779
488
|
}
|
|
780
489
|
};
|
|
490
|
+
Object.defineProperty(ResultBase.prototype, RESULT_BRAND, {
|
|
491
|
+
value: true,
|
|
492
|
+
enumerable: false,
|
|
493
|
+
configurable: false,
|
|
494
|
+
writable: false
|
|
495
|
+
});
|
|
781
496
|
var Ok = class extends ResultBase {
|
|
782
497
|
_tag = "Ok";
|
|
783
498
|
value;
|
|
@@ -857,5 +572,5 @@ const Result = {
|
|
|
857
572
|
};
|
|
858
573
|
|
|
859
574
|
//#endregion
|
|
860
|
-
export {
|
|
861
|
-
//# sourceMappingURL=result-
|
|
575
|
+
export { matchTag as _, fromNullable as a, ok as c, tryAsync as d, tryFn as f, sequence as g, all as h, err as i, okIf as l, zip as m, Ok as n, fromPromise as o, combine as p, Result as r, fromThrowable as s, Err as t, okIfLazy as u, isResult as v };
|
|
576
|
+
//# sourceMappingURL=result-q9Na2bGa.mjs.map
|