@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.
Files changed (55) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +137 -296
  3. package/dist/collections.cjs +12 -0
  4. package/dist/collections.d.cts +3 -0
  5. package/dist/collections.d.mts +3 -0
  6. package/dist/collections.mjs +4 -0
  7. package/dist/errors-2WOswg7r.mjs +95 -0
  8. package/dist/errors-2WOswg7r.mjs.map +1 -0
  9. package/dist/errors-BFjY06EV.d.cts +55 -0
  10. package/dist/errors-BFjY06EV.d.cts.map +1 -0
  11. package/dist/errors-C5qGMRiU.d.mts +55 -0
  12. package/dist/errors-C5qGMRiU.d.mts.map +1 -0
  13. package/dist/errors-D2EMzJQl.cjs +209 -0
  14. package/dist/errors-D2EMzJQl.cjs.map +1 -0
  15. package/dist/errors.cjs +21 -0
  16. package/dist/errors.d.cts +2 -0
  17. package/dist/errors.d.mts +2 -0
  18. package/dist/errors.mjs +3 -0
  19. package/dist/flatten-B8bN6fiI.d.mts +71 -0
  20. package/dist/flatten-B8bN6fiI.d.mts.map +1 -0
  21. package/dist/flatten-C6Y9hx79.mjs +147 -0
  22. package/dist/flatten-C6Y9hx79.mjs.map +1 -0
  23. package/dist/flatten-DK7eJKPx.d.cts +71 -0
  24. package/dist/flatten-DK7eJKPx.d.cts.map +1 -0
  25. package/dist/flatten-Df9U40nO.cjs +182 -0
  26. package/dist/flatten-Df9U40nO.cjs.map +1 -0
  27. package/dist/index.cjs +113 -1018
  28. package/dist/index.cjs.map +1 -1
  29. package/dist/index.d.cts +12 -546
  30. package/dist/index.d.cts.map +1 -1
  31. package/dist/index.d.mts +12 -546
  32. package/dist/index.d.mts.map +1 -1
  33. package/dist/index.mjs +25 -935
  34. package/dist/index.mjs.map +1 -1
  35. package/dist/mapOrElse-B0_4r6Jn.mjs +96 -0
  36. package/dist/mapOrElse-B0_4r6Jn.mjs.map +1 -0
  37. package/dist/mapOrElse-B5gx9x2E.d.mts +64 -0
  38. package/dist/mapOrElse-B5gx9x2E.d.mts.map +1 -0
  39. package/dist/mapOrElse-DIe7LkYV.d.cts +64 -0
  40. package/dist/mapOrElse-DIe7LkYV.d.cts.map +1 -0
  41. package/dist/mapOrElse-H-GvAUka.cjs +137 -0
  42. package/dist/mapOrElse-H-GvAUka.cjs.map +1 -0
  43. package/dist/operators.cjs +33 -0
  44. package/dist/operators.d.cts +3 -0
  45. package/dist/operators.d.mts +3 -0
  46. package/dist/operators.mjs +4 -0
  47. package/dist/result-CY-KIivk.cjs +1100 -0
  48. package/dist/result-CY-KIivk.cjs.map +1 -0
  49. package/dist/result-DKKaNdOx.mjs +861 -0
  50. package/dist/result-DKKaNdOx.mjs.map +1 -0
  51. package/dist/sequence-Br6tAIIu.d.cts +419 -0
  52. package/dist/sequence-Br6tAIIu.d.cts.map +1 -0
  53. package/dist/sequence-C0jlR3AY.d.mts +419 -0
  54. package/dist/sequence-C0jlR3AY.d.mts.map +1 -0
  55. package/package.json +44 -10
@@ -0,0 +1,1100 @@
1
+ const require_errors = require('./errors-D2EMzJQl.cjs');
2
+
3
+ //#region src/core/pipeable.ts
4
+ var Pipeable = class {
5
+ pipe(...ops) {
6
+ let ret = this;
7
+ for (const op of ops) ret = op(ret);
8
+ return ret;
9
+ }
10
+ async pipeAsync(...ops) {
11
+ let ret = this;
12
+ for (const op of ops) ret = await op(ret);
13
+ return ret;
14
+ }
15
+ };
16
+
17
+ //#endregion
18
+ //#region src/core/brand.ts
19
+ const RESULT_BRAND = Symbol.for("@shirudo/result.brand");
20
+
21
+ //#endregion
22
+ //#region src/core/isResult.ts
23
+ const hasOwn = (value, key) => Object.prototype.hasOwnProperty.call(value, key);
24
+ const hasResultMethods = (value) => {
25
+ return typeof value.isOk === "function" && typeof value.isErr === "function";
26
+ };
27
+ const hasValidPayload = (value) => {
28
+ if (value._tag === "Ok") return hasOwn(value, "value") && !hasOwn(value, "error");
29
+ if (value._tag === "Err") return hasOwn(value, "error") && !hasOwn(value, "value");
30
+ return false;
31
+ };
32
+ /**
33
+ * Checks whether a value is a Result.
34
+ * Pure function alternative for runtime checks.
35
+ */
36
+ function isResult(value) {
37
+ if (value === null || typeof value !== "object") return false;
38
+ const candidate = value;
39
+ return candidate[RESULT_BRAND] === true && hasResultMethods(candidate) && hasValidPayload(candidate);
40
+ }
41
+
42
+ //#endregion
43
+ //#region src/core/matcher.ts
44
+ const matchesTag = (value, key, tag) => {
45
+ return value !== null && typeof value === "object" && value[key] === tag;
46
+ };
47
+ function matchTag(result, key, handlers) {
48
+ if (result.isOk()) throw new require_errors.MatchOnOkError("matchTag");
49
+ if (result.isErr()) {
50
+ const error = result.error;
51
+ const handler = handlers[error[key]];
52
+ if (typeof handler !== "function") throw new require_errors.InvalidResultStateError("matchTag");
53
+ return handler(error);
54
+ }
55
+ throw new require_errors.InvalidResultStateError("matchTag");
56
+ }
57
+ /**
58
+ * Matcher for Err values (returns an arbitrary return type, e.g. string messages).
59
+ *
60
+ * - `.when(Ctor, handler)` matches via `instanceof`
61
+ * - `.whenGuard(guard, handler)` matches via Type-Guard
62
+ * - `.run()` is only allowed if all error cases have been handled (`E` has been reduced to `never`)
63
+ */
64
+ var ErrorMatchBuilder = class ErrorMatchBuilder {
65
+ #error;
66
+ #matched;
67
+ #value;
68
+ constructor(error, matched = false, value) {
69
+ this.#error = error;
70
+ this.#matched = matched;
71
+ this.#value = value;
72
+ Object.freeze(this);
73
+ }
74
+ when(ctor, handler) {
75
+ if (this.#matched) return this;
76
+ if (this.#error instanceof ctor) return new ErrorMatchBuilder(this.#error, true, handler(this.#error));
77
+ return this;
78
+ }
79
+ whenGuard(guard, handler) {
80
+ if (this.#matched) return this;
81
+ const error = this.#error;
82
+ if (guard(error)) return new ErrorMatchBuilder(this.#error, true, handler(error));
83
+ return this;
84
+ }
85
+ whenTag(key, tag, handler) {
86
+ if (this.#matched) return this;
87
+ if (matchesTag(this.#error, key, tag)) return new ErrorMatchBuilder(this.#error, true, handler(this.#error));
88
+ return this;
89
+ }
90
+ otherwise(handler) {
91
+ if (this.#matched) return this.#value;
92
+ return handler(this.#error);
93
+ }
94
+ run() {
95
+ if (this.#matched) return this.#value;
96
+ throw this.#error;
97
+ }
98
+ };
99
+ var AsyncErrorMatchBuilder = class AsyncErrorMatchBuilder {
100
+ #error;
101
+ #matched;
102
+ #value;
103
+ constructor(error, matched = false, value) {
104
+ this.#error = error;
105
+ this.#matched = matched;
106
+ this.#value = value;
107
+ Object.freeze(this);
108
+ }
109
+ when(ctor, handler) {
110
+ if (this.#matched) return this;
111
+ if (this.#error instanceof ctor) {
112
+ const value = Promise.resolve().then(() => handler(this.#error));
113
+ return new AsyncErrorMatchBuilder(this.#error, true, value);
114
+ }
115
+ return this;
116
+ }
117
+ whenGuard(guard, handler) {
118
+ if (this.#matched) return this;
119
+ const error = this.#error;
120
+ if (guard(error)) {
121
+ const value = Promise.resolve().then(() => handler(error));
122
+ return new AsyncErrorMatchBuilder(this.#error, true, value);
123
+ }
124
+ return this;
125
+ }
126
+ whenTag(key, tag, handler) {
127
+ if (this.#matched) return this;
128
+ if (matchesTag(this.#error, key, tag)) {
129
+ const value = Promise.resolve().then(() => handler(this.#error));
130
+ return new AsyncErrorMatchBuilder(this.#error, true, value);
131
+ }
132
+ return this;
133
+ }
134
+ async otherwise(handler) {
135
+ if (this.#matched) return await this.#value;
136
+ return await handler(this.#error);
137
+ }
138
+ async run() {
139
+ if (this.#matched) return await this.#value;
140
+ throw this.#error;
141
+ }
142
+ };
143
+ const expectResultReturn = (value, handlerName) => {
144
+ if (isResult(value)) return value;
145
+ throw new require_errors.MatchErrHandlerNotResultError(handlerName, value);
146
+ };
147
+ /**
148
+ * Matcher for `Result` Errors, which returns a `Result` again.
149
+ *
150
+ * Handlers must return a `Result`.
151
+ * Wrap recovered values with `ok(...)` and mapped errors with `err(...)`.
152
+ */
153
+ var ErrMatchBuilder = class ErrMatchBuilder {
154
+ #error;
155
+ #resolved;
156
+ constructor(error, resolved) {
157
+ this.#error = error;
158
+ this.#resolved = resolved;
159
+ Object.freeze(this);
160
+ }
161
+ static fromResult(result) {
162
+ if (result.isOk()) return new ErrMatchBuilder(void 0, result);
163
+ if (result.isErr()) return new ErrMatchBuilder(result.error);
164
+ throw new require_errors.InvalidResultStateError("ErrMatchBuilder.fromResult");
165
+ }
166
+ when(ctor, handler) {
167
+ if (this.#resolved) return this;
168
+ if (this.#error instanceof ctor) {
169
+ const resolved = expectResultReturn(handler(this.#error), "when");
170
+ return new ErrMatchBuilder(this.#error, resolved);
171
+ }
172
+ return this;
173
+ }
174
+ whenGuard(guard, handler) {
175
+ if (this.#resolved) return this;
176
+ const error = this.#error;
177
+ if (guard(error)) {
178
+ const resolved = expectResultReturn(handler(error), "whenGuard");
179
+ return new ErrMatchBuilder(this.#error, resolved);
180
+ }
181
+ return this;
182
+ }
183
+ whenTag(key, tag, handler) {
184
+ if (this.#resolved) return this;
185
+ if (matchesTag(this.#error, key, tag)) {
186
+ const resolved = expectResultReturn(handler(this.#error), "whenTag");
187
+ return new ErrMatchBuilder(this.#error, resolved);
188
+ }
189
+ return this;
190
+ }
191
+ otherwise(handler) {
192
+ if (this.#resolved) return this.#resolved;
193
+ return expectResultReturn(handler(this.#error), "otherwise");
194
+ }
195
+ run() {
196
+ if (this.#resolved) return this.#resolved;
197
+ throw this.#error;
198
+ }
199
+ };
200
+ var AsyncErrMatchBuilder = class AsyncErrMatchBuilder {
201
+ #error;
202
+ #resolved;
203
+ constructor(error, resolved) {
204
+ this.#error = error;
205
+ this.#resolved = resolved;
206
+ Object.freeze(this);
207
+ }
208
+ static fromResult(result) {
209
+ if (result.isOk()) return new AsyncErrMatchBuilder(void 0, Promise.resolve(result));
210
+ if (result.isErr()) return new AsyncErrMatchBuilder(result.error);
211
+ throw new require_errors.InvalidResultStateError("AsyncErrMatchBuilder.fromResult");
212
+ }
213
+ when(ctor, handler) {
214
+ if (this.#resolved) return this;
215
+ if (this.#error instanceof ctor) {
216
+ const resolved = Promise.resolve().then(() => handler(this.#error)).then((out) => expectResultReturn(out, "when"));
217
+ return new AsyncErrMatchBuilder(this.#error, resolved);
218
+ }
219
+ return this;
220
+ }
221
+ whenGuard(guard, handler) {
222
+ if (this.#resolved) return this;
223
+ const error = this.#error;
224
+ if (guard(error)) {
225
+ const resolved = Promise.resolve().then(() => handler(error)).then((out) => expectResultReturn(out, "whenGuard"));
226
+ return new AsyncErrMatchBuilder(this.#error, resolved);
227
+ }
228
+ return this;
229
+ }
230
+ whenTag(key, tag, handler) {
231
+ if (this.#resolved) return this;
232
+ if (matchesTag(this.#error, key, tag)) {
233
+ const resolved = Promise.resolve().then(() => handler(this.#error)).then((out) => expectResultReturn(out, "whenTag"));
234
+ return new AsyncErrMatchBuilder(this.#error, resolved);
235
+ }
236
+ return this;
237
+ }
238
+ async otherwise(handler) {
239
+ if (this.#resolved) return await this.#resolved;
240
+ return expectResultReturn(await handler(this.#error), "otherwise");
241
+ }
242
+ async run() {
243
+ if (this.#resolved) return await this.#resolved;
244
+ throw this.#error;
245
+ }
246
+ };
247
+
248
+ //#endregion
249
+ //#region src/core/sequence.ts
250
+ /**
251
+ * Combines a list of Results into a single Result of a list.
252
+ * Short-circuits on the first Err.
253
+ * Analogous to Rust `collect::<Result<Vec<_>, _>>()`.
254
+ */
255
+ function sequence(results) {
256
+ const values = [];
257
+ for (const result of results) {
258
+ if (result.isOk()) {
259
+ values.push(result.value);
260
+ continue;
261
+ }
262
+ if (result.isErr()) return result;
263
+ throw new require_errors.InvalidResultStateError("sequence");
264
+ }
265
+ return ok(values);
266
+ }
267
+ /**
268
+ * Alias for `sequence`.
269
+ */
270
+ function all(results) {
271
+ return sequence(results);
272
+ }
273
+
274
+ //#endregion
275
+ //#region src/core/zip.ts
276
+ function zipImpl(left, right) {
277
+ if (left.isErr()) return left;
278
+ if (right.isErr()) return right;
279
+ if (left.isOk() && right.isOk()) return ok([left.value, right.value]);
280
+ throw new require_errors.InvalidResultStateError("zip");
281
+ }
282
+ function zip(...args) {
283
+ if (args.length === 1) {
284
+ const right$1 = args[0];
285
+ return (left$1) => zipImpl(left$1, right$1);
286
+ }
287
+ const [left, right] = args;
288
+ return zipImpl(left, right);
289
+ }
290
+ function combineImpl(left, right) {
291
+ if (left.isOk() && right.isOk()) return ok([left.value, right.value]);
292
+ const errors = [];
293
+ if (left.isErr()) errors.push(left.error);
294
+ if (right.isErr()) errors.push(right.error);
295
+ if (!left.isOk() && !left.isErr()) throw new require_errors.InvalidResultStateError("combine");
296
+ if (!right.isOk() && !right.isErr()) throw new require_errors.InvalidResultStateError("combine");
297
+ return err(errors);
298
+ }
299
+ function combine(...args) {
300
+ if (args.length === 1) {
301
+ const right$1 = args[0];
302
+ return (left$1) => combineImpl(left$1, right$1);
303
+ }
304
+ const [left, right] = args;
305
+ return combineImpl(left, right);
306
+ }
307
+
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 require_errors.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 require_errors.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 require_errors.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 require_errors.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 require_errors.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 require_errors.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 require_errors.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 require_errors.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 require_errors.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 require_errors.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 require_errors.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 require_errors.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 require_errors.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 require_errors.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 require_errors.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 require_errors.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 require_errors.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 require_errors.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 require_errors.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
+ //#endregion
631
+ //#region src/core/result.ts
632
+ 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
+ isOk() {
643
+ if (this._tag === "Ok") return true;
644
+ if (this._tag === "Err") return false;
645
+ throw new require_errors.InvalidResultStateError("Result.isOk");
646
+ }
647
+ isErr() {
648
+ if (this._tag === "Err") return true;
649
+ if (this._tag === "Ok") return false;
650
+ throw new require_errors.InvalidResultStateError("Result.isErr");
651
+ }
652
+ unwrapOr(defaultValue) {
653
+ if (this._tag === "Ok") return this.value;
654
+ if (this._tag === "Err") return defaultValue;
655
+ throw new require_errors.InvalidResultStateError("Result.unwrapOr");
656
+ }
657
+ unwrap() {
658
+ if (this._tag === "Ok") return this.value;
659
+ if (this._tag === "Err") throw new require_errors.UnwrapOnErrError(this.error);
660
+ throw new require_errors.InvalidResultStateError("Result.unwrap");
661
+ }
662
+ unwrapErr() {
663
+ if (this._tag === "Err") return this.error;
664
+ if (this._tag === "Ok") throw new require_errors.UnwrapErrOnOkError(this.value);
665
+ throw new require_errors.InvalidResultStateError("Result.unwrapErr");
666
+ }
667
+ unwrapOrElse(fn) {
668
+ if (this._tag === "Ok") return this.value;
669
+ if (this._tag === "Err") return fn(this.error);
670
+ throw new require_errors.InvalidResultStateError("Result.unwrapOrElse");
671
+ }
672
+ unwrapOrThrow() {
673
+ if (this._tag === "Ok") return this.value;
674
+ if (this._tag === "Err") throw this.error;
675
+ throw new require_errors.InvalidResultStateError("Result.unwrapOrThrow");
676
+ }
677
+ expect(message) {
678
+ if (this._tag === "Ok") return this.value;
679
+ if (this._tag === "Err") throw new require_errors.ExpectOkError(message);
680
+ throw new require_errors.InvalidResultStateError("Result.expect");
681
+ }
682
+ expectErr(message) {
683
+ if (this._tag === "Err") return this.error;
684
+ if (this._tag === "Ok") throw new require_errors.ExpectErrError(message);
685
+ throw new require_errors.InvalidResultStateError("Result.expectErr");
686
+ }
687
+ toPromise() {
688
+ if (this._tag === "Ok") return Promise.resolve(this.value);
689
+ if (this._tag === "Err") return Promise.reject(this.error);
690
+ throw new require_errors.InvalidResultStateError("Result.toPromise");
691
+ }
692
+ toNullable() {
693
+ if (this._tag === "Ok") return this.value;
694
+ if (this._tag === "Err") return null;
695
+ throw new require_errors.InvalidResultStateError("Result.toNullable");
696
+ }
697
+ /**
698
+ * Folds the Result into a single value by applying one of two functions.
699
+ */
700
+ fold(onOk, onErr) {
701
+ if (this._tag === "Ok") return onOk(this.value);
702
+ if (this._tag === "Err") return onErr(this.error);
703
+ throw new require_errors.InvalidResultStateError("Result.fold");
704
+ }
705
+ /**
706
+ * Enables `yield* result` in generators (Do-notation).
707
+ *
708
+ * The iterator yields the `Result` itself; the runner (see `task`) decides:
709
+ * - Ok → sends back the Ok value (`next(value)`), `yield*` yields `T`
710
+ * - Err → aborts and returns the Err Result
711
+ */
712
+ *[Symbol.iterator]() {
713
+ return yield this;
714
+ }
715
+ matchError() {
716
+ if (this._tag === "Err") return new ErrorMatchBuilder(this.error);
717
+ if (this._tag === "Ok") throw new require_errors.MatchOnOkError("matchError");
718
+ throw new require_errors.InvalidResultStateError("Result.matchError");
719
+ }
720
+ matchErrorAsync() {
721
+ if (this._tag === "Err") return new AsyncErrorMatchBuilder(this.error);
722
+ if (this._tag === "Ok") throw new require_errors.MatchOnOkError("matchErrorAsync");
723
+ throw new require_errors.InvalidResultStateError("Result.matchErrorAsync");
724
+ }
725
+ /**
726
+ * Matches on the Err value via `.when(...)` chain.
727
+ *
728
+ * Note: for type safety reasons, `.match()` can only be called on a Result already narrowed to `Err`,
729
+ * e.g. inside `if (result.isErr()) { ... }`.
730
+ *
731
+ * @deprecated Use `.matchError()` for clearer Err-only semantics.
732
+ */
733
+ match() {
734
+ if (this._tag === "Err") return new ErrorMatchBuilder(this.error);
735
+ if (this._tag === "Ok") throw new require_errors.MatchOnOkError();
736
+ throw new require_errors.InvalidResultStateError("Result.match");
737
+ }
738
+ /**
739
+ * Matches on the Err value, but normalizes every branch to a `Result`:
740
+ * - Handlers must return a `Result`
741
+ * - use `ok(...)` for recovery and `err(...)` for mapped errors
742
+ */
743
+ matchErr() {
744
+ return ErrMatchBuilder.fromResult(this);
745
+ }
746
+ matchErrAsync() {
747
+ return AsyncErrMatchBuilder.fromResult(this);
748
+ }
749
+ /**
750
+ * Serializes the Result into a simple object format.
751
+ * Preserves the original types.
752
+ */
753
+ serialize() {
754
+ if (this._tag === "Ok") return {
755
+ isSuccess: true,
756
+ data: this.value
757
+ };
758
+ if (this._tag === "Err") return {
759
+ isSuccess: false,
760
+ error: this.error
761
+ };
762
+ throw new require_errors.InvalidResultStateError("Result.serialize");
763
+ }
764
+ /**
765
+ * Serializes the Result into a user-friendly format.
766
+ * Converts Errors to readable strings.
767
+ */
768
+ toUserFriendly() {
769
+ if (this._tag === "Ok") return {
770
+ isSuccess: true,
771
+ data: this.value
772
+ };
773
+ if (this._tag !== "Err") throw new require_errors.InvalidResultStateError("Result.toUserFriendly");
774
+ const error = this.error;
775
+ return {
776
+ isSuccess: false,
777
+ error: error && typeof error === "object" && "message" in error ? String(error.message) : String(error)
778
+ };
779
+ }
780
+ };
781
+ var Ok = class extends ResultBase {
782
+ _tag = "Ok";
783
+ value;
784
+ constructor(value) {
785
+ super();
786
+ this.value = value;
787
+ Object.freeze(this);
788
+ }
789
+ };
790
+ var Err = class extends ResultBase {
791
+ _tag = "Err";
792
+ error;
793
+ constructor(error) {
794
+ super();
795
+ this.error = error;
796
+ Object.freeze(this);
797
+ }
798
+ };
799
+ function ok(value) {
800
+ return new Ok(value);
801
+ }
802
+ function err(error) {
803
+ return new Err(error);
804
+ }
805
+ function okIf(condition, okValue, errValue) {
806
+ return condition ? ok(okValue) : err(errValue);
807
+ }
808
+ function okIfLazy(condition, okFn, errFn) {
809
+ return condition ? ok(okFn()) : err(errFn());
810
+ }
811
+ function fromNullable(value, error) {
812
+ if (value === null || value === void 0) return err(error);
813
+ return ok(value);
814
+ }
815
+ async function fromPromise(promise, errorMapper) {
816
+ try {
817
+ return ok(await promise);
818
+ } catch (error) {
819
+ return err(errorMapper ? errorMapper(error) : error);
820
+ }
821
+ }
822
+ function tryFn(fn) {
823
+ try {
824
+ return ok(fn());
825
+ } catch (error) {
826
+ return err(error);
827
+ }
828
+ }
829
+ function fromThrowable(fn, errorMapper) {
830
+ return (...args) => {
831
+ try {
832
+ return ok(fn(...args));
833
+ } catch (error) {
834
+ return err(errorMapper ? errorMapper(error) : error);
835
+ }
836
+ };
837
+ }
838
+ async function tryAsync(fn, errorMapper) {
839
+ try {
840
+ return ok(await fn());
841
+ } catch (error) {
842
+ return err(errorMapper ? errorMapper(error) : error);
843
+ }
844
+ }
845
+ const Result = {
846
+ ok,
847
+ err,
848
+ is: isResult,
849
+ fromNullable,
850
+ fromPromise,
851
+ fromThrowable,
852
+ try: tryFn,
853
+ tryAsync,
854
+ sequence,
855
+ all,
856
+ combine
857
+ };
858
+
859
+ //#endregion
860
+ Object.defineProperty(exports, 'Err', {
861
+ enumerable: true,
862
+ get: function () {
863
+ return Err;
864
+ }
865
+ });
866
+ Object.defineProperty(exports, 'Ok', {
867
+ enumerable: true,
868
+ get: function () {
869
+ return Ok;
870
+ }
871
+ });
872
+ Object.defineProperty(exports, 'Result', {
873
+ enumerable: true,
874
+ get: function () {
875
+ return Result;
876
+ }
877
+ });
878
+ Object.defineProperty(exports, 'all', {
879
+ enumerable: true,
880
+ get: function () {
881
+ return all;
882
+ }
883
+ });
884
+ Object.defineProperty(exports, 'bimap', {
885
+ enumerable: true,
886
+ get: function () {
887
+ return bimap;
888
+ }
889
+ });
890
+ Object.defineProperty(exports, 'collectFirstOk', {
891
+ enumerable: true,
892
+ get: function () {
893
+ return collectFirstOk;
894
+ }
895
+ });
896
+ Object.defineProperty(exports, 'combine', {
897
+ enumerable: true,
898
+ get: function () {
899
+ return combine;
900
+ }
901
+ });
902
+ Object.defineProperty(exports, 'err', {
903
+ enumerable: true,
904
+ get: function () {
905
+ return err;
906
+ }
907
+ });
908
+ Object.defineProperty(exports, 'filter', {
909
+ enumerable: true,
910
+ get: function () {
911
+ return filter;
912
+ }
913
+ });
914
+ Object.defineProperty(exports, 'filterAsync', {
915
+ enumerable: true,
916
+ get: function () {
917
+ return filterAsync;
918
+ }
919
+ });
920
+ Object.defineProperty(exports, 'flatMap', {
921
+ enumerable: true,
922
+ get: function () {
923
+ return flatMap;
924
+ }
925
+ });
926
+ Object.defineProperty(exports, 'flatMapAsync', {
927
+ enumerable: true,
928
+ get: function () {
929
+ return flatMapAsync;
930
+ }
931
+ });
932
+ Object.defineProperty(exports, 'fromNullable', {
933
+ enumerable: true,
934
+ get: function () {
935
+ return fromNullable;
936
+ }
937
+ });
938
+ Object.defineProperty(exports, 'fromPromise', {
939
+ enumerable: true,
940
+ get: function () {
941
+ return fromPromise;
942
+ }
943
+ });
944
+ Object.defineProperty(exports, 'fromThrowable', {
945
+ enumerable: true,
946
+ get: function () {
947
+ return fromThrowable;
948
+ }
949
+ });
950
+ Object.defineProperty(exports, 'isResult', {
951
+ enumerable: true,
952
+ get: function () {
953
+ return isResult;
954
+ }
955
+ });
956
+ Object.defineProperty(exports, 'map', {
957
+ enumerable: true,
958
+ get: function () {
959
+ return map;
960
+ }
961
+ });
962
+ Object.defineProperty(exports, 'mapAsync', {
963
+ enumerable: true,
964
+ get: function () {
965
+ return mapAsync;
966
+ }
967
+ });
968
+ Object.defineProperty(exports, 'mapBoth', {
969
+ enumerable: true,
970
+ get: function () {
971
+ return mapBoth;
972
+ }
973
+ });
974
+ Object.defineProperty(exports, 'mapErr', {
975
+ enumerable: true,
976
+ get: function () {
977
+ return mapErr;
978
+ }
979
+ });
980
+ Object.defineProperty(exports, 'mapErrAsync', {
981
+ enumerable: true,
982
+ get: function () {
983
+ return mapErrAsync;
984
+ }
985
+ });
986
+ Object.defineProperty(exports, 'match', {
987
+ enumerable: true,
988
+ get: function () {
989
+ return match;
990
+ }
991
+ });
992
+ Object.defineProperty(exports, 'matchAsync', {
993
+ enumerable: true,
994
+ get: function () {
995
+ return matchAsync;
996
+ }
997
+ });
998
+ Object.defineProperty(exports, 'matchTag', {
999
+ enumerable: true,
1000
+ get: function () {
1001
+ return matchTag;
1002
+ }
1003
+ });
1004
+ Object.defineProperty(exports, 'ok', {
1005
+ enumerable: true,
1006
+ get: function () {
1007
+ return ok;
1008
+ }
1009
+ });
1010
+ Object.defineProperty(exports, 'okIf', {
1011
+ enumerable: true,
1012
+ get: function () {
1013
+ return okIf;
1014
+ }
1015
+ });
1016
+ Object.defineProperty(exports, 'okIfLazy', {
1017
+ enumerable: true,
1018
+ get: function () {
1019
+ return okIfLazy;
1020
+ }
1021
+ });
1022
+ Object.defineProperty(exports, 'recover', {
1023
+ enumerable: true,
1024
+ get: function () {
1025
+ return recover;
1026
+ }
1027
+ });
1028
+ Object.defineProperty(exports, 'recoverWith', {
1029
+ enumerable: true,
1030
+ get: function () {
1031
+ return recoverWith;
1032
+ }
1033
+ });
1034
+ Object.defineProperty(exports, 'sequence', {
1035
+ enumerable: true,
1036
+ get: function () {
1037
+ return sequence;
1038
+ }
1039
+ });
1040
+ Object.defineProperty(exports, 'swap', {
1041
+ enumerable: true,
1042
+ get: function () {
1043
+ return swap;
1044
+ }
1045
+ });
1046
+ Object.defineProperty(exports, 'tap', {
1047
+ enumerable: true,
1048
+ get: function () {
1049
+ return tap;
1050
+ }
1051
+ });
1052
+ Object.defineProperty(exports, 'tapAsync', {
1053
+ enumerable: true,
1054
+ get: function () {
1055
+ return tapAsync;
1056
+ }
1057
+ });
1058
+ Object.defineProperty(exports, 'tryAsync', {
1059
+ enumerable: true,
1060
+ get: function () {
1061
+ return tryAsync;
1062
+ }
1063
+ });
1064
+ Object.defineProperty(exports, 'tryCatch', {
1065
+ enumerable: true,
1066
+ get: function () {
1067
+ return tryCatch;
1068
+ }
1069
+ });
1070
+ Object.defineProperty(exports, 'tryCatchAsync', {
1071
+ enumerable: true,
1072
+ get: function () {
1073
+ return tryCatchAsync;
1074
+ }
1075
+ });
1076
+ Object.defineProperty(exports, 'tryFn', {
1077
+ enumerable: true,
1078
+ get: function () {
1079
+ return tryFn;
1080
+ }
1081
+ });
1082
+ Object.defineProperty(exports, 'tryMap', {
1083
+ enumerable: true,
1084
+ get: function () {
1085
+ return tryMap;
1086
+ }
1087
+ });
1088
+ Object.defineProperty(exports, 'tryMapAsync', {
1089
+ enumerable: true,
1090
+ get: function () {
1091
+ return tryMapAsync;
1092
+ }
1093
+ });
1094
+ Object.defineProperty(exports, 'zip', {
1095
+ enumerable: true,
1096
+ get: function () {
1097
+ return zip;
1098
+ }
1099
+ });
1100
+ //# sourceMappingURL=result-CY-KIivk.cjs.map