@konker.dev/neverthrow-r 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Provision: supply the accumulated requirements `R` to a `ResultR` /
3
+ * `ResultAsyncR` and exit back into a plain `neverthrow` `Result` /
4
+ * `ResultAsync`.
5
+ *
6
+ * @remarks
7
+ * The rest of the package builds up an `R` channel through intersection;
8
+ * this module is where you discharge it. Two flavours:
9
+ *
10
+ * - {@link provide} — supply the full environment in one call. Returns the
11
+ * underlying `Result` (or `ResultAsync`).
12
+ * - {@link provideSome} — supply a *subset* of the requirements. Returns a
13
+ * new `ResultR` over the keys that remain unsatisfied.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { asks } from '@konker.dev/neverthrow-r/constructors';
18
+ * import { provide } from '@konker.dev/neverthrow-r/provide';
19
+ *
20
+ * type Deps = { factor: number };
21
+ *
22
+ * const program = asks((r: Deps) => r.factor * 2);
23
+ *
24
+ * provide(program, { factor: 10 }); // Ok(20)
25
+ * ```
26
+ *
27
+ * @module
28
+ */
29
+ export function provide(rr, deps) {
30
+ return rr(deps);
31
+ }
32
+ export function provideSome(rr, partial) {
33
+ return (rest) => rr({ ...partial, ...rest });
34
+ }
35
+ //# sourceMappingURL=provide.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provide.js","sourceRoot":"","sources":["../src/provide.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AA8CH,MAAM,UAAU,OAAO,CACrB,EAA4C,EAC5C,IAAO;IAEP,OAAO,EAAE,CAAC,IAAI,CAAC,CAAC;AAClB,CAAC;AAoDD,MAAM,UAAU,WAAW,CACzB,EAA4C,EAC5C,OAAU;IAEV,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,GAAG,OAAO,EAAE,GAAI,IAAe,EAAkB,CAAC,CAAC;AAC3E,CAAC"}
package/dist/sync.d.ts ADDED
@@ -0,0 +1,317 @@
1
+ /**
2
+ * Sync combinators over `ResultR`. This is the canonical surface — the async
3
+ * module ({@link mapAsync}, {@link andThenAsync}, …) mirrors it 1:1 over
4
+ * `ResultAsyncR`.
5
+ *
6
+ * @remarks
7
+ * Every combinator is curried so it composes cleanly under {@link pipe}: the
8
+ * transforming function comes first, then the operand `ResultR`. Each one
9
+ * delegates to the corresponding neverthrow `Result` method, threading the
10
+ * environment `r` through and **intersecting** requirements across composed
11
+ * steps (`R1 & R2`).
12
+ *
13
+ * Reach for this module when the whole chain is synchronous. The moment a
14
+ * step needs to await, switch into {@link bridges} (sync→async one-shot) or
15
+ * the {@link async} module (already-async chain).
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { okR } from '@konker.dev/neverthrow-r/constructors';
20
+ * import { andThen, map } from '@konker.dev/neverthrow-r/sync';
21
+ * import { pipe } from '@konker.dev/neverthrow-r/pipe';
22
+ *
23
+ * const program = pipe(
24
+ * okR<number>(2),
25
+ * map((n) => n + 1),
26
+ * andThen((n) => okR<string>(`got ${n}`)),
27
+ * );
28
+ *
29
+ * program(undefined); // Ok('got 3')
30
+ * ```
31
+ *
32
+ * @module
33
+ */
34
+ import type { ResultR } from './types.js';
35
+ /**
36
+ * Transforms the success value of a `ResultR` with a pure function.
37
+ *
38
+ * @remarks
39
+ * Threads `R` and `E` through unchanged; only `T` changes. The wrapped
40
+ * function `f` is not called when the underlying `Result` is an `Err`.
41
+ *
42
+ * To transform the error channel instead, see {@link mapErr}. To compose
43
+ * with another `ResultR`-returning step, use {@link andThen}. For side
44
+ * effects on the success value without changing it, use {@link andTee}.
45
+ *
46
+ * @typeParam A - The input success type.
47
+ * @typeParam B - The output success type.
48
+ *
49
+ * @param f - Pure transformation from `A` to `B`.
50
+ * @returns A function taking a `ResultR<R, A, E>` and returning a
51
+ * `ResultR<R, B, E>`.
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * import { okR } from '@konker.dev/neverthrow-r/constructors';
56
+ * import { map } from '@konker.dev/neverthrow-r/sync';
57
+ * import { pipe } from '@konker.dev/neverthrow-r/pipe';
58
+ *
59
+ * const doubled = pipe(okR<number>(2), map((n) => n * 2));
60
+ * doubled(undefined); // Ok(4)
61
+ * ```
62
+ *
63
+ * @see {@link mapErr}
64
+ * @see {@link andThen}
65
+ * @see {@link mapAsync}
66
+ */
67
+ export declare const map: <A, B>(f: (a: A) => B) => <R, E>(rr: ResultR<R, A, E>) => ResultR<R, B, E>;
68
+ /**
69
+ * Transforms the error value of a `ResultR` with a pure function.
70
+ *
71
+ * @remarks
72
+ * Symmetric to {@link map}, but operates on the `E` channel. `T` and `R` pass
73
+ * through unchanged. Useful for narrowing a wide error type to a domain-
74
+ * specific one, or for annotating where in a pipeline an error occurred.
75
+ *
76
+ * @typeParam E - The input error type.
77
+ * @typeParam F - The output error type.
78
+ *
79
+ * @param f - Pure transformation from `E` to `F`.
80
+ *
81
+ * @example
82
+ * ```ts
83
+ * import { errR } from '@konker.dev/neverthrow-r/constructors';
84
+ * import { mapErr } from '@konker.dev/neverthrow-r/sync';
85
+ * import { pipe } from '@konker.dev/neverthrow-r/pipe';
86
+ *
87
+ * const annotated = pipe(
88
+ * errR<string>('not found'),
89
+ * mapErr((msg) => ({ tag: 'lookup', message: msg })),
90
+ * );
91
+ *
92
+ * annotated(undefined); // Err({ tag: 'lookup', message: 'not found' })
93
+ * ```
94
+ *
95
+ * @see {@link map}
96
+ * @see {@link mapErrAsync}
97
+ */
98
+ export declare const mapErr: <E, F>(f: (e: E) => F) => <R, T>(rr: ResultR<R, T, E>) => ResultR<R, T, F>;
99
+ /**
100
+ * Chains another `ResultR`-returning step onto a successful result.
101
+ *
102
+ * @remarks
103
+ * This is the workhorse for sequencing computations that each may fail. The
104
+ * continuation `f` receives the previous step's success value and returns a
105
+ * new `ResultR` whose requirements `R2` are **intersected** into the chain
106
+ * (`R1 & R2`) and whose error type `E2` is **unioned** with the chain's
107
+ * (`E1 | E2`).
108
+ *
109
+ * If the previous step is an `Err`, `f` is not called and the error
110
+ * propagates.
111
+ *
112
+ * For multi-step chains that accumulate intermediate values into a named
113
+ * scope, prefer the do-notation helpers in {@link do}.
114
+ *
115
+ * @typeParam A - The previous step's success type.
116
+ * @typeParam R2 - The continuation's requirements.
117
+ * @typeParam B - The continuation's success type.
118
+ * @typeParam E2 - The continuation's error type.
119
+ *
120
+ * @param f - Function from the previous success value to a `ResultR`.
121
+ * @returns A function taking a `ResultR<R1, A, E1>` and returning a
122
+ * `ResultR<R1 & R2, B, E1 | E2>`.
123
+ *
124
+ * @example
125
+ * ```ts
126
+ * import { asks, okR } from '@konker.dev/neverthrow-r/constructors';
127
+ * import { andThen } from '@konker.dev/neverthrow-r/sync';
128
+ * import { pipe } from '@konker.dev/neverthrow-r/pipe';
129
+ *
130
+ * type Config = { factor: number };
131
+ *
132
+ * const program = pipe(
133
+ * okR<number>(2),
134
+ * andThen((n) => asks((r: Config) => n * r.factor)),
135
+ * );
136
+ *
137
+ * program({ factor: 10 }); // Ok(20)
138
+ * ```
139
+ *
140
+ * @see {@link orElse} for the error-channel mirror.
141
+ * @see {@link andThrough} for a chain that returns the previous value.
142
+ * @see {@link andThenAsync}
143
+ * @see {@link bindR}
144
+ */
145
+ export declare const andThen: <A, R2, B, E2>(f: (a: A) => ResultR<R2, B, E2>) => <R1, E1>(rr: ResultR<R1, A, E1>) => ResultR<R1 & R2, B, E1 | E2>;
146
+ /**
147
+ * Recovers from an `Err` by running another `ResultR`-returning step.
148
+ *
149
+ * @remarks
150
+ * The error-channel mirror of {@link andThen}: `f` is called only when the
151
+ * previous step is an `Err`. The recovery's requirements `R2` are intersected
152
+ * (`R1 & R2`) and its success type `U` is unioned with the original `T`
153
+ * (`T | U`). The recovery itself can still fail, with its own error type `F`.
154
+ *
155
+ * @typeParam E - The previous step's error type.
156
+ * @typeParam R2 - The recovery's requirements.
157
+ * @typeParam U - The recovery's success type.
158
+ * @typeParam F - The recovery's error type.
159
+ *
160
+ * @param f - Function from the previous error to a recovery `ResultR`.
161
+ *
162
+ * @example
163
+ * ```ts
164
+ * import { errR, okR } from '@konker.dev/neverthrow-r/constructors';
165
+ * import { orElse } from '@konker.dev/neverthrow-r/sync';
166
+ * import { pipe } from '@konker.dev/neverthrow-r/pipe';
167
+ *
168
+ * const withFallback = pipe(
169
+ * errR<string, number>('boom'),
170
+ * orElse(() => okR<number>(0)),
171
+ * );
172
+ *
173
+ * withFallback(undefined); // Ok(0)
174
+ * ```
175
+ *
176
+ * @see {@link andThen}
177
+ * @see {@link orElseAsync}
178
+ */
179
+ export declare const orElse: <E, R2, U, F>(f: (e: E) => ResultR<R2, U, F>) => <R1, T>(rr: ResultR<R1, T, E>) => ResultR<R1 & R2, T | U, F>;
180
+ /**
181
+ * Eliminates a `ResultR` into a single value by handling both branches.
182
+ *
183
+ * @remarks
184
+ * Unlike the other combinators, `match` does not return a `ResultR` — it
185
+ * collapses the chain into a plain value. Both branch functions can return
186
+ * the same type, or different types that get unioned.
187
+ *
188
+ * The result is still a function of the environment `R`, so calling it
189
+ * requires providing `R` directly (or pulling it via {@link provide}).
190
+ *
191
+ * @typeParam T - The success type.
192
+ * @typeParam E - The error type.
193
+ * @typeParam A - The result type of the `Ok` branch.
194
+ * @typeParam B - The result type of the `Err` branch (defaults to `A`).
195
+ *
196
+ * @param okFn - Handler for the success branch.
197
+ * @param errFn - Handler for the error branch.
198
+ *
199
+ * @example
200
+ * ```ts
201
+ * import { okR } from '@konker.dev/neverthrow-r/constructors';
202
+ * import { match } from '@konker.dev/neverthrow-r/sync';
203
+ * import { pipe } from '@konker.dev/neverthrow-r/pipe';
204
+ *
205
+ * const rendered = pipe(
206
+ * okR<number>(42),
207
+ * match(
208
+ * (n) => `got ${n}`,
209
+ * (e: never) => `error: ${String(e)}`,
210
+ * ),
211
+ * );
212
+ *
213
+ * rendered(undefined); // 'got 42'
214
+ * ```
215
+ *
216
+ * @see {@link matchAsync}
217
+ */
218
+ export declare const match: <T, E, A, B = A>(okFn: (t: T) => A, errFn: (e: E) => B) => <R>(rr: ResultR<R, T, E>) => (r: R) => A | B;
219
+ /**
220
+ * Runs a side effect with the success value, propagating the value unchanged.
221
+ *
222
+ * @remarks
223
+ * Pass-through combinator for logging, telemetry, or any other observe-only
224
+ * step. The callback's return value is discarded; the chain continues with
225
+ * the original `T`. The callback is not invoked when the chain is in `Err`.
226
+ *
227
+ * @typeParam T - The success type (preserved through the step).
228
+ *
229
+ * @param f - Side-effecting callback receiving the success value.
230
+ *
231
+ * @example
232
+ * ```ts
233
+ * import { okR } from '@konker.dev/neverthrow-r/constructors';
234
+ * import { andTee } from '@konker.dev/neverthrow-r/sync';
235
+ * import { pipe } from '@konker.dev/neverthrow-r/pipe';
236
+ *
237
+ * const traced = pipe(
238
+ * okR<number>(2),
239
+ * andTee((n) => console.log('saw', n)),
240
+ * );
241
+ *
242
+ * traced(undefined); // logs 'saw 2', returns Ok(2)
243
+ * ```
244
+ *
245
+ * @see {@link orTee}
246
+ * @see {@link andThrough} for a tee that can itself fail.
247
+ * @see {@link andTeeAsync}
248
+ */
249
+ export declare const andTee: <T>(f: (t: T) => unknown) => <R, E>(rr: ResultR<R, T, E>) => ResultR<R, T, E>;
250
+ /**
251
+ * Runs a side effect with the error value, propagating the error unchanged.
252
+ *
253
+ * @remarks
254
+ * Error-channel mirror of {@link andTee}. Useful for logging failures without
255
+ * altering the error type or recovering from it.
256
+ *
257
+ * @typeParam E - The error type (preserved through the step).
258
+ *
259
+ * @param f - Side-effecting callback receiving the error value.
260
+ *
261
+ * @example
262
+ * ```ts
263
+ * import { errR } from '@konker.dev/neverthrow-r/constructors';
264
+ * import { orTee } from '@konker.dev/neverthrow-r/sync';
265
+ * import { pipe } from '@konker.dev/neverthrow-r/pipe';
266
+ *
267
+ * const logged = pipe(
268
+ * errR<string>('boom'),
269
+ * orTee((e) => console.error('failed:', e)),
270
+ * );
271
+ *
272
+ * logged(undefined); // logs 'failed: boom', returns Err('boom')
273
+ * ```
274
+ *
275
+ * @see {@link andTee}
276
+ * @see {@link orTeeAsync}
277
+ */
278
+ export declare const orTee: <E>(f: (e: E) => unknown) => <R, T>(rr: ResultR<R, T, E>) => ResultR<R, T, E>;
279
+ /**
280
+ * Runs a fallible step for its side effect, then propagates the original
281
+ * success value if the step succeeds.
282
+ *
283
+ * @remarks
284
+ * Like {@link andTee}, but the tee step is itself a `ResultR` that can fail.
285
+ * If the tee step returns `Err`, that error replaces the chain's value; if it
286
+ * returns `Ok`, the chain continues with the *original* success value (the
287
+ * tee's success value is discarded).
288
+ *
289
+ * Common shape: validate a value via a fallible check without losing the
290
+ * value itself.
291
+ *
292
+ * @typeParam T - The success type (preserved through the step on success).
293
+ * @typeParam R2 - The tee's requirements (intersected into the chain).
294
+ * @typeParam F - The tee's error type (unioned with the chain's).
295
+ *
296
+ * @param f - Function from the success value to a `ResultR` whose value is
297
+ * ignored on success.
298
+ *
299
+ * @example
300
+ * ```ts
301
+ * import { errR, okR } from '@konker.dev/neverthrow-r/constructors';
302
+ * import { andThrough } from '@konker.dev/neverthrow-r/sync';
303
+ * import { pipe } from '@konker.dev/neverthrow-r/pipe';
304
+ *
305
+ * const validated = pipe(
306
+ * okR<number>(2),
307
+ * andThrough((n) => (n > 0 ? okR<unknown>(undefined) : errR<string>('non-positive'))),
308
+ * );
309
+ *
310
+ * validated(undefined); // Ok(2)
311
+ * ```
312
+ *
313
+ * @see {@link andTee} for the infallible version.
314
+ * @see {@link andThroughAsync}
315
+ */
316
+ export declare const andThrough: <T, R2, F>(f: (t: T) => ResultR<R2, unknown, F>) => <R1, E>(rr: ResultR<R1, T, E>) => ResultR<R1 & R2, T, E | F>;
317
+ //# sourceMappingURL=sync.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sync.d.ts","sourceRoot":"","sources":["../src/sync.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MA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AAO,EAAE,CAAC,CAAC,MAC9C,EAAE,EAAE,CAAC,EAAE,IAAI,OAAO,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC,KAAG,OAAO,CAAC,EAAE,GAAG,EAAE,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,CAEvB,CAAC"}
package/dist/sync.js ADDED
@@ -0,0 +1,316 @@
1
+ /**
2
+ * Sync combinators over `ResultR`. This is the canonical surface — the async
3
+ * module ({@link mapAsync}, {@link andThenAsync}, …) mirrors it 1:1 over
4
+ * `ResultAsyncR`.
5
+ *
6
+ * @remarks
7
+ * Every combinator is curried so it composes cleanly under {@link pipe}: the
8
+ * transforming function comes first, then the operand `ResultR`. Each one
9
+ * delegates to the corresponding neverthrow `Result` method, threading the
10
+ * environment `r` through and **intersecting** requirements across composed
11
+ * steps (`R1 & R2`).
12
+ *
13
+ * Reach for this module when the whole chain is synchronous. The moment a
14
+ * step needs to await, switch into {@link bridges} (sync→async one-shot) or
15
+ * the {@link async} module (already-async chain).
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { okR } from '@konker.dev/neverthrow-r/constructors';
20
+ * import { andThen, map } from '@konker.dev/neverthrow-r/sync';
21
+ * import { pipe } from '@konker.dev/neverthrow-r/pipe';
22
+ *
23
+ * const program = pipe(
24
+ * okR<number>(2),
25
+ * map((n) => n + 1),
26
+ * andThen((n) => okR<string>(`got ${n}`)),
27
+ * );
28
+ *
29
+ * program(undefined); // Ok('got 3')
30
+ * ```
31
+ *
32
+ * @module
33
+ */
34
+ /**
35
+ * Transforms the success value of a `ResultR` with a pure function.
36
+ *
37
+ * @remarks
38
+ * Threads `R` and `E` through unchanged; only `T` changes. The wrapped
39
+ * function `f` is not called when the underlying `Result` is an `Err`.
40
+ *
41
+ * To transform the error channel instead, see {@link mapErr}. To compose
42
+ * with another `ResultR`-returning step, use {@link andThen}. For side
43
+ * effects on the success value without changing it, use {@link andTee}.
44
+ *
45
+ * @typeParam A - The input success type.
46
+ * @typeParam B - The output success type.
47
+ *
48
+ * @param f - Pure transformation from `A` to `B`.
49
+ * @returns A function taking a `ResultR<R, A, E>` and returning a
50
+ * `ResultR<R, B, E>`.
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * import { okR } from '@konker.dev/neverthrow-r/constructors';
55
+ * import { map } from '@konker.dev/neverthrow-r/sync';
56
+ * import { pipe } from '@konker.dev/neverthrow-r/pipe';
57
+ *
58
+ * const doubled = pipe(okR<number>(2), map((n) => n * 2));
59
+ * doubled(undefined); // Ok(4)
60
+ * ```
61
+ *
62
+ * @see {@link mapErr}
63
+ * @see {@link andThen}
64
+ * @see {@link mapAsync}
65
+ */
66
+ export const map = (f) => (rr) => (r) => rr(r).map(f);
67
+ /**
68
+ * Transforms the error value of a `ResultR` with a pure function.
69
+ *
70
+ * @remarks
71
+ * Symmetric to {@link map}, but operates on the `E` channel. `T` and `R` pass
72
+ * through unchanged. Useful for narrowing a wide error type to a domain-
73
+ * specific one, or for annotating where in a pipeline an error occurred.
74
+ *
75
+ * @typeParam E - The input error type.
76
+ * @typeParam F - The output error type.
77
+ *
78
+ * @param f - Pure transformation from `E` to `F`.
79
+ *
80
+ * @example
81
+ * ```ts
82
+ * import { errR } from '@konker.dev/neverthrow-r/constructors';
83
+ * import { mapErr } from '@konker.dev/neverthrow-r/sync';
84
+ * import { pipe } from '@konker.dev/neverthrow-r/pipe';
85
+ *
86
+ * const annotated = pipe(
87
+ * errR<string>('not found'),
88
+ * mapErr((msg) => ({ tag: 'lookup', message: msg })),
89
+ * );
90
+ *
91
+ * annotated(undefined); // Err({ tag: 'lookup', message: 'not found' })
92
+ * ```
93
+ *
94
+ * @see {@link map}
95
+ * @see {@link mapErrAsync}
96
+ */
97
+ export const mapErr = (f) => (rr) => (r) => rr(r).mapErr(f);
98
+ /**
99
+ * Chains another `ResultR`-returning step onto a successful result.
100
+ *
101
+ * @remarks
102
+ * This is the workhorse for sequencing computations that each may fail. The
103
+ * continuation `f` receives the previous step's success value and returns a
104
+ * new `ResultR` whose requirements `R2` are **intersected** into the chain
105
+ * (`R1 & R2`) and whose error type `E2` is **unioned** with the chain's
106
+ * (`E1 | E2`).
107
+ *
108
+ * If the previous step is an `Err`, `f` is not called and the error
109
+ * propagates.
110
+ *
111
+ * For multi-step chains that accumulate intermediate values into a named
112
+ * scope, prefer the do-notation helpers in {@link do}.
113
+ *
114
+ * @typeParam A - The previous step's success type.
115
+ * @typeParam R2 - The continuation's requirements.
116
+ * @typeParam B - The continuation's success type.
117
+ * @typeParam E2 - The continuation's error type.
118
+ *
119
+ * @param f - Function from the previous success value to a `ResultR`.
120
+ * @returns A function taking a `ResultR<R1, A, E1>` and returning a
121
+ * `ResultR<R1 & R2, B, E1 | E2>`.
122
+ *
123
+ * @example
124
+ * ```ts
125
+ * import { asks, okR } from '@konker.dev/neverthrow-r/constructors';
126
+ * import { andThen } from '@konker.dev/neverthrow-r/sync';
127
+ * import { pipe } from '@konker.dev/neverthrow-r/pipe';
128
+ *
129
+ * type Config = { factor: number };
130
+ *
131
+ * const program = pipe(
132
+ * okR<number>(2),
133
+ * andThen((n) => asks((r: Config) => n * r.factor)),
134
+ * );
135
+ *
136
+ * program({ factor: 10 }); // Ok(20)
137
+ * ```
138
+ *
139
+ * @see {@link orElse} for the error-channel mirror.
140
+ * @see {@link andThrough} for a chain that returns the previous value.
141
+ * @see {@link andThenAsync}
142
+ * @see {@link bindR}
143
+ */
144
+ export const andThen = (f) => (rr) => (r) => rr(r).andThen((a) => f(a)(r));
145
+ /**
146
+ * Recovers from an `Err` by running another `ResultR`-returning step.
147
+ *
148
+ * @remarks
149
+ * The error-channel mirror of {@link andThen}: `f` is called only when the
150
+ * previous step is an `Err`. The recovery's requirements `R2` are intersected
151
+ * (`R1 & R2`) and its success type `U` is unioned with the original `T`
152
+ * (`T | U`). The recovery itself can still fail, with its own error type `F`.
153
+ *
154
+ * @typeParam E - The previous step's error type.
155
+ * @typeParam R2 - The recovery's requirements.
156
+ * @typeParam U - The recovery's success type.
157
+ * @typeParam F - The recovery's error type.
158
+ *
159
+ * @param f - Function from the previous error to a recovery `ResultR`.
160
+ *
161
+ * @example
162
+ * ```ts
163
+ * import { errR, okR } from '@konker.dev/neverthrow-r/constructors';
164
+ * import { orElse } from '@konker.dev/neverthrow-r/sync';
165
+ * import { pipe } from '@konker.dev/neverthrow-r/pipe';
166
+ *
167
+ * const withFallback = pipe(
168
+ * errR<string, number>('boom'),
169
+ * orElse(() => okR<number>(0)),
170
+ * );
171
+ *
172
+ * withFallback(undefined); // Ok(0)
173
+ * ```
174
+ *
175
+ * @see {@link andThen}
176
+ * @see {@link orElseAsync}
177
+ */
178
+ export const orElse = (f) => (rr) => (r) => rr(r).orElse((e) => f(e)(r));
179
+ /**
180
+ * Eliminates a `ResultR` into a single value by handling both branches.
181
+ *
182
+ * @remarks
183
+ * Unlike the other combinators, `match` does not return a `ResultR` — it
184
+ * collapses the chain into a plain value. Both branch functions can return
185
+ * the same type, or different types that get unioned.
186
+ *
187
+ * The result is still a function of the environment `R`, so calling it
188
+ * requires providing `R` directly (or pulling it via {@link provide}).
189
+ *
190
+ * @typeParam T - The success type.
191
+ * @typeParam E - The error type.
192
+ * @typeParam A - The result type of the `Ok` branch.
193
+ * @typeParam B - The result type of the `Err` branch (defaults to `A`).
194
+ *
195
+ * @param okFn - Handler for the success branch.
196
+ * @param errFn - Handler for the error branch.
197
+ *
198
+ * @example
199
+ * ```ts
200
+ * import { okR } from '@konker.dev/neverthrow-r/constructors';
201
+ * import { match } from '@konker.dev/neverthrow-r/sync';
202
+ * import { pipe } from '@konker.dev/neverthrow-r/pipe';
203
+ *
204
+ * const rendered = pipe(
205
+ * okR<number>(42),
206
+ * match(
207
+ * (n) => `got ${n}`,
208
+ * (e: never) => `error: ${String(e)}`,
209
+ * ),
210
+ * );
211
+ *
212
+ * rendered(undefined); // 'got 42'
213
+ * ```
214
+ *
215
+ * @see {@link matchAsync}
216
+ */
217
+ export const match = (okFn, errFn) => (rr) => (r) => rr(r).match(okFn, errFn);
218
+ /**
219
+ * Runs a side effect with the success value, propagating the value unchanged.
220
+ *
221
+ * @remarks
222
+ * Pass-through combinator for logging, telemetry, or any other observe-only
223
+ * step. The callback's return value is discarded; the chain continues with
224
+ * the original `T`. The callback is not invoked when the chain is in `Err`.
225
+ *
226
+ * @typeParam T - The success type (preserved through the step).
227
+ *
228
+ * @param f - Side-effecting callback receiving the success value.
229
+ *
230
+ * @example
231
+ * ```ts
232
+ * import { okR } from '@konker.dev/neverthrow-r/constructors';
233
+ * import { andTee } from '@konker.dev/neverthrow-r/sync';
234
+ * import { pipe } from '@konker.dev/neverthrow-r/pipe';
235
+ *
236
+ * const traced = pipe(
237
+ * okR<number>(2),
238
+ * andTee((n) => console.log('saw', n)),
239
+ * );
240
+ *
241
+ * traced(undefined); // logs 'saw 2', returns Ok(2)
242
+ * ```
243
+ *
244
+ * @see {@link orTee}
245
+ * @see {@link andThrough} for a tee that can itself fail.
246
+ * @see {@link andTeeAsync}
247
+ */
248
+ export const andTee = (f) => (rr) => (r) => rr(r).andTee(f);
249
+ /**
250
+ * Runs a side effect with the error value, propagating the error unchanged.
251
+ *
252
+ * @remarks
253
+ * Error-channel mirror of {@link andTee}. Useful for logging failures without
254
+ * altering the error type or recovering from it.
255
+ *
256
+ * @typeParam E - The error type (preserved through the step).
257
+ *
258
+ * @param f - Side-effecting callback receiving the error value.
259
+ *
260
+ * @example
261
+ * ```ts
262
+ * import { errR } from '@konker.dev/neverthrow-r/constructors';
263
+ * import { orTee } from '@konker.dev/neverthrow-r/sync';
264
+ * import { pipe } from '@konker.dev/neverthrow-r/pipe';
265
+ *
266
+ * const logged = pipe(
267
+ * errR<string>('boom'),
268
+ * orTee((e) => console.error('failed:', e)),
269
+ * );
270
+ *
271
+ * logged(undefined); // logs 'failed: boom', returns Err('boom')
272
+ * ```
273
+ *
274
+ * @see {@link andTee}
275
+ * @see {@link orTeeAsync}
276
+ */
277
+ export const orTee = (f) => (rr) => (r) => rr(r).orTee(f);
278
+ /**
279
+ * Runs a fallible step for its side effect, then propagates the original
280
+ * success value if the step succeeds.
281
+ *
282
+ * @remarks
283
+ * Like {@link andTee}, but the tee step is itself a `ResultR` that can fail.
284
+ * If the tee step returns `Err`, that error replaces the chain's value; if it
285
+ * returns `Ok`, the chain continues with the *original* success value (the
286
+ * tee's success value is discarded).
287
+ *
288
+ * Common shape: validate a value via a fallible check without losing the
289
+ * value itself.
290
+ *
291
+ * @typeParam T - The success type (preserved through the step on success).
292
+ * @typeParam R2 - The tee's requirements (intersected into the chain).
293
+ * @typeParam F - The tee's error type (unioned with the chain's).
294
+ *
295
+ * @param f - Function from the success value to a `ResultR` whose value is
296
+ * ignored on success.
297
+ *
298
+ * @example
299
+ * ```ts
300
+ * import { errR, okR } from '@konker.dev/neverthrow-r/constructors';
301
+ * import { andThrough } from '@konker.dev/neverthrow-r/sync';
302
+ * import { pipe } from '@konker.dev/neverthrow-r/pipe';
303
+ *
304
+ * const validated = pipe(
305
+ * okR<number>(2),
306
+ * andThrough((n) => (n > 0 ? okR<unknown>(undefined) : errR<string>('non-positive'))),
307
+ * );
308
+ *
309
+ * validated(undefined); // Ok(2)
310
+ * ```
311
+ *
312
+ * @see {@link andTee} for the infallible version.
313
+ * @see {@link andThroughAsync}
314
+ */
315
+ export const andThrough = (f) => (rr) => (r) => rr(r).andThrough((t) => f(t)(r));
316
+ //# sourceMappingURL=sync.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sync.js","sourceRoot":"","sources":["../src/sync.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAIH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,CAAC,MAAM,GAAG,GACd,CAAO,CAAc,EAAE,EAAE,CACzB,CAAO,EAAoB,EAAoB,EAAE,CACjD,CAAC,CAAC,EAAE,EAAE,CACJ,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;A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