@nlozgachev/pipelined 0.12.0 → 0.13.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/LICENCE +28 -0
- package/README.md +1 -1
- package/{types/src/Types/NonEmptyList.d.ts → dist/NonEmptyList-BlGFjor5.d.mts} +4 -3
- package/dist/NonEmptyList-BlGFjor5.d.ts +30 -0
- package/dist/Task-BB8Wmc1J.d.ts +677 -0
- package/dist/Task-ChKyH0pF.d.mts +677 -0
- package/dist/chunk-DBIC62UV.mjs +6 -0
- package/dist/chunk-QPTGO5AS.mjs +150 -0
- package/dist/composition.d.mts +495 -0
- package/dist/composition.d.ts +495 -0
- package/dist/composition.js +188 -0
- package/dist/composition.mjs +135 -0
- package/dist/core.d.mts +2170 -0
- package/dist/core.d.ts +2170 -0
- package/dist/core.js +698 -0
- package/dist/core.mjs +517 -0
- package/dist/types.d.mts +54 -0
- package/{types/src/Types/Brand.d.ts → dist/types.d.ts} +6 -4
- package/dist/types.js +41 -0
- package/dist/types.mjs +14 -0
- package/dist/utils.d.mts +1285 -0
- package/dist/utils.d.ts +1285 -0
- package/dist/utils.js +722 -0
- package/dist/utils.mjs +553 -0
- package/package.json +63 -69
- package/esm/mod.js +0 -3
- package/esm/package.json +0 -3
- package/esm/src/Composition/compose.js +0 -3
- package/esm/src/Composition/converge.js +0 -3
- package/esm/src/Composition/curry.js +0 -42
- package/esm/src/Composition/flip.js +0 -20
- package/esm/src/Composition/flow.js +0 -8
- package/esm/src/Composition/fn.js +0 -85
- package/esm/src/Composition/index.js +0 -13
- package/esm/src/Composition/juxt.js +0 -3
- package/esm/src/Composition/memoize.js +0 -66
- package/esm/src/Composition/not.js +0 -25
- package/esm/src/Composition/on.js +0 -12
- package/esm/src/Composition/pipe.js +0 -3
- package/esm/src/Composition/tap.js +0 -33
- package/esm/src/Composition/uncurry.js +0 -32
- package/esm/src/Core/Deferred.js +0 -30
- package/esm/src/Core/InternalTypes.js +0 -1
- package/esm/src/Core/Lens.js +0 -98
- package/esm/src/Core/Logged.js +0 -111
- package/esm/src/Core/Option.js +0 -191
- package/esm/src/Core/Optional.js +0 -160
- package/esm/src/Core/Predicate.js +0 -133
- package/esm/src/Core/Reader.js +0 -134
- package/esm/src/Core/Refinement.js +0 -115
- package/esm/src/Core/RemoteData.js +0 -211
- package/esm/src/Core/Result.js +0 -170
- package/esm/src/Core/State.js +0 -181
- package/esm/src/Core/Task.js +0 -223
- package/esm/src/Core/TaskOption.js +0 -106
- package/esm/src/Core/TaskResult.js +0 -127
- package/esm/src/Core/TaskValidation.js +0 -128
- package/esm/src/Core/These.js +0 -245
- package/esm/src/Core/Tuple.js +0 -112
- package/esm/src/Core/Validation.js +0 -212
- package/esm/src/Core/index.js +0 -18
- package/esm/src/Types/Brand.js +0 -28
- package/esm/src/Types/NonEmptyList.js +0 -14
- package/esm/src/Types/index.js +0 -2
- package/esm/src/Utils/Arr.js +0 -570
- package/esm/src/Utils/Dict.js +0 -421
- package/esm/src/Utils/Num.js +0 -124
- package/esm/src/Utils/Rec.js +0 -241
- package/esm/src/Utils/Str.js +0 -134
- package/esm/src/Utils/Uniq.js +0 -265
- package/esm/src/Utils/index.js +0 -6
- package/script/mod.js +0 -19
- package/script/package.json +0 -3
- package/script/src/Composition/compose.js +0 -6
- package/script/src/Composition/converge.js +0 -6
- package/script/src/Composition/curry.js +0 -48
- package/script/src/Composition/flip.js +0 -24
- package/script/src/Composition/flow.js +0 -11
- package/script/src/Composition/fn.js +0 -98
- package/script/src/Composition/index.js +0 -29
- package/script/src/Composition/juxt.js +0 -6
- package/script/src/Composition/memoize.js +0 -71
- package/script/src/Composition/not.js +0 -29
- package/script/src/Composition/on.js +0 -16
- package/script/src/Composition/pipe.js +0 -6
- package/script/src/Composition/tap.js +0 -37
- package/script/src/Composition/uncurry.js +0 -38
- package/script/src/Core/Deferred.js +0 -33
- package/script/src/Core/InternalTypes.js +0 -2
- package/script/src/Core/Lens.js +0 -101
- package/script/src/Core/Logged.js +0 -114
- package/script/src/Core/Option.js +0 -194
- package/script/src/Core/Optional.js +0 -163
- package/script/src/Core/Predicate.js +0 -136
- package/script/src/Core/Reader.js +0 -137
- package/script/src/Core/Refinement.js +0 -118
- package/script/src/Core/RemoteData.js +0 -214
- package/script/src/Core/Result.js +0 -173
- package/script/src/Core/State.js +0 -184
- package/script/src/Core/Task.js +0 -226
- package/script/src/Core/TaskOption.js +0 -109
- package/script/src/Core/TaskResult.js +0 -130
- package/script/src/Core/TaskValidation.js +0 -131
- package/script/src/Core/These.js +0 -248
- package/script/src/Core/Tuple.js +0 -115
- package/script/src/Core/Validation.js +0 -215
- package/script/src/Core/index.js +0 -34
- package/script/src/Types/Brand.js +0 -31
- package/script/src/Types/NonEmptyList.js +0 -18
- package/script/src/Types/index.js +0 -18
- package/script/src/Utils/Arr.js +0 -573
- package/script/src/Utils/Dict.js +0 -424
- package/script/src/Utils/Num.js +0 -127
- package/script/src/Utils/Rec.js +0 -244
- package/script/src/Utils/Str.js +0 -137
- package/script/src/Utils/Uniq.js +0 -268
- package/script/src/Utils/index.js +0 -22
- package/types/mod.d.ts +0 -4
- package/types/mod.d.ts.map +0 -1
- package/types/src/Composition/compose.d.ts +0 -33
- package/types/src/Composition/compose.d.ts.map +0 -1
- package/types/src/Composition/converge.d.ts +0 -21
- package/types/src/Composition/converge.d.ts.map +0 -1
- package/types/src/Composition/curry.d.ts +0 -43
- package/types/src/Composition/curry.d.ts.map +0 -1
- package/types/src/Composition/flip.d.ts +0 -21
- package/types/src/Composition/flip.d.ts.map +0 -1
- package/types/src/Composition/flow.d.ts +0 -56
- package/types/src/Composition/flow.d.ts.map +0 -1
- package/types/src/Composition/fn.d.ts +0 -76
- package/types/src/Composition/fn.d.ts.map +0 -1
- package/types/src/Composition/index.d.ts +0 -14
- package/types/src/Composition/index.d.ts.map +0 -1
- package/types/src/Composition/juxt.d.ts +0 -18
- package/types/src/Composition/juxt.d.ts.map +0 -1
- package/types/src/Composition/memoize.d.ts +0 -46
- package/types/src/Composition/memoize.d.ts.map +0 -1
- package/types/src/Composition/not.d.ts +0 -26
- package/types/src/Composition/not.d.ts.map +0 -1
- package/types/src/Composition/on.d.ts +0 -13
- package/types/src/Composition/on.d.ts.map +0 -1
- package/types/src/Composition/pipe.d.ts +0 -56
- package/types/src/Composition/pipe.d.ts.map +0 -1
- package/types/src/Composition/tap.d.ts +0 -31
- package/types/src/Composition/tap.d.ts.map +0 -1
- package/types/src/Composition/uncurry.d.ts +0 -54
- package/types/src/Composition/uncurry.d.ts.map +0 -1
- package/types/src/Core/Deferred.d.ts +0 -49
- package/types/src/Core/Deferred.d.ts.map +0 -1
- package/types/src/Core/InternalTypes.d.ts +0 -23
- package/types/src/Core/InternalTypes.d.ts.map +0 -1
- package/types/src/Core/Lens.d.ts +0 -118
- package/types/src/Core/Lens.d.ts.map +0 -1
- package/types/src/Core/Logged.d.ts +0 -126
- package/types/src/Core/Logged.d.ts.map +0 -1
- package/types/src/Core/Option.d.ts +0 -209
- package/types/src/Core/Option.d.ts.map +0 -1
- package/types/src/Core/Optional.d.ts +0 -158
- package/types/src/Core/Optional.d.ts.map +0 -1
- package/types/src/Core/Predicate.d.ts +0 -161
- package/types/src/Core/Predicate.d.ts.map +0 -1
- package/types/src/Core/Reader.d.ts +0 -156
- package/types/src/Core/Reader.d.ts.map +0 -1
- package/types/src/Core/Refinement.d.ts +0 -138
- package/types/src/Core/Refinement.d.ts.map +0 -1
- package/types/src/Core/RemoteData.d.ts +0 -197
- package/types/src/Core/RemoteData.d.ts.map +0 -1
- package/types/src/Core/Result.d.ts +0 -182
- package/types/src/Core/Result.d.ts.map +0 -1
- package/types/src/Core/State.d.ts +0 -192
- package/types/src/Core/State.d.ts.map +0 -1
- package/types/src/Core/Task.d.ts +0 -219
- package/types/src/Core/Task.d.ts.map +0 -1
- package/types/src/Core/TaskOption.d.ts +0 -121
- package/types/src/Core/TaskOption.d.ts.map +0 -1
- package/types/src/Core/TaskResult.d.ts +0 -119
- package/types/src/Core/TaskResult.d.ts.map +0 -1
- package/types/src/Core/TaskValidation.d.ts +0 -144
- package/types/src/Core/TaskValidation.d.ts.map +0 -1
- package/types/src/Core/These.d.ts +0 -225
- package/types/src/Core/These.d.ts.map +0 -1
- package/types/src/Core/Tuple.d.ts +0 -129
- package/types/src/Core/Tuple.d.ts.map +0 -1
- package/types/src/Core/Validation.d.ts +0 -203
- package/types/src/Core/Validation.d.ts.map +0 -1
- package/types/src/Core/index.d.ts +0 -19
- package/types/src/Core/index.d.ts.map +0 -1
- package/types/src/Types/Brand.d.ts.map +0 -1
- package/types/src/Types/NonEmptyList.d.ts.map +0 -1
- package/types/src/Types/index.d.ts +0 -3
- package/types/src/Types/index.d.ts.map +0 -1
- package/types/src/Utils/Arr.d.ts +0 -403
- package/types/src/Utils/Arr.d.ts.map +0 -1
- package/types/src/Utils/Dict.d.ts +0 -310
- package/types/src/Utils/Dict.d.ts.map +0 -1
- package/types/src/Utils/Num.d.ts +0 -110
- package/types/src/Utils/Num.d.ts.map +0 -1
- package/types/src/Utils/Rec.d.ts +0 -159
- package/types/src/Utils/Rec.d.ts.map +0 -1
- package/types/src/Utils/Str.d.ts +0 -128
- package/types/src/Utils/Str.d.ts.map +0 -1
- package/types/src/Utils/Uniq.d.ts +0 -179
- package/types/src/Utils/Uniq.d.ts.map +0 -1
- package/types/src/Utils/index.d.ts +0 -7
- package/types/src/Utils/index.d.ts.map +0 -1
|
@@ -0,0 +1,677 @@
|
|
|
1
|
+
import { N as NonEmptyList } from './NonEmptyList-BlGFjor5.mjs';
|
|
2
|
+
|
|
3
|
+
declare const _deferred: unique symbol;
|
|
4
|
+
/**
|
|
5
|
+
* A nominally typed, one-shot async value that supports `await` but enforces infallibility.
|
|
6
|
+
*
|
|
7
|
+
* Two design choices work together to make the guarantee structural rather than documentary:
|
|
8
|
+
*
|
|
9
|
+
* - The phantom `[_deferred]` symbol makes the type **nominal**: only values produced by
|
|
10
|
+
* `Deferred.fromPromise` satisfy it. A plain object `{ then: ... }` does not.
|
|
11
|
+
* - The single-parameter `.then()` **excludes rejection handlers** by construction. There is
|
|
12
|
+
* no second argument to pass, so chaining and `.catch()` are impossible.
|
|
13
|
+
*
|
|
14
|
+
* This makes `Deferred<A>` the natural return type for `Task<A>`, which is guaranteed to
|
|
15
|
+
* never reject.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const value = await Deferred.fromPromise(Promise.resolve(42));
|
|
20
|
+
* // value === 42
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
type Deferred<A> = {
|
|
24
|
+
readonly [_deferred]: A;
|
|
25
|
+
readonly then: (onfulfilled: (value: A) => void) => void;
|
|
26
|
+
};
|
|
27
|
+
declare namespace Deferred {
|
|
28
|
+
/**
|
|
29
|
+
* Wraps a `Promise` into a `Deferred`, structurally excluding rejection handlers,
|
|
30
|
+
* `.catch()`, `.finally()`, and chainable `.then()`.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* const d = Deferred.fromPromise(Promise.resolve("hello"));
|
|
35
|
+
* const value = await d; // "hello"
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
const fromPromise: <A>(p: Promise<A>) => Deferred<A>;
|
|
39
|
+
/**
|
|
40
|
+
* Converts a `Deferred` back into a `Promise`.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const p = Deferred.toPromise(Deferred.fromPromise(Promise.resolve(42)));
|
|
45
|
+
* // p is Promise<42>
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
const toPromise: <A>(d: Deferred<A>) => Promise<A>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
type WithKind<K extends string> = {
|
|
52
|
+
readonly kind: K;
|
|
53
|
+
};
|
|
54
|
+
type WithValue<T> = {
|
|
55
|
+
readonly value: T;
|
|
56
|
+
};
|
|
57
|
+
type WithError<T> = {
|
|
58
|
+
readonly error: T;
|
|
59
|
+
};
|
|
60
|
+
type WithErrors<T> = {
|
|
61
|
+
readonly errors: NonEmptyList<T>;
|
|
62
|
+
};
|
|
63
|
+
type WithFirst<T> = {
|
|
64
|
+
readonly first: T;
|
|
65
|
+
};
|
|
66
|
+
type WithSecond<T> = {
|
|
67
|
+
readonly second: T;
|
|
68
|
+
};
|
|
69
|
+
type WithLog<T> = {
|
|
70
|
+
readonly log: ReadonlyArray<T>;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
type Ok<A> = WithKind<"Ok"> & WithValue<A>;
|
|
74
|
+
type Err<E> = WithKind<"Error"> & WithError<E>;
|
|
75
|
+
/**
|
|
76
|
+
* Result represents a value that can be one of two types: a success (Ok) or a failure (Err).
|
|
77
|
+
* Use Result when an operation can fail with a meaningful error value.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* const divide = (a: number, b: number): Result<string, number> =>
|
|
82
|
+
* b === 0 ? Result.err("Division by zero") : Result.ok(a / b);
|
|
83
|
+
*
|
|
84
|
+
* pipe(
|
|
85
|
+
* divide(10, 2),
|
|
86
|
+
* Result.map(n => n * 2),
|
|
87
|
+
* Result.getOrElse(() => 0)
|
|
88
|
+
* ); // 10
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
type Result<E, A> = Ok<A> | Err<E>;
|
|
92
|
+
declare namespace Result {
|
|
93
|
+
/**
|
|
94
|
+
* Creates a successful Result with the given value.
|
|
95
|
+
*/
|
|
96
|
+
const ok: <A>(value: A) => Ok<A>;
|
|
97
|
+
/**
|
|
98
|
+
* Creates a failed Result with the given error.
|
|
99
|
+
*/
|
|
100
|
+
const err: <E>(error: E) => Err<E>;
|
|
101
|
+
/**
|
|
102
|
+
* Type guard that checks if an Result is Ok.
|
|
103
|
+
*/
|
|
104
|
+
const isOk: <E, A>(data: Result<E, A>) => data is Ok<A>;
|
|
105
|
+
/**
|
|
106
|
+
* Type guard that checks if an Result is Err.
|
|
107
|
+
*/
|
|
108
|
+
const isErr: <E, A>(data: Result<E, A>) => data is Err<E>;
|
|
109
|
+
/**
|
|
110
|
+
* Creates an Result from a function that may throw.
|
|
111
|
+
* Catches any errors and transforms them using the onError function.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```ts
|
|
115
|
+
* const parseJson = (s: string): Result<string, unknown> =>
|
|
116
|
+
* Result.tryCatch(
|
|
117
|
+
* () => JSON.parse(s),
|
|
118
|
+
* (e) => `Parse error: ${e}`
|
|
119
|
+
* );
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
const tryCatch: <E, A>(f: () => A, onError: (e: unknown) => E) => Result<E, A>;
|
|
123
|
+
/**
|
|
124
|
+
* Transforms the success value inside an Result.
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* pipe(Result.ok(5), Result.map(n => n * 2)); // Ok(10)
|
|
129
|
+
* pipe(Result.err("error"), Result.map(n => n * 2)); // Err("error")
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
const map: <E, A, B>(f: (a: A) => B) => (data: Result<E, A>) => Result<E, B>;
|
|
133
|
+
/**
|
|
134
|
+
* Transforms the error value inside an Result.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* pipe(Result.err("oops"), Result.mapError(e => e.toUpperCase())); // Err("OOPS")
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
const mapError: <E, F, A>(f: (e: E) => F) => (data: Result<E, A>) => Result<F, A>;
|
|
142
|
+
/**
|
|
143
|
+
* Chains Result computations. If the first is Ok, passes the value to f.
|
|
144
|
+
* If the first is Err, propagates the error.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```ts
|
|
148
|
+
* const validatePositive = (n: number): Result<string, number> =>
|
|
149
|
+
* n > 0 ? Result.ok(n) : Result.err("Must be positive");
|
|
150
|
+
*
|
|
151
|
+
* pipe(Result.ok(5), Result.chain(validatePositive)); // Ok(5)
|
|
152
|
+
* pipe(Result.ok(-1), Result.chain(validatePositive)); // Err("Must be positive")
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
const chain: <E, A, B>(f: (a: A) => Result<E, B>) => (data: Result<E, A>) => Result<E, B>;
|
|
156
|
+
/**
|
|
157
|
+
* Extracts the value from an Result by providing handlers for both cases.
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```ts
|
|
161
|
+
* pipe(
|
|
162
|
+
* Result.ok(5),
|
|
163
|
+
* Result.fold(
|
|
164
|
+
* e => `Error: ${e}`,
|
|
165
|
+
* n => `Value: ${n}`
|
|
166
|
+
* )
|
|
167
|
+
* ); // "Value: 5"
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
const fold: <E, A, B>(onErr: (e: E) => B, onOk: (a: A) => B) => (data: Result<E, A>) => B;
|
|
171
|
+
/**
|
|
172
|
+
* Pattern matches on a Result, returning the result of the matching case.
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```ts
|
|
176
|
+
* pipe(
|
|
177
|
+
* result,
|
|
178
|
+
* Result.match({
|
|
179
|
+
* ok: value => `Got ${value}`,
|
|
180
|
+
* err: error => `Failed: ${error}`
|
|
181
|
+
* })
|
|
182
|
+
* );
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
const match: <E, A, B>(cases: {
|
|
186
|
+
ok: (a: A) => B;
|
|
187
|
+
err: (e: E) => B;
|
|
188
|
+
}) => (data: Result<E, A>) => B;
|
|
189
|
+
/**
|
|
190
|
+
* Returns the success value or a default value if the Result is an error.
|
|
191
|
+
* The default is a thunk `() => B` — evaluated only when the Result is Err.
|
|
192
|
+
* The default can be a different type, widening the result to `A | B`.
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```ts
|
|
196
|
+
* pipe(Result.ok(5), Result.getOrElse(() => 0)); // 5
|
|
197
|
+
* pipe(Result.err("error"), Result.getOrElse(() => 0)); // 0
|
|
198
|
+
* pipe(Result.err("error"), Result.getOrElse(() => null)); // null — typed as number | null
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
const getOrElse: <E, A, B>(defaultValue: () => B) => (data: Result<E, A>) => A | B;
|
|
202
|
+
/**
|
|
203
|
+
* Executes a side effect on the success value without changing the Result.
|
|
204
|
+
* Useful for logging or debugging.
|
|
205
|
+
*
|
|
206
|
+
* @example
|
|
207
|
+
* ```ts
|
|
208
|
+
* pipe(
|
|
209
|
+
* Result.ok(5),
|
|
210
|
+
* Result.tap(n => console.log("Value:", n)),
|
|
211
|
+
* Result.map(n => n * 2)
|
|
212
|
+
* );
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
const tap: <E, A>(f: (a: A) => void) => (data: Result<E, A>) => Result<E, A>;
|
|
216
|
+
/**
|
|
217
|
+
* Recovers from an error by providing a fallback Result.
|
|
218
|
+
* The fallback can produce a different success type, widening the result to `Result<E, A | B>`.
|
|
219
|
+
*/
|
|
220
|
+
const recover: <E, A, B>(fallback: (e: E) => Result<E, B>) => (data: Result<E, A>) => Result<E, A | B>;
|
|
221
|
+
/**
|
|
222
|
+
* Recovers from an error unless it matches the blocked error.
|
|
223
|
+
* The fallback can produce a different success type, widening the result to `Result<E, A | B>`.
|
|
224
|
+
*/
|
|
225
|
+
const recoverUnless: <E, A, B>(blockedErr: E, fallback: () => Result<E, B>) => (data: Result<E, A>) => Result<E, A | B>;
|
|
226
|
+
/**
|
|
227
|
+
* Converts a Result to an Option.
|
|
228
|
+
* Ok becomes Some, Err becomes None (the error is discarded).
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```ts
|
|
232
|
+
* Result.toOption(Result.ok(42)); // Some(42)
|
|
233
|
+
* Result.toOption(Result.err("oops")); // None
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
236
|
+
const toOption: <E, A>(data: Result<E, A>) => Option<A>;
|
|
237
|
+
/**
|
|
238
|
+
* Applies a function wrapped in an Result to a value wrapped in an Result.
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* ```ts
|
|
242
|
+
* const add = (a: number) => (b: number) => a + b;
|
|
243
|
+
* pipe(
|
|
244
|
+
* Result.ok(add),
|
|
245
|
+
* Result.ap(Result.ok(5)),
|
|
246
|
+
* Result.ap(Result.ok(3))
|
|
247
|
+
* ); // Ok(8)
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
const ap: <E, A>(arg: Result<E, A>) => <B>(data: Result<E, (a: A) => B>) => Result<E, B>;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
type Some<A> = WithKind<"Some"> & WithValue<A>;
|
|
254
|
+
type None = WithKind<"None">;
|
|
255
|
+
/**
|
|
256
|
+
* Option represents an optional value: every Option is either Some (contains a value) or None (empty).
|
|
257
|
+
* Use Option instead of null/undefined to make optionality explicit and composable.
|
|
258
|
+
*
|
|
259
|
+
* @example
|
|
260
|
+
* ```ts
|
|
261
|
+
* const findUser = (id: string): Option<User> =>
|
|
262
|
+
* users.has(id) ? Option.some(users.get(id)!) : Option.none();
|
|
263
|
+
*
|
|
264
|
+
* pipe(
|
|
265
|
+
* findUser("123"),
|
|
266
|
+
* Option.map(user => user.name),
|
|
267
|
+
* Option.getOrElse(() => "Unknown")
|
|
268
|
+
* );
|
|
269
|
+
* ```
|
|
270
|
+
*/
|
|
271
|
+
type Option<T> = Some<T> | None;
|
|
272
|
+
declare namespace Option {
|
|
273
|
+
/**
|
|
274
|
+
* Creates a Some containing the given value.
|
|
275
|
+
*/
|
|
276
|
+
const some: <A>(value: A) => Some<A>;
|
|
277
|
+
/**
|
|
278
|
+
* Type guard that checks if a Option is Some.
|
|
279
|
+
*/
|
|
280
|
+
const isSome: <A>(data: Option<A>) => data is Some<A>;
|
|
281
|
+
/**
|
|
282
|
+
* Creates a None (empty Option).
|
|
283
|
+
*/
|
|
284
|
+
const none: () => None;
|
|
285
|
+
/**
|
|
286
|
+
* Type guard that checks if a Option is None.
|
|
287
|
+
*/
|
|
288
|
+
const isNone: <A>(data: Option<A>) => data is None;
|
|
289
|
+
/**
|
|
290
|
+
* Creates a Option from a nullable value.
|
|
291
|
+
* Returns None if the value is null or undefined, Some otherwise.
|
|
292
|
+
*
|
|
293
|
+
* @example
|
|
294
|
+
* ```ts
|
|
295
|
+
* Option.fromNullable(null); // None
|
|
296
|
+
* Option.fromNullable(42); // Some(42)
|
|
297
|
+
* ```
|
|
298
|
+
*/
|
|
299
|
+
const fromNullable: <A>(value: A | null | undefined) => Option<A>;
|
|
300
|
+
/**
|
|
301
|
+
* Extracts the value from a Option, returning null if None.
|
|
302
|
+
*/
|
|
303
|
+
const toNullable: <A>(data: Option<A>) => A | null;
|
|
304
|
+
/**
|
|
305
|
+
* Extracts the value from a Option, returning undefined if None.
|
|
306
|
+
*/
|
|
307
|
+
const toUndefined: <A>(data: Option<A>) => A | undefined;
|
|
308
|
+
/**
|
|
309
|
+
* Creates a Option from a possibly undefined value.
|
|
310
|
+
* Returns None if undefined, Some otherwise.
|
|
311
|
+
*/
|
|
312
|
+
const fromUndefined: <A>(value: A | undefined) => Option<A>;
|
|
313
|
+
/**
|
|
314
|
+
* Converts an Option to a Result.
|
|
315
|
+
* Some becomes Ok, None becomes Err with the provided error.
|
|
316
|
+
*
|
|
317
|
+
* @example
|
|
318
|
+
* ```ts
|
|
319
|
+
* pipe(
|
|
320
|
+
* Option.some(42),
|
|
321
|
+
* Option.toResult(() => "Value was missing")
|
|
322
|
+
* ); // Ok(42)
|
|
323
|
+
*
|
|
324
|
+
* pipe(
|
|
325
|
+
* Option.none(),
|
|
326
|
+
* Option.toResult(() => "Value was missing")
|
|
327
|
+
* ); // Err("Value was missing")
|
|
328
|
+
* ```
|
|
329
|
+
*/
|
|
330
|
+
const toResult: <E>(onNone: () => E) => <A>(data: Option<A>) => Result<E, A>;
|
|
331
|
+
/**
|
|
332
|
+
* Creates an Option from a Result.
|
|
333
|
+
* Ok becomes Some, Err becomes None (the error is discarded).
|
|
334
|
+
*
|
|
335
|
+
* @example
|
|
336
|
+
* ```ts
|
|
337
|
+
* Option.fromResult(Result.ok(42)); // Some(42)
|
|
338
|
+
* Option.fromResult(Result.err("oops")); // None
|
|
339
|
+
* ```
|
|
340
|
+
*/
|
|
341
|
+
const fromResult: <E, A>(data: Result<E, A>) => Option<A>;
|
|
342
|
+
/**
|
|
343
|
+
* Transforms the value inside a Option if it exists.
|
|
344
|
+
*
|
|
345
|
+
* @example
|
|
346
|
+
* ```ts
|
|
347
|
+
* pipe(Option.some(5), Option.map(n => n * 2)); // Some(10)
|
|
348
|
+
* pipe(Option.none(), Option.map(n => n * 2)); // None
|
|
349
|
+
* ```
|
|
350
|
+
*/
|
|
351
|
+
const map: <A, B>(f: (a: A) => B) => (data: Option<A>) => Option<B>;
|
|
352
|
+
/**
|
|
353
|
+
* Chains Option computations. If the first is Some, passes the value to f.
|
|
354
|
+
* If the first is None, propagates None.
|
|
355
|
+
*
|
|
356
|
+
* @example
|
|
357
|
+
* ```ts
|
|
358
|
+
* const parseNumber = (s: string): Option<number> => {
|
|
359
|
+
* const n = parseInt(s, 10);
|
|
360
|
+
* return isNaN(n) ? Option.none() : Option.some(n);
|
|
361
|
+
* };
|
|
362
|
+
*
|
|
363
|
+
* pipe(Option.some("42"), Option.chain(parseNumber)); // Some(42)
|
|
364
|
+
* pipe(Option.some("abc"), Option.chain(parseNumber)); // None
|
|
365
|
+
* ```
|
|
366
|
+
*/
|
|
367
|
+
const chain: <A, B>(f: (a: A) => Option<B>) => (data: Option<A>) => Option<B>;
|
|
368
|
+
/**
|
|
369
|
+
* Extracts the value from a Option by providing handlers for both cases.
|
|
370
|
+
*
|
|
371
|
+
* @example
|
|
372
|
+
* ```ts
|
|
373
|
+
* pipe(
|
|
374
|
+
* Option.some(5),
|
|
375
|
+
* Option.fold(
|
|
376
|
+
* () => "No value",
|
|
377
|
+
* n => `Value: ${n}`
|
|
378
|
+
* )
|
|
379
|
+
* ); // "Value: 5"
|
|
380
|
+
* ```
|
|
381
|
+
*/
|
|
382
|
+
const fold: <A, B>(onNone: () => B, onSome: (a: A) => B) => (data: Option<A>) => B;
|
|
383
|
+
/**
|
|
384
|
+
* Pattern matches on a Option, returning the result of the matching case.
|
|
385
|
+
*
|
|
386
|
+
* @example
|
|
387
|
+
* ```ts
|
|
388
|
+
* pipe(
|
|
389
|
+
* optionUser,
|
|
390
|
+
* Option.match({
|
|
391
|
+
* some: user => `Hello, ${user.name}`,
|
|
392
|
+
* none: () => "Hello, stranger"
|
|
393
|
+
* })
|
|
394
|
+
* );
|
|
395
|
+
* ```
|
|
396
|
+
*/
|
|
397
|
+
const match: <A, B>(cases: {
|
|
398
|
+
none: () => B;
|
|
399
|
+
some: (a: A) => B;
|
|
400
|
+
}) => (data: Option<A>) => B;
|
|
401
|
+
/**
|
|
402
|
+
* Returns the value inside an Option, or a default value if None.
|
|
403
|
+
* The default is a thunk `() => B` — evaluated only when the Option is None.
|
|
404
|
+
* The default can be a different type, widening the result to `A | B`.
|
|
405
|
+
*
|
|
406
|
+
* @example
|
|
407
|
+
* ```ts
|
|
408
|
+
* pipe(Option.some(5), Option.getOrElse(() => 0)); // 5
|
|
409
|
+
* pipe(Option.none(), Option.getOrElse(() => 0)); // 0
|
|
410
|
+
* pipe(Option.none<string>(), Option.getOrElse(() => null)); // null — typed as string | null
|
|
411
|
+
* ```
|
|
412
|
+
*/
|
|
413
|
+
const getOrElse: <A, B>(defaultValue: () => B) => (data: Option<A>) => A | B;
|
|
414
|
+
/**
|
|
415
|
+
* Executes a side effect on the value without changing the Option.
|
|
416
|
+
* Useful for logging or debugging.
|
|
417
|
+
*
|
|
418
|
+
* @example
|
|
419
|
+
* ```ts
|
|
420
|
+
* pipe(
|
|
421
|
+
* Option.some(5),
|
|
422
|
+
* Option.tap(n => console.log("Value:", n)),
|
|
423
|
+
* Option.map(n => n * 2)
|
|
424
|
+
* );
|
|
425
|
+
* ```
|
|
426
|
+
*/
|
|
427
|
+
const tap: <A>(f: (a: A) => void) => (data: Option<A>) => Option<A>;
|
|
428
|
+
/**
|
|
429
|
+
* Filters a Option based on a predicate.
|
|
430
|
+
* Returns None if the predicate returns false or if the Option is already None.
|
|
431
|
+
*
|
|
432
|
+
* @example
|
|
433
|
+
* ```ts
|
|
434
|
+
* pipe(Option.some(5), Option.filter(n => n > 3)); // Some(5)
|
|
435
|
+
* pipe(Option.some(2), Option.filter(n => n > 3)); // None
|
|
436
|
+
* ```
|
|
437
|
+
*/
|
|
438
|
+
const filter: <A>(predicate: (a: A) => boolean) => (data: Option<A>) => Option<A>;
|
|
439
|
+
/**
|
|
440
|
+
* Recovers from a None by providing a fallback Option.
|
|
441
|
+
* The fallback can produce a different type, widening the result to `Option<A | B>`.
|
|
442
|
+
*/
|
|
443
|
+
const recover: <A, B>(fallback: () => Option<B>) => (data: Option<A>) => Option<A | B>;
|
|
444
|
+
/**
|
|
445
|
+
* Applies a function wrapped in a Option to a value wrapped in a Option.
|
|
446
|
+
*
|
|
447
|
+
* @example
|
|
448
|
+
* ```ts
|
|
449
|
+
* const add = (a: number) => (b: number) => a + b;
|
|
450
|
+
* pipe(
|
|
451
|
+
* Option.some(add),
|
|
452
|
+
* Option.ap(Option.some(5)),
|
|
453
|
+
* Option.ap(Option.some(3))
|
|
454
|
+
* ); // Some(8)
|
|
455
|
+
* ```
|
|
456
|
+
*/
|
|
457
|
+
const ap: <A>(arg: Option<A>) => <B>(data: Option<(a: A) => B>) => Option<B>;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* A lazy async computation that always resolves.
|
|
462
|
+
*
|
|
463
|
+
* Two guarantees:
|
|
464
|
+
* - **Lazy** — nothing starts until you call it.
|
|
465
|
+
* - **Infallible** — it never rejects. If failure is possible, encode it in the
|
|
466
|
+
* return type using `TaskResult<E, A>` instead.
|
|
467
|
+
*
|
|
468
|
+
* Calling a Task returns a `Deferred<A>` — a one-shot async value that supports
|
|
469
|
+
* `await` but has no `.catch()`, `.finally()`, or chainable `.then()`.
|
|
470
|
+
*
|
|
471
|
+
* **Consuming a Task:**
|
|
472
|
+
*
|
|
473
|
+
* Use `await task()` to run it and get the value directly:
|
|
474
|
+
* ```ts
|
|
475
|
+
* const value: number = await task();
|
|
476
|
+
* ```
|
|
477
|
+
*
|
|
478
|
+
* When you need an explicit `Promise<A>` (e.g. for a third-party API), convert
|
|
479
|
+
* the `Deferred` with `Deferred.toPromise`:
|
|
480
|
+
* ```ts
|
|
481
|
+
* const p: Promise<number> = Deferred.toPromise(task());
|
|
482
|
+
* ```
|
|
483
|
+
*
|
|
484
|
+
* @example
|
|
485
|
+
* ```ts
|
|
486
|
+
* const getTimestamp: Task<number> = Task.resolve(Date.now());
|
|
487
|
+
*
|
|
488
|
+
* // Nothing runs yet — getTimestamp is just a description
|
|
489
|
+
* const formatted = pipe(
|
|
490
|
+
* getTimestamp,
|
|
491
|
+
* Task.map(ts => new Date(ts).toISOString())
|
|
492
|
+
* );
|
|
493
|
+
*
|
|
494
|
+
* // Execute when ready
|
|
495
|
+
* const result = await formatted();
|
|
496
|
+
* ```
|
|
497
|
+
*/
|
|
498
|
+
type Task<A> = () => Deferred<A>;
|
|
499
|
+
declare namespace Task {
|
|
500
|
+
/**
|
|
501
|
+
* Creates a Task that immediately resolves to the given value.
|
|
502
|
+
*
|
|
503
|
+
* @example
|
|
504
|
+
* ```ts
|
|
505
|
+
* const task = Task.resolve(42);
|
|
506
|
+
* const value = await task(); // 42
|
|
507
|
+
* ```
|
|
508
|
+
*/
|
|
509
|
+
const resolve: <A>(value: A) => Task<A>;
|
|
510
|
+
/**
|
|
511
|
+
* Creates a Task from a function that returns a Promise.
|
|
512
|
+
*
|
|
513
|
+
* @example
|
|
514
|
+
* ```ts
|
|
515
|
+
* const getTimestamp = Task.from(() => Promise.resolve(Date.now()));
|
|
516
|
+
* ```
|
|
517
|
+
*/
|
|
518
|
+
const from: <A>(f: () => Promise<A>) => Task<A>;
|
|
519
|
+
/**
|
|
520
|
+
* Transforms the value inside a Task.
|
|
521
|
+
*
|
|
522
|
+
* @example
|
|
523
|
+
* ```ts
|
|
524
|
+
* pipe(
|
|
525
|
+
* Task.resolve(5),
|
|
526
|
+
* Task.map(n => n * 2)
|
|
527
|
+
* )(); // Deferred<10>
|
|
528
|
+
* ```
|
|
529
|
+
*/
|
|
530
|
+
const map: <A, B>(f: (a: A) => B) => (data: Task<A>) => Task<B>;
|
|
531
|
+
/**
|
|
532
|
+
* Chains Task computations. Passes the resolved value of the first Task to f.
|
|
533
|
+
*
|
|
534
|
+
* @example
|
|
535
|
+
* ```ts
|
|
536
|
+
* const readUserId: Task<string> = Task.resolve(session.userId);
|
|
537
|
+
* const loadPrefs = (id: string): Task<Preferences> =>
|
|
538
|
+
* Task.resolve(prefsCache.get(id));
|
|
539
|
+
*
|
|
540
|
+
* pipe(
|
|
541
|
+
* readUserId,
|
|
542
|
+
* Task.chain(loadPrefs)
|
|
543
|
+
* )(); // Deferred<Preferences>
|
|
544
|
+
* ```
|
|
545
|
+
*/
|
|
546
|
+
const chain: <A, B>(f: (a: A) => Task<B>) => (data: Task<A>) => Task<B>;
|
|
547
|
+
/**
|
|
548
|
+
* Applies a function wrapped in a Task to a value wrapped in a Task.
|
|
549
|
+
* Both Tasks run in parallel.
|
|
550
|
+
*
|
|
551
|
+
* @example
|
|
552
|
+
* ```ts
|
|
553
|
+
* const add = (a: number) => (b: number) => a + b;
|
|
554
|
+
* pipe(
|
|
555
|
+
* Task.resolve(add),
|
|
556
|
+
* Task.ap(Task.resolve(5)),
|
|
557
|
+
* Task.ap(Task.resolve(3))
|
|
558
|
+
* )(); // Deferred<8>
|
|
559
|
+
* ```
|
|
560
|
+
*/
|
|
561
|
+
const ap: <A>(arg: Task<A>) => <B>(data: Task<(a: A) => B>) => Task<B>;
|
|
562
|
+
/**
|
|
563
|
+
* Executes a side effect on the value without changing the Task.
|
|
564
|
+
* Useful for logging or debugging.
|
|
565
|
+
*
|
|
566
|
+
* @example
|
|
567
|
+
* ```ts
|
|
568
|
+
* pipe(
|
|
569
|
+
* loadConfig,
|
|
570
|
+
* Task.tap(cfg => console.log("Config:", cfg)),
|
|
571
|
+
* Task.map(buildReport)
|
|
572
|
+
* );
|
|
573
|
+
* ```
|
|
574
|
+
*/
|
|
575
|
+
const tap: <A>(f: (a: A) => void) => (data: Task<A>) => Task<A>;
|
|
576
|
+
/**
|
|
577
|
+
* Runs multiple Tasks in parallel and collects their results.
|
|
578
|
+
*
|
|
579
|
+
* @example
|
|
580
|
+
* ```ts
|
|
581
|
+
* Task.all([loadConfig, detectLocale, loadTheme])();
|
|
582
|
+
* // Deferred<[Config, string, Theme]>
|
|
583
|
+
* ```
|
|
584
|
+
*/
|
|
585
|
+
const all: <T extends readonly Task<unknown>[]>(tasks: T) => Task<{ [K in keyof T]: T[K] extends Task<infer A> ? A : never; }>;
|
|
586
|
+
/**
|
|
587
|
+
* Delays the execution of a Task by the specified milliseconds.
|
|
588
|
+
* Useful for debouncing or rate limiting.
|
|
589
|
+
*
|
|
590
|
+
* @example
|
|
591
|
+
* ```ts
|
|
592
|
+
* pipe(
|
|
593
|
+
* Task.resolve(42),
|
|
594
|
+
* Task.delay(1000)
|
|
595
|
+
* )(); // Resolves after 1 second
|
|
596
|
+
* ```
|
|
597
|
+
*/
|
|
598
|
+
const delay: (ms: number) => <A>(data: Task<A>) => Task<A>;
|
|
599
|
+
/**
|
|
600
|
+
* Runs a Task a fixed number of times sequentially, collecting all results into an array.
|
|
601
|
+
* An optional delay (ms) can be inserted between runs.
|
|
602
|
+
*
|
|
603
|
+
* @example
|
|
604
|
+
* ```ts
|
|
605
|
+
* pipe(
|
|
606
|
+
* pollSensor,
|
|
607
|
+
* Task.repeat({ times: 5, delay: 1000 })
|
|
608
|
+
* )(); // Task<Reading[]> — 5 readings, one per second
|
|
609
|
+
* ```
|
|
610
|
+
*/
|
|
611
|
+
const repeat: (options: {
|
|
612
|
+
times: number;
|
|
613
|
+
delay?: number;
|
|
614
|
+
}) => <A>(task: Task<A>) => Task<A[]>;
|
|
615
|
+
/**
|
|
616
|
+
* Runs a Task repeatedly until the result satisfies a predicate, returning that result.
|
|
617
|
+
* An optional delay (ms) can be inserted between runs.
|
|
618
|
+
*
|
|
619
|
+
* @example
|
|
620
|
+
* ```ts
|
|
621
|
+
* pipe(
|
|
622
|
+
* checkStatus,
|
|
623
|
+
* Task.repeatUntil({ when: (s) => s === "ready", delay: 500 })
|
|
624
|
+
* )(); // polls every 500ms until status is "ready"
|
|
625
|
+
* ```
|
|
626
|
+
*/
|
|
627
|
+
const repeatUntil: <A>(options: {
|
|
628
|
+
when: (a: A) => boolean;
|
|
629
|
+
delay?: number;
|
|
630
|
+
}) => (task: Task<A>) => Task<A>;
|
|
631
|
+
/**
|
|
632
|
+
* Resolves with the value of the first Task to complete. All Tasks start
|
|
633
|
+
* immediately; the rest are abandoned once one resolves.
|
|
634
|
+
*
|
|
635
|
+
* @example
|
|
636
|
+
* ```ts
|
|
637
|
+
* const fast = Task.from(() => new Promise<string>(r => setTimeout(() => r("fast"), 10)));
|
|
638
|
+
* const slow = Task.from(() => new Promise<string>(r => setTimeout(() => r("slow"), 200)));
|
|
639
|
+
*
|
|
640
|
+
* await Task.race([fast, slow])(); // "fast"
|
|
641
|
+
* ```
|
|
642
|
+
*/
|
|
643
|
+
const race: <A>(tasks: ReadonlyArray<Task<A>>) => Task<A>;
|
|
644
|
+
/**
|
|
645
|
+
* Runs an array of Tasks one at a time in order, collecting all results.
|
|
646
|
+
* Each Task starts only after the previous one resolves.
|
|
647
|
+
*
|
|
648
|
+
* @example
|
|
649
|
+
* ```ts
|
|
650
|
+
* let log: number[] = [];
|
|
651
|
+
* const makeTask = (n: number) => Task.from(() => {
|
|
652
|
+
* log.push(n);
|
|
653
|
+
* return Promise.resolve(n);
|
|
654
|
+
* });
|
|
655
|
+
*
|
|
656
|
+
* await Task.sequential([makeTask(1), makeTask(2), makeTask(3)])();
|
|
657
|
+
* // log = [1, 2, 3] — tasks ran in order
|
|
658
|
+
* ```
|
|
659
|
+
*/
|
|
660
|
+
const sequential: <A>(tasks: ReadonlyArray<Task<A>>) => Task<ReadonlyArray<A>>;
|
|
661
|
+
/**
|
|
662
|
+
* Converts a `Task<A>` into a `Task<Result<E, A>>`, resolving to `Err` if the
|
|
663
|
+
* Task does not complete within the given time.
|
|
664
|
+
*
|
|
665
|
+
* @example
|
|
666
|
+
* ```ts
|
|
667
|
+
* pipe(
|
|
668
|
+
* heavyComputation,
|
|
669
|
+
* Task.timeout(5000, () => "timed out"),
|
|
670
|
+
* TaskResult.chain(processResult)
|
|
671
|
+
* );
|
|
672
|
+
* ```
|
|
673
|
+
*/
|
|
674
|
+
const timeout: <E>(ms: number, onTimeout: () => E) => <A>(task: Task<A>) => Task<Result<E, A>>;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
export { Deferred as D, type Err as E, type None as N, Option as O, Result as R, type Some as S, Task as T, type WithValue as W, type WithLog as a, type WithKind as b, type WithError as c, type WithErrors as d, type WithFirst as e, type WithSecond as f, type Ok as g };
|