@nlozgachev/pipelined 0.44.0 → 0.45.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -6
- package/dist/{InternalTypes-BL23H8Qr.d.mts → InternalTypes-CLE7qlOc.d.mts} +33 -28
- package/dist/{InternalTypes-7o9-yrHq.d.ts → InternalTypes-Mssktd7z.d.ts} +33 -28
- package/dist/{Validation-DM2eh6wj.d.ts → Validation-BMsvixWH.d.ts} +475 -443
- package/dist/{Validation-Dz70wtcG.d.mts → Validation-v38R0qH-.d.mts} +475 -443
- package/dist/{chunk-LKTOK5IT.mjs → chunk-2LKJF45J.mjs} +160 -68
- package/dist/{chunk-X6XQX3OZ.mjs → chunk-KOYYDQH4.mjs} +2 -2
- package/dist/{chunk-ND476266.mjs → chunk-VSU36S2K.mjs} +432 -309
- package/dist/{chunk-74JKKJ4R.mjs → chunk-XTVF5R6R.mjs} +8 -5
- package/dist/composition.d.mts +10 -10
- package/dist/composition.d.ts +10 -10
- package/dist/composition.js +9 -6
- package/dist/composition.mjs +2 -2
- package/dist/core.d.mts +453 -402
- package/dist/core.d.ts +453 -402
- package/dist/core.js +439 -313
- package/dist/core.mjs +2 -2
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +599 -381
- package/dist/index.mjs +4 -4
- package/dist/types.d.mts +23 -21
- package/dist/types.d.ts +23 -21
- package/dist/types.js +8 -5
- package/dist/types.mjs +1 -1
- package/dist/utils.d.mts +466 -216
- package/dist/utils.d.ts +466 -216
- package/dist/utils.js +406 -238
- package/dist/utils.mjs +3 -3
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { h as WithKind, o as WithValue, e as WithError, T as Thenable, D as Deferred, a as NonEmptyArr, f as WithErrors } from './InternalTypes-Mssktd7z.js';
|
|
2
2
|
import { Duration } from './types.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -96,7 +96,7 @@ type None = WithKind<"None">;
|
|
|
96
96
|
*
|
|
97
97
|
* @example
|
|
98
98
|
* ```ts
|
|
99
|
-
* const user = { name: "Alice", email: Maybe.some("alice@example.com") };
|
|
99
|
+
* const user = { name: "Alice", email: Maybe.make.some("alice@example.com") };
|
|
100
100
|
*
|
|
101
101
|
* pipe(
|
|
102
102
|
* user.email,
|
|
@@ -107,91 +107,99 @@ type None = WithKind<"None">;
|
|
|
107
107
|
*/
|
|
108
108
|
type Maybe<T> = Some<T> | None;
|
|
109
109
|
declare namespace Maybe {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
110
|
+
namespace make {
|
|
111
|
+
/**
|
|
112
|
+
* Creates a Some containing the given value.
|
|
113
|
+
*/
|
|
114
|
+
const some: <A>(value: A) => Some<A>;
|
|
115
|
+
/**
|
|
116
|
+
* Creates a None (empty Maybe).
|
|
117
|
+
*/
|
|
118
|
+
const none: () => None;
|
|
119
|
+
}
|
|
120
|
+
namespace is {
|
|
121
|
+
/**
|
|
122
|
+
* Type guard that checks if a Maybe is Some.
|
|
123
|
+
*/
|
|
124
|
+
const some: <A>(data: Maybe<A>) => data is Some<A>;
|
|
125
|
+
/**
|
|
126
|
+
* Type guard that checks if a Maybe is None.
|
|
127
|
+
*/
|
|
128
|
+
const none: <A>(data: Maybe<A>) => data is None;
|
|
129
|
+
}
|
|
130
|
+
namespace to {
|
|
131
|
+
/**
|
|
132
|
+
* Extracts the value from a Maybe, returning null if None.
|
|
133
|
+
*/
|
|
134
|
+
const nullable: <A>(data: Maybe<A>) => A | null;
|
|
135
|
+
/**
|
|
136
|
+
* Extracts the value from a Maybe, returning undefined if None.
|
|
137
|
+
*/
|
|
138
|
+
const undefined: <A>(data: Maybe<A>) => A | undefined;
|
|
139
|
+
/**
|
|
140
|
+
* Converts a Maybe to a Result.
|
|
141
|
+
* Some becomes Ok, None becomes Err with the provided error.
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```ts
|
|
145
|
+
* pipe(
|
|
146
|
+
* Maybe.make.some(42),
|
|
147
|
+
* Maybe.to.Result(() => "Value was missing")
|
|
148
|
+
* ); // Ok(42)
|
|
149
|
+
*
|
|
150
|
+
* pipe(
|
|
151
|
+
* Maybe.make.none(),
|
|
152
|
+
* Maybe.to.Result(() => "Value was missing")
|
|
153
|
+
* ); // Err("Value was missing")
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
const Result: <E>(onNone: () => E) => <A>(data: Maybe<A>) => Result<E, A>;
|
|
157
|
+
}
|
|
158
|
+
namespace from {
|
|
159
|
+
/**
|
|
160
|
+
* Creates a Maybe from a nullable value.
|
|
161
|
+
* Returns None if the value is null or undefined, Some otherwise.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```ts
|
|
165
|
+
* Maybe.from.nullable(null); // None
|
|
166
|
+
* Maybe.from.nullable(42); // Some(42)
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
const nullable: <A>(value: A | null | undefined) => Maybe<A>;
|
|
170
|
+
/**
|
|
171
|
+
* Creates a Maybe from a predicate applied to a value.
|
|
172
|
+
* Returns Some if the predicate passes, None otherwise.
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```ts
|
|
176
|
+
* Maybe.from.Predicate((n: number) => n >= 18)(21); // Some(21)
|
|
177
|
+
* Maybe.from.Predicate((n: number) => n >= 18)(15); // None
|
|
178
|
+
*
|
|
179
|
+
* pipe("hello", Maybe.from.Predicate((s: string) => s.length > 0)); // Some("hello")
|
|
180
|
+
* pipe("", Maybe.from.Predicate((s: string) => s.length > 0)); // None
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
const Predicate: <A>(pred: (a: A) => boolean) => (a: A) => Maybe<A>;
|
|
184
|
+
/**
|
|
185
|
+
* Creates a Maybe from a Result.
|
|
186
|
+
* Ok becomes Some, Err becomes None (the error is discarded).
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* ```ts
|
|
190
|
+
* Maybe.from.Result(Result.make.ok(42)); // Some(42)
|
|
191
|
+
* Maybe.from.Result(Result.make.err("oops")); // None
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
const Result: <E, A>(data: Result<E, A>) => Maybe<A>;
|
|
195
|
+
}
|
|
188
196
|
/**
|
|
189
197
|
* Transforms the value inside a Maybe if it exists.
|
|
190
198
|
*
|
|
191
199
|
* @example
|
|
192
200
|
* ```ts
|
|
193
|
-
* pipe(Maybe.some(5), Maybe.map(n => n * 2)); // Some(10)
|
|
194
|
-
* pipe(Maybe.none(), Maybe.map(n => n * 2)); // None
|
|
201
|
+
* pipe(Maybe.make.some(5), Maybe.map(n => n * 2)); // Some(10)
|
|
202
|
+
* pipe(Maybe.make.none(), Maybe.map(n => n * 2)); // None
|
|
195
203
|
* ```
|
|
196
204
|
*/
|
|
197
205
|
const map: <A, B>(f: (a: A) => B) => (data: Maybe<A>) => Maybe<B>;
|
|
@@ -203,11 +211,11 @@ declare namespace Maybe {
|
|
|
203
211
|
* ```ts
|
|
204
212
|
* const parseNumber = (s: string): Maybe<number> => {
|
|
205
213
|
* const n = parseInt(s, 10);
|
|
206
|
-
* return isNaN(n) ? Maybe.none() : Maybe.some(n);
|
|
214
|
+
* return isNaN(n) ? Maybe.make.none() : Maybe.make.some(n);
|
|
207
215
|
* };
|
|
208
216
|
*
|
|
209
|
-
* pipe(Maybe.some("42"), Maybe.chain(parseNumber)); // Some(42)
|
|
210
|
-
* pipe(Maybe.some("abc"), Maybe.chain(parseNumber)); // None
|
|
217
|
+
* pipe(Maybe.make.some("42"), Maybe.chain(parseNumber)); // Some(42)
|
|
218
|
+
* pipe(Maybe.make.some("abc"), Maybe.chain(parseNumber)); // None
|
|
211
219
|
* ```
|
|
212
220
|
*/
|
|
213
221
|
const chain: <A, B>(f: (a: A) => Maybe<B>) => (data: Maybe<A>) => Maybe<B>;
|
|
@@ -217,7 +225,7 @@ declare namespace Maybe {
|
|
|
217
225
|
* @example
|
|
218
226
|
* ```ts
|
|
219
227
|
* pipe(
|
|
220
|
-
* Maybe.some(5),
|
|
228
|
+
* Maybe.make.some(5),
|
|
221
229
|
* Maybe.fold(
|
|
222
230
|
* () => "No value",
|
|
223
231
|
* n => `Value: ${n}`
|
|
@@ -251,9 +259,9 @@ declare namespace Maybe {
|
|
|
251
259
|
*
|
|
252
260
|
* @example
|
|
253
261
|
* ```ts
|
|
254
|
-
* pipe(Maybe.some(5), Maybe.getOrElse(() => 0)); // 5
|
|
255
|
-
* pipe(Maybe.none(), Maybe.getOrElse(() => 0)); // 0
|
|
256
|
-
* pipe(Maybe.none<string>(), Maybe.getOrElse(() => null)); // null — typed as string | null
|
|
262
|
+
* pipe(Maybe.make.some(5), Maybe.getOrElse(() => 0)); // 5
|
|
263
|
+
* pipe(Maybe.make.none(), Maybe.getOrElse(() => 0)); // 0
|
|
264
|
+
* pipe(Maybe.make.none<string>(), Maybe.getOrElse(() => null)); // null — typed as string | null
|
|
257
265
|
* ```
|
|
258
266
|
*/
|
|
259
267
|
const getOrElse: <A, B>(defaultValue: () => B) => (data: Maybe<A>) => A | B;
|
|
@@ -264,7 +272,7 @@ declare namespace Maybe {
|
|
|
264
272
|
* @example
|
|
265
273
|
* ```ts
|
|
266
274
|
* pipe(
|
|
267
|
-
* Maybe.some(5),
|
|
275
|
+
* Maybe.make.some(5),
|
|
268
276
|
* Maybe.tap(n => console.log("Value:", n)),
|
|
269
277
|
* Maybe.map(n => n * 2)
|
|
270
278
|
* );
|
|
@@ -277,8 +285,8 @@ declare namespace Maybe {
|
|
|
277
285
|
*
|
|
278
286
|
* @example
|
|
279
287
|
* ```ts
|
|
280
|
-
* pipe(Maybe.some(5), Maybe.filter(n => n > 3)); // Some(5)
|
|
281
|
-
* pipe(Maybe.some(2), Maybe.filter(n => n > 3)); // None
|
|
288
|
+
* pipe(Maybe.make.some(5), Maybe.filter(n => n > 3)); // Some(5)
|
|
289
|
+
* pipe(Maybe.make.some(2), Maybe.filter(n => n > 3)); // None
|
|
282
290
|
* ```
|
|
283
291
|
*/
|
|
284
292
|
const filter: <A>(predicate: (a: A) => boolean) => (data: Maybe<A>) => Maybe<A>;
|
|
@@ -294,9 +302,9 @@ declare namespace Maybe {
|
|
|
294
302
|
* ```ts
|
|
295
303
|
* const add = (a: number) => (b: number) => a + b;
|
|
296
304
|
* pipe(
|
|
297
|
-
* Maybe.some(add),
|
|
298
|
-
* Maybe.ap(Maybe.some(5)),
|
|
299
|
-
* Maybe.ap(Maybe.some(3))
|
|
305
|
+
* Maybe.make.some(add),
|
|
306
|
+
* Maybe.ap(Maybe.make.some(5)),
|
|
307
|
+
* Maybe.ap(Maybe.make.some(3))
|
|
300
308
|
* ); // Some(8)
|
|
301
309
|
* ```
|
|
302
310
|
*/
|
|
@@ -307,7 +315,7 @@ declare namespace Maybe {
|
|
|
307
315
|
*
|
|
308
316
|
* @example
|
|
309
317
|
* ```ts
|
|
310
|
-
* pipe(Maybe.some(42), Maybe.bindTo("value")); // Some({ value: 42 })
|
|
318
|
+
* pipe(Maybe.make.some(42), Maybe.bindTo("value")); // Some({ value: 42 })
|
|
311
319
|
* ```
|
|
312
320
|
*/
|
|
313
321
|
const bindTo: <K extends string>(key: K) => <A>(data: Maybe<A>) => Maybe<{ [P in K]: A; }>;
|
|
@@ -317,8 +325,8 @@ declare namespace Maybe {
|
|
|
317
325
|
* @example
|
|
318
326
|
* ```ts
|
|
319
327
|
* pipe(
|
|
320
|
-
* Maybe.some({ a: 1 }),
|
|
321
|
-
* Maybe.bind("b", ({ a }) => Maybe.some(a + 1))
|
|
328
|
+
* Maybe.make.some({ a: 1 }),
|
|
329
|
+
* Maybe.bind("b", ({ a }) => Maybe.make.some(a + 1))
|
|
322
330
|
* ); // Some({ a: 1, b: 2 })
|
|
323
331
|
* ```
|
|
324
332
|
*/
|
|
@@ -330,8 +338,8 @@ declare namespace Maybe {
|
|
|
330
338
|
* @example
|
|
331
339
|
* ```ts
|
|
332
340
|
* Maybe.struct({
|
|
333
|
-
* name: Maybe.some("Alice"),
|
|
334
|
-
* age: Maybe.some(30)
|
|
341
|
+
* name: Maybe.make.some("Alice"),
|
|
342
|
+
* age: Maybe.make.some(30)
|
|
335
343
|
* }); // Some({ name: "Alice", age: 30 })
|
|
336
344
|
* ```
|
|
337
345
|
*/
|
|
@@ -425,7 +433,7 @@ type Err<E> = WithKind<"Err"> & WithError<E>;
|
|
|
425
433
|
* @example
|
|
426
434
|
* ```ts
|
|
427
435
|
* const divide = (a: number, b: number): Result<string, number> =>
|
|
428
|
-
* b === 0 ? Result.err("Division by zero") : Result.ok(a / b);
|
|
436
|
+
* b === 0 ? Result.make.err("Division by zero") : Result.make.ok(a / b);
|
|
429
437
|
*
|
|
430
438
|
* pipe(
|
|
431
439
|
* divide(10, 2),
|
|
@@ -436,22 +444,26 @@ type Err<E> = WithKind<"Err"> & WithError<E>;
|
|
|
436
444
|
*/
|
|
437
445
|
type Result<E, A> = Ok<A> | Err<E>;
|
|
438
446
|
declare namespace Result {
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
447
|
+
namespace make {
|
|
448
|
+
/**
|
|
449
|
+
* Creates a successful Result with the given value.
|
|
450
|
+
*/
|
|
451
|
+
const ok: <A>(value: A) => Ok<A>;
|
|
452
|
+
/**
|
|
453
|
+
* Creates a failed Result with the given error.
|
|
454
|
+
*/
|
|
455
|
+
const err: <E>(e: E) => Err<E>;
|
|
456
|
+
}
|
|
457
|
+
namespace is {
|
|
458
|
+
/**
|
|
459
|
+
* Type guard that checks if a Result is Ok.
|
|
460
|
+
*/
|
|
461
|
+
const ok: <E, A>(data: Result<E, A>) => data is Ok<A>;
|
|
462
|
+
/**
|
|
463
|
+
* Type guard that checks if a Result is Err.
|
|
464
|
+
*/
|
|
465
|
+
const err: <E, A>(data: Result<E, A>) => data is Err<E>;
|
|
466
|
+
}
|
|
455
467
|
/**
|
|
456
468
|
* Creates a Result from a function that may throw.
|
|
457
469
|
* Catches any errors and transforms them using the onError function.
|
|
@@ -471,8 +483,8 @@ declare namespace Result {
|
|
|
471
483
|
*
|
|
472
484
|
* @example
|
|
473
485
|
* ```ts
|
|
474
|
-
* pipe(Result.ok(5), Result.map(n => n * 2)); // Ok(10)
|
|
475
|
-
* pipe(Result.err("error"), Result.map(n => n * 2)); // Err("error")
|
|
486
|
+
* pipe(Result.make.ok(5), Result.map(n => n * 2)); // Ok(10)
|
|
487
|
+
* pipe(Result.make.err("error"), Result.map(n => n * 2)); // Err("error")
|
|
476
488
|
* ```
|
|
477
489
|
*/
|
|
478
490
|
const map: <E, A, B>(f: (a: A) => B) => (data: Result<E, A>) => Result<E, B>;
|
|
@@ -481,7 +493,7 @@ declare namespace Result {
|
|
|
481
493
|
*
|
|
482
494
|
* @example
|
|
483
495
|
* ```ts
|
|
484
|
-
* pipe(Result.err("oops"), Result.mapError(e => e.toUpperCase())); // Err("OOPS")
|
|
496
|
+
* pipe(Result.make.err("oops"), Result.mapError(e => e.toUpperCase())); // Err("OOPS")
|
|
485
497
|
* ```
|
|
486
498
|
*/
|
|
487
499
|
const mapError: <E, F, A>(f: (e: E) => F) => (data: Result<E, A>) => Result<F, A>;
|
|
@@ -492,10 +504,10 @@ declare namespace Result {
|
|
|
492
504
|
* @example
|
|
493
505
|
* ```ts
|
|
494
506
|
* const validatePositive = (n: number): Result<string, number> =>
|
|
495
|
-
* n > 0 ? Result.ok(n) : Result.err("Must be positive");
|
|
507
|
+
* n > 0 ? Result.make.ok(n) : Result.make.err("Must be positive");
|
|
496
508
|
*
|
|
497
|
-
* pipe(Result.ok(5), Result.chain(validatePositive)); // Ok(5)
|
|
498
|
-
* pipe(Result.ok(-1), Result.chain(validatePositive)); // Err("Must be positive")
|
|
509
|
+
* pipe(Result.make.ok(5), Result.chain(validatePositive)); // Ok(5)
|
|
510
|
+
* pipe(Result.make.ok(-1), Result.chain(validatePositive)); // Err("Must be positive")
|
|
499
511
|
* ```
|
|
500
512
|
*/
|
|
501
513
|
const chain: <E, A, B>(f: (a: A) => Result<E, B>) => (data: Result<E, A>) => Result<E, B>;
|
|
@@ -505,7 +517,7 @@ declare namespace Result {
|
|
|
505
517
|
* @example
|
|
506
518
|
* ```ts
|
|
507
519
|
* pipe(
|
|
508
|
-
* Result.ok(5),
|
|
520
|
+
* Result.make.ok(5),
|
|
509
521
|
* Result.fold(
|
|
510
522
|
* e => `Error: ${e}`,
|
|
511
523
|
* n => `Value: ${n}`
|
|
@@ -539,9 +551,9 @@ declare namespace Result {
|
|
|
539
551
|
*
|
|
540
552
|
* @example
|
|
541
553
|
* ```ts
|
|
542
|
-
* pipe(Result.ok(5), Result.getOrElse(() => 0)); // 5
|
|
543
|
-
* pipe(Result.err("error"), Result.getOrElse(() => 0)); // 0
|
|
544
|
-
* pipe(Result.err("error"), Result.getOrElse(() => null)); // null — typed as number | null
|
|
554
|
+
* pipe(Result.make.ok(5), Result.getOrElse(() => 0)); // 5
|
|
555
|
+
* pipe(Result.make.err("error"), Result.getOrElse(() => 0)); // 0
|
|
556
|
+
* pipe(Result.make.err("error"), Result.getOrElse(() => null)); // null — typed as number | null
|
|
545
557
|
* ```
|
|
546
558
|
*/
|
|
547
559
|
const getOrElse: <E, A, B>(defaultValue: () => B) => (data: Result<E, A>) => A | B;
|
|
@@ -552,7 +564,7 @@ declare namespace Result {
|
|
|
552
564
|
* @example
|
|
553
565
|
* ```ts
|
|
554
566
|
* pipe(
|
|
555
|
-
* Result.ok(5),
|
|
567
|
+
* Result.make.ok(5),
|
|
556
568
|
* Result.tap(n => console.log("Value:", n)),
|
|
557
569
|
* Result.map(n => n * 2)
|
|
558
570
|
* );
|
|
@@ -566,63 +578,65 @@ declare namespace Result {
|
|
|
566
578
|
* @example
|
|
567
579
|
* ```ts
|
|
568
580
|
* pipe(
|
|
569
|
-
* Result.err("not found"),
|
|
581
|
+
* Result.make.err("not found"),
|
|
570
582
|
* Result.tapError(e => console.error("validation failed:", e)),
|
|
571
583
|
* Result.chain(save),
|
|
572
584
|
* )
|
|
573
585
|
* ```
|
|
574
586
|
*/
|
|
575
587
|
const tapError: <E, A>(f: (e: E) => void) => (data: Result<E, A>) => Result<E, A>;
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
588
|
+
namespace from {
|
|
589
|
+
/**
|
|
590
|
+
* Creates a Result from a predicate applied to a value.
|
|
591
|
+
* Returns Ok if the predicate passes, Err from onFalse otherwise.
|
|
592
|
+
*
|
|
593
|
+
* @example
|
|
594
|
+
* ```ts
|
|
595
|
+
* pipe(5, Result.from.Predicate(n => n > 0, n => `${n} is not positive`)); // Ok(5)
|
|
596
|
+
* pipe(-1, Result.from.Predicate(n => n > 0, n => `${n} is not positive`)); // Err("-1 is not positive")
|
|
597
|
+
* pipe("", Result.from.Predicate(s => s.length > 0, () => "empty string")); // Err("empty string")
|
|
598
|
+
* ```
|
|
599
|
+
*/
|
|
600
|
+
const Predicate: <E, A>(pred: (a: A) => boolean, onFalse: (a: A) => E) => (a: A) => Result<E, A>;
|
|
601
|
+
/**
|
|
602
|
+
* Creates a Result from a nullable value.
|
|
603
|
+
* Returns Ok if the value is not null or undefined, error from onNull otherwise.
|
|
604
|
+
*
|
|
605
|
+
* @example
|
|
606
|
+
* ```ts
|
|
607
|
+
* pipe(null, Result.from.nullable(() => "is null")); // Err("is null")
|
|
608
|
+
* pipe(42, Result.from.nullable(() => "is null")); // Ok(42)
|
|
609
|
+
* ```
|
|
610
|
+
*/
|
|
611
|
+
const nullable: <E>(onNull: () => E) => <A>(value: A | null | undefined) => Result<E, A>;
|
|
612
|
+
/**
|
|
613
|
+
* Creates a Result from a Maybe.
|
|
614
|
+
* Some becomes Ok, None becomes error from onNone.
|
|
615
|
+
*
|
|
616
|
+
* @example
|
|
617
|
+
* ```ts
|
|
618
|
+
* pipe(Maybe.make.none(), Result.from.Maybe(() => "is none")); // Err("is none")
|
|
619
|
+
* pipe(Maybe.make.some(42), Result.from.Maybe(() => "is none")); // Ok(42)
|
|
620
|
+
* ```
|
|
621
|
+
*/
|
|
622
|
+
const Maybe: <E>(onNone: () => E) => <A>(maybe: Maybe<A>) => Result<E, A>;
|
|
623
|
+
/**
|
|
624
|
+
* Wraps a throwing function of any arguments, returning a new function
|
|
625
|
+
* that catches errors and returns a Result.
|
|
626
|
+
*
|
|
627
|
+
* @example
|
|
628
|
+
* ```ts
|
|
629
|
+
* const safeParse = Result.from.throwable(
|
|
630
|
+
* (s: string) => JSON.parse(s),
|
|
631
|
+
* (e) => new Error(`Parse error: ${e}`)
|
|
632
|
+
* );
|
|
633
|
+
*
|
|
634
|
+
* safeParse('{"a":1}'); // Ok({ a: 1 })
|
|
635
|
+
* safeParse('invalid'); // Err(Error)
|
|
636
|
+
* ```
|
|
637
|
+
*/
|
|
638
|
+
const throwable: <Args extends readonly unknown[], A, E>(f: (...args: Args) => A, onError: (e: unknown) => E) => (...args: Args) => Result<E, A>;
|
|
639
|
+
}
|
|
626
640
|
/**
|
|
627
641
|
* Recovers from an error by providing a fallback Result.
|
|
628
642
|
* The fallback can produce a different success type, widening the result to `Result<E, A | B>`.
|
|
@@ -635,23 +649,25 @@ declare namespace Result {
|
|
|
635
649
|
* @example
|
|
636
650
|
* ```ts
|
|
637
651
|
* pipe(
|
|
638
|
-
* Result.err(new Error("not found")),
|
|
639
|
-
* Result.recoverUnless(e => e.message === "fatal", () => Result.ok(0))
|
|
652
|
+
* Result.make.err(new Error("not found")),
|
|
653
|
+
* Result.recoverUnless(e => e.message === "fatal", () => Result.make.ok(0))
|
|
640
654
|
* ); // Ok(0)
|
|
641
655
|
* ```
|
|
642
656
|
*/
|
|
643
657
|
const recoverUnless: <E, A, B>(isBlocked: (e: E) => boolean, fallback: () => Result<E, B>) => (data: Result<E, A>) => Result<E, A | B>;
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
658
|
+
namespace to {
|
|
659
|
+
/**
|
|
660
|
+
* Converts a Result to a Maybe.
|
|
661
|
+
* Ok becomes Some, Err becomes None (the error is discarded).
|
|
662
|
+
*
|
|
663
|
+
* @example
|
|
664
|
+
* ```ts
|
|
665
|
+
* Result.to.Maybe(Result.make.ok(42)); // Some(42)
|
|
666
|
+
* Result.to.Maybe(Result.make.err("oops")); // None
|
|
667
|
+
* ```
|
|
668
|
+
*/
|
|
669
|
+
const Maybe: <E, A>(data: Result<E, A>) => Maybe<A>;
|
|
670
|
+
}
|
|
655
671
|
/**
|
|
656
672
|
* Applies a function wrapped in a Result to a value wrapped in a Result.
|
|
657
673
|
*
|
|
@@ -659,9 +675,9 @@ declare namespace Result {
|
|
|
659
675
|
* ```ts
|
|
660
676
|
* const add = (a: number) => (b: number) => a + b;
|
|
661
677
|
* pipe(
|
|
662
|
-
* Result.ok(add),
|
|
663
|
-
* Result.ap(Result.ok(5)),
|
|
664
|
-
* Result.ap(Result.ok(3))
|
|
678
|
+
* Result.make.ok(add),
|
|
679
|
+
* Result.ap(Result.make.ok(5)),
|
|
680
|
+
* Result.ap(Result.make.ok(3))
|
|
665
681
|
* ); // Ok(8)
|
|
666
682
|
* ```
|
|
667
683
|
*/
|
|
@@ -672,7 +688,7 @@ declare namespace Result {
|
|
|
672
688
|
*
|
|
673
689
|
* @example
|
|
674
690
|
* ```ts
|
|
675
|
-
* pipe(Result.ok(42), Result.bindTo("value")); // Ok({ value: 42 })
|
|
691
|
+
* pipe(Result.make.ok(42), Result.bindTo("value")); // Ok({ value: 42 })
|
|
676
692
|
* ```
|
|
677
693
|
*/
|
|
678
694
|
const bindTo: <K extends string>(key: K) => <E, A>(data: Result<E, A>) => Result<E, { [P in K]: A; }>;
|
|
@@ -682,8 +698,8 @@ declare namespace Result {
|
|
|
682
698
|
* @example
|
|
683
699
|
* ```ts
|
|
684
700
|
* pipe(
|
|
685
|
-
* Result.ok({ a: 1 }),
|
|
686
|
-
* Result.bind("b", ({ a }) => Result.ok(a + 1))
|
|
701
|
+
* Result.make.ok({ a: 1 }),
|
|
702
|
+
* Result.bind("b", ({ a }) => Result.make.ok(a + 1))
|
|
687
703
|
* ); // Ok({ a: 1, b: 2 })
|
|
688
704
|
* ```
|
|
689
705
|
*/
|
|
@@ -695,8 +711,8 @@ declare namespace Result {
|
|
|
695
711
|
* @example
|
|
696
712
|
* ```ts
|
|
697
713
|
* Result.struct({
|
|
698
|
-
* name: Result.ok("Alice"),
|
|
699
|
-
* age: Result.ok(30)
|
|
714
|
+
* name: Result.make.ok("Alice"),
|
|
715
|
+
* age: Result.make.ok(30)
|
|
700
716
|
* }); // Ok({ name: "Alice", age: 30 })
|
|
701
717
|
* ```
|
|
702
718
|
*/
|
|
@@ -704,19 +720,17 @@ declare namespace Result {
|
|
|
704
720
|
}
|
|
705
721
|
|
|
706
722
|
/**
|
|
707
|
-
*
|
|
708
|
-
*
|
|
723
|
+
* TaskMaybe represents a lazy, infallible async operation that resolves to a `Maybe<A>`.
|
|
724
|
+
* It is a type alias for `Task<Maybe<A>>`.
|
|
725
|
+
*
|
|
726
|
+
* Use Task.Maybe for async operations that can result in a missing value (e.g. database lookups).
|
|
709
727
|
*
|
|
710
728
|
* @example
|
|
711
729
|
* ```ts
|
|
712
730
|
* const findUser = (id: string): Task.Maybe<User> =>
|
|
713
|
-
* Task.Maybe.tryCatch(() =>
|
|
714
|
-
*
|
|
715
|
-
*
|
|
716
|
-
* findUser("123"),
|
|
717
|
-
* Task.Maybe.map(user => user.name),
|
|
718
|
-
* Task.Maybe.getOrElse(() => "Unknown")
|
|
719
|
-
* )();
|
|
731
|
+
* Task.Maybe.tryCatch((signal) =>
|
|
732
|
+
* fetch(`/users/${id}`, { signal }).then(r => r.ok ? r.json() : null)
|
|
733
|
+
* );
|
|
720
734
|
* ```
|
|
721
735
|
*/
|
|
722
736
|
type TaskMaybe<A> = Task<Maybe<A>>;
|
|
@@ -729,24 +743,26 @@ declare namespace TaskMaybe {
|
|
|
729
743
|
* Creates a Task.Maybe that resolves to None.
|
|
730
744
|
*/
|
|
731
745
|
const none: <A = never>() => TaskMaybe<A>;
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
746
|
+
namespace from {
|
|
747
|
+
/**
|
|
748
|
+
* Lifts an Option into a Task.Maybe.
|
|
749
|
+
*/
|
|
750
|
+
const Maybe: <A>(option: Maybe<A>) => TaskMaybe<A>;
|
|
751
|
+
/**
|
|
752
|
+
* Creates a Task.Maybe from a nullable value.
|
|
753
|
+
* Returns Some if the value is not null or undefined, None otherwise.
|
|
754
|
+
*/
|
|
755
|
+
const nullable: <A>(value: A | null | undefined) => TaskMaybe<A>;
|
|
756
|
+
/**
|
|
757
|
+
* Creates a Task.Maybe from a Result.
|
|
758
|
+
* Ok becomes Some, Error becomes None (the error value is discarded).
|
|
759
|
+
*/
|
|
760
|
+
const Result: <E, A>(result: Result<E, A>) => TaskMaybe<A>;
|
|
761
|
+
/**
|
|
762
|
+
* Lifts a Task into a Task.Maybe by wrapping its result in Some.
|
|
763
|
+
*/
|
|
764
|
+
const Task: <A>(task: Task<A>) => TaskMaybe<A>;
|
|
765
|
+
}
|
|
750
766
|
/**
|
|
751
767
|
* Creates a Task.Maybe from a Promise-returning function.
|
|
752
768
|
* Returns Some if the promise resolves, None if it rejects.
|
|
@@ -818,18 +834,20 @@ declare namespace TaskMaybe {
|
|
|
818
834
|
* Filters the value inside a Task.Maybe. Returns None if the predicate fails.
|
|
819
835
|
*/
|
|
820
836
|
const filter: <A>(predicate: (a: A) => boolean) => (data: TaskMaybe<A>) => TaskMaybe<A>;
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
837
|
+
namespace to {
|
|
838
|
+
/**
|
|
839
|
+
* Converts a Task.Maybe to a Task.Result, using onNone to produce the error value.
|
|
840
|
+
*
|
|
841
|
+
* @example
|
|
842
|
+
* ```ts
|
|
843
|
+
* pipe(
|
|
844
|
+
* findUser("123"),
|
|
845
|
+
* Task.Maybe.to.Result(() => "User not found")
|
|
846
|
+
* );
|
|
847
|
+
* ```
|
|
848
|
+
*/
|
|
849
|
+
const Result: <E>(onNone: () => E) => <A>(data: TaskMaybe<A>) => Task.Result<E, A>;
|
|
850
|
+
}
|
|
833
851
|
/**
|
|
834
852
|
* Lifts a Task.Maybe value into an accumulator object.
|
|
835
853
|
*
|
|
@@ -901,25 +919,27 @@ declare namespace TaskResult {
|
|
|
901
919
|
* Creates a failed Task.Result with the given error.
|
|
902
920
|
*/
|
|
903
921
|
const err: <E, A>(error: E) => TaskResult<E, A>;
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
922
|
+
namespace from {
|
|
923
|
+
/**
|
|
924
|
+
* Creates a Task.Result from a nullable value.
|
|
925
|
+
* Returns Ok if the value is not null or undefined, err from onNull otherwise.
|
|
926
|
+
*/
|
|
927
|
+
const nullable: <E>(onNull: () => E) => <A>(value: A | null | undefined) => TaskResult<E, A>;
|
|
928
|
+
/**
|
|
929
|
+
* Creates a Task.Result from a Maybe.
|
|
930
|
+
* Some becomes Ok, None becomes err from onNone.
|
|
931
|
+
*/
|
|
932
|
+
const Maybe: <E>(onNone: () => E) => <A>(maybe: Maybe<A>) => TaskResult<E, A>;
|
|
933
|
+
/**
|
|
934
|
+
* Lifts a Result into a Task.Result.
|
|
935
|
+
*/
|
|
936
|
+
const Result: <E, A>(result: Result<E, A>) => TaskResult<E, A>;
|
|
937
|
+
/**
|
|
938
|
+
* Wraps a Promise-returning function of any arguments, returning a new function
|
|
939
|
+
* that catches rejections and returns a Task.Result.
|
|
940
|
+
*/
|
|
941
|
+
const throwable: <Args extends readonly unknown[], A, E>(f: (...args: Args) => Promise<A>, onError: (e: unknown) => E) => (...args: Args) => TaskResult<E, A>;
|
|
942
|
+
}
|
|
923
943
|
/**
|
|
924
944
|
* Creates a Task.Result from a function that may throw.
|
|
925
945
|
* Catches any errors and transforms them using the onError function.
|
|
@@ -1005,7 +1025,7 @@ declare namespace TaskResult {
|
|
|
1005
1025
|
* Task.Result.chain(user => fetchPosts(user.id)),
|
|
1006
1026
|
* Task.Result.run(controller.signal),
|
|
1007
1027
|
* );
|
|
1008
|
-
* if (Result.
|
|
1028
|
+
* if (Result.is.ok(result)) render(result.value);
|
|
1009
1029
|
* ```
|
|
1010
1030
|
*/
|
|
1011
1031
|
const run: (signal?: AbortSignal) => <E, A>(task: TaskResult<E, A>) => Deferred<Result<E, A>>;
|
|
@@ -1082,26 +1102,28 @@ declare namespace TaskValidation {
|
|
|
1082
1102
|
* Creates a failed Task.Validation from multiple errors.
|
|
1083
1103
|
*/
|
|
1084
1104
|
const failedAll: <E, A>(errors: NonEmptyArr<E>) => TaskValidation<E, A>;
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
+
namespace from {
|
|
1106
|
+
/**
|
|
1107
|
+
* Lifts a Validation into a Task.Validation.
|
|
1108
|
+
*/
|
|
1109
|
+
const Validation: <E, A>(validation: Validation<E, A>) => TaskValidation<E, A>;
|
|
1110
|
+
/**
|
|
1111
|
+
* Creates a Task.Validation from a nullable value.
|
|
1112
|
+
* If the value is null or undefined, returns Failed with the error from onNull.
|
|
1113
|
+
* Otherwise, returns Passed.
|
|
1114
|
+
*/
|
|
1115
|
+
const nullable: <E>(onNull: () => E) => <A>(value: A | null | undefined) => TaskValidation<E, A>;
|
|
1116
|
+
/**
|
|
1117
|
+
* Creates a Task.Validation from a Maybe.
|
|
1118
|
+
* Some becomes Passed, None becomes Failed with the error from onNone.
|
|
1119
|
+
*/
|
|
1120
|
+
const Maybe: <E>(onNone: () => E) => <A>(maybe: Maybe<A>) => TaskValidation<E, A>;
|
|
1121
|
+
/**
|
|
1122
|
+
* Creates a Task.Validation from a Result.
|
|
1123
|
+
* Ok becomes Passed, Err(e) becomes Failed([e]).
|
|
1124
|
+
*/
|
|
1125
|
+
const Result: <E, A>(result: Result<E, A>) => TaskValidation<E, A>;
|
|
1126
|
+
}
|
|
1105
1127
|
/**
|
|
1106
1128
|
* Creates a Task.Validation from a Promise-returning function.
|
|
1107
1129
|
* Catches any errors and transforms them using the onError function.
|
|
@@ -1265,9 +1287,9 @@ declare namespace TaskValidation {
|
|
|
1265
1287
|
* ```
|
|
1266
1288
|
*
|
|
1267
1289
|
* When you need an explicit `Promise<A>` (e.g. for a third-party API), convert
|
|
1268
|
-
* the `Deferred` with `Deferred.
|
|
1290
|
+
* the `Deferred` with `Deferred.to.Promise`:
|
|
1269
1291
|
* ```ts
|
|
1270
|
-
* const p: Promise<number> = Deferred.
|
|
1292
|
+
* const p: Promise<number> = Deferred.to.Promise(task());
|
|
1271
1293
|
* ```
|
|
1272
1294
|
*
|
|
1273
1295
|
* @example
|
|
@@ -1296,27 +1318,29 @@ declare namespace Task {
|
|
|
1296
1318
|
* ```
|
|
1297
1319
|
*/
|
|
1298
1320
|
const resolve: <A>(value: A) => Task<A>;
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1321
|
+
namespace from {
|
|
1322
|
+
/**
|
|
1323
|
+
* Creates a Task from a function that returns a Promise.
|
|
1324
|
+
* The factory optionally receives an `AbortSignal` forwarded from the call site.
|
|
1325
|
+
*
|
|
1326
|
+
* @example
|
|
1327
|
+
* ```ts
|
|
1328
|
+
* const getTimestamp = Task.from.Promise(() => Promise.resolve(Date.now()));
|
|
1329
|
+
* ```
|
|
1330
|
+
*/
|
|
1331
|
+
const Promise: <A>(f: (signal?: AbortSignal) => Thenable<A>) => Task<A>;
|
|
1332
|
+
/**
|
|
1333
|
+
* Creates a Task from a lazy synchronous thunk.
|
|
1334
|
+
* Unlike `Task.resolve(f())`, `from.sync` does not evaluate `f` until the Task is called.
|
|
1335
|
+
*
|
|
1336
|
+
* @example
|
|
1337
|
+
* ```ts
|
|
1338
|
+
* const t = Task.from.sync(() => Date.now()); // Date.now() not called yet
|
|
1339
|
+
* const ts = await t(); // called here, every time
|
|
1340
|
+
* ```
|
|
1341
|
+
*/
|
|
1342
|
+
const sync: <A>(f: () => A) => Task<A>;
|
|
1343
|
+
}
|
|
1320
1344
|
/**
|
|
1321
1345
|
* Transforms the value inside a Task.
|
|
1322
1346
|
*
|
|
@@ -1439,8 +1463,8 @@ declare namespace Task {
|
|
|
1439
1463
|
*
|
|
1440
1464
|
* @example
|
|
1441
1465
|
* ```ts
|
|
1442
|
-
* const fast = Task.from(() => new Promise<string>(r => setTimeout(() => r("fast"), 10)));
|
|
1443
|
-
* const slow = Task.from(() => new Promise<string>(r => setTimeout(() => r("slow"), 200)));
|
|
1466
|
+
* const fast = Task.from.Promise(() => new Promise<string>(r => setTimeout(() => r("fast"), 10)));
|
|
1467
|
+
* const slow = Task.from.Promise(() => new Promise<string>(r => setTimeout(() => r("slow"), 200)));
|
|
1444
1468
|
*
|
|
1445
1469
|
* await Task.race([fast, slow])(); // "fast"
|
|
1446
1470
|
* ```
|
|
@@ -1464,7 +1488,7 @@ declare namespace Task {
|
|
|
1464
1488
|
* @example
|
|
1465
1489
|
* ```ts
|
|
1466
1490
|
* let log: number[] = [];
|
|
1467
|
-
* const makeTask = (n: number) => Task.from(() => {
|
|
1491
|
+
* const makeTask = (n: number) => Task.from.Promise(() => {
|
|
1468
1492
|
* log.push(n);
|
|
1469
1493
|
* return Promise.resolve(n);
|
|
1470
1494
|
* });
|
|
@@ -1571,14 +1595,14 @@ type Failed<E> = WithKind<"Failed"> & WithErrors<E>;
|
|
|
1571
1595
|
* @example
|
|
1572
1596
|
* ```ts
|
|
1573
1597
|
* const validateName = (name: string): Validation<string, string> =>
|
|
1574
|
-
* name.length > 0 ? Validation.passed(name) : Validation.failed("Name is required");
|
|
1598
|
+
* name.length > 0 ? Validation.make.passed(name) : Validation.make.failed("Name is required");
|
|
1575
1599
|
*
|
|
1576
1600
|
* const validateAge = (age: number): Validation<string, number> =>
|
|
1577
|
-
* age >= 0 ? Validation.passed(age) : Validation.failed("Age must be positive");
|
|
1601
|
+
* age >= 0 ? Validation.make.passed(age) : Validation.make.failed("Age must be positive");
|
|
1578
1602
|
*
|
|
1579
1603
|
* // Accumulates all errors using ap
|
|
1580
1604
|
* pipe(
|
|
1581
|
-
* Validation.passed((name: string) => (age: number) => ({ name, age })),
|
|
1605
|
+
* Validation.make.passed((name: string) => (age: number) => ({ name, age })),
|
|
1582
1606
|
* Validation.ap(validateName("")),
|
|
1583
1607
|
* Validation.ap(validateAge(-1))
|
|
1584
1608
|
* );
|
|
@@ -1587,88 +1611,107 @@ type Failed<E> = WithKind<"Failed"> & WithErrors<E>;
|
|
|
1587
1611
|
*/
|
|
1588
1612
|
type Validation<E, A> = Passed<A> | Failed<E>;
|
|
1589
1613
|
declare namespace Validation {
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1614
|
+
namespace make {
|
|
1615
|
+
/**
|
|
1616
|
+
* Wraps a value in a passed Validation.
|
|
1617
|
+
*
|
|
1618
|
+
* @example
|
|
1619
|
+
* ```ts
|
|
1620
|
+
* Validation.make.passed(42); // Passed(42)
|
|
1621
|
+
* ```
|
|
1622
|
+
*/
|
|
1623
|
+
const passed: <E, A>(value: A) => Validation<E, A>;
|
|
1624
|
+
/**
|
|
1625
|
+
* Creates a failed Validation from a single error.
|
|
1626
|
+
*
|
|
1627
|
+
* @example
|
|
1628
|
+
* ```ts
|
|
1629
|
+
* Validation.make.failed("Invalid input");
|
|
1630
|
+
* ```
|
|
1631
|
+
*/
|
|
1632
|
+
const failed: <E>(error: E) => Failed<E>;
|
|
1633
|
+
/**
|
|
1634
|
+
* Creates a failed Validation from multiple errors.
|
|
1635
|
+
*
|
|
1636
|
+
* @example
|
|
1637
|
+
* ```ts
|
|
1638
|
+
* Validation.make.failedAll(["Invalid input"]);
|
|
1639
|
+
* ```
|
|
1640
|
+
*/
|
|
1641
|
+
const failedAll: <E>(errors: NonEmptyArr<E>) => Failed<E>;
|
|
1642
|
+
}
|
|
1643
|
+
namespace is {
|
|
1644
|
+
/**
|
|
1645
|
+
* Type guard that checks if a Validation is passed.
|
|
1646
|
+
*/
|
|
1647
|
+
const passed: <E, A>(data: Validation<E, A>) => data is Passed<A>;
|
|
1648
|
+
/**
|
|
1649
|
+
* Type guard that checks if a Validation is failed.
|
|
1650
|
+
*/
|
|
1651
|
+
const failed: <E, A>(data: Validation<E, A>) => data is Failed<E>;
|
|
1652
|
+
}
|
|
1653
|
+
namespace from {
|
|
1654
|
+
/**
|
|
1655
|
+
* Creates a Validation from a predicate applied to a value.
|
|
1656
|
+
* Returns Passed if the predicate passes, Failed from `onFalse` otherwise.
|
|
1657
|
+
*
|
|
1658
|
+
* @example
|
|
1659
|
+
* ```ts
|
|
1660
|
+
* const validateName = Validation.from.Predicate(
|
|
1661
|
+
* (s: string) => s.length > 0,
|
|
1662
|
+
* () => "Name is required"
|
|
1663
|
+
* );
|
|
1664
|
+
*
|
|
1665
|
+
* validateName("Alice"); // Passed("Alice")
|
|
1666
|
+
* validateName(""); // Failed(["Name is required"])
|
|
1667
|
+
* ```
|
|
1668
|
+
*/
|
|
1669
|
+
const Predicate: <E, A>(pred: (a: A) => boolean, onFalse: (a: A) => E) => (a: A) => Validation<E, A>;
|
|
1670
|
+
/**
|
|
1671
|
+
* Creates a Validation from a nullable value.
|
|
1672
|
+
* If the value is null or undefined, returns Failed with the error from onNull.
|
|
1673
|
+
* Otherwise, returns Passed.
|
|
1674
|
+
*
|
|
1675
|
+
* @example
|
|
1676
|
+
* ```ts
|
|
1677
|
+
* pipe(null, Validation.from.nullable(() => "is null")); // Failed(["is null"])
|
|
1678
|
+
* pipe(42, Validation.from.nullable(() => "is null")); // Passed(42)
|
|
1679
|
+
* ```
|
|
1680
|
+
*/
|
|
1681
|
+
const nullable: <E>(onNull: () => E) => <A>(value: A | null | undefined) => Validation<E, A>;
|
|
1682
|
+
/**
|
|
1683
|
+
* Creates a Validation from a Maybe.
|
|
1684
|
+
* If the Maybe is None, returns Failed with the error from onNone.
|
|
1685
|
+
* Otherwise, returns Passed.
|
|
1686
|
+
*
|
|
1687
|
+
* @example
|
|
1688
|
+
* ```ts
|
|
1689
|
+
* pipe(Maybe.make.none(), Validation.from.Maybe(() => "is none")); // Failed(["is none"])
|
|
1690
|
+
* pipe(Maybe.make.some(42), Validation.from.Maybe(() => "is none")); // Passed(42)
|
|
1691
|
+
* ```
|
|
1692
|
+
*/
|
|
1693
|
+
const Maybe: <E>(onNone: () => E) => <A>(maybe: Maybe<A>) => Validation<E, A>;
|
|
1694
|
+
/**
|
|
1695
|
+
* Converts a `Result` to a `Validation`. `Ok` becomes `Passed`; `Err(e)` becomes `Failed([e])`.
|
|
1696
|
+
*
|
|
1697
|
+
* Useful when bridging from error-short-circuiting `Result` pipelines into
|
|
1698
|
+
* error-accumulating `Validation` pipelines.
|
|
1699
|
+
*
|
|
1700
|
+
* @example
|
|
1701
|
+
* ```ts
|
|
1702
|
+
* Validation.from.Result(Result.make.ok(42)); // Passed(42)
|
|
1703
|
+
* Validation.from.Result(Result.make.err("bad")); // Failed(["bad"])
|
|
1704
|
+
* ```
|
|
1705
|
+
*/
|
|
1706
|
+
const Result: <E, A>(data: Result<E, A>) => Validation<E, A>;
|
|
1707
|
+
}
|
|
1665
1708
|
/**
|
|
1666
1709
|
* Transforms the success value inside a Validation.
|
|
1667
1710
|
*
|
|
1668
1711
|
* @example
|
|
1669
1712
|
* ```ts
|
|
1670
|
-
* pipe(Validation.passed(5), Validation.map(n => n * 2)); // Passed(10)
|
|
1671
|
-
* pipe(Validation.failed("oops"), Validation.map(n => n * 2)); // Failed(["oops"])
|
|
1713
|
+
* pipe(Validation.make.passed(5), Validation.map(n => n * 2)); // Passed(10)
|
|
1714
|
+
* pipe(Validation.make.failed("oops"), Validation.map(n => n * 2)); // Failed(["oops"])
|
|
1672
1715
|
* ```
|
|
1673
1716
|
*/
|
|
1674
1717
|
const map: <A, B>(f: (a: A) => B) => <E>(data: Validation<E, A>) => Validation<E, B>;
|
|
@@ -1677,7 +1720,7 @@ declare namespace Validation {
|
|
|
1677
1720
|
*
|
|
1678
1721
|
* @example
|
|
1679
1722
|
* ```ts
|
|
1680
|
-
* pipe(Validation.failed("oops"), Validation.mapError(e => e.toUpperCase())); // Failed(["OOPS"])
|
|
1723
|
+
* pipe(Validation.make.failed("oops"), Validation.mapError(e => e.toUpperCase())); // Failed(["OOPS"])
|
|
1681
1724
|
* ```
|
|
1682
1725
|
*/
|
|
1683
1726
|
const mapError: <E, F, A>(f: (e: E) => F) => (data: Validation<E, A>) => Validation<F, A>;
|
|
@@ -1689,15 +1732,15 @@ declare namespace Validation {
|
|
|
1689
1732
|
* ```ts
|
|
1690
1733
|
* const add = (a: number) => (b: number) => a + b;
|
|
1691
1734
|
* pipe(
|
|
1692
|
-
* Validation.passed(add),
|
|
1693
|
-
* Validation.ap(Validation.passed(5)),
|
|
1694
|
-
* Validation.ap(Validation.passed(3))
|
|
1735
|
+
* Validation.make.passed(add),
|
|
1736
|
+
* Validation.ap(Validation.make.passed(5)),
|
|
1737
|
+
* Validation.ap(Validation.make.passed(3))
|
|
1695
1738
|
* ); // Passed(8)
|
|
1696
1739
|
*
|
|
1697
1740
|
* pipe(
|
|
1698
|
-
* Validation.passed(add),
|
|
1699
|
-
* Validation.ap(Validation.failed<string, number>("bad a")),
|
|
1700
|
-
* Validation.ap(Validation.failed<string, number>("bad b"))
|
|
1741
|
+
* Validation.make.passed(add),
|
|
1742
|
+
* Validation.ap(Validation.make.failed<string, number>("bad a")),
|
|
1743
|
+
* Validation.ap(Validation.make.failed<string, number>("bad b"))
|
|
1701
1744
|
* ); // Failed(["bad a", "bad b"])
|
|
1702
1745
|
* ```
|
|
1703
1746
|
*/
|
|
@@ -1708,7 +1751,7 @@ declare namespace Validation {
|
|
|
1708
1751
|
* @example
|
|
1709
1752
|
* ```ts
|
|
1710
1753
|
* pipe(
|
|
1711
|
-
* Validation.passed(42),
|
|
1754
|
+
* Validation.make.passed(42),
|
|
1712
1755
|
* Validation.fold(
|
|
1713
1756
|
* errors => `Errors: ${errors.join(", ")}`,
|
|
1714
1757
|
* value => `Value: ${value}`
|
|
@@ -1741,9 +1784,9 @@ declare namespace Validation {
|
|
|
1741
1784
|
*
|
|
1742
1785
|
* @example
|
|
1743
1786
|
* ```ts
|
|
1744
|
-
* pipe(Validation.passed(5), Validation.getOrElse(() => 0)); // 5
|
|
1745
|
-
* pipe(Validation.failed("oops"), Validation.getOrElse(() => 0)); // 0
|
|
1746
|
-
* pipe(Validation.failed("oops"), Validation.getOrElse(() => null)); // null — typed as number | null
|
|
1787
|
+
* pipe(Validation.make.passed(5), Validation.getOrElse(() => 0)); // 5
|
|
1788
|
+
* pipe(Validation.make.failed("oops"), Validation.getOrElse(() => 0)); // 0
|
|
1789
|
+
* pipe(Validation.make.failed("oops"), Validation.getOrElse(() => null)); // null — typed as number | null
|
|
1747
1790
|
* ```
|
|
1748
1791
|
*/
|
|
1749
1792
|
const getOrElse: <E, A, B>(defaultValue: () => B) => (data: Validation<E, A>) => A | B;
|
|
@@ -1753,7 +1796,7 @@ declare namespace Validation {
|
|
|
1753
1796
|
* @example
|
|
1754
1797
|
* ```ts
|
|
1755
1798
|
* pipe(
|
|
1756
|
-
* Validation.passed(5),
|
|
1799
|
+
* Validation.make.passed(5),
|
|
1757
1800
|
* Validation.tap(n => console.log("Value:", n)),
|
|
1758
1801
|
* Validation.map(n => n * 2)
|
|
1759
1802
|
* );
|
|
@@ -1767,7 +1810,7 @@ declare namespace Validation {
|
|
|
1767
1810
|
* @example
|
|
1768
1811
|
* ```ts
|
|
1769
1812
|
* pipe(
|
|
1770
|
-
* Validation.failed("Name required"),
|
|
1813
|
+
* Validation.make.failed("Name required"),
|
|
1771
1814
|
* Validation.tapError(errors => console.error("validation failed:", errors)),
|
|
1772
1815
|
* Validation.map(toUser)
|
|
1773
1816
|
* );
|
|
@@ -1787,47 +1830,36 @@ declare namespace Validation {
|
|
|
1787
1830
|
* @example
|
|
1788
1831
|
* ```ts
|
|
1789
1832
|
* pipe(
|
|
1790
|
-
* Validation.failed("field-error"),
|
|
1791
|
-
* Validation.recoverUnless(e => e === "fatal", () => Validation.passed(0))
|
|
1833
|
+
* Validation.make.failed("field-error"),
|
|
1834
|
+
* Validation.recoverUnless(e => e === "fatal", () => Validation.make.passed(0))
|
|
1792
1835
|
* ); // Passed(0)
|
|
1793
1836
|
* ```
|
|
1794
1837
|
*/
|
|
1795
1838
|
const recoverUnless: <E, A, B>(isBlocked: (e: E) => boolean, fallback: () => Validation<E, B>) => (data: Validation<E, A>) => Validation<E, A | B>;
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
*
|
|
1821
|
-
* Useful when bridging from error-short-circuiting `Result` pipelines into
|
|
1822
|
-
* error-accumulating `Validation` pipelines.
|
|
1823
|
-
*
|
|
1824
|
-
* @example
|
|
1825
|
-
* ```ts
|
|
1826
|
-
* Validation.fromResult(Result.ok(42)); // Passed(42)
|
|
1827
|
-
* Validation.fromResult(Result.err("bad")); // Failed(["bad"])
|
|
1828
|
-
* ```
|
|
1829
|
-
*/
|
|
1830
|
-
const fromResult: <E, A>(data: Result<E, A>) => Validation<E, A>;
|
|
1839
|
+
namespace to {
|
|
1840
|
+
/**
|
|
1841
|
+
* Converts a Validation to a Result.
|
|
1842
|
+
* Passed becomes Ok, Failed becomes Err with the accumulated error list.
|
|
1843
|
+
*
|
|
1844
|
+
* @example
|
|
1845
|
+
* ```ts
|
|
1846
|
+
* Validation.to.Result(Validation.make.passed(42)); // Ok(42)
|
|
1847
|
+
* Validation.to.Result(Validation.make.failed("oops")); // Err(["oops"])
|
|
1848
|
+
* ```
|
|
1849
|
+
*/
|
|
1850
|
+
const Result: <E, A>(data: Validation<E, A>) => Result<NonEmptyArr<E>, A>;
|
|
1851
|
+
/**
|
|
1852
|
+
* Converts a Validation to a Maybe. `Passed` becomes `Some`; `Failed` becomes `None`
|
|
1853
|
+
* (errors are discarded).
|
|
1854
|
+
*
|
|
1855
|
+
* @example
|
|
1856
|
+
* ```ts
|
|
1857
|
+
* Validation.to.Maybe(Validation.make.passed(42)); // Some(42)
|
|
1858
|
+
* Validation.to.Maybe(Validation.make.failed("bad")); // None
|
|
1859
|
+
* ```
|
|
1860
|
+
*/
|
|
1861
|
+
const Maybe: <E, A>(data: Validation<E, A>) => Maybe<A>;
|
|
1862
|
+
}
|
|
1831
1863
|
/**
|
|
1832
1864
|
* Combines two independent Validation instances into a tuple.
|
|
1833
1865
|
* If both are Passed, returns Passed with both values as a tuple.
|
|
@@ -1836,13 +1868,13 @@ declare namespace Validation {
|
|
|
1836
1868
|
* @example
|
|
1837
1869
|
* ```ts
|
|
1838
1870
|
* Validation.product(
|
|
1839
|
-
* Validation.passed("alice"),
|
|
1840
|
-
* Validation.passed(30)
|
|
1871
|
+
* Validation.make.passed("alice"),
|
|
1872
|
+
* Validation.make.passed(30)
|
|
1841
1873
|
* ); // Passed(["alice", 30])
|
|
1842
1874
|
*
|
|
1843
1875
|
* Validation.product(
|
|
1844
|
-
* Validation.failed("Name required"),
|
|
1845
|
-
* Validation.failed("Age must be >= 0")
|
|
1876
|
+
* Validation.make.failed("Name required"),
|
|
1877
|
+
* Validation.make.failed("Age must be >= 0")
|
|
1846
1878
|
* ); // Failed(["Name required", "Age must be >= 0"])
|
|
1847
1879
|
* ```
|
|
1848
1880
|
*/
|
|
@@ -1870,13 +1902,13 @@ declare namespace Validation {
|
|
|
1870
1902
|
* @example
|
|
1871
1903
|
* ```ts
|
|
1872
1904
|
* Validation.struct({
|
|
1873
|
-
* name: Validation.passed("Alice"),
|
|
1874
|
-
* age: Validation.passed(30)
|
|
1905
|
+
* name: Validation.make.passed("Alice"),
|
|
1906
|
+
* age: Validation.make.passed(30)
|
|
1875
1907
|
* }); // Passed({ name: "Alice", age: 30 })
|
|
1876
1908
|
*
|
|
1877
1909
|
* Validation.struct({
|
|
1878
|
-
* name: Validation.failed("Name required"),
|
|
1879
|
-
* age: Validation.failed("Age must be >= 0")
|
|
1910
|
+
* name: Validation.make.failed("Name required"),
|
|
1911
|
+
* age: Validation.make.failed("Age must be >= 0")
|
|
1880
1912
|
* }); // Failed(["Name required", "Age must be >= 0"])
|
|
1881
1913
|
* ```
|
|
1882
1914
|
*/
|