@onrails/result 0.2.0 → 0.3.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 (60) hide show
  1. package/DESIGN.md +10 -18
  2. package/README.md +14 -59
  3. package/RECIPES.md +12 -25
  4. package/dist/{async-u8U5P2EH.d.cts → async-BR3AX0ck.d.cts} +54 -18
  5. package/dist/{async-1fIhfzFu.d.ts → async-SZ7BEl0g.d.ts} +54 -18
  6. package/dist/compat/neverthrow.cjs +39 -51
  7. package/dist/compat/neverthrow.cjs.map +1 -1
  8. package/dist/compat/neverthrow.d.cts +3 -3
  9. package/dist/compat/neverthrow.d.ts +3 -3
  10. package/dist/compat/neverthrow.js +39 -51
  11. package/dist/compat/neverthrow.js.map +1 -1
  12. package/dist/extra.cjs +1 -1
  13. package/dist/extra.cjs.map +1 -1
  14. package/dist/extra.d.cts +5 -11
  15. package/dist/extra.d.ts +5 -11
  16. package/dist/extra.js +1 -1
  17. package/dist/extra.js.map +1 -1
  18. package/dist/fluent.cjs +55 -27
  19. package/dist/fluent.cjs.map +1 -1
  20. package/dist/fluent.d.cts +37 -6
  21. package/dist/fluent.d.ts +37 -6
  22. package/dist/fluent.js +55 -27
  23. package/dist/fluent.js.map +1 -1
  24. package/dist/index.cjs +80 -94
  25. package/dist/index.cjs.map +1 -1
  26. package/dist/index.d.cts +146 -44
  27. package/dist/index.d.ts +146 -44
  28. package/dist/index.js +77 -94
  29. package/dist/index.js.map +1 -1
  30. package/dist/railway.cjs +98 -172
  31. package/dist/railway.cjs.map +1 -1
  32. package/dist/railway.d.cts +25 -179
  33. package/dist/railway.d.ts +25 -179
  34. package/dist/railway.js +99 -162
  35. package/dist/railway.js.map +1 -1
  36. package/dist/try-gen.cjs +1 -1
  37. package/dist/try-gen.cjs.map +1 -1
  38. package/dist/try-gen.js +1 -1
  39. package/dist/try-gen.js.map +1 -1
  40. package/package.json +1 -16
  41. package/skills/railway-do-notation/SKILL.md +29 -68
  42. package/skills/result-composition/SKILL.md +2 -4
  43. package/dist/$.cjs +0 -40
  44. package/dist/$.cjs.map +0 -1
  45. package/dist/$.js +0 -36
  46. package/dist/$.js.map +0 -1
  47. package/dist/_.d.cts +0 -2
  48. package/dist/_.d.ts +0 -2
  49. package/dist/interop.cjs +0 -433
  50. package/dist/interop.cjs.map +0 -1
  51. package/dist/interop.d.cts +0 -75
  52. package/dist/interop.d.ts +0 -75
  53. package/dist/interop.js +0 -429
  54. package/dist/interop.js.map +0 -1
  55. package/dist/validation.cjs +0 -53
  56. package/dist/validation.cjs.map +0 -1
  57. package/dist/validation.d.cts +0 -72
  58. package/dist/validation.d.ts +0 -72
  59. package/dist/validation.js +0 -48
  60. package/dist/validation.js.map +0 -1
package/dist/index.d.ts CHANGED
@@ -1,10 +1,15 @@
1
- import { R as ResultAsync } from './async-1fIhfzFu.js';
2
- import { R as Result, E as Err, O as Ok } from './types-BQ9vv0nD.js';
3
- export { U as UnexpectedError } from './types-BQ9vv0nD.js';
4
- export { InferErr, InferOk, asyncAfter, fromAsync, fromResult } from './interop.js';
1
+ import { R as ResultAsync, I as InferErr, a as InferOk } from './async-SZ7BEl0g.js';
2
+ import { R as Result, U as UnexpectedError, E as Err, O as Ok } from './types-BQ9vv0nD.js';
5
3
  export { flow } from './pipe.js';
6
4
  export { $, tryGen, yieldResult } from './try-gen.js';
7
5
 
6
+ /**
7
+ * @onrails/result — THE lift module. Every "value → {@link ResultAsync}"
8
+ * entry point is defined exactly once, here: `okAsync`, `errAsync`,
9
+ * `fromPromise`, `fromSafePromise`, `fromResult`, `fromAsync`, `asyncAfter`,
10
+ * `tryAsync`.
11
+ */
12
+
8
13
  /**
9
14
  * Lifts a value into an `Ok` async result.
10
15
  *
@@ -42,20 +47,49 @@ declare const fromPromise: typeof ResultAsync.fromPromise;
42
47
  */
43
48
  declare const fromSafePromise: typeof ResultAsync.fromSafePromise;
44
49
  /**
45
- * Heterogeneous async tuple combine — branches overlap in wall-clock time.
46
- * Returns the first `Err` in **input** order (not completion order).
50
+ * Lifts an already-settled sync {@link Result} into a {@link ResultAsync}, so
51
+ * it can be chained alongside async steps in a railway.
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * const ra = fromResult(ok(1)); // ResultAsync<number, never>
56
+ * await fromResult(err(error)).resolve(); // Err(error)
57
+ * ```
58
+ */
59
+ declare const fromResult: typeof ResultAsync.fromResult;
60
+ /**
61
+ * Bind a sync {@link Result} into an async step without widening defects.
62
+ * Dual-form: data-first for one-shots, data-last (curried) for `pipe`/`flow`.
47
63
  *
48
64
  * @example
49
65
  * ```ts
50
- * const combined = parallelTupleAsync([
51
- * loadProfile(id),
52
- * loadMetrics(id),
53
- * loadFlags(id),
54
- * ] as const);
55
- * // ResultAsync<readonly [Profile, Metrics, Flags], …>
66
+ * asyncAfter(result, (u) => tryAsync(db.insert(u), toErr)); // data-first
67
+ * asyncAfter((u) => tryAsync(db.insert(u), toErr)); // data-last
56
68
  * ```
57
69
  */
58
- declare const parallelTupleAsync: typeof ResultAsync.combineTupleParallel;
70
+ declare const asyncAfter: {
71
+ <T, U, E, F>(result: Result<T, E>, fn: (value: T) => ResultAsync<U, F>): ResultAsync<U, E | F>;
72
+ <T, U, F>(fn: (value: T) => ResultAsync<U, F>): <E>(result: Result<T, E>) => ResultAsync<U, E | F>;
73
+ };
74
+ type AnyResult = Result<unknown, unknown>;
75
+ /**
76
+ * Lifts a `(...args) => Promise<Result<T, E>>` function (typical of interop or
77
+ * boundary code) into one returning `(...args) => ResultAsync<T, E>`. Expected
78
+ * `Err`s pass through; an unexpected promise rejection (a defect) is routed via
79
+ * `onDefect`, defaulting to {@link UnexpectedError} and widening the error union.
80
+ *
81
+ * @param fn - a function returning a promise that already yields a `Result`
82
+ * @param onDefect - maps an unexpected rejection to the `Err` channel
83
+ *
84
+ * @example
85
+ * ```ts
86
+ * const loadUser = fromAsync(
87
+ * (id: string): Promise<Result<User, NotFound>> => api.getUser(id),
88
+ * );
89
+ * const ra = loadUser("u1"); // ResultAsync<User, NotFound | UnexpectedError>
90
+ * ```
91
+ */
92
+ declare const fromAsync: <A extends readonly unknown[], R extends AnyResult>(fn: (...args: A) => Promise<R>, onDefect?: (error: unknown) => InferErr<R> | UnexpectedError) => ((...args: A) => ResultAsync<InferOk<R>, InferErr<R> | UnexpectedError>);
59
93
  /**
60
94
  * Convenience wrapper over {@link fromPromise} with default `Error`
61
95
  * normalization. Call without `onReject` to get `ResultAsync<T, Error>`,
@@ -76,6 +110,16 @@ declare const parallelTupleAsync: typeof ResultAsync.combineTupleParallel;
76
110
  declare function tryAsync<T>(promise: PromiseLike<T>): ResultAsync<T, Error>;
77
111
  declare function tryAsync<T, E>(promise: PromiseLike<T>, onReject: (error: unknown) => E): ResultAsync<T, E>;
78
112
 
113
+ /**
114
+ * @onrails/result — sync aggregation, one module, two axes:
115
+ *
116
+ * short-circuit (first `Err` wins) — {@link combine} / {@link combineTuple}
117
+ * accumulate expected failures — {@link validateAll} / {@link validateTuple}
118
+ *
119
+ * The accumulate pair collects every failure into a readonly array by
120
+ * default, or folds them with an explicit `combineErrors` when given one.
121
+ */
122
+
79
123
  /**
80
124
  * Combines a homogeneous array of results into a single result holding an
81
125
  * array of the `Ok` values, in input order. Short-circuits on the first `Err`
@@ -89,6 +133,11 @@ declare function tryAsync<T, E>(promise: PromiseLike<T>, onReject: (error: unkno
89
133
  * ```
90
134
  */
91
135
  declare const combine: <T, E>(results: readonly Result<T, E>[]) => Result<T[], E>;
136
+ type CombineTuple<R extends readonly Result<unknown, unknown>[]> = Result<{
137
+ [K in keyof R]: InferOk<R[K]>;
138
+ }, {
139
+ [K in keyof R]: InferErr<R[K]>;
140
+ }[number]>;
92
141
  /**
93
142
  * Heterogeneous tuple combine — like {@link combine} but preserves each
94
143
  * branch's `Ok` type by position, so the result destructures type-safely.
@@ -104,19 +153,51 @@ declare const combine: <T, E>(results: readonly Result<T, E>[]) => Result<T[], E
104
153
  * ```
105
154
  */
106
155
  declare const combineTuple: <const R extends readonly Result<unknown, unknown>[]>(results: R) => CombineTuple<R>;
107
- type _OkValue<R> = R extends {
108
- _tag: "Ok";
109
- readonly value: infer T;
110
- } ? T : never;
111
- type _ErrValue<R> = R extends {
112
- _tag: "Err";
113
- readonly error: infer E;
114
- } ? E : never;
115
- type CombineTuple<R extends readonly Result<unknown, unknown>[]> = Result<{
116
- [K in keyof R]: _OkValue<R[K]>;
117
- }, {
118
- [K in keyof R]: _ErrValue<R[K]>;
119
- }[number]>;
156
+ /**
157
+ * Accumulate independent validation failures. Returns `Ok<T[]>` only when
158
+ * every input is `Ok`. Without a combiner, failures are collected into a
159
+ * readonly array; with `combineErrors` they are folded into a single `E`.
160
+ *
161
+ * Unlike {@link combine}, this does **not** short-circuit on first failure —
162
+ * use for independent checks where you want to report all problems at once.
163
+ *
164
+ * @example
165
+ * ```ts
166
+ * const checks: Result<string, string[]>[] = [ok("Ada"), err(["age required"])];
167
+ *
168
+ * validateAll(checks);
169
+ * // Result<string[], readonly string[][]>
170
+ *
171
+ * validateAll(checks, (left, right) => [...left, ...right]);
172
+ * // Result<string[], string[]>
173
+ * ```
174
+ */
175
+ declare function validateAll<T, E>(results: readonly Result<T, E>[]): Result<T[], readonly E[]>;
176
+ declare function validateAll<T, E>(results: readonly Result<T, E>[], combineErrors: (left: E, right: E) => E): Result<T[], E>;
177
+ /**
178
+ * Tuple-preserving variant of {@link validateAll}. Heterogeneous input tuple
179
+ * → preserved `Ok` tuple shape. Without a combiner, the error union is
180
+ * collected into a readonly array; with `combineErrors`, all inputs must
181
+ * share the error type `E` and failures fold into a single `E`.
182
+ *
183
+ * @example
184
+ * ```ts
185
+ * const name: Result<string, string[]> = ok("Ada");
186
+ * const age: Result<number, string[]> = ok(36);
187
+ *
188
+ * validateTuple([name, age] as const);
189
+ * // Result<readonly [string, number], readonly string[][]>
190
+ *
191
+ * validateTuple([name, age] as const, (l, r) => [...l, ...r]);
192
+ * // Result<readonly [string, number], string[]>
193
+ * ```
194
+ */
195
+ declare function validateTuple<const R extends readonly Result<unknown, unknown>[]>(results: R): Result<{
196
+ [K in keyof R]: InferOk<R[K]>;
197
+ }, readonly InferErr<R[number]>[]>;
198
+ declare function validateTuple<const R extends readonly Result<unknown, unknown>[], E = InferErr<R[number]>>(results: R & readonly Result<unknown, E>[], combineErrors: (left: E, right: E) => E): Result<{
199
+ [K in keyof R]: InferOk<R[K]>;
200
+ }, E>;
120
201
 
121
202
  /**
122
203
  * Lifts a value into the success track.
@@ -170,8 +251,10 @@ declare const isErr: <T, E>(result: Result<T, E>) => result is Err<T, E>;
170
251
  * pipe(ok("x"), map((s) => s.length));// Ok 1 — curried
171
252
  * ```
172
253
  */
173
- declare function map<T, U, E>(result: Result<T, E>, fn: (value: T) => U): Result<U, E>;
174
- declare function map<T, U>(fn: (value: T) => U): <E>(result: Result<T, E>) => Result<U, E>;
254
+ declare const map: {
255
+ <T, U, E>(result: Result<T, E>, fn: (value: T) => U): Result<U, E>;
256
+ <T, U>(fn: (value: T) => U): <E>(result: Result<T, E>) => Result<U, E>;
257
+ };
175
258
  /**
176
259
  * Transform the `Err` value, passing `Ok` through unchanged. Useful for
177
260
  * unifying heterogeneous failure types into one app-level union.
@@ -185,8 +268,10 @@ declare function map<T, U>(fn: (value: T) => U): <E>(result: Result<T, E>) => Re
185
268
  * );
186
269
  * ```
187
270
  */
188
- declare function mapErr<T, E, F>(result: Result<T, E>, fn: (error: E) => F): Result<T, F>;
189
- declare function mapErr<E, F>(fn: (error: E) => F): <T>(result: Result<T, E>) => Result<T, F>;
271
+ declare const mapErr: {
272
+ <T, E, F>(result: Result<T, E>, fn: (error: E) => F): Result<T, F>;
273
+ <E, F>(fn: (error: E) => F): <T>(result: Result<T, E>) => Result<T, F>;
274
+ };
190
275
  /**
191
276
  * Transform both tracks at once — `Ok` via `onOk`, `Err` via `onErr`.
192
277
  * Equivalent to `mapErr(onErr)(map(onOk)(result))` but in one pass.
@@ -196,8 +281,10 @@ declare function mapErr<E, F>(fn: (error: E) => F): <T>(result: Result<T, E>) =>
196
281
  * bimap(parsed, (cfg) => cfg.name, (e) => ({ kind: "input", cause: e }));
197
282
  * ```
198
283
  */
199
- declare function bimap<T, U, E, F>(result: Result<T, E>, onOk: (value: T) => U, onErr: (error: E) => F): Result<U, F>;
200
- declare function bimap<T, U, E, F>(onOk: (value: T) => U, onErr: (error: E) => F): (result: Result<T, E>) => Result<U, F>;
284
+ declare const bimap: {
285
+ <T, U, E, F>(result: Result<T, E>, onOk: (value: T) => U, onErr: (error: E) => F): Result<U, F>;
286
+ <T, U, E, F>(onOk: (value: T) => U, onErr: (error: E) => F): (result: Result<T, E>) => Result<U, F>;
287
+ };
201
288
  /**
202
289
  * Canonical bind (Fantasy Land `chain`). Chains a Result-returning step,
203
290
  * widening the error union to `E | F`. Short-circuits on `Err`.
@@ -210,8 +297,10 @@ declare function bimap<T, U, E, F>(onOk: (value: T) => U, onErr: (error: E) => F
210
297
  * // Result<Data, ParseError | { kind: "missing_id" }>
211
298
  * ```
212
299
  */
213
- declare function flatMap<T, U, E, F>(result: Result<T, E>, fn: (value: T) => Result<U, F>): Result<U, E | F>;
214
- declare function flatMap<T, U, F>(fn: (value: T) => Result<U, F>): <E>(result: Result<T, E>) => Result<U, E | F>;
300
+ declare const flatMap: {
301
+ <T, U, E, F>(result: Result<T, E>, fn: (value: T) => Result<U, F>): Result<U, E | F>;
302
+ <T, U, F>(fn: (value: T) => Result<U, F>): <E>(result: Result<T, E>) => Result<U, E | F>;
303
+ };
215
304
  /**
216
305
  * Error-track bind — runs `fn` only when the result is `Err`, allowing
217
306
  * a failed workflow to recover to `Ok` or remap the failure. Mirror of
@@ -224,8 +313,10 @@ declare function flatMap<T, U, F>(fn: (value: T) => Result<U, F>): <E>(result: R
224
313
  * );
225
314
  * ```
226
315
  */
227
- declare function recover<T, E, F>(result: Result<T, E>, fn: (error: E) => Result<T, F>): Result<T, F>;
228
- declare function recover<T, E, F>(fn: (error: E) => Result<T, F>): (result: Result<T, E>) => Result<T, F>;
316
+ declare const recover: {
317
+ <T, E, F>(result: Result<T, E>, fn: (error: E) => Result<T, F>): Result<T, F>;
318
+ <T, E, F>(fn: (error: E) => Result<T, F>): (result: Result<T, E>) => Result<T, F>;
319
+ };
229
320
  /**
230
321
  * Observe the `Ok` value for side effects (logging, metrics) without
231
322
  * changing the carried value. Passes `Err` through untouched.
@@ -239,8 +330,10 @@ declare function recover<T, E, F>(fn: (error: E) => Result<T, F>): (result: Resu
239
330
  * );
240
331
  * ```
241
332
  */
242
- declare function tap<T, E>(result: Result<T, E>, fn: (value: T) => void): Result<T, E>;
243
- declare function tap<T>(fn: (value: T) => void): <E>(result: Result<T, E>) => Result<T, E>;
333
+ declare const tap: {
334
+ <T, E>(result: Result<T, E>, fn: (value: T) => void): Result<T, E>;
335
+ <T>(fn: (value: T) => void): <E>(result: Result<T, E>) => Result<T, E>;
336
+ };
244
337
  /**
245
338
  * Observe the `Err` value for side effects (logging, metrics) without
246
339
  * changing the carried error. Passes `Ok` through untouched.
@@ -253,8 +346,10 @@ declare function tap<T>(fn: (value: T) => void): <E>(result: Result<T, E>) => Re
253
346
  * );
254
347
  * ```
255
348
  */
256
- declare function tapErr<T, E>(result: Result<T, E>, fn: (error: E) => void): Result<T, E>;
257
- declare function tapErr<E>(fn: (error: E) => void): <T>(result: Result<T, E>) => Result<T, E>;
349
+ declare const tapErr: {
350
+ <T, E>(result: Result<T, E>, fn: (error: E) => void): Result<T, E>;
351
+ <E>(fn: (error: E) => void): <T>(result: Result<T, E>) => Result<T, E>;
352
+ };
258
353
  /**
259
354
  * Terminal collapse — fold both tracks into a single value. Dual-form:
260
355
  * 3-args data-first, 2-args curried for {@link pipe}. Returns whatever
@@ -269,8 +364,10 @@ declare function tapErr<E>(fn: (error: E) => void): <T>(result: Result<T, E>) =>
269
364
  * const html = match(parsed, (cfg) => render(cfg), (e) => renderError(e));
270
365
  * ```
271
366
  */
272
- declare function match<T, E, U>(result: Result<T, E>, onOk: (value: T) => U, onErr: (error: E) => U): U;
273
- declare function match<T, E, U>(onOk: (value: T) => U, onErr: (error: E) => U): (result: Result<T, E>) => U;
367
+ declare const match: {
368
+ <T, E, U>(result: Result<T, E>, onOk: (value: T) => U, onErr: (error: E) => U): U;
369
+ <T, E, U>(onOk: (value: T) => U, onErr: (error: E) => U): (result: Result<T, E>) => U;
370
+ };
274
371
  /**
275
372
  * Returns the `Ok` value, or `defaultValue` when the result is `Err`.
276
373
  *
@@ -279,7 +376,10 @@ declare function match<T, E, U>(onOk: (value: T) => U, onErr: (error: E) => U):
279
376
  * unwrapOr(parsedSetting, "default-value");
280
377
  * ```
281
378
  */
282
- declare const unwrapOr: <T, E>(result: Result<T, E>, defaultValue: T) => T;
379
+ declare const unwrapOr: {
380
+ <T, E>(result: Result<T, E>, defaultValue: T): T;
381
+ <T>(defaultValue: T): <E>(result: Result<T, E>) => T;
382
+ };
283
383
  /**
284
384
  * Test/assert helper — returns the `Ok` value, or **throws the original `Err`
285
385
  * value** when called on an `Err`. This is the assertion tier (RFC 0001 §4):
@@ -298,6 +398,8 @@ declare const unwrapOr: <T, E>(result: Result<T, E>, defaultValue: T) => T;
298
398
  * ```
299
399
  */
300
400
  declare function unwrapOk<T, E>(result: Result<T, E>): T;
401
+ /** Alias of {@link unwrapOk} for syntax cohesion with `@onrails/maybe`. */
402
+ declare const unwrap: typeof unwrapOk;
301
403
  /**
302
404
  * Test/assert helper — returns the `Err` value, or **throws a `TypeError`**
303
405
  * when called on an `Ok`. Mirror of {@link unwrapOk} on the error track and
@@ -364,4 +466,4 @@ declare function pipe<A, B, C, D, E, F, G>(value: A, ab: (a: A) => B, bc: (b: B)
364
466
  declare function pipe<A, B, C, D, E, F, G, H>(value: A, ab: (a: A) => B, bc: (b: B) => C, cd: (c: C) => D, de: (d: D) => E, ef: (e: E) => F, fg: (f: F) => G, gh: (g: G) => H): H;
365
467
  declare function pipe<A, B, C, D, E, F, G, H, I>(value: A, ab: (a: A) => B, bc: (b: B) => C, cd: (c: C) => D, de: (d: D) => E, ef: (e: E) => F, fg: (f: F) => G, gh: (g: G) => H, hi: (h: H) => I): I;
366
468
 
367
- export { Err, Ok, Result, ResultAsync, bimap, combine, combineTuple, err, errAsync, flatMap, fromPromise, fromSafePromise, isErr, isOk, map, mapErr, match, ok, okAsync, parallelTupleAsync, pipe, recover, tap, tapErr, tryAsync, trySync, unwrapErr, unwrapOk, unwrapOr };
469
+ export { Err, InferErr, InferOk, Ok, Result, ResultAsync, UnexpectedError, asyncAfter, bimap, combine, combineTuple, err, errAsync, flatMap, fromAsync, fromPromise, fromResult, fromSafePromise, isErr, isOk, map, mapErr, match, ok, okAsync, pipe, recover, tap, tapErr, tryAsync, trySync, unwrap, unwrapErr, unwrapOk, unwrapOr, validateAll, validateTuple };
package/dist/index.js CHANGED
@@ -1,3 +1,10 @@
1
+ // src/internal/dual.ts
2
+ var dual = (arity, body) => {
3
+ const run = body;
4
+ const dispatcher = (...args) => args.length >= arity ? run(...args) : (self) => run(self, ...args);
5
+ return dispatcher;
6
+ };
7
+
1
8
  // src/result.ts
2
9
  var ok = (value) => ({
3
10
  _tag: "Ok",
@@ -9,65 +16,47 @@ var err = (error) => ({
9
16
  });
10
17
  var isOk = (result) => result._tag === "Ok";
11
18
  var isErr = (result) => result._tag === "Err";
12
- var mapImpl = (result, fn) => isOk(result) ? ok(fn(result.value)) : err(result.error);
13
- function map(...args) {
14
- if (args.length === 2) return mapImpl(args[0], args[1]);
15
- const fn = args[0];
16
- return (result) => mapImpl(result, fn);
17
- }
18
- var mapErrImpl = (result, fn) => isErr(result) ? err(fn(result.error)) : ok(result.value);
19
- function mapErr(...args) {
20
- if (args.length === 2) return mapErrImpl(args[0], args[1]);
21
- const fn = args[0];
22
- return (result) => mapErrImpl(result, fn);
23
- }
24
- var bimapImpl = (result, onOk, onErr) => isOk(result) ? ok(onOk(result.value)) : err(onErr(result.error));
25
- function bimap(...args) {
26
- if (args.length === 3) return bimapImpl(args[0], args[1], args[2]);
27
- const [onOk, onErr] = args;
28
- return (result) => bimapImpl(result, onOk, onErr);
29
- }
30
- var flatMapImpl = (result, fn) => isOk(result) ? fn(result.value) : err(result.error);
31
- function flatMap(...args) {
32
- if (args.length === 2) return flatMapImpl(args[0], args[1]);
33
- const fn = args[0];
34
- return (result) => flatMapImpl(result, fn);
35
- }
36
- var recoverImpl = (result, fn) => isErr(result) ? fn(result.error) : ok(result.value);
37
- function recover(...args) {
38
- if (args.length === 2) return recoverImpl(args[0], args[1]);
39
- const fn = args[0];
40
- return (result) => recoverImpl(result, fn);
41
- }
42
- var tapImpl = (result, fn) => {
19
+ var map = dual(
20
+ 2,
21
+ (result, fn) => isOk(result) ? ok(fn(result.value)) : err(result.error)
22
+ );
23
+ var mapErr = dual(
24
+ 2,
25
+ (result, fn) => isErr(result) ? err(fn(result.error)) : ok(result.value)
26
+ );
27
+ var bimap = dual(
28
+ 3,
29
+ (result, onOk, onErr) => isOk(result) ? ok(onOk(result.value)) : err(onErr(result.error))
30
+ );
31
+ var flatMap = dual(
32
+ 2,
33
+ (result, fn) => isOk(result) ? fn(result.value) : err(result.error)
34
+ );
35
+ var recover = dual(
36
+ 2,
37
+ (result, fn) => isErr(result) ? fn(result.error) : ok(result.value)
38
+ );
39
+ var tap = dual(2, (result, fn) => {
43
40
  if (isOk(result)) fn(result.value);
44
41
  return result;
45
- };
46
- function tap(...args) {
47
- if (args.length === 2) return tapImpl(args[0], args[1]);
48
- const fn = args[0];
49
- return (result) => tapImpl(result, fn);
50
- }
51
- var tapErrImpl = (result, fn) => {
42
+ });
43
+ var tapErr = dual(2, (result, fn) => {
52
44
  if (isErr(result)) fn(result.error);
53
45
  return result;
54
- };
55
- function tapErr(...args) {
56
- if (args.length === 2) return tapErrImpl(args[0], args[1]);
57
- const fn = args[0];
58
- return (result) => tapErrImpl(result, fn);
59
- }
60
- var matchImpl = (result, onOk, onErr) => isOk(result) ? onOk(result.value) : onErr(result.error);
61
- function match(...args) {
62
- if (args.length === 3) return matchImpl(args[0], args[1], args[2]);
63
- const [onOk, onErr] = args;
64
- return (result) => matchImpl(result, onOk, onErr);
65
- }
66
- var unwrapOr = (result, defaultValue) => isOk(result) ? result.value : defaultValue;
46
+ });
47
+ var match = dual(
48
+ 3,
49
+ (result, onOk, onErr) => isOk(result) ? onOk(result.value) : onErr(result.error)
50
+ );
51
+ var unwrapOr = dual(
52
+ 2,
53
+ (result, defaultValue) => isOk(result) ? result.value : defaultValue
54
+ );
67
55
  function unwrapOk(result) {
68
56
  if (isErr(result)) throw result.error;
69
57
  return result.value;
70
58
  }
59
+ var unwrap = unwrapOk;
71
60
  function unwrapErr(result) {
72
61
  if (isOk(result)) throw new TypeError("unwrapErr called on Ok");
73
62
  return result.error;
@@ -100,17 +89,6 @@ var UnexpectedError = class extends Error {
100
89
  };
101
90
 
102
91
  // src/async.ts
103
- var liftPromiseResult = async (promise, onDefect) => {
104
- try {
105
- const result = await promise;
106
- if (isErr(result)) {
107
- return result;
108
- }
109
- return isResultLike(result.value) ? result.value : result;
110
- } catch (error) {
111
- return err(onDefect(error));
112
- }
113
- };
114
92
  var sequenceSettled = (settled) => {
115
93
  const values = [];
116
94
  for (const result of settled) {
@@ -205,16 +183,23 @@ var ResultAsync = class _ResultAsync {
205
183
  }
206
184
  /**
207
185
  * Lifts an existing `Promise<Result<T, E>>` (e.g. from interop code) into a
208
- * {@link ResultAsync}, auto-flattening one level of double-wrapping. A thrown
209
- * defect is routed through `onDefect`, defaulting to {@link UnexpectedError}.
186
+ * {@link ResultAsync}. A thrown defect is routed through `onDefect`,
187
+ * defaulting to {@link UnexpectedError}. The `Ok` value passes through
188
+ * verbatim — even when it happens to be Result-shaped.
210
189
  *
211
190
  * @param promise - a promise that already yields a `Result`
212
191
  * @param onDefect - maps an unexpected throw to the `Err` channel
213
- * @see {@link fromAsync} from `@onrails/result/interop`
192
+ * @see {@link fromAsync} from `@onrails/result`
214
193
  */
215
194
  static fromResultPromise(promise, onDefect) {
216
195
  const mapDefect = onDefect ?? ((error) => new UnexpectedError("Unexpected async defect", error));
217
- return new _ResultAsync(() => liftPromiseResult(promise, mapDefect));
196
+ return new _ResultAsync(async () => {
197
+ try {
198
+ return await promise;
199
+ } catch (error) {
200
+ return err(mapDefect(error));
201
+ }
202
+ });
218
203
  }
219
204
  /**
220
205
  * Combines a homogeneous array of async results into one. Resolves
@@ -298,7 +283,7 @@ var ResultAsync = class _ResultAsync {
298
283
  *
299
284
  * @example
300
285
  * ```ts
301
- * load(id).mapErr((e): AppError => ({ kind: "load", cause: e }));
286
+ * load(id).mapErr((e): AppError => ({ kind: "load", cause: e })); // ResultAsync<T, AppError>
302
287
  * ```
303
288
  */
304
289
  mapErr(fn) {
@@ -322,18 +307,12 @@ var ResultAsync = class _ResultAsync {
322
307
  return first;
323
308
  }
324
309
  const next = fn(first.value);
325
- if (next instanceof _ResultAsync) {
326
- return next.resolve();
327
- }
328
- if (isResultLike(next)) {
329
- return next;
330
- }
331
- return isCompatLike(next) ? next.inner : next;
310
+ return next instanceof _ResultAsync ? next.resolve() : next;
332
311
  });
333
312
  }
334
313
  /**
335
314
  * neverthrow-compat alias of {@link flatMap}. Kept as the documented compat
336
- * tier; prefer `flatMap` in new code.
315
+ * tier; prefer {@link flatMap} in new code.
337
316
  *
338
317
  * @example
339
318
  * ```ts
@@ -350,7 +329,7 @@ var ResultAsync = class _ResultAsync {
350
329
  *
351
330
  * @example
352
331
  * ```ts
353
- * loadFromCache(id).recover(() => loadFromOrigin(id));
332
+ * loadFromCache(id).recover(() => loadFromOrigin(id)); // ResultAsync<T, F>
354
333
  * ```
355
334
  */
356
335
  recover(fn) {
@@ -365,7 +344,7 @@ var ResultAsync = class _ResultAsync {
365
344
  }
366
345
  /**
367
346
  * neverthrow-compat alias of {@link recover}. Kept as the documented compat
368
- * tier; prefer `recover` in new code.
347
+ * tier; prefer {@link recover} in new code.
369
348
  *
370
349
  * @example
371
350
  * ```ts
@@ -473,19 +452,18 @@ var ResultAsync = class _ResultAsync {
473
452
  );
474
453
  }
475
454
  };
476
- function isResultLike(v) {
477
- return typeof v === "object" && v !== null && "_tag" in v && (v._tag === "Ok" || v._tag === "Err");
478
- }
479
- function isCompatLike(v) {
480
- return typeof v === "object" && v !== null && "inner" in v && isResultLike(v.inner);
481
- }
482
455
 
483
456
  // src/async-lift.ts
484
457
  var okAsync = ResultAsync.ok;
485
458
  var errAsync = ResultAsync.err;
486
459
  var fromPromise = ResultAsync.fromPromise;
487
460
  var fromSafePromise = ResultAsync.fromSafePromise;
488
- var parallelTupleAsync = ResultAsync.combineTupleParallel;
461
+ var fromResult = ResultAsync.fromResult;
462
+ var asyncAfter = dual(
463
+ 2,
464
+ (result, fn) => fromResult(result).flatMap(fn)
465
+ );
466
+ var fromAsync = (fn, onDefect) => (...args) => ResultAsync.fromResultPromise(fn(...args), onDefect);
489
467
  var toError = (error) => error instanceof Error ? error : new Error(String(error));
490
468
  function tryAsync(promise, onReject) {
491
469
  return onReject ? ResultAsync.fromPromise(promise, onReject) : ResultAsync.fromPromise(promise, toError);
@@ -504,17 +482,22 @@ var combineTuple = (results) => (
504
482
  // Runtime identical to combine; the cast restores per-index tuple types.
505
483
  combine(results)
506
484
  );
507
-
508
- // src/interop.ts
509
- var fromResult = (result) => ResultAsync.fromResult(result);
510
- function asyncAfter(resultOrFn, fn) {
511
- if (fn === void 0) {
512
- const f = resultOrFn;
513
- return (result) => fromResult(result).flatMap(f);
485
+ function validateAll(results, combineErrors) {
486
+ const values = [];
487
+ const errors = [];
488
+ for (const result of results) {
489
+ if (isErr(result)) {
490
+ errors.push(result.error);
491
+ } else {
492
+ values.push(result.value);
493
+ }
514
494
  }
515
- return fromResult(resultOrFn).flatMap(fn);
495
+ if (errors.length === 0) return ok(values);
496
+ return err(combineErrors ? errors.reduce(combineErrors) : errors);
497
+ }
498
+ function validateTuple(results, combineErrors) {
499
+ return combineErrors ? validateAll(results, combineErrors) : validateAll(results);
516
500
  }
517
- var fromAsync = (fn, onDefect) => (...args) => ResultAsync.fromResultPromise(fn(...args), onDefect);
518
501
 
519
502
  // src/pipe.ts
520
503
  function flow(first, ...rest) {
@@ -553,6 +536,6 @@ var tryGen = (fn) => {
553
536
  }
554
537
  };
555
538
 
556
- export { $, ResultAsync, asyncAfter, bimap, combine, combineTuple, err, errAsync, flatMap, flow, fromAsync, fromPromise, fromResult, fromSafePromise, isErr, isOk, map, mapErr, match, ok, okAsync, parallelTupleAsync, pipe, recover, tap, tapErr, tryAsync, tryGen, trySync, unwrapErr, unwrapOk, unwrapOr, yieldResult };
539
+ export { $, ResultAsync, UnexpectedError, asyncAfter, bimap, combine, combineTuple, err, errAsync, flatMap, flow, fromAsync, fromPromise, fromResult, fromSafePromise, isErr, isOk, map, mapErr, match, ok, okAsync, pipe, recover, tap, tapErr, tryAsync, tryGen, trySync, unwrap, unwrapErr, unwrapOk, unwrapOr, validateAll, validateTuple, yieldResult };
557
540
  //# sourceMappingURL=index.js.map
558
541
  //# sourceMappingURL=index.js.map