@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
package/dist/core.d.mts
ADDED
|
@@ -0,0 +1,2170 @@
|
|
|
1
|
+
import { O as Option, W as WithValue, a as WithLog, R as Result, b as WithKind, c as WithError, T as Task, d as WithErrors, e as WithFirst, f as WithSecond } from './Task-ChKyH0pF.mjs';
|
|
2
|
+
export { D as Deferred, E as Err, N as None, g as Ok, S as Some } from './Task-ChKyH0pF.mjs';
|
|
3
|
+
import { N as NonEmptyList } from './NonEmptyList-BlGFjor5.mjs';
|
|
4
|
+
|
|
5
|
+
/** Keys of T for which undefined is assignable (i.e. optional fields). */
|
|
6
|
+
type OptionalKeys<T> = {
|
|
7
|
+
[K in keyof T]-?: undefined extends T[K] ? K : never;
|
|
8
|
+
}[keyof T];
|
|
9
|
+
/**
|
|
10
|
+
* Optional<S, A> focuses on a value A inside a structure S that may or may
|
|
11
|
+
* not be present. Like a Lens, but get returns Option<A>.
|
|
12
|
+
*
|
|
13
|
+
* Compose with other Optionals via `andThen`, or with a Lens via `andThenLens`.
|
|
14
|
+
* Convert a Lens to an Optional with `Lens.toOptional`.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* type Profile = { username: string; bio?: string };
|
|
19
|
+
*
|
|
20
|
+
* const bioOpt = Optional.prop<Profile>()("bio");
|
|
21
|
+
*
|
|
22
|
+
* pipe(profile, Optional.get(bioOpt)); // Some("hello") or None
|
|
23
|
+
* pipe(profile, Optional.set(bioOpt)("hello")); // new Profile with bio set
|
|
24
|
+
* pipe(profile, Optional.modify(bioOpt)(s => s + "!")); // appends if present
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
type Optional<S, A> = {
|
|
28
|
+
readonly get: (s: S) => Option<A>;
|
|
29
|
+
readonly set: (a: A) => (s: S) => S;
|
|
30
|
+
};
|
|
31
|
+
declare namespace Optional {
|
|
32
|
+
/**
|
|
33
|
+
* Constructs an Optional from a getter (returning Option<A>) and a setter.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* const firstChar = Optional.make(
|
|
38
|
+
* (s: string) => s.length > 0 ? Option.some(s[0]) : Option.none(),
|
|
39
|
+
* (c) => (s) => s.length > 0 ? c + s.slice(1) : s,
|
|
40
|
+
* );
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
const make: <S, A>(get: (s: S) => Option<A>, set: (a: A) => (s: S) => S) => Optional<S, A>;
|
|
44
|
+
/**
|
|
45
|
+
* Creates an Optional that focuses on an optional property of an object.
|
|
46
|
+
* Only keys whose type includes undefined (i.e. `field?: T`) are accepted.
|
|
47
|
+
* Call with the structure type first, then the key.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* type Profile = { username: string; bio?: string };
|
|
52
|
+
* const bioOpt = Optional.prop<Profile>()("bio");
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
const prop: <S>() => <K extends OptionalKeys<S>>(key: K) => Optional<S, NonNullable<S[K]>>;
|
|
56
|
+
/**
|
|
57
|
+
* Creates an Optional that focuses on an element at a given index in an array.
|
|
58
|
+
* Returns None when the index is out of bounds; set is a no-op when out of bounds.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* const firstItem = Optional.index<string>(0);
|
|
63
|
+
*
|
|
64
|
+
* pipe(["a", "b"], Optional.get(firstItem)); // Some("a")
|
|
65
|
+
* pipe([], Optional.get(firstItem)); // None
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
const index: <A>(i: number) => Optional<A[], A>;
|
|
69
|
+
/**
|
|
70
|
+
* Reads the focused value from a structure, returning Option<A>.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* pipe(profile, Optional.get(bioOpt)); // Some("...") or None
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
const get: <S, A>(opt: Optional<S, A>) => (s: S) => Option<A>;
|
|
78
|
+
/**
|
|
79
|
+
* Replaces the focused value within a structure.
|
|
80
|
+
* For indexed focuses, this is a no-op when the index is out of bounds.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* pipe(profile, Optional.set(bioOpt)("hello"));
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
const set: <S, A>(opt: Optional<S, A>) => (a: A) => (s: S) => S;
|
|
88
|
+
/**
|
|
89
|
+
* Applies a function to the focused value if it is present; returns the
|
|
90
|
+
* structure unchanged if the focus is absent.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* pipe(profile, Optional.modify(bioOpt)(s => s.toUpperCase()));
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
const modify: <S, A>(opt: Optional<S, A>) => (f: (a: A) => A) => (s: S) => S;
|
|
98
|
+
/**
|
|
99
|
+
* Returns the focused value or a default when the focus is absent.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* pipe(profile, Optional.getOrElse(bioOpt)(() => "no bio"));
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
const getOrElse: <S, A>(opt: Optional<S, A>) => (defaultValue: () => A) => (s: S) => A;
|
|
107
|
+
/**
|
|
108
|
+
* Extracts a value from an Optional focus using handlers for the present
|
|
109
|
+
* and absent cases.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```ts
|
|
113
|
+
* pipe(profile, Optional.fold(bioOpt)(() => "no bio", (bio) => bio.toUpperCase()));
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
const fold: <S, A>(opt: Optional<S, A>) => <B>(onNone: () => B, onSome: (a: A) => B) => (s: S) => B;
|
|
117
|
+
/**
|
|
118
|
+
* Pattern matches on an Optional focus using a named-case object.
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```ts
|
|
122
|
+
* pipe(
|
|
123
|
+
* profile,
|
|
124
|
+
* Optional.match(bioOpt)({ none: () => "no bio", some: (bio) => bio }),
|
|
125
|
+
* );
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
const match: <S, A>(opt: Optional<S, A>) => <B>(cases: {
|
|
129
|
+
none: () => B;
|
|
130
|
+
some: (a: A) => B;
|
|
131
|
+
}) => (s: S) => B;
|
|
132
|
+
/**
|
|
133
|
+
* Composes two Optionals: focuses through the outer, then through the inner.
|
|
134
|
+
* Returns None if either focus is absent.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* const deepOpt = pipe(
|
|
139
|
+
* Optional.prop<User>()("address"),
|
|
140
|
+
* Optional.andThen(Optional.prop<Address>()("landmark")),
|
|
141
|
+
* );
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
const andThen: <A, B>(inner: Optional<A, B>) => <S>(outer: Optional<S, A>) => Optional<S, B>;
|
|
145
|
+
/**
|
|
146
|
+
* Composes an Optional with a Lens, producing an Optional.
|
|
147
|
+
* The Lens focuses within the value found by the Optional.
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```ts
|
|
151
|
+
* const cityOpt = pipe(
|
|
152
|
+
* Optional.prop<User>()("address"),
|
|
153
|
+
* Optional.andThenLens(Lens.prop<Address>()("city")),
|
|
154
|
+
* );
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
const andThenLens: <A, B>(inner: Lens<A, B>) => <S>(outer: Optional<S, A>) => Optional<S, B>;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Lens<S, A> focuses on a single value A inside a structure S, providing
|
|
162
|
+
* a composable way to read and immutably update nested data.
|
|
163
|
+
*
|
|
164
|
+
* A Lens always succeeds: the focused value is guaranteed to exist.
|
|
165
|
+
* For optional or indexed focuses, use Optional<S, A>.
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```ts
|
|
169
|
+
* type Address = { city: string; zip: string };
|
|
170
|
+
* type User = { name: string; address: Address };
|
|
171
|
+
*
|
|
172
|
+
* const addressLens = Lens.prop<User>()("address");
|
|
173
|
+
* const cityLens = Lens.prop<Address>()("city");
|
|
174
|
+
* const userCityLens = pipe(addressLens, Lens.andThen(cityLens));
|
|
175
|
+
*
|
|
176
|
+
* pipe(user, Lens.get(userCityLens)); // "Berlin"
|
|
177
|
+
* pipe(user, Lens.set(userCityLens)("Hamburg")); // new User with city updated
|
|
178
|
+
* pipe(user, Lens.modify(userCityLens)(c => c.toUpperCase())); // "BERLIN"
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
type Lens<S, A> = {
|
|
182
|
+
readonly get: (s: S) => A;
|
|
183
|
+
readonly set: (a: A) => (s: S) => S;
|
|
184
|
+
};
|
|
185
|
+
declare namespace Lens {
|
|
186
|
+
/**
|
|
187
|
+
* Constructs a Lens from a getter and a setter.
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```ts
|
|
191
|
+
* const nameLens = Lens.make(
|
|
192
|
+
* (user: User) => user.name,
|
|
193
|
+
* (name) => (user) => ({ ...user, name }),
|
|
194
|
+
* );
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
197
|
+
const make: <S, A>(get: (s: S) => A, set: (a: A) => (s: S) => S) => Lens<S, A>;
|
|
198
|
+
/**
|
|
199
|
+
* Creates a Lens that focuses on a property of an object.
|
|
200
|
+
* Call with the structure type first, then the key.
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* ```ts
|
|
204
|
+
* const nameLens = Lens.prop<User>()("name");
|
|
205
|
+
* ```
|
|
206
|
+
*/
|
|
207
|
+
const prop: <S>() => <K extends keyof S>(key: K) => Lens<S, S[K]>;
|
|
208
|
+
/**
|
|
209
|
+
* Reads the focused value from a structure.
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```ts
|
|
213
|
+
* pipe(user, Lens.get(nameLens)); // "Alice"
|
|
214
|
+
* ```
|
|
215
|
+
*/
|
|
216
|
+
const get: <S, A>(lens: Lens<S, A>) => (s: S) => A;
|
|
217
|
+
/**
|
|
218
|
+
* Replaces the focused value within a structure, returning a new structure.
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* ```ts
|
|
222
|
+
* pipe(user, Lens.set(nameLens)("Bob")); // new User with name "Bob"
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
225
|
+
const set: <S, A>(lens: Lens<S, A>) => (a: A) => (s: S) => S;
|
|
226
|
+
/**
|
|
227
|
+
* Applies a function to the focused value, returning a new structure.
|
|
228
|
+
*
|
|
229
|
+
* @example
|
|
230
|
+
* ```ts
|
|
231
|
+
* pipe(user, Lens.modify(nameLens)(n => n.toUpperCase())); // "ALICE"
|
|
232
|
+
* ```
|
|
233
|
+
*/
|
|
234
|
+
const modify: <S, A>(lens: Lens<S, A>) => (f: (a: A) => A) => (s: S) => S;
|
|
235
|
+
/**
|
|
236
|
+
* Composes two Lenses: focuses through the outer, then through the inner.
|
|
237
|
+
* Use in a pipe chain to build up a deep focus step by step.
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* ```ts
|
|
241
|
+
* const userCityLens = pipe(
|
|
242
|
+
* Lens.prop<User>()("address"),
|
|
243
|
+
* Lens.andThen(Lens.prop<Address>()("city")),
|
|
244
|
+
* );
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
247
|
+
const andThen: <A, B>(inner: Lens<A, B>) => <S>(outer: Lens<S, A>) => Lens<S, B>;
|
|
248
|
+
/**
|
|
249
|
+
* Composes a Lens with an Optional, producing an Optional.
|
|
250
|
+
* Use when the next step in the focus is optional (may be absent).
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```ts
|
|
254
|
+
* const userBioOpt = pipe(
|
|
255
|
+
* Lens.prop<User>()("profile"),
|
|
256
|
+
* Lens.andThenOptional(Optional.prop<Profile>()("bio")),
|
|
257
|
+
* );
|
|
258
|
+
* ```
|
|
259
|
+
*/
|
|
260
|
+
const andThenOptional: <A, B>(inner: Optional<A, B>) => <S>(outer: Lens<S, A>) => Optional<S, B>;
|
|
261
|
+
/**
|
|
262
|
+
* Converts a Lens to an Optional. Every Lens is a valid Optional
|
|
263
|
+
* whose get always returns Some.
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* ```ts
|
|
267
|
+
* pipe(
|
|
268
|
+
* Lens.prop<User>()("address"),
|
|
269
|
+
* Lens.toOptional,
|
|
270
|
+
* Optional.andThen(Optional.prop<Address>()("landmark")),
|
|
271
|
+
* );
|
|
272
|
+
* ```
|
|
273
|
+
*/
|
|
274
|
+
const toOptional: <S, A>(lens: Lens<S, A>) => Optional<S, A>;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* A value paired with an accumulated log.
|
|
279
|
+
*
|
|
280
|
+
* `Logged<W, A>` pairs a result `A` with a sequence of log entries `W`. When
|
|
281
|
+
* you sequence two `Logged` computations with `chain`, the logs are
|
|
282
|
+
* automatically concatenated — you never have to thread the log array through
|
|
283
|
+
* your code manually.
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* ```ts
|
|
287
|
+
* const program = pipe(
|
|
288
|
+
* Logged.make<string, number>(0),
|
|
289
|
+
* Logged.chain(n => pipe(
|
|
290
|
+
* Logged.tell("start"),
|
|
291
|
+
* Logged.map(() => n + 1),
|
|
292
|
+
* )),
|
|
293
|
+
* Logged.chain(n => pipe(
|
|
294
|
+
* Logged.tell("done"),
|
|
295
|
+
* Logged.map(() => n * 10),
|
|
296
|
+
* )),
|
|
297
|
+
* );
|
|
298
|
+
*
|
|
299
|
+
* Logged.run(program); // [10, ["start", "done"]]
|
|
300
|
+
* ```
|
|
301
|
+
*/
|
|
302
|
+
type Logged<L, A> = WithValue<A> & WithLog<L>;
|
|
303
|
+
declare namespace Logged {
|
|
304
|
+
/**
|
|
305
|
+
* Wraps a pure value into a `Logged` with an empty log.
|
|
306
|
+
*
|
|
307
|
+
* @example
|
|
308
|
+
* ```ts
|
|
309
|
+
* Logged.make<string, number>(42); // { value: 42, log: [] }
|
|
310
|
+
* ```
|
|
311
|
+
*/
|
|
312
|
+
const make: <W, A>(value: A) => Logged<W, A>;
|
|
313
|
+
/**
|
|
314
|
+
* Creates a `Logged` that records a single log entry and produces no
|
|
315
|
+
* meaningful value. Use this to append to the log inside a `chain`.
|
|
316
|
+
*
|
|
317
|
+
* @example
|
|
318
|
+
* ```ts
|
|
319
|
+
* Logged.tell("operation completed"); // { value: undefined, log: ["operation completed"] }
|
|
320
|
+
* ```
|
|
321
|
+
*/
|
|
322
|
+
const tell: <W>(entry: W) => Logged<W, undefined>;
|
|
323
|
+
/**
|
|
324
|
+
* Transforms the value inside a `Logged` without affecting the log.
|
|
325
|
+
*
|
|
326
|
+
* @example
|
|
327
|
+
* ```ts
|
|
328
|
+
* pipe(
|
|
329
|
+
* Logged.make<string, number>(5),
|
|
330
|
+
* Logged.map(n => n * 2),
|
|
331
|
+
* ); // { value: 10, log: [] }
|
|
332
|
+
* ```
|
|
333
|
+
*/
|
|
334
|
+
const map: <W, A, B>(f: (a: A) => B) => (data: Logged<W, A>) => Logged<W, B>;
|
|
335
|
+
/**
|
|
336
|
+
* Sequences two `Logged` computations, concatenating their logs.
|
|
337
|
+
* The value from the first is passed to `f`; the resulting log entries are
|
|
338
|
+
* appended after the entries from the first.
|
|
339
|
+
*
|
|
340
|
+
* Data-last — the first computation is the data being piped.
|
|
341
|
+
*
|
|
342
|
+
* @example
|
|
343
|
+
* ```ts
|
|
344
|
+
* const result = pipe(
|
|
345
|
+
* Logged.make<string, number>(1),
|
|
346
|
+
* Logged.chain(n => pipe(Logged.tell("step"), Logged.map(() => n + 1))),
|
|
347
|
+
* Logged.chain(n => pipe(Logged.tell("done"), Logged.map(() => n * 10))),
|
|
348
|
+
* );
|
|
349
|
+
*
|
|
350
|
+
* Logged.run(result); // [20, ["step", "done"]]
|
|
351
|
+
* ```
|
|
352
|
+
*/
|
|
353
|
+
const chain: <W, A, B>(f: (a: A) => Logged<W, B>) => (data: Logged<W, A>) => Logged<W, B>;
|
|
354
|
+
/**
|
|
355
|
+
* Applies a function wrapped in a `Logged` to a value wrapped in a `Logged`,
|
|
356
|
+
* concatenating both logs.
|
|
357
|
+
*
|
|
358
|
+
* @example
|
|
359
|
+
* ```ts
|
|
360
|
+
* const fn: Logged<string, (n: number) => number> = {
|
|
361
|
+
* value: n => n * 2,
|
|
362
|
+
* log: ["fn-loaded"],
|
|
363
|
+
* };
|
|
364
|
+
* const arg: Logged<string, number> = { value: 5, log: ["arg-loaded"] };
|
|
365
|
+
*
|
|
366
|
+
* const result = pipe(fn, Logged.ap(arg));
|
|
367
|
+
* Logged.run(result); // [10, ["fn-loaded", "arg-loaded"]]
|
|
368
|
+
* ```
|
|
369
|
+
*/
|
|
370
|
+
const ap: <W, A>(arg: Logged<W, A>) => <B>(data: Logged<W, (a: A) => B>) => Logged<W, B>;
|
|
371
|
+
/**
|
|
372
|
+
* Runs a side effect on the value without changing the `Logged`.
|
|
373
|
+
* Useful for debugging or inspecting intermediate values.
|
|
374
|
+
*
|
|
375
|
+
* @example
|
|
376
|
+
* ```ts
|
|
377
|
+
* pipe(
|
|
378
|
+
* Logged.make<string, number>(42),
|
|
379
|
+
* Logged.tap(n => console.log("value:", n)),
|
|
380
|
+
* );
|
|
381
|
+
* ```
|
|
382
|
+
*/
|
|
383
|
+
const tap: <W, A>(f: (a: A) => void) => (data: Logged<W, A>) => Logged<W, A>;
|
|
384
|
+
/**
|
|
385
|
+
* Extracts the value and log as a `readonly [A, ReadonlyArray<W>]` tuple.
|
|
386
|
+
* Use this at the boundary where you need to consume both.
|
|
387
|
+
*
|
|
388
|
+
* @example
|
|
389
|
+
* ```ts
|
|
390
|
+
* const result = pipe(
|
|
391
|
+
* Logged.make<string, number>(1),
|
|
392
|
+
* Logged.chain(n => pipe(Logged.tell("incremented"), Logged.map(() => n + 1))),
|
|
393
|
+
* );
|
|
394
|
+
*
|
|
395
|
+
* const [value, log] = Logged.run(result);
|
|
396
|
+
* // value = 2, log = ["incremented"]
|
|
397
|
+
* ```
|
|
398
|
+
*/
|
|
399
|
+
const run: <W, A>(data: Logged<W, A>) => readonly [A, ReadonlyArray<W>];
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* A function from `A` to `A is B` — a type predicate paired with a runtime check.
|
|
404
|
+
*
|
|
405
|
+
* A `Refinement<A, B>` proves at compile time that a value of type `A` is actually
|
|
406
|
+
* the narrower type `B extends A`, backed by a runtime boolean test. Use it to
|
|
407
|
+
* express domain invariants (non-empty strings, positive numbers, valid emails) as
|
|
408
|
+
* first-class, composable values rather than one-off type guards scattered across
|
|
409
|
+
* the codebase.
|
|
410
|
+
*
|
|
411
|
+
* @example
|
|
412
|
+
* ```ts
|
|
413
|
+
* type NonEmptyString = string & { readonly _tag: "NonEmptyString" };
|
|
414
|
+
*
|
|
415
|
+
* const isNonEmpty: Refinement<string, NonEmptyString> =
|
|
416
|
+
* Refinement.make(s => s.length > 0);
|
|
417
|
+
*
|
|
418
|
+
* pipe(
|
|
419
|
+
* "hello",
|
|
420
|
+
* Refinement.toFilter(isNonEmpty)
|
|
421
|
+
* ); // Some("hello")
|
|
422
|
+
* ```
|
|
423
|
+
*/
|
|
424
|
+
type Refinement<A, B extends A> = (a: A) => a is B;
|
|
425
|
+
declare namespace Refinement {
|
|
426
|
+
/**
|
|
427
|
+
* Creates a `Refinement<A, B>` from a plain boolean predicate.
|
|
428
|
+
*
|
|
429
|
+
* This is an unsafe cast — the caller is responsible for ensuring that the
|
|
430
|
+
* predicate truly characterises values of type `B`. Use this only when
|
|
431
|
+
* bootstrapping a new refinement; prefer `compose`, `and`, or `or` to build
|
|
432
|
+
* derived refinements from existing ones.
|
|
433
|
+
*
|
|
434
|
+
* @example
|
|
435
|
+
* ```ts
|
|
436
|
+
* type PositiveNumber = number & { readonly _tag: "PositiveNumber" };
|
|
437
|
+
*
|
|
438
|
+
* const isPositive: Refinement<number, PositiveNumber> =
|
|
439
|
+
* Refinement.make(n => n > 0);
|
|
440
|
+
* ```
|
|
441
|
+
*/
|
|
442
|
+
const make: <A, B extends A>(f: (a: A) => boolean) => Refinement<A, B>;
|
|
443
|
+
/**
|
|
444
|
+
* Chains two refinements: if `ab` narrows `A` to `B` and `bc` narrows `B` to `C`,
|
|
445
|
+
* the result narrows `A` directly to `C`.
|
|
446
|
+
*
|
|
447
|
+
* Data-last — the first refinement `ab` is the data being piped.
|
|
448
|
+
*
|
|
449
|
+
* @example
|
|
450
|
+
* ```ts
|
|
451
|
+
* type NonEmptyString = string & { readonly _tag: "NonEmpty" };
|
|
452
|
+
* type TrimmedString = NonEmptyString & { readonly _tag: "Trimmed" };
|
|
453
|
+
*
|
|
454
|
+
* const isNonEmpty: Refinement<string, NonEmptyString> =
|
|
455
|
+
* Refinement.make(s => s.length > 0);
|
|
456
|
+
* const isTrimmed: Refinement<NonEmptyString, TrimmedString> =
|
|
457
|
+
* Refinement.make(s => s === s.trim());
|
|
458
|
+
*
|
|
459
|
+
* const isNonEmptyTrimmed: Refinement<string, TrimmedString> = pipe(
|
|
460
|
+
* isNonEmpty,
|
|
461
|
+
* Refinement.compose(isTrimmed)
|
|
462
|
+
* );
|
|
463
|
+
* ```
|
|
464
|
+
*/
|
|
465
|
+
const compose: <A, B extends A, C extends B>(bc: Refinement<B, C>) => (ab: Refinement<A, B>) => Refinement<A, C>;
|
|
466
|
+
/**
|
|
467
|
+
* Intersects two refinements: the result narrows `A` to `B & C`, passing only
|
|
468
|
+
* when both refinements hold simultaneously.
|
|
469
|
+
*
|
|
470
|
+
* Data-last — the first refinement is the data being piped.
|
|
471
|
+
*
|
|
472
|
+
* @example
|
|
473
|
+
* ```ts
|
|
474
|
+
* const isString: Refinement<unknown, string> = Refinement.make(x => typeof x === "string");
|
|
475
|
+
* const isNonEmpty: Refinement<unknown, { length: number }> =
|
|
476
|
+
* Refinement.make(x => (x as any).length > 0);
|
|
477
|
+
*
|
|
478
|
+
* const isNonEmptyString = pipe(isString, Refinement.and(isNonEmpty));
|
|
479
|
+
* isNonEmptyString("hi"); // true
|
|
480
|
+
* isNonEmptyString(""); // false
|
|
481
|
+
* ```
|
|
482
|
+
*/
|
|
483
|
+
const and: <A, C extends A>(second: Refinement<A, C>) => <B extends A>(first: Refinement<A, B>) => Refinement<A, B & C>;
|
|
484
|
+
/**
|
|
485
|
+
* Unions two refinements: the result narrows `A` to `B | C`, passing when either
|
|
486
|
+
* refinement holds.
|
|
487
|
+
*
|
|
488
|
+
* Data-last — the first refinement is the data being piped.
|
|
489
|
+
*
|
|
490
|
+
* @example
|
|
491
|
+
* ```ts
|
|
492
|
+
* const isString: Refinement<unknown, string> = Refinement.make(x => typeof x === "string");
|
|
493
|
+
* const isNumber: Refinement<unknown, number> = Refinement.make(x => typeof x === "number");
|
|
494
|
+
*
|
|
495
|
+
* const isStringOrNumber = pipe(isString, Refinement.or(isNumber));
|
|
496
|
+
* isStringOrNumber("hi"); // true
|
|
497
|
+
* isStringOrNumber(42); // true
|
|
498
|
+
* isStringOrNumber(true); // false
|
|
499
|
+
* ```
|
|
500
|
+
*/
|
|
501
|
+
const or: <A, C extends A>(second: Refinement<A, C>) => <B extends A>(first: Refinement<A, B>) => Refinement<A, B | C>;
|
|
502
|
+
/**
|
|
503
|
+
* Converts a `Refinement<A, B>` into a function `(a: A) => Option<B>`.
|
|
504
|
+
*
|
|
505
|
+
* Returns `Some(a)` when the refinement holds, `None` otherwise. Useful for
|
|
506
|
+
* integrating runtime validation into an `Option`-based pipeline.
|
|
507
|
+
*
|
|
508
|
+
* @example
|
|
509
|
+
* ```ts
|
|
510
|
+
* type PositiveNumber = number & { readonly _tag: "Positive" };
|
|
511
|
+
* const isPositive: Refinement<number, PositiveNumber> =
|
|
512
|
+
* Refinement.make(n => n > 0);
|
|
513
|
+
*
|
|
514
|
+
* pipe(-1, Refinement.toFilter(isPositive)); // None
|
|
515
|
+
* pipe(42, Refinement.toFilter(isPositive)); // Some(42)
|
|
516
|
+
* ```
|
|
517
|
+
*/
|
|
518
|
+
const toFilter: <A, B extends A>(r: Refinement<A, B>) => (a: A) => Option<B>;
|
|
519
|
+
/**
|
|
520
|
+
* Converts a `Refinement<A, B>` into a function `(a: A) => Result<E, B>`.
|
|
521
|
+
*
|
|
522
|
+
* Returns `Ok(a)` when the refinement holds, `Err(onFail(a))` otherwise. Use
|
|
523
|
+
* this to surface validation failures as typed errors inside a `Result` pipeline.
|
|
524
|
+
*
|
|
525
|
+
* @example
|
|
526
|
+
* ```ts
|
|
527
|
+
* type NonEmptyString = string & { readonly _tag: "NonEmpty" };
|
|
528
|
+
* const isNonEmpty: Refinement<string, NonEmptyString> =
|
|
529
|
+
* Refinement.make(s => s.length > 0);
|
|
530
|
+
*
|
|
531
|
+
* pipe("", Refinement.toResult(isNonEmpty, () => "must not be empty")); // Err(...)
|
|
532
|
+
* pipe("hi", Refinement.toResult(isNonEmpty, () => "must not be empty")); // Ok("hi")
|
|
533
|
+
* ```
|
|
534
|
+
*/
|
|
535
|
+
const toResult: <A, B extends A, E>(r: Refinement<A, B>, onFail: (a: A) => E) => (a: A) => Result<E, B>;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* A boolean-valued function over a type `A`.
|
|
540
|
+
*
|
|
541
|
+
* A `Predicate<A>` is the simpler sibling of `Refinement<A, B>`: it tests whether a
|
|
542
|
+
* value satisfies a condition at runtime but carries no compile-time narrowing guarantee.
|
|
543
|
+
* Use it when you need to combine, negate, or adapt boolean checks as first-class values
|
|
544
|
+
* and do not require the extra type information that a `Refinement` provides.
|
|
545
|
+
*
|
|
546
|
+
* Every `Refinement<A, B>` is a `Predicate<A>` — convert with `Predicate.fromRefinement`
|
|
547
|
+
* when you want to compose a narrowing check alongside plain predicates.
|
|
548
|
+
*
|
|
549
|
+
* @example
|
|
550
|
+
* ```ts
|
|
551
|
+
* const isAdult: Predicate<number> = n => n >= 18;
|
|
552
|
+
* const isRetired: Predicate<number> = n => n >= 65;
|
|
553
|
+
*
|
|
554
|
+
* const isWorkingAge: Predicate<number> = pipe(
|
|
555
|
+
* isAdult,
|
|
556
|
+
* Predicate.and(Predicate.not(isRetired))
|
|
557
|
+
* );
|
|
558
|
+
*
|
|
559
|
+
* isWorkingAge(30); // true
|
|
560
|
+
* isWorkingAge(15); // false
|
|
561
|
+
* isWorkingAge(70); // false
|
|
562
|
+
* ```
|
|
563
|
+
*/
|
|
564
|
+
type Predicate<A> = (a: A) => boolean;
|
|
565
|
+
declare namespace Predicate {
|
|
566
|
+
/**
|
|
567
|
+
* Negates a predicate: the result passes exactly when the original fails.
|
|
568
|
+
*
|
|
569
|
+
* @example
|
|
570
|
+
* ```ts
|
|
571
|
+
* const isBlank: Predicate<string> = s => s.trim().length === 0;
|
|
572
|
+
* const isNotBlank = Predicate.not(isBlank);
|
|
573
|
+
*
|
|
574
|
+
* isNotBlank("hello"); // true
|
|
575
|
+
* isNotBlank(" "); // false
|
|
576
|
+
* ```
|
|
577
|
+
*/
|
|
578
|
+
const not: <A>(p: Predicate<A>) => Predicate<A>;
|
|
579
|
+
/**
|
|
580
|
+
* Combines two predicates with logical AND: passes only when both hold.
|
|
581
|
+
*
|
|
582
|
+
* Data-last — the first predicate is the data being piped.
|
|
583
|
+
*
|
|
584
|
+
* @example
|
|
585
|
+
* ```ts
|
|
586
|
+
* const isPositive: Predicate<number> = n => n > 0;
|
|
587
|
+
* const isEven: Predicate<number> = n => n % 2 === 0;
|
|
588
|
+
*
|
|
589
|
+
* const isPositiveEven: Predicate<number> = pipe(isPositive, Predicate.and(isEven));
|
|
590
|
+
*
|
|
591
|
+
* isPositiveEven(4); // true
|
|
592
|
+
* isPositiveEven(3); // false — positive but odd
|
|
593
|
+
* isPositiveEven(-2); // false — even but not positive
|
|
594
|
+
* ```
|
|
595
|
+
*/
|
|
596
|
+
const and: <A>(second: Predicate<A>) => (first: Predicate<A>) => Predicate<A>;
|
|
597
|
+
/**
|
|
598
|
+
* Combines two predicates with logical OR: passes when either holds.
|
|
599
|
+
*
|
|
600
|
+
* Data-last — the first predicate is the data being piped.
|
|
601
|
+
*
|
|
602
|
+
* @example
|
|
603
|
+
* ```ts
|
|
604
|
+
* const isChild: Predicate<number> = n => n < 13;
|
|
605
|
+
* const isSenior: Predicate<number> = n => n >= 65;
|
|
606
|
+
*
|
|
607
|
+
* const getsDiscount: Predicate<number> = pipe(isChild, Predicate.or(isSenior));
|
|
608
|
+
*
|
|
609
|
+
* getsDiscount(8); // true
|
|
610
|
+
* getsDiscount(70); // true
|
|
611
|
+
* getsDiscount(30); // false
|
|
612
|
+
* ```
|
|
613
|
+
*/
|
|
614
|
+
const or: <A>(second: Predicate<A>) => (first: Predicate<A>) => Predicate<A>;
|
|
615
|
+
/**
|
|
616
|
+
* Adapts a `Predicate<A>` to work on a different input type `B` by applying `f`
|
|
617
|
+
* to extract the relevant `A` from a `B` before running the check.
|
|
618
|
+
*
|
|
619
|
+
* Data-last — the predicate is the data being piped; `f` is the extractor.
|
|
620
|
+
*
|
|
621
|
+
* @example
|
|
622
|
+
* ```ts
|
|
623
|
+
* type User = { name: string; age: number };
|
|
624
|
+
*
|
|
625
|
+
* const isAdult: Predicate<number> = n => n >= 18;
|
|
626
|
+
*
|
|
627
|
+
* // Lift isAdult to work on Users by extracting the age field
|
|
628
|
+
* const isAdultUser: Predicate<User> = pipe(
|
|
629
|
+
* isAdult,
|
|
630
|
+
* Predicate.using((u: User) => u.age)
|
|
631
|
+
* );
|
|
632
|
+
*
|
|
633
|
+
* isAdultUser({ name: "Alice", age: 30 }); // true
|
|
634
|
+
* isAdultUser({ name: "Bob", age: 15 }); // false
|
|
635
|
+
* ```
|
|
636
|
+
*/
|
|
637
|
+
const using: <A, B>(f: (b: B) => A) => (p: Predicate<A>) => Predicate<B>;
|
|
638
|
+
/**
|
|
639
|
+
* Combines an array of predicates with AND: passes only when every predicate holds.
|
|
640
|
+
* Returns `true` for an empty array (vacuous truth).
|
|
641
|
+
*
|
|
642
|
+
* @example
|
|
643
|
+
* ```ts
|
|
644
|
+
* const checks: Predicate<string>[] = [
|
|
645
|
+
* s => s.length > 0,
|
|
646
|
+
* s => s.length <= 100,
|
|
647
|
+
* s => !s.includes("<"),
|
|
648
|
+
* ];
|
|
649
|
+
*
|
|
650
|
+
* Predicate.all(checks)("hello"); // true
|
|
651
|
+
* Predicate.all(checks)(""); // false — too short
|
|
652
|
+
* Predicate.all(checks)("<b>"); // false — contains "<"
|
|
653
|
+
* Predicate.all([])("anything"); // true
|
|
654
|
+
* ```
|
|
655
|
+
*/
|
|
656
|
+
const all: <A>(predicates: ReadonlyArray<Predicate<A>>) => Predicate<A>;
|
|
657
|
+
/**
|
|
658
|
+
* Combines an array of predicates with OR: passes when at least one holds.
|
|
659
|
+
* Returns `false` for an empty array.
|
|
660
|
+
*
|
|
661
|
+
* @example
|
|
662
|
+
* ```ts
|
|
663
|
+
* const acceptedFormats: Predicate<string>[] = [
|
|
664
|
+
* s => s.endsWith(".jpg"),
|
|
665
|
+
* s => s.endsWith(".png"),
|
|
666
|
+
* s => s.endsWith(".webp"),
|
|
667
|
+
* ];
|
|
668
|
+
*
|
|
669
|
+
* Predicate.any(acceptedFormats)("photo.jpg"); // true
|
|
670
|
+
* Predicate.any(acceptedFormats)("photo.gif"); // false
|
|
671
|
+
* Predicate.any([])("anything"); // false
|
|
672
|
+
* ```
|
|
673
|
+
*/
|
|
674
|
+
const any: <A>(predicates: ReadonlyArray<Predicate<A>>) => Predicate<A>;
|
|
675
|
+
/**
|
|
676
|
+
* Converts a `Refinement<A, B>` into a `Predicate<A>`, discarding the compile-time
|
|
677
|
+
* narrowing. Use this when you want to combine a type guard with plain predicates
|
|
678
|
+
* using `and`, `or`, or `all`.
|
|
679
|
+
*
|
|
680
|
+
* @example
|
|
681
|
+
* ```ts
|
|
682
|
+
* const isString: Refinement<unknown, string> =
|
|
683
|
+
* Refinement.make(x => typeof x === "string");
|
|
684
|
+
*
|
|
685
|
+
* const isShortString: Predicate<unknown> = pipe(
|
|
686
|
+
* Predicate.fromRefinement(isString),
|
|
687
|
+
* Predicate.and(x => (x as string).length < 10)
|
|
688
|
+
* );
|
|
689
|
+
*
|
|
690
|
+
* isShortString("hi"); // true
|
|
691
|
+
* isShortString("a very long string that exceeds ten characters"); // false
|
|
692
|
+
* isShortString(42); // false
|
|
693
|
+
* ```
|
|
694
|
+
*/
|
|
695
|
+
const fromRefinement: <A, B extends A>(r: Refinement<A, B>) => Predicate<A>;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
/**
|
|
699
|
+
* A computation that reads from a shared environment `R` and produces a value `A`.
|
|
700
|
+
* Use Reader to thread a dependency (config, logger, DB pool) through a pipeline
|
|
701
|
+
* without passing it explicitly to every function.
|
|
702
|
+
*
|
|
703
|
+
* @example
|
|
704
|
+
* ```ts
|
|
705
|
+
* type Config = { baseUrl: string; apiKey: string };
|
|
706
|
+
*
|
|
707
|
+
* const buildUrl = (path: string): Reader<Config, string> =>
|
|
708
|
+
* (config) => `${config.baseUrl}${path}`;
|
|
709
|
+
*
|
|
710
|
+
* const withAuth = (url: string): Reader<Config, string> =>
|
|
711
|
+
* (config) => `${url}?key=${config.apiKey}`;
|
|
712
|
+
*
|
|
713
|
+
* const fetchEndpoint = (path: string): Reader<Config, string> =>
|
|
714
|
+
* pipe(
|
|
715
|
+
* buildUrl(path),
|
|
716
|
+
* Reader.chain(withAuth)
|
|
717
|
+
* );
|
|
718
|
+
*
|
|
719
|
+
* // Inject the config once at the edge
|
|
720
|
+
* fetchEndpoint("/users")(appConfig); // "https://api.example.com/users?key=secret"
|
|
721
|
+
* ```
|
|
722
|
+
*/
|
|
723
|
+
type Reader<R, A> = (env: R) => A;
|
|
724
|
+
declare namespace Reader {
|
|
725
|
+
/**
|
|
726
|
+
* Lifts a pure value into a Reader. The environment is ignored.
|
|
727
|
+
*
|
|
728
|
+
* @example
|
|
729
|
+
* ```ts
|
|
730
|
+
* const always42: Reader<Config, number> = Reader.resolve(42);
|
|
731
|
+
* always42(anyConfig); // 42
|
|
732
|
+
* ```
|
|
733
|
+
*/
|
|
734
|
+
const resolve: <R, A>(value: A) => Reader<R, A>;
|
|
735
|
+
/**
|
|
736
|
+
* Returns the full environment as the result.
|
|
737
|
+
* The fundamental way to access the environment in a pipeline.
|
|
738
|
+
*
|
|
739
|
+
* @example
|
|
740
|
+
* ```ts
|
|
741
|
+
* pipe(
|
|
742
|
+
* Reader.ask<Config>(),
|
|
743
|
+
* Reader.map(config => config.baseUrl)
|
|
744
|
+
* )(appConfig); // "https://api.example.com"
|
|
745
|
+
* ```
|
|
746
|
+
*/
|
|
747
|
+
const ask: <R>() => Reader<R, R>;
|
|
748
|
+
/**
|
|
749
|
+
* Projects a value from the environment using a selector function.
|
|
750
|
+
* Equivalent to `pipe(Reader.ask(), Reader.map(f))` but more direct.
|
|
751
|
+
*
|
|
752
|
+
* @example
|
|
753
|
+
* ```ts
|
|
754
|
+
* const getBaseUrl: Reader<Config, string> = Reader.asks(c => c.baseUrl);
|
|
755
|
+
* getBaseUrl(appConfig); // "https://api.example.com"
|
|
756
|
+
* ```
|
|
757
|
+
*/
|
|
758
|
+
const asks: <R, A>(f: (env: R) => A) => Reader<R, A>;
|
|
759
|
+
/**
|
|
760
|
+
* Transforms the value produced by a Reader.
|
|
761
|
+
*
|
|
762
|
+
* @example
|
|
763
|
+
* ```ts
|
|
764
|
+
* pipe(
|
|
765
|
+
* Reader.asks((c: Config) => c.baseUrl),
|
|
766
|
+
* Reader.map(url => url.toUpperCase())
|
|
767
|
+
* )(appConfig); // "HTTPS://API.EXAMPLE.COM"
|
|
768
|
+
* ```
|
|
769
|
+
*/
|
|
770
|
+
const map: <R, A, B>(f: (a: A) => B) => (data: Reader<R, A>) => Reader<R, B>;
|
|
771
|
+
/**
|
|
772
|
+
* Sequences two Readers. Both see the same environment.
|
|
773
|
+
* The output of the first is passed to `f`, which returns the next Reader.
|
|
774
|
+
*
|
|
775
|
+
* @example
|
|
776
|
+
* ```ts
|
|
777
|
+
* const buildUrl = (path: string): Reader<Config, string> =>
|
|
778
|
+
* Reader.asks(c => `${c.baseUrl}${path}`);
|
|
779
|
+
*
|
|
780
|
+
* const addAuth = (url: string): Reader<Config, string> =>
|
|
781
|
+
* Reader.asks(c => `${url}?key=${c.apiKey}`);
|
|
782
|
+
*
|
|
783
|
+
* pipe(
|
|
784
|
+
* buildUrl("/items"),
|
|
785
|
+
* Reader.chain(addAuth)
|
|
786
|
+
* )(appConfig); // "https://api.example.com/items?key=secret"
|
|
787
|
+
* ```
|
|
788
|
+
*/
|
|
789
|
+
const chain: <R, A, B>(f: (a: A) => Reader<R, B>) => (data: Reader<R, A>) => Reader<R, B>;
|
|
790
|
+
/**
|
|
791
|
+
* Applies a function wrapped in a Reader to a value wrapped in a Reader.
|
|
792
|
+
* Both Readers see the same environment.
|
|
793
|
+
*
|
|
794
|
+
* @example
|
|
795
|
+
* ```ts
|
|
796
|
+
* const add = (a: number) => (b: number) => a + b;
|
|
797
|
+
* pipe(
|
|
798
|
+
* Reader.resolve<Config, typeof add>(add),
|
|
799
|
+
* Reader.ap(Reader.asks(c => c.timeout)),
|
|
800
|
+
* Reader.ap(Reader.resolve(5))
|
|
801
|
+
* )(appConfig);
|
|
802
|
+
* ```
|
|
803
|
+
*/
|
|
804
|
+
const ap: <R, A>(arg: Reader<R, A>) => <B>(data: Reader<R, (a: A) => B>) => Reader<R, B>;
|
|
805
|
+
/**
|
|
806
|
+
* Executes a side effect on the produced value without changing the Reader.
|
|
807
|
+
* Useful for logging or debugging inside a pipeline.
|
|
808
|
+
*
|
|
809
|
+
* @example
|
|
810
|
+
* ```ts
|
|
811
|
+
* pipe(
|
|
812
|
+
* buildUrl("/users"),
|
|
813
|
+
* Reader.tap(url => console.log("Requesting:", url)),
|
|
814
|
+
* Reader.chain(addAuth)
|
|
815
|
+
* )(appConfig);
|
|
816
|
+
* ```
|
|
817
|
+
*/
|
|
818
|
+
const tap: <R, A>(f: (a: A) => void) => (data: Reader<R, A>) => Reader<R, A>;
|
|
819
|
+
/**
|
|
820
|
+
* Adapts a Reader to work with a different (typically wider) environment
|
|
821
|
+
* by transforming the environment before passing it to the Reader.
|
|
822
|
+
* This lets you compose Readers that expect different environments.
|
|
823
|
+
*
|
|
824
|
+
* @example
|
|
825
|
+
* ```ts
|
|
826
|
+
* type AppEnv = { db: DbPool; config: Config; logger: Logger };
|
|
827
|
+
*
|
|
828
|
+
* // buildUrl only needs Config
|
|
829
|
+
* const buildUrl: Reader<Config, string> = Reader.asks(c => c.baseUrl);
|
|
830
|
+
*
|
|
831
|
+
* // Zoom in from AppEnv to Config
|
|
832
|
+
* const buildUrlFromApp: Reader<AppEnv, string> =
|
|
833
|
+
* pipe(buildUrl, Reader.local((env: AppEnv) => env.config));
|
|
834
|
+
*
|
|
835
|
+
* buildUrlFromApp(appEnv); // works with the full AppEnv
|
|
836
|
+
* ```
|
|
837
|
+
*/
|
|
838
|
+
const local: <R2, R>(f: (env: R2) => R) => <A>(data: Reader<R, A>) => Reader<R2, A>;
|
|
839
|
+
/**
|
|
840
|
+
* Runs a Reader by supplying the environment. Use this at the edge of your
|
|
841
|
+
* program where the environment is available.
|
|
842
|
+
*
|
|
843
|
+
* @example
|
|
844
|
+
* ```ts
|
|
845
|
+
* pipe(
|
|
846
|
+
* buildEndpoint("/users"),
|
|
847
|
+
* Reader.run(appConfig)
|
|
848
|
+
* ); // "https://api.example.com/users?key=secret"
|
|
849
|
+
* ```
|
|
850
|
+
*/
|
|
851
|
+
const run: <R>(env: R) => <A>(data: Reader<R, A>) => A;
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
type NotAsked = WithKind<"NotAsked">;
|
|
855
|
+
type Loading = WithKind<"Loading">;
|
|
856
|
+
type Failure<E> = WithKind<"Failure"> & WithError<E>;
|
|
857
|
+
type Success<A> = WithKind<"Success"> & WithValue<A>;
|
|
858
|
+
/**
|
|
859
|
+
* RemoteData represents the state of an async data fetch.
|
|
860
|
+
* It has four states: NotAsked, Loading, Failure, and Success.
|
|
861
|
+
*
|
|
862
|
+
* Use RemoteData to model data fetching states explicitly,
|
|
863
|
+
* replacing the common `{ data: T | null; loading: boolean; error: Error | null }` pattern.
|
|
864
|
+
*
|
|
865
|
+
* @example
|
|
866
|
+
* ```ts
|
|
867
|
+
* const renderUser = pipe(
|
|
868
|
+
* userData,
|
|
869
|
+
* RemoteData.match({
|
|
870
|
+
* notAsked: () => "Click to load",
|
|
871
|
+
* loading: () => "Loading...",
|
|
872
|
+
* failure: e => `Error: ${e.message}`,
|
|
873
|
+
* success: user => `Hello, ${user.name}!`
|
|
874
|
+
* })
|
|
875
|
+
* );
|
|
876
|
+
* ```
|
|
877
|
+
*/
|
|
878
|
+
type RemoteData<E, A> = NotAsked | Loading | Failure<E> | Success<A>;
|
|
879
|
+
declare namespace RemoteData {
|
|
880
|
+
/**
|
|
881
|
+
* Creates a NotAsked RemoteData.
|
|
882
|
+
*/
|
|
883
|
+
const notAsked: <E, A>() => RemoteData<E, A>;
|
|
884
|
+
/**
|
|
885
|
+
* Creates a Loading RemoteData.
|
|
886
|
+
*/
|
|
887
|
+
const loading: <E, A>() => RemoteData<E, A>;
|
|
888
|
+
/**
|
|
889
|
+
* Creates a Failure RemoteData with the given error.
|
|
890
|
+
*/
|
|
891
|
+
const failure: <E, A>(error: E) => RemoteData<E, A>;
|
|
892
|
+
/**
|
|
893
|
+
* Creates a Success RemoteData with the given value.
|
|
894
|
+
*/
|
|
895
|
+
const success: <E, A>(value: A) => RemoteData<E, A>;
|
|
896
|
+
/**
|
|
897
|
+
* Type guard that checks if a RemoteData is NotAsked.
|
|
898
|
+
*/
|
|
899
|
+
const isNotAsked: <E, A>(data: RemoteData<E, A>) => data is NotAsked;
|
|
900
|
+
/**
|
|
901
|
+
* Type guard that checks if a RemoteData is Loading.
|
|
902
|
+
*/
|
|
903
|
+
const isLoading: <E, A>(data: RemoteData<E, A>) => data is Loading;
|
|
904
|
+
/**
|
|
905
|
+
* Type guard that checks if a RemoteData is Failure.
|
|
906
|
+
*/
|
|
907
|
+
const isFailure: <E, A>(data: RemoteData<E, A>) => data is Failure<E>;
|
|
908
|
+
/**
|
|
909
|
+
* Type guard that checks if a RemoteData is Success.
|
|
910
|
+
*/
|
|
911
|
+
const isSuccess: <E, A>(data: RemoteData<E, A>) => data is Success<A>;
|
|
912
|
+
/**
|
|
913
|
+
* Transforms the success value inside a RemoteData.
|
|
914
|
+
*
|
|
915
|
+
* @example
|
|
916
|
+
* ```ts
|
|
917
|
+
* pipe(RemoteData.success(5), RemoteData.map(n => n * 2)); // Success(10)
|
|
918
|
+
* pipe(RemoteData.loading(), RemoteData.map(n => n * 2)); // Loading
|
|
919
|
+
* ```
|
|
920
|
+
*/
|
|
921
|
+
const map: <A, B>(f: (a: A) => B) => <E>(data: RemoteData<E, A>) => RemoteData<E, B>;
|
|
922
|
+
/**
|
|
923
|
+
* Transforms the error value inside a RemoteData.
|
|
924
|
+
*
|
|
925
|
+
* @example
|
|
926
|
+
* ```ts
|
|
927
|
+
* pipe(RemoteData.failure("oops"), RemoteData.mapError(e => e.toUpperCase())); // Failure("OOPS")
|
|
928
|
+
* ```
|
|
929
|
+
*/
|
|
930
|
+
const mapError: <E, F>(f: (e: E) => F) => <A>(data: RemoteData<E, A>) => RemoteData<F, A>;
|
|
931
|
+
/**
|
|
932
|
+
* Chains RemoteData computations. If the input is Success, passes the value to f.
|
|
933
|
+
* Otherwise, propagates the current state.
|
|
934
|
+
*
|
|
935
|
+
* @example
|
|
936
|
+
* ```ts
|
|
937
|
+
* pipe(
|
|
938
|
+
* RemoteData.success(5),
|
|
939
|
+
* RemoteData.chain(n => n > 0 ? RemoteData.success(n) : RemoteData.failure("negative"))
|
|
940
|
+
* );
|
|
941
|
+
* ```
|
|
942
|
+
*/
|
|
943
|
+
const chain: <E, A, B>(f: (a: A) => RemoteData<E, B>) => (data: RemoteData<E, A>) => RemoteData<E, B>;
|
|
944
|
+
/**
|
|
945
|
+
* Applies a function wrapped in a RemoteData to a value wrapped in a RemoteData.
|
|
946
|
+
*
|
|
947
|
+
* @example
|
|
948
|
+
* ```ts
|
|
949
|
+
* const add = (a: number) => (b: number) => a + b;
|
|
950
|
+
* pipe(
|
|
951
|
+
* RemoteData.success(add),
|
|
952
|
+
* RemoteData.ap(RemoteData.success(5)),
|
|
953
|
+
* RemoteData.ap(RemoteData.success(3))
|
|
954
|
+
* ); // Success(8)
|
|
955
|
+
* ```
|
|
956
|
+
*/
|
|
957
|
+
const ap: <E, A>(arg: RemoteData<E, A>) => <B>(data: RemoteData<E, (a: A) => B>) => RemoteData<E, B>;
|
|
958
|
+
/**
|
|
959
|
+
* Extracts the value from a RemoteData by providing handlers for all four cases.
|
|
960
|
+
*
|
|
961
|
+
* @example
|
|
962
|
+
* ```ts
|
|
963
|
+
* pipe(
|
|
964
|
+
* userData,
|
|
965
|
+
* RemoteData.fold(
|
|
966
|
+
* () => "Not asked",
|
|
967
|
+
* () => "Loading...",
|
|
968
|
+
* e => `Error: ${e}`,
|
|
969
|
+
* value => `Got: ${value}`
|
|
970
|
+
* )
|
|
971
|
+
* );
|
|
972
|
+
* ```
|
|
973
|
+
*/
|
|
974
|
+
const fold: <E, A, B>(onNotAsked: () => B, onLoading: () => B, onFailure: (e: E) => B, onSuccess: (a: A) => B) => (data: RemoteData<E, A>) => B;
|
|
975
|
+
/**
|
|
976
|
+
* Pattern matches on a RemoteData, returning the result of the matching case.
|
|
977
|
+
*
|
|
978
|
+
* @example
|
|
979
|
+
* ```ts
|
|
980
|
+
* pipe(
|
|
981
|
+
* userData,
|
|
982
|
+
* RemoteData.match({
|
|
983
|
+
* notAsked: () => "Click to load",
|
|
984
|
+
* loading: () => "Loading...",
|
|
985
|
+
* failure: e => `Error: ${e}`,
|
|
986
|
+
* success: user => `Hello, ${user.name}!`
|
|
987
|
+
* })
|
|
988
|
+
* );
|
|
989
|
+
* ```
|
|
990
|
+
*/
|
|
991
|
+
const match: <E, A, B>(cases: {
|
|
992
|
+
notAsked: () => B;
|
|
993
|
+
loading: () => B;
|
|
994
|
+
failure: (e: E) => B;
|
|
995
|
+
success: (a: A) => B;
|
|
996
|
+
}) => (data: RemoteData<E, A>) => B;
|
|
997
|
+
/**
|
|
998
|
+
* Returns the success value or a default value if the RemoteData is not Success.
|
|
999
|
+
* The default can be a different type, widening the result to `A | B`.
|
|
1000
|
+
*
|
|
1001
|
+
* @example
|
|
1002
|
+
* ```ts
|
|
1003
|
+
* pipe(RemoteData.success(5), RemoteData.getOrElse(() => 0)); // 5
|
|
1004
|
+
* pipe(RemoteData.loading(), RemoteData.getOrElse(() => 0)); // 0
|
|
1005
|
+
* pipe(RemoteData.loading<string, number>(), RemoteData.getOrElse(() => null)); // null — typed as number | null
|
|
1006
|
+
* ```
|
|
1007
|
+
*/
|
|
1008
|
+
const getOrElse: <E, A, B>(defaultValue: () => B) => (data: RemoteData<E, A>) => A | B;
|
|
1009
|
+
/**
|
|
1010
|
+
* Executes a side effect on the success value without changing the RemoteData.
|
|
1011
|
+
*
|
|
1012
|
+
* @example
|
|
1013
|
+
* ```ts
|
|
1014
|
+
* pipe(
|
|
1015
|
+
* RemoteData.success(5),
|
|
1016
|
+
* RemoteData.tap(n => console.log("Value:", n)),
|
|
1017
|
+
* RemoteData.map(n => n * 2)
|
|
1018
|
+
* );
|
|
1019
|
+
* ```
|
|
1020
|
+
*/
|
|
1021
|
+
const tap: <E, A>(f: (a: A) => void) => (data: RemoteData<E, A>) => RemoteData<E, A>;
|
|
1022
|
+
/**
|
|
1023
|
+
* Recovers from a Failure state by providing a fallback RemoteData.
|
|
1024
|
+
* The fallback can produce a different success type, widening the result to `RemoteData<E, A | B>`.
|
|
1025
|
+
*/
|
|
1026
|
+
const recover: <E, A, B>(fallback: (e: E) => RemoteData<E, B>) => (data: RemoteData<E, A>) => RemoteData<E, A | B>;
|
|
1027
|
+
/**
|
|
1028
|
+
* Converts a RemoteData to an Option.
|
|
1029
|
+
* Success becomes Some, all other states become None.
|
|
1030
|
+
*/
|
|
1031
|
+
const toOption: <E, A>(data: RemoteData<E, A>) => Option<A>;
|
|
1032
|
+
/**
|
|
1033
|
+
* Converts a RemoteData to a Result.
|
|
1034
|
+
* Success becomes Ok, Failure becomes Err.
|
|
1035
|
+
* NotAsked and Loading become Err with the provided fallback error.
|
|
1036
|
+
*
|
|
1037
|
+
* @example
|
|
1038
|
+
* ```ts
|
|
1039
|
+
* pipe(
|
|
1040
|
+
* RemoteData.success(42),
|
|
1041
|
+
* RemoteData.toResult(() => "not loaded")
|
|
1042
|
+
* ); // Ok(42)
|
|
1043
|
+
* ```
|
|
1044
|
+
*/
|
|
1045
|
+
const toResult: <E>(onNotReady: () => E) => <A>(data: RemoteData<E, A>) => Result<E, A>;
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
/**
|
|
1049
|
+
* A synchronous computation that threads a piece of mutable state `S` through
|
|
1050
|
+
* a pipeline without exposing mutation at call sites.
|
|
1051
|
+
*
|
|
1052
|
+
* At runtime a `State<S, A>` is just a function from an initial state to a pair
|
|
1053
|
+
* `[value, nextState]`. Nothing runs until you supply the initial state with
|
|
1054
|
+
* `State.run`, `State.evaluate`, or `State.execute`.
|
|
1055
|
+
*
|
|
1056
|
+
* @example
|
|
1057
|
+
* ```ts
|
|
1058
|
+
* type Counter = number;
|
|
1059
|
+
*
|
|
1060
|
+
* const increment: State<Counter, undefined> = State.modify(n => n + 1);
|
|
1061
|
+
* const getCount: State<Counter, Counter> = State.get();
|
|
1062
|
+
*
|
|
1063
|
+
* const program = pipe(
|
|
1064
|
+
* increment,
|
|
1065
|
+
* State.chain(() => increment),
|
|
1066
|
+
* State.chain(() => getCount),
|
|
1067
|
+
* );
|
|
1068
|
+
*
|
|
1069
|
+
* State.run(0)(program); // [2, 2] — value is 2, final state is 2
|
|
1070
|
+
* ```
|
|
1071
|
+
*/
|
|
1072
|
+
type State<S, A> = (s: S) => readonly [A, S];
|
|
1073
|
+
declare namespace State {
|
|
1074
|
+
/**
|
|
1075
|
+
* Lifts a pure value into a State computation. The state passes through unchanged.
|
|
1076
|
+
*
|
|
1077
|
+
* @example
|
|
1078
|
+
* ```ts
|
|
1079
|
+
* State.run(10)(State.resolve(42)); // [42, 10] — value 42, state unchanged
|
|
1080
|
+
* ```
|
|
1081
|
+
*/
|
|
1082
|
+
const resolve: <S, A>(value: A) => State<S, A>;
|
|
1083
|
+
/**
|
|
1084
|
+
* Produces the current state as the value, without modifying it.
|
|
1085
|
+
*
|
|
1086
|
+
* @example
|
|
1087
|
+
* ```ts
|
|
1088
|
+
* const readStack: State<string[], string[]> = State.get();
|
|
1089
|
+
* State.run(["a", "b"])(readStack); // [["a", "b"], ["a", "b"]]
|
|
1090
|
+
* ```
|
|
1091
|
+
*/
|
|
1092
|
+
const get: <S>() => State<S, S>;
|
|
1093
|
+
/**
|
|
1094
|
+
* Reads a projection of the state without modifying it.
|
|
1095
|
+
* Equivalent to `pipe(State.get(), State.map(f))` but more direct.
|
|
1096
|
+
*
|
|
1097
|
+
* @example
|
|
1098
|
+
* ```ts
|
|
1099
|
+
* type AppState = { count: number; label: string };
|
|
1100
|
+
* const readCount: State<AppState, number> = State.gets(s => s.count);
|
|
1101
|
+
* State.run({ count: 5, label: "x" })(readCount); // [5, { count: 5, label: "x" }]
|
|
1102
|
+
* ```
|
|
1103
|
+
*/
|
|
1104
|
+
const gets: <S, A>(f: (s: S) => A) => State<S, A>;
|
|
1105
|
+
/**
|
|
1106
|
+
* Replaces the current state with a new value. Produces no meaningful value.
|
|
1107
|
+
*
|
|
1108
|
+
* @example
|
|
1109
|
+
* ```ts
|
|
1110
|
+
* const reset: State<number, undefined> = State.put(0);
|
|
1111
|
+
* State.run(99)(reset); // [undefined, 0]
|
|
1112
|
+
* ```
|
|
1113
|
+
*/
|
|
1114
|
+
const put: <S>(newState: S) => State<S, undefined>;
|
|
1115
|
+
/**
|
|
1116
|
+
* Applies a function to the current state to produce the next state.
|
|
1117
|
+
* Produces no meaningful value.
|
|
1118
|
+
*
|
|
1119
|
+
* @example
|
|
1120
|
+
* ```ts
|
|
1121
|
+
* const push = (item: string): State<string[], undefined> =>
|
|
1122
|
+
* State.modify(stack => [...stack, item]);
|
|
1123
|
+
*
|
|
1124
|
+
* State.run(["a"])(push("b")); // [undefined, ["a", "b"]]
|
|
1125
|
+
* ```
|
|
1126
|
+
*/
|
|
1127
|
+
const modify: <S>(f: (s: S) => S) => State<S, undefined>;
|
|
1128
|
+
/**
|
|
1129
|
+
* Transforms the value produced by a State computation.
|
|
1130
|
+
* The state transformation is unchanged.
|
|
1131
|
+
*
|
|
1132
|
+
* @example
|
|
1133
|
+
* ```ts
|
|
1134
|
+
* const readLength: State<string[], number> = pipe(
|
|
1135
|
+
* State.get<string[]>(),
|
|
1136
|
+
* State.map(stack => stack.length),
|
|
1137
|
+
* );
|
|
1138
|
+
*
|
|
1139
|
+
* State.run(["a", "b", "c"])(readLength); // [3, ["a", "b", "c"]]
|
|
1140
|
+
* ```
|
|
1141
|
+
*/
|
|
1142
|
+
const map: <S, A, B>(f: (a: A) => B) => (st: State<S, A>) => State<S, B>;
|
|
1143
|
+
/**
|
|
1144
|
+
* Sequences two State computations. The state output of the first is passed
|
|
1145
|
+
* as the state input to the second.
|
|
1146
|
+
*
|
|
1147
|
+
* Data-last — the first computation is the data being piped.
|
|
1148
|
+
*
|
|
1149
|
+
* @example
|
|
1150
|
+
* ```ts
|
|
1151
|
+
* const push = (item: string): State<string[], undefined> =>
|
|
1152
|
+
* State.modify(stack => [...stack, item]);
|
|
1153
|
+
*
|
|
1154
|
+
* const program = pipe(
|
|
1155
|
+
* push("a"),
|
|
1156
|
+
* State.chain(() => push("b")),
|
|
1157
|
+
* State.chain(() => State.get<string[]>()),
|
|
1158
|
+
* );
|
|
1159
|
+
*
|
|
1160
|
+
* State.evaluate([])(program); // ["a", "b"]
|
|
1161
|
+
* ```
|
|
1162
|
+
*/
|
|
1163
|
+
const chain: <S, A, B>(f: (a: A) => State<S, B>) => (st: State<S, A>) => State<S, B>;
|
|
1164
|
+
/**
|
|
1165
|
+
* Applies a function wrapped in a State to a value wrapped in a State.
|
|
1166
|
+
* The function computation runs first; its output state is the input to the
|
|
1167
|
+
* argument computation.
|
|
1168
|
+
*
|
|
1169
|
+
* @example
|
|
1170
|
+
* ```ts
|
|
1171
|
+
* const addCounted = (n: number) => (m: number) => n + m;
|
|
1172
|
+
* const program = pipe(
|
|
1173
|
+
* State.resolve<number, typeof addCounted>(addCounted),
|
|
1174
|
+
* State.ap(State.gets((s: number) => s * 2)),
|
|
1175
|
+
* State.ap(State.gets((s: number) => s)),
|
|
1176
|
+
* );
|
|
1177
|
+
*
|
|
1178
|
+
* State.evaluate(3)(program); // 6 + 3 = 9
|
|
1179
|
+
* ```
|
|
1180
|
+
*/
|
|
1181
|
+
const ap: <S, A>(arg: State<S, A>) => <B>(fn: State<S, (a: A) => B>) => State<S, B>;
|
|
1182
|
+
/**
|
|
1183
|
+
* Runs a side effect on the produced value without changing the State computation.
|
|
1184
|
+
*
|
|
1185
|
+
* @example
|
|
1186
|
+
* ```ts
|
|
1187
|
+
* pipe(
|
|
1188
|
+
* State.get<number>(),
|
|
1189
|
+
* State.tap(n => console.log("current:", n)),
|
|
1190
|
+
* State.chain(() => State.modify(n => n + 1)),
|
|
1191
|
+
* );
|
|
1192
|
+
* ```
|
|
1193
|
+
*/
|
|
1194
|
+
const tap: <S, A>(f: (a: A) => void) => (st: State<S, A>) => State<S, A>;
|
|
1195
|
+
/**
|
|
1196
|
+
* Runs a State computation with an initial state, returning both the
|
|
1197
|
+
* produced value and the final state as a pair.
|
|
1198
|
+
*
|
|
1199
|
+
* Data-last — the computation is the data being piped.
|
|
1200
|
+
*
|
|
1201
|
+
* @example
|
|
1202
|
+
* ```ts
|
|
1203
|
+
* const program = pipe(
|
|
1204
|
+
* State.modify<number>(n => n + 1),
|
|
1205
|
+
* State.chain(() => State.get<number>()),
|
|
1206
|
+
* );
|
|
1207
|
+
*
|
|
1208
|
+
* State.run(0)(program); // [1, 1]
|
|
1209
|
+
* ```
|
|
1210
|
+
*/
|
|
1211
|
+
const run: <S>(initialState: S) => <A>(st: State<S, A>) => readonly [A, S];
|
|
1212
|
+
/**
|
|
1213
|
+
* Runs a State computation with an initial state, returning only the
|
|
1214
|
+
* produced value (discarding the final state).
|
|
1215
|
+
*
|
|
1216
|
+
* @example
|
|
1217
|
+
* ```ts
|
|
1218
|
+
* State.evaluate([])(pipe(
|
|
1219
|
+
* State.modify<string[]>(s => [...s, "x"]),
|
|
1220
|
+
* State.chain(() => State.get<string[]>()),
|
|
1221
|
+
* )); // ["x"]
|
|
1222
|
+
* ```
|
|
1223
|
+
*/
|
|
1224
|
+
const evaluate: <S>(initialState: S) => <A>(st: State<S, A>) => A;
|
|
1225
|
+
/**
|
|
1226
|
+
* Runs a State computation with an initial state, returning only the
|
|
1227
|
+
* final state (discarding the produced value).
|
|
1228
|
+
*
|
|
1229
|
+
* @example
|
|
1230
|
+
* ```ts
|
|
1231
|
+
* State.execute(0)(pipe(
|
|
1232
|
+
* State.modify<number>(n => n + 10),
|
|
1233
|
+
* State.chain(() => State.modify<number>(n => n * 2)),
|
|
1234
|
+
* )); // 20
|
|
1235
|
+
* ```
|
|
1236
|
+
*/
|
|
1237
|
+
const execute: <S>(initialState: S) => <A>(st: State<S, A>) => S;
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
/**
|
|
1241
|
+
* A Task that can fail with an error of type E or succeed with a value of type A.
|
|
1242
|
+
* Combines async operations with typed error handling.
|
|
1243
|
+
*
|
|
1244
|
+
* @example
|
|
1245
|
+
* ```ts
|
|
1246
|
+
* const fetchUser = (id: string): TaskResult<Error, User> =>
|
|
1247
|
+
* TaskResult.tryCatch(
|
|
1248
|
+
* () => fetch(`/users/${id}`).then(r => r.json()),
|
|
1249
|
+
* (e) => new Error(`Failed to fetch user: ${e}`)
|
|
1250
|
+
* );
|
|
1251
|
+
* ```
|
|
1252
|
+
*/
|
|
1253
|
+
type TaskResult<E, A> = Task<Result<E, A>>;
|
|
1254
|
+
declare namespace TaskResult {
|
|
1255
|
+
/**
|
|
1256
|
+
* Wraps a value in a successful TaskResult.
|
|
1257
|
+
*/
|
|
1258
|
+
const ok: <E, A>(value: A) => TaskResult<E, A>;
|
|
1259
|
+
/**
|
|
1260
|
+
* Creates a failed TaskResult with the given error.
|
|
1261
|
+
*/
|
|
1262
|
+
const err: <E, A>(error: E) => TaskResult<E, A>;
|
|
1263
|
+
/**
|
|
1264
|
+
* Creates a TaskResult from a function that may throw.
|
|
1265
|
+
* Catches any errors and transforms them using the onError function.
|
|
1266
|
+
*
|
|
1267
|
+
* @example
|
|
1268
|
+
* ```ts
|
|
1269
|
+
* const parseJson = (s: string): TaskResult<string, unknown> =>
|
|
1270
|
+
* TaskResult.tryCatch(
|
|
1271
|
+
* async () => JSON.parse(s),
|
|
1272
|
+
* (e) => `Parse error: ${e}`
|
|
1273
|
+
* );
|
|
1274
|
+
* ```
|
|
1275
|
+
*/
|
|
1276
|
+
const tryCatch: <E, A>(f: () => Promise<A>, onError: (e: unknown) => E) => TaskResult<E, A>;
|
|
1277
|
+
/**
|
|
1278
|
+
* Transforms the success value inside a TaskResult.
|
|
1279
|
+
*/
|
|
1280
|
+
const map: <E, A, B>(f: (a: A) => B) => (data: TaskResult<E, A>) => TaskResult<E, B>;
|
|
1281
|
+
/**
|
|
1282
|
+
* Transforms the error value inside a TaskResult.
|
|
1283
|
+
*/
|
|
1284
|
+
const mapError: <E, F, A>(f: (e: E) => F) => (data: TaskResult<E, A>) => TaskResult<F, A>;
|
|
1285
|
+
/**
|
|
1286
|
+
* Chains TaskResult computations. If the first succeeds, passes the value to f.
|
|
1287
|
+
* If the first fails, propagates the error.
|
|
1288
|
+
*/
|
|
1289
|
+
const chain: <E, A, B>(f: (a: A) => TaskResult<E, B>) => (data: TaskResult<E, A>) => TaskResult<E, B>;
|
|
1290
|
+
/**
|
|
1291
|
+
* Extracts the value from a TaskResult by providing handlers for both cases.
|
|
1292
|
+
*/
|
|
1293
|
+
const fold: <E, A, B>(onErr: (e: E) => B, onOk: (a: A) => B) => (data: TaskResult<E, A>) => Task<B>;
|
|
1294
|
+
/**
|
|
1295
|
+
* Pattern matches on a TaskResult, returning a Task of the result.
|
|
1296
|
+
*/
|
|
1297
|
+
const match: <E, A, B>(cases: {
|
|
1298
|
+
err: (e: E) => B;
|
|
1299
|
+
ok: (a: A) => B;
|
|
1300
|
+
}) => (data: TaskResult<E, A>) => Task<B>;
|
|
1301
|
+
/**
|
|
1302
|
+
* Recovers from an error by providing a fallback TaskResult.
|
|
1303
|
+
* The fallback can produce a different success type, widening the result to `TaskResult<E, A | B>`.
|
|
1304
|
+
*/
|
|
1305
|
+
const recover: <E, A, B>(fallback: (e: E) => TaskResult<E, B>) => (data: TaskResult<E, A>) => TaskResult<E, A | B>;
|
|
1306
|
+
/**
|
|
1307
|
+
* Returns the success value or a default value if the TaskResult is an error.
|
|
1308
|
+
* The default can be a different type, widening the result to `Task<A | B>`.
|
|
1309
|
+
*/
|
|
1310
|
+
const getOrElse: <E, A, B>(defaultValue: () => B) => (data: TaskResult<E, A>) => Task<A | B>;
|
|
1311
|
+
/**
|
|
1312
|
+
* Executes a side effect on the success value without changing the TaskResult.
|
|
1313
|
+
* Useful for logging or debugging.
|
|
1314
|
+
*/
|
|
1315
|
+
const tap: <E, A>(f: (a: A) => void) => (data: TaskResult<E, A>) => TaskResult<E, A>;
|
|
1316
|
+
/**
|
|
1317
|
+
* Re-runs a TaskResult on `Err` with configurable attempts, backoff, and retry condition.
|
|
1318
|
+
*
|
|
1319
|
+
* @param options.attempts - Total number of attempts (1 = no retry, 3 = up to 3 tries)
|
|
1320
|
+
* @param options.backoff - Fixed delay in ms, or a function `(attempt) => ms` for computed delay
|
|
1321
|
+
* @param options.when - Only retry when this returns true; defaults to always retry on Err
|
|
1322
|
+
*
|
|
1323
|
+
* @example
|
|
1324
|
+
* ```ts
|
|
1325
|
+
* // Retry up to 3 times with exponential backoff
|
|
1326
|
+
* pipe(
|
|
1327
|
+
* fetchUser,
|
|
1328
|
+
* TaskResult.retry({ attempts: 3, backoff: n => n * 1000 })
|
|
1329
|
+
* );
|
|
1330
|
+
*
|
|
1331
|
+
* // Only retry on network errors, not auth errors
|
|
1332
|
+
* pipe(
|
|
1333
|
+
* fetchUser,
|
|
1334
|
+
* TaskResult.retry({ attempts: 3, when: e => e instanceof NetworkError })
|
|
1335
|
+
* );
|
|
1336
|
+
* ```
|
|
1337
|
+
*/
|
|
1338
|
+
const retry: <E>(options: {
|
|
1339
|
+
attempts: number;
|
|
1340
|
+
backoff?: number | ((attempt: number) => number);
|
|
1341
|
+
when?: (error: E) => boolean;
|
|
1342
|
+
}) => <A>(data: TaskResult<E, A>) => TaskResult<E, A>;
|
|
1343
|
+
/**
|
|
1344
|
+
* Fails a TaskResult with a typed error if it does not resolve within the given time.
|
|
1345
|
+
*
|
|
1346
|
+
* @example
|
|
1347
|
+
* ```ts
|
|
1348
|
+
* pipe(
|
|
1349
|
+
* fetchUser,
|
|
1350
|
+
* TaskResult.timeout(5000, () => new TimeoutError("fetch user timed out"))
|
|
1351
|
+
* );
|
|
1352
|
+
* ```
|
|
1353
|
+
*/
|
|
1354
|
+
const timeout: <E>(ms: number, onTimeout: () => E) => <A>(data: TaskResult<E, A>) => TaskResult<E, A>;
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1357
|
+
/**
|
|
1358
|
+
* A Task that resolves to an optional value.
|
|
1359
|
+
* Combines async operations with the Option type for values that may not exist.
|
|
1360
|
+
*
|
|
1361
|
+
* @example
|
|
1362
|
+
* ```ts
|
|
1363
|
+
* const findUser = (id: string): TaskOption<User> =>
|
|
1364
|
+
* TaskOption.tryCatch(() => db.users.findById(id));
|
|
1365
|
+
*
|
|
1366
|
+
* pipe(
|
|
1367
|
+
* findUser("123"),
|
|
1368
|
+
* TaskOption.map(user => user.name),
|
|
1369
|
+
* TaskOption.getOrElse(() => "Unknown")
|
|
1370
|
+
* )();
|
|
1371
|
+
* ```
|
|
1372
|
+
*/
|
|
1373
|
+
type TaskOption<A> = Task<Option<A>>;
|
|
1374
|
+
declare namespace TaskOption {
|
|
1375
|
+
/**
|
|
1376
|
+
* Wraps a value in a Some inside a Task.
|
|
1377
|
+
*/
|
|
1378
|
+
const some: <A>(value: A) => TaskOption<A>;
|
|
1379
|
+
/**
|
|
1380
|
+
* Creates a TaskOption that resolves to None.
|
|
1381
|
+
*/
|
|
1382
|
+
const none: <A = never>() => TaskOption<A>;
|
|
1383
|
+
/**
|
|
1384
|
+
* Lifts an Option into a TaskOption.
|
|
1385
|
+
*/
|
|
1386
|
+
const fromOption: <A>(option: Option<A>) => TaskOption<A>;
|
|
1387
|
+
/**
|
|
1388
|
+
* Lifts a Task into a TaskOption by wrapping its result in Some.
|
|
1389
|
+
*/
|
|
1390
|
+
const fromTask: <A>(task: Task<A>) => TaskOption<A>;
|
|
1391
|
+
/**
|
|
1392
|
+
* Creates a TaskOption from a Promise-returning function.
|
|
1393
|
+
* Returns Some if the promise resolves, None if it rejects.
|
|
1394
|
+
*
|
|
1395
|
+
* @example
|
|
1396
|
+
* ```ts
|
|
1397
|
+
* const fetchUser = TaskOption.tryCatch(() =>
|
|
1398
|
+
* fetch("/user/1").then(r => r.json())
|
|
1399
|
+
* );
|
|
1400
|
+
* ```
|
|
1401
|
+
*/
|
|
1402
|
+
const tryCatch: <A>(f: () => Promise<A>) => TaskOption<A>;
|
|
1403
|
+
/**
|
|
1404
|
+
* Transforms the value inside a TaskOption.
|
|
1405
|
+
*/
|
|
1406
|
+
const map: <A, B>(f: (a: A) => B) => (data: TaskOption<A>) => TaskOption<B>;
|
|
1407
|
+
/**
|
|
1408
|
+
* Chains TaskOption computations. If the first resolves to Some, passes the
|
|
1409
|
+
* value to f. If the first resolves to None, propagates None.
|
|
1410
|
+
*
|
|
1411
|
+
* @example
|
|
1412
|
+
* ```ts
|
|
1413
|
+
* pipe(
|
|
1414
|
+
* findUser("123"),
|
|
1415
|
+
* TaskOption.chain(user => findOrg(user.orgId))
|
|
1416
|
+
* )();
|
|
1417
|
+
* ```
|
|
1418
|
+
*/
|
|
1419
|
+
const chain: <A, B>(f: (a: A) => TaskOption<B>) => (data: TaskOption<A>) => TaskOption<B>;
|
|
1420
|
+
/**
|
|
1421
|
+
* Applies a function wrapped in a TaskOption to a value wrapped in a TaskOption.
|
|
1422
|
+
* Both Tasks run in parallel.
|
|
1423
|
+
*/
|
|
1424
|
+
const ap: <A>(arg: TaskOption<A>) => <B>(data: TaskOption<(a: A) => B>) => TaskOption<B>;
|
|
1425
|
+
/**
|
|
1426
|
+
* Extracts a value from a TaskOption by providing handlers for both cases.
|
|
1427
|
+
*/
|
|
1428
|
+
const fold: <A, B>(onNone: () => B, onSome: (a: A) => B) => (data: TaskOption<A>) => Task<B>;
|
|
1429
|
+
/**
|
|
1430
|
+
* Pattern matches on a TaskOption, returning a Task of the result.
|
|
1431
|
+
*
|
|
1432
|
+
* @example
|
|
1433
|
+
* ```ts
|
|
1434
|
+
* pipe(
|
|
1435
|
+
* findUser("123"),
|
|
1436
|
+
* TaskOption.match({
|
|
1437
|
+
* some: user => `Hello, ${user.name}`,
|
|
1438
|
+
* none: () => "User not found"
|
|
1439
|
+
* })
|
|
1440
|
+
* )();
|
|
1441
|
+
* ```
|
|
1442
|
+
*/
|
|
1443
|
+
const match: <A, B>(cases: {
|
|
1444
|
+
none: () => B;
|
|
1445
|
+
some: (a: A) => B;
|
|
1446
|
+
}) => (data: TaskOption<A>) => Task<B>;
|
|
1447
|
+
/**
|
|
1448
|
+
* Returns the value or a default if the TaskOption resolves to None.
|
|
1449
|
+
* The default can be a different type, widening the result to `Task<A | B>`.
|
|
1450
|
+
*/
|
|
1451
|
+
const getOrElse: <A, B>(defaultValue: () => B) => (data: TaskOption<A>) => Task<A | B>;
|
|
1452
|
+
/**
|
|
1453
|
+
* Executes a side effect on the value without changing the TaskOption.
|
|
1454
|
+
* Useful for logging or debugging.
|
|
1455
|
+
*/
|
|
1456
|
+
const tap: <A>(f: (a: A) => void) => (data: TaskOption<A>) => TaskOption<A>;
|
|
1457
|
+
/**
|
|
1458
|
+
* Filters the value inside a TaskOption. Returns None if the predicate fails.
|
|
1459
|
+
*/
|
|
1460
|
+
const filter: <A>(predicate: (a: A) => boolean) => (data: TaskOption<A>) => TaskOption<A>;
|
|
1461
|
+
/**
|
|
1462
|
+
* Converts a TaskOption to a TaskResult, using onNone to produce the error value.
|
|
1463
|
+
*
|
|
1464
|
+
* @example
|
|
1465
|
+
* ```ts
|
|
1466
|
+
* pipe(
|
|
1467
|
+
* findUser("123"),
|
|
1468
|
+
* TaskOption.toTaskResult(() => "User not found")
|
|
1469
|
+
* );
|
|
1470
|
+
* ```
|
|
1471
|
+
*/
|
|
1472
|
+
const toTaskResult: <E>(onNone: () => E) => <A>(data: TaskOption<A>) => TaskResult<E, A>;
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1475
|
+
type Valid<A> = WithKind<"Valid"> & WithValue<A>;
|
|
1476
|
+
type Invalid<E> = WithKind<"Invalid"> & WithErrors<E>;
|
|
1477
|
+
/**
|
|
1478
|
+
* Validation represents a value that is either valid with a success value,
|
|
1479
|
+
* or invalid with accumulated errors.
|
|
1480
|
+
* Unlike Result, Validation can accumulate multiple errors instead of short-circuiting.
|
|
1481
|
+
*
|
|
1482
|
+
* Use Validation when you need to collect all errors (e.g., form validation).
|
|
1483
|
+
* Use Result when you want to fail fast on the first error.
|
|
1484
|
+
*
|
|
1485
|
+
* @example
|
|
1486
|
+
* ```ts
|
|
1487
|
+
* const validateName = (name: string): Validation<string, string> =>
|
|
1488
|
+
* name.length > 0 ? Validation.valid(name) : Validation.invalid("Name is required");
|
|
1489
|
+
*
|
|
1490
|
+
* const validateAge = (age: number): Validation<string, number> =>
|
|
1491
|
+
* age >= 0 ? Validation.valid(age) : Validation.invalid("Age must be positive");
|
|
1492
|
+
*
|
|
1493
|
+
* // Accumulates all errors using ap
|
|
1494
|
+
* pipe(
|
|
1495
|
+
* Validation.valid((name: string) => (age: number) => ({ name, age })),
|
|
1496
|
+
* Validation.ap(validateName("")),
|
|
1497
|
+
* Validation.ap(validateAge(-1))
|
|
1498
|
+
* );
|
|
1499
|
+
* // Invalid(["Name is required", "Age must be positive"])
|
|
1500
|
+
* ```
|
|
1501
|
+
*/
|
|
1502
|
+
type Validation<E, A> = Valid<A> | Invalid<E>;
|
|
1503
|
+
declare namespace Validation {
|
|
1504
|
+
/**
|
|
1505
|
+
* Wraps a value in a valid Validation.
|
|
1506
|
+
*
|
|
1507
|
+
* @example
|
|
1508
|
+
* ```ts
|
|
1509
|
+
* Validation.valid(42); // Valid(42)
|
|
1510
|
+
* ```
|
|
1511
|
+
*/
|
|
1512
|
+
const valid: <E, A>(value: A) => Validation<E, A>;
|
|
1513
|
+
/**
|
|
1514
|
+
* Creates an invalid Validation from a single error.
|
|
1515
|
+
*
|
|
1516
|
+
* @example
|
|
1517
|
+
* ```ts
|
|
1518
|
+
* Validation.invalid("Invalid input");
|
|
1519
|
+
* ```
|
|
1520
|
+
*/
|
|
1521
|
+
const invalid: <E>(error: E) => Invalid<E>;
|
|
1522
|
+
/**
|
|
1523
|
+
* Creates an invalid Validation from multiple errors.
|
|
1524
|
+
*
|
|
1525
|
+
* @example
|
|
1526
|
+
* ```ts
|
|
1527
|
+
* Validation.invalidAll(["Invalid input"]);
|
|
1528
|
+
* ```
|
|
1529
|
+
*/
|
|
1530
|
+
const invalidAll: <E>(errors: NonEmptyList<E>) => Invalid<E>;
|
|
1531
|
+
/**
|
|
1532
|
+
* Type guard that checks if a Validation is valid.
|
|
1533
|
+
*/
|
|
1534
|
+
const isValid: <E, A>(data: Validation<E, A>) => data is Valid<A>;
|
|
1535
|
+
/**
|
|
1536
|
+
* Type guard that checks if a Validation is invalid.
|
|
1537
|
+
*/
|
|
1538
|
+
const isInvalid: <E, A>(data: Validation<E, A>) => data is Invalid<E>;
|
|
1539
|
+
/**
|
|
1540
|
+
* Transforms the success value inside a Validation.
|
|
1541
|
+
*
|
|
1542
|
+
* @example
|
|
1543
|
+
* ```ts
|
|
1544
|
+
* pipe(Validation.valid(5), Validation.map(n => n * 2)); // Valid(10)
|
|
1545
|
+
* pipe(Validation.invalid("oops"), Validation.map(n => n * 2)); // Invalid(["oops"])
|
|
1546
|
+
* ```
|
|
1547
|
+
*/
|
|
1548
|
+
const map: <A, B>(f: (a: A) => B) => <E>(data: Validation<E, A>) => Validation<E, B>;
|
|
1549
|
+
/**
|
|
1550
|
+
* Applies a function wrapped in a Validation to a value wrapped in a Validation.
|
|
1551
|
+
* Accumulates errors from both sides.
|
|
1552
|
+
*
|
|
1553
|
+
* @example
|
|
1554
|
+
* ```ts
|
|
1555
|
+
* const add = (a: number) => (b: number) => a + b;
|
|
1556
|
+
* pipe(
|
|
1557
|
+
* Validation.valid(add),
|
|
1558
|
+
* Validation.ap(Validation.valid(5)),
|
|
1559
|
+
* Validation.ap(Validation.valid(3))
|
|
1560
|
+
* ); // Valid(8)
|
|
1561
|
+
*
|
|
1562
|
+
* pipe(
|
|
1563
|
+
* Validation.valid(add),
|
|
1564
|
+
* Validation.ap(Validation.invalid<string, number>("bad a")),
|
|
1565
|
+
* Validation.ap(Validation.invalid<string, number>("bad b"))
|
|
1566
|
+
* ); // Invalid(["bad a", "bad b"])
|
|
1567
|
+
* ```
|
|
1568
|
+
*/
|
|
1569
|
+
const ap: <E, A>(arg: Validation<E, A>) => <B>(data: Validation<E, (a: A) => B>) => Validation<E, B>;
|
|
1570
|
+
/**
|
|
1571
|
+
* Extracts the value from a Validation by providing handlers for both cases.
|
|
1572
|
+
*
|
|
1573
|
+
* @example
|
|
1574
|
+
* ```ts
|
|
1575
|
+
* pipe(
|
|
1576
|
+
* Validation.valid(42),
|
|
1577
|
+
* Validation.fold(
|
|
1578
|
+
* errors => `Errors: ${errors.join(", ")}`,
|
|
1579
|
+
* value => `Value: ${value}`
|
|
1580
|
+
* )
|
|
1581
|
+
* );
|
|
1582
|
+
* ```
|
|
1583
|
+
*/
|
|
1584
|
+
const fold: <E, A, B>(onInvalid: (errors: NonEmptyList<E>) => B, onValid: (a: A) => B) => (data: Validation<E, A>) => B;
|
|
1585
|
+
/**
|
|
1586
|
+
* Pattern matches on a Validation, returning the result of the matching case.
|
|
1587
|
+
*
|
|
1588
|
+
* @example
|
|
1589
|
+
* ```ts
|
|
1590
|
+
* pipe(
|
|
1591
|
+
* validation,
|
|
1592
|
+
* Validation.match({
|
|
1593
|
+
* valid: value => `Got ${value}`,
|
|
1594
|
+
* invalid: errors => `Failed: ${errors.join(", ")}`
|
|
1595
|
+
* })
|
|
1596
|
+
* );
|
|
1597
|
+
* ```
|
|
1598
|
+
*/
|
|
1599
|
+
const match: <E, A, B>(cases: {
|
|
1600
|
+
valid: (a: A) => B;
|
|
1601
|
+
invalid: (errors: NonEmptyList<E>) => B;
|
|
1602
|
+
}) => (data: Validation<E, A>) => B;
|
|
1603
|
+
/**
|
|
1604
|
+
* Returns the success value or a default value if the Validation is invalid.
|
|
1605
|
+
* The default can be a different type, widening the result to `A | B`.
|
|
1606
|
+
*
|
|
1607
|
+
* @example
|
|
1608
|
+
* ```ts
|
|
1609
|
+
* pipe(Validation.valid(5), Validation.getOrElse(() => 0)); // 5
|
|
1610
|
+
* pipe(Validation.invalid("oops"), Validation.getOrElse(() => 0)); // 0
|
|
1611
|
+
* pipe(Validation.invalid("oops"), Validation.getOrElse(() => null)); // null — typed as number | null
|
|
1612
|
+
* ```
|
|
1613
|
+
*/
|
|
1614
|
+
const getOrElse: <E, A, B>(defaultValue: () => B) => (data: Validation<E, A>) => A | B;
|
|
1615
|
+
/**
|
|
1616
|
+
* Executes a side effect on the success value without changing the Validation.
|
|
1617
|
+
*
|
|
1618
|
+
* @example
|
|
1619
|
+
* ```ts
|
|
1620
|
+
* pipe(
|
|
1621
|
+
* Validation.valid(5),
|
|
1622
|
+
* Validation.tap(n => console.log("Value:", n)),
|
|
1623
|
+
* Validation.map(n => n * 2)
|
|
1624
|
+
* );
|
|
1625
|
+
* ```
|
|
1626
|
+
*/
|
|
1627
|
+
const tap: <E, A>(f: (a: A) => void) => (data: Validation<E, A>) => Validation<E, A>;
|
|
1628
|
+
/**
|
|
1629
|
+
* Recovers from an Invalid state by providing a fallback Validation.
|
|
1630
|
+
* The fallback receives the accumulated error list so callers can inspect which errors occurred.
|
|
1631
|
+
* The fallback can produce a different success type, widening the result to `Validation<E, A | B>`.
|
|
1632
|
+
*/
|
|
1633
|
+
const recover: <E, A, B>(fallback: (errors: NonEmptyList<E>) => Validation<E, B>) => (data: Validation<E, A>) => Validation<E, A | B>;
|
|
1634
|
+
/**
|
|
1635
|
+
* Recovers from an Invalid state unless the errors contain any of the blocked errors.
|
|
1636
|
+
* The fallback can produce a different success type, widening the result to `Validation<E, A | B>`.
|
|
1637
|
+
*/
|
|
1638
|
+
const recoverUnless: <E, A, B>(blockedErrors: readonly E[], fallback: () => Validation<E, B>) => (data: Validation<E, A>) => Validation<E, A | B>;
|
|
1639
|
+
/**
|
|
1640
|
+
* Combines two independent Validation instances into a tuple.
|
|
1641
|
+
* If both are Valid, returns Valid with both values as a tuple.
|
|
1642
|
+
* If either is Invalid, accumulates errors from both sides.
|
|
1643
|
+
*
|
|
1644
|
+
* @example
|
|
1645
|
+
* ```ts
|
|
1646
|
+
* Validation.product(
|
|
1647
|
+
* Validation.valid("alice"),
|
|
1648
|
+
* Validation.valid(30)
|
|
1649
|
+
* ); // Valid(["alice", 30])
|
|
1650
|
+
*
|
|
1651
|
+
* Validation.product(
|
|
1652
|
+
* Validation.invalid("Name required"),
|
|
1653
|
+
* Validation.invalid("Age must be >= 0")
|
|
1654
|
+
* ); // Invalid(["Name required", "Age must be >= 0"])
|
|
1655
|
+
* ```
|
|
1656
|
+
*/
|
|
1657
|
+
const product: <E, A, B>(first: Validation<E, A>, second: Validation<E, B>) => Validation<E, readonly [A, B]>;
|
|
1658
|
+
/**
|
|
1659
|
+
* Combines a non-empty list of Validation instances, accumulating all errors.
|
|
1660
|
+
* If all are Valid, returns Valid with all values collected into an array.
|
|
1661
|
+
* If any are Invalid, returns Invalid with all accumulated errors.
|
|
1662
|
+
*
|
|
1663
|
+
* @example
|
|
1664
|
+
* ```ts
|
|
1665
|
+
* Validation.productAll([
|
|
1666
|
+
* validateName(name),
|
|
1667
|
+
* validateEmail(email),
|
|
1668
|
+
* validateAge(age)
|
|
1669
|
+
* ]);
|
|
1670
|
+
* // Valid([name, email, age]) or Invalid([...all errors])
|
|
1671
|
+
* ```
|
|
1672
|
+
*/
|
|
1673
|
+
const productAll: <E, A>(data: NonEmptyList<Validation<E, A>>) => Validation<E, readonly A[]>;
|
|
1674
|
+
}
|
|
1675
|
+
|
|
1676
|
+
/**
|
|
1677
|
+
* A Task that resolves to a Validation — combining async operations with
|
|
1678
|
+
* error accumulation. Unlike TaskResult, multiple failures are collected
|
|
1679
|
+
* rather than short-circuiting on the first error.
|
|
1680
|
+
*
|
|
1681
|
+
* @example
|
|
1682
|
+
* ```ts
|
|
1683
|
+
* const validateName = (name: string): TaskValidation<string, string> =>
|
|
1684
|
+
* name.length > 0
|
|
1685
|
+
* ? TaskValidation.valid(name)
|
|
1686
|
+
* : TaskValidation.invalid("Name is required");
|
|
1687
|
+
*
|
|
1688
|
+
* // Accumulate errors from multiple async validations using ap
|
|
1689
|
+
* pipe(
|
|
1690
|
+
* TaskValidation.valid((name: string) => (age: number) => ({ name, age })),
|
|
1691
|
+
* TaskValidation.ap(validateName("")),
|
|
1692
|
+
* TaskValidation.ap(validateAge(-1))
|
|
1693
|
+
* )();
|
|
1694
|
+
* // Invalid(["Name is required", "Age must be positive"])
|
|
1695
|
+
* ```
|
|
1696
|
+
*/
|
|
1697
|
+
type TaskValidation<E, A> = Task<Validation<E, A>>;
|
|
1698
|
+
declare namespace TaskValidation {
|
|
1699
|
+
/**
|
|
1700
|
+
* Wraps a value in a valid TaskValidation.
|
|
1701
|
+
*/
|
|
1702
|
+
const valid: <E, A>(value: A) => TaskValidation<E, A>;
|
|
1703
|
+
/**
|
|
1704
|
+
* Creates a failed TaskValidation with a single error.
|
|
1705
|
+
*/
|
|
1706
|
+
const invalid: <E, A>(error: E) => TaskValidation<E, A>;
|
|
1707
|
+
/**
|
|
1708
|
+
* Creates an invalid TaskValidation from multiple errors.
|
|
1709
|
+
*/
|
|
1710
|
+
const invalidAll: <E, A>(errors: NonEmptyList<E>) => TaskValidation<E, A>;
|
|
1711
|
+
/**
|
|
1712
|
+
* Lifts a Validation into a TaskValidation.
|
|
1713
|
+
*/
|
|
1714
|
+
const fromValidation: <E, A>(validation: Validation<E, A>) => TaskValidation<E, A>;
|
|
1715
|
+
/**
|
|
1716
|
+
* Creates a TaskValidation from a Promise-returning function.
|
|
1717
|
+
* Catches any errors and transforms them using the onError function.
|
|
1718
|
+
*
|
|
1719
|
+
* @example
|
|
1720
|
+
* ```ts
|
|
1721
|
+
* const fetchUser = (id: string): TaskValidation<string, User> =>
|
|
1722
|
+
* TaskValidation.tryCatch(
|
|
1723
|
+
* () => fetch(`/users/${id}`).then(r => r.json()),
|
|
1724
|
+
* e => `Failed to fetch user: ${e}`
|
|
1725
|
+
* );
|
|
1726
|
+
* ```
|
|
1727
|
+
*/
|
|
1728
|
+
const tryCatch: <E, A>(f: () => Promise<A>, onError: (e: unknown) => E) => TaskValidation<E, A>;
|
|
1729
|
+
/**
|
|
1730
|
+
* Transforms the success value inside a TaskValidation.
|
|
1731
|
+
*/
|
|
1732
|
+
const map: <E, A, B>(f: (a: A) => B) => (data: TaskValidation<E, A>) => TaskValidation<E, B>;
|
|
1733
|
+
/**
|
|
1734
|
+
* Applies a function wrapped in a TaskValidation to a value wrapped in a
|
|
1735
|
+
* TaskValidation. Both Tasks run in parallel and errors from both sides
|
|
1736
|
+
* are accumulated.
|
|
1737
|
+
*
|
|
1738
|
+
* @example
|
|
1739
|
+
* ```ts
|
|
1740
|
+
* pipe(
|
|
1741
|
+
* TaskValidation.valid((name: string) => (age: number) => ({ name, age })),
|
|
1742
|
+
* TaskValidation.ap(validateName(name)),
|
|
1743
|
+
* TaskValidation.ap(validateAge(age))
|
|
1744
|
+
* )();
|
|
1745
|
+
* ```
|
|
1746
|
+
*/
|
|
1747
|
+
const ap: <E, A>(arg: TaskValidation<E, A>) => <B>(data: TaskValidation<E, (a: A) => B>) => TaskValidation<E, B>;
|
|
1748
|
+
/**
|
|
1749
|
+
* Extracts a value from a TaskValidation by providing handlers for both cases.
|
|
1750
|
+
*/
|
|
1751
|
+
const fold: <E, A, B>(onInvalid: (errors: NonEmptyList<E>) => B, onValid: (a: A) => B) => (data: TaskValidation<E, A>) => Task<B>;
|
|
1752
|
+
/**
|
|
1753
|
+
* Pattern matches on a TaskValidation, returning a Task of the result.
|
|
1754
|
+
*
|
|
1755
|
+
* @example
|
|
1756
|
+
* ```ts
|
|
1757
|
+
* pipe(
|
|
1758
|
+
* validateForm(input),
|
|
1759
|
+
* TaskValidation.match({
|
|
1760
|
+
* valid: data => save(data),
|
|
1761
|
+
* invalid: errors => showErrors(errors)
|
|
1762
|
+
* })
|
|
1763
|
+
* )();
|
|
1764
|
+
* ```
|
|
1765
|
+
*/
|
|
1766
|
+
const match: <E, A, B>(cases: {
|
|
1767
|
+
valid: (a: A) => B;
|
|
1768
|
+
invalid: (errors: NonEmptyList<E>) => B;
|
|
1769
|
+
}) => (data: TaskValidation<E, A>) => Task<B>;
|
|
1770
|
+
/**
|
|
1771
|
+
* Returns the success value or a default value if the TaskValidation is invalid.
|
|
1772
|
+
* The default can be a different type, widening the result to `Task<A | B>`.
|
|
1773
|
+
*/
|
|
1774
|
+
const getOrElse: <E, A, B>(defaultValue: () => B) => (data: TaskValidation<E, A>) => Task<A | B>;
|
|
1775
|
+
/**
|
|
1776
|
+
* Executes a side effect on the success value without changing the TaskValidation.
|
|
1777
|
+
* Useful for logging or debugging.
|
|
1778
|
+
*/
|
|
1779
|
+
const tap: <E, A>(f: (a: A) => void) => (data: TaskValidation<E, A>) => TaskValidation<E, A>;
|
|
1780
|
+
/**
|
|
1781
|
+
* Recovers from an Invalid state by providing a fallback TaskValidation.
|
|
1782
|
+
* The fallback receives the accumulated error list so callers can inspect which errors occurred.
|
|
1783
|
+
* The fallback can produce a different success type, widening the result to `TaskValidation<E, A | B>`.
|
|
1784
|
+
*/
|
|
1785
|
+
const recover: <E, A, B>(fallback: (errors: NonEmptyList<E>) => TaskValidation<E, B>) => (data: TaskValidation<E, A>) => TaskValidation<E, A | B>;
|
|
1786
|
+
/**
|
|
1787
|
+
* Runs two TaskValidations concurrently and combines their results into a tuple.
|
|
1788
|
+
* If both are Valid, returns Valid with both values. If either fails, accumulates
|
|
1789
|
+
* errors from both sides.
|
|
1790
|
+
*
|
|
1791
|
+
* @example
|
|
1792
|
+
* ```ts
|
|
1793
|
+
* await TaskValidation.product(
|
|
1794
|
+
* validateName(form.name),
|
|
1795
|
+
* validateAge(form.age),
|
|
1796
|
+
* )(); // Valid(["Alice", 30]) or Invalid([...errors])
|
|
1797
|
+
* ```
|
|
1798
|
+
*/
|
|
1799
|
+
const product: <E, A, B>(first: TaskValidation<E, A>, second: TaskValidation<E, B>) => TaskValidation<E, readonly [A, B]>;
|
|
1800
|
+
/**
|
|
1801
|
+
* Runs all TaskValidations concurrently and collects results.
|
|
1802
|
+
* If all are Valid, returns Valid with all values as an array.
|
|
1803
|
+
* If any fail, returns Invalid with all accumulated errors.
|
|
1804
|
+
*
|
|
1805
|
+
* @example
|
|
1806
|
+
* ```ts
|
|
1807
|
+
* await TaskValidation.productAll([
|
|
1808
|
+
* validateName(form.name),
|
|
1809
|
+
* validateEmail(form.email),
|
|
1810
|
+
* validateAge(form.age),
|
|
1811
|
+
* ])(); // Valid([name, email, age]) or Invalid([...all errors])
|
|
1812
|
+
* ```
|
|
1813
|
+
*/
|
|
1814
|
+
const productAll: <E, A>(data: NonEmptyList<TaskValidation<E, A>>) => TaskValidation<E, readonly A[]>;
|
|
1815
|
+
}
|
|
1816
|
+
|
|
1817
|
+
type TheseFirst<T> = WithKind<"First"> & WithFirst<T>;
|
|
1818
|
+
type TheseSecond<T> = WithKind<"Second"> & WithSecond<T>;
|
|
1819
|
+
type TheseBoth<First, Second> = WithKind<"Both"> & WithFirst<First> & WithSecond<Second>;
|
|
1820
|
+
/**
|
|
1821
|
+
* These<A, B> is an inclusive-OR type: it holds a first value (A), a second
|
|
1822
|
+
* value (B), or both simultaneously. Neither side carries a success/failure
|
|
1823
|
+
* connotation — it is a neutral pair where any combination is valid.
|
|
1824
|
+
*
|
|
1825
|
+
* - First(a) — only a first value
|
|
1826
|
+
* - Second(b) — only a second value
|
|
1827
|
+
* - Both(a, b) — first and second values simultaneously
|
|
1828
|
+
*
|
|
1829
|
+
* A common use: lenient parsers or processors that carry a diagnostic note
|
|
1830
|
+
* alongside a result, without losing either piece of information.
|
|
1831
|
+
*
|
|
1832
|
+
* @example
|
|
1833
|
+
* ```ts
|
|
1834
|
+
* const parse = (s: string): These<number, string> => {
|
|
1835
|
+
* const trimmed = s.trim();
|
|
1836
|
+
* const n = parseFloat(trimmed);
|
|
1837
|
+
* if (isNaN(n)) return These.second("Not a number");
|
|
1838
|
+
* if (s !== trimmed) return These.both(n, "Leading/trailing whitespace trimmed");
|
|
1839
|
+
* return These.first(n);
|
|
1840
|
+
* };
|
|
1841
|
+
* ```
|
|
1842
|
+
*/
|
|
1843
|
+
type These<A, B> = TheseFirst<A> | TheseSecond<B> | TheseBoth<A, B>;
|
|
1844
|
+
declare namespace These {
|
|
1845
|
+
/**
|
|
1846
|
+
* Creates a These holding only a first value.
|
|
1847
|
+
*
|
|
1848
|
+
* @example
|
|
1849
|
+
* ```ts
|
|
1850
|
+
* These.first(42); // { kind: "First", first: 42 }
|
|
1851
|
+
* ```
|
|
1852
|
+
*/
|
|
1853
|
+
const first: <A>(value: A) => TheseFirst<A>;
|
|
1854
|
+
/**
|
|
1855
|
+
* Creates a These holding only a second value.
|
|
1856
|
+
*
|
|
1857
|
+
* @example
|
|
1858
|
+
* ```ts
|
|
1859
|
+
* These.second("warning"); // { kind: "Second", second: "warning" }
|
|
1860
|
+
* ```
|
|
1861
|
+
*/
|
|
1862
|
+
const second: <B>(value: B) => TheseSecond<B>;
|
|
1863
|
+
/**
|
|
1864
|
+
* Creates a These holding both a first and a second value simultaneously.
|
|
1865
|
+
*
|
|
1866
|
+
* @example
|
|
1867
|
+
* ```ts
|
|
1868
|
+
* These.both(42, "Deprecated API used"); // { kind: "Both", first: 42, second: "Deprecated API used" }
|
|
1869
|
+
* ```
|
|
1870
|
+
*/
|
|
1871
|
+
const both: <A, B>(first: A, second: B) => TheseBoth<A, B>;
|
|
1872
|
+
/**
|
|
1873
|
+
* Type guard — checks if a These holds only a first value.
|
|
1874
|
+
*/
|
|
1875
|
+
const isFirst: <A, B>(data: These<A, B>) => data is TheseFirst<A>;
|
|
1876
|
+
/**
|
|
1877
|
+
* Type guard — checks if a These holds only a second value.
|
|
1878
|
+
*/
|
|
1879
|
+
const isSecond: <A, B>(data: These<A, B>) => data is TheseSecond<B>;
|
|
1880
|
+
/**
|
|
1881
|
+
* Type guard — checks if a These holds both values simultaneously.
|
|
1882
|
+
*/
|
|
1883
|
+
const isBoth: <A, B>(data: These<A, B>) => data is TheseBoth<A, B>;
|
|
1884
|
+
/**
|
|
1885
|
+
* Returns true if the These contains a first value (First or Both).
|
|
1886
|
+
*/
|
|
1887
|
+
const hasFirst: <A, B>(data: These<A, B>) => data is TheseFirst<A> | TheseBoth<A, B>;
|
|
1888
|
+
/**
|
|
1889
|
+
* Returns true if the These contains a second value (Second or Both).
|
|
1890
|
+
*/
|
|
1891
|
+
const hasSecond: <A, B>(data: These<A, B>) => data is TheseSecond<B> | TheseBoth<A, B>;
|
|
1892
|
+
/**
|
|
1893
|
+
* Transforms the first value, leaving the second unchanged.
|
|
1894
|
+
*
|
|
1895
|
+
* @example
|
|
1896
|
+
* ```ts
|
|
1897
|
+
* pipe(These.first(5), These.mapFirst(n => n * 2)); // First(10)
|
|
1898
|
+
* pipe(These.both(5, "warn"), These.mapFirst(n => n * 2)); // Both(10, "warn")
|
|
1899
|
+
* pipe(These.second("warn"), These.mapFirst(n => n * 2)); // Second("warn")
|
|
1900
|
+
* ```
|
|
1901
|
+
*/
|
|
1902
|
+
const mapFirst: <A, C>(f: (a: A) => C) => <B>(data: These<A, B>) => These<C, B>;
|
|
1903
|
+
/**
|
|
1904
|
+
* Transforms the second value, leaving the first unchanged.
|
|
1905
|
+
*
|
|
1906
|
+
* @example
|
|
1907
|
+
* ```ts
|
|
1908
|
+
* pipe(These.second("warn"), These.mapSecond(e => e.toUpperCase())); // Second("WARN")
|
|
1909
|
+
* pipe(These.both(5, "warn"), These.mapSecond(e => e.toUpperCase())); // Both(5, "WARN")
|
|
1910
|
+
* ```
|
|
1911
|
+
*/
|
|
1912
|
+
const mapSecond: <B, D>(f: (b: B) => D) => <A>(data: These<A, B>) => These<A, D>;
|
|
1913
|
+
/**
|
|
1914
|
+
* Transforms both the first and second values independently.
|
|
1915
|
+
*
|
|
1916
|
+
* @example
|
|
1917
|
+
* ```ts
|
|
1918
|
+
* pipe(
|
|
1919
|
+
* These.both(5, "warn"),
|
|
1920
|
+
* These.mapBoth(n => n * 2, e => e.toUpperCase())
|
|
1921
|
+
* ); // Both(10, "WARN")
|
|
1922
|
+
* ```
|
|
1923
|
+
*/
|
|
1924
|
+
const mapBoth: <A, C, B, D>(onFirst: (a: A) => C, onSecond: (b: B) => D) => (data: These<A, B>) => These<C, D>;
|
|
1925
|
+
/**
|
|
1926
|
+
* Chains These computations by passing the first value to f.
|
|
1927
|
+
* Second propagates unchanged; First and Both apply f to the first value.
|
|
1928
|
+
*
|
|
1929
|
+
* @example
|
|
1930
|
+
* ```ts
|
|
1931
|
+
* const double = (n: number): These<number, string> => These.first(n * 2);
|
|
1932
|
+
*
|
|
1933
|
+
* pipe(These.first(5), These.chainFirst(double)); // First(10)
|
|
1934
|
+
* pipe(These.both(5, "warn"), These.chainFirst(double)); // First(10)
|
|
1935
|
+
* pipe(These.second("warn"), These.chainFirst(double)); // Second("warn")
|
|
1936
|
+
* ```
|
|
1937
|
+
*/
|
|
1938
|
+
const chainFirst: <A, B, C>(f: (a: A) => These<C, B>) => (data: These<A, B>) => These<C, B>;
|
|
1939
|
+
/**
|
|
1940
|
+
* Chains These computations by passing the second value to f.
|
|
1941
|
+
* First propagates unchanged; Second and Both apply f to the second value.
|
|
1942
|
+
*
|
|
1943
|
+
* @example
|
|
1944
|
+
* ```ts
|
|
1945
|
+
* const shout = (s: string): These<number, string> => These.second(s.toUpperCase());
|
|
1946
|
+
*
|
|
1947
|
+
* pipe(These.second("warn"), These.chainSecond(shout)); // Second("WARN")
|
|
1948
|
+
* pipe(These.both(5, "warn"), These.chainSecond(shout)); // Second("WARN")
|
|
1949
|
+
* pipe(These.first(5), These.chainSecond(shout)); // First(5)
|
|
1950
|
+
* ```
|
|
1951
|
+
*/
|
|
1952
|
+
const chainSecond: <A, B, D>(f: (b: B) => These<A, D>) => (data: These<A, B>) => These<A, D>;
|
|
1953
|
+
/**
|
|
1954
|
+
* Extracts a value from a These by providing handlers for all three cases.
|
|
1955
|
+
*
|
|
1956
|
+
* @example
|
|
1957
|
+
* ```ts
|
|
1958
|
+
* pipe(
|
|
1959
|
+
* these,
|
|
1960
|
+
* These.fold(
|
|
1961
|
+
* a => `First: ${a}`,
|
|
1962
|
+
* b => `Second: ${b}`,
|
|
1963
|
+
* (a, b) => `Both: ${a} / ${b}`
|
|
1964
|
+
* )
|
|
1965
|
+
* );
|
|
1966
|
+
* ```
|
|
1967
|
+
*/
|
|
1968
|
+
const fold: <A, B, C>(onFirst: (a: A) => C, onSecond: (b: B) => C, onBoth: (a: A, b: B) => C) => (data: These<A, B>) => C;
|
|
1969
|
+
/**
|
|
1970
|
+
* Pattern matches on a These, returning the result of the matching case.
|
|
1971
|
+
*
|
|
1972
|
+
* @example
|
|
1973
|
+
* ```ts
|
|
1974
|
+
* pipe(
|
|
1975
|
+
* these,
|
|
1976
|
+
* These.match({
|
|
1977
|
+
* first: a => `First: ${a}`,
|
|
1978
|
+
* second: b => `Second: ${b}`,
|
|
1979
|
+
* both: (a, b) => `Both: ${a} / ${b}`
|
|
1980
|
+
* })
|
|
1981
|
+
* );
|
|
1982
|
+
* ```
|
|
1983
|
+
*/
|
|
1984
|
+
const match: <A, B, C>(cases: {
|
|
1985
|
+
first: (a: A) => C;
|
|
1986
|
+
second: (b: B) => C;
|
|
1987
|
+
both: (a: A, b: B) => C;
|
|
1988
|
+
}) => (data: These<A, B>) => C;
|
|
1989
|
+
/**
|
|
1990
|
+
* Returns the first value, or a default if the These has no first value.
|
|
1991
|
+
* The default can be a different type, widening the result to `A | C`.
|
|
1992
|
+
*
|
|
1993
|
+
* @example
|
|
1994
|
+
* ```ts
|
|
1995
|
+
* pipe(These.first(5), These.getFirstOrElse(() => 0)); // 5
|
|
1996
|
+
* pipe(These.both(5, "warn"), These.getFirstOrElse(() => 0)); // 5
|
|
1997
|
+
* pipe(These.second("warn"), These.getFirstOrElse(() => 0)); // 0
|
|
1998
|
+
* pipe(These.second("warn"), These.getFirstOrElse(() => null)); // null — typed as number | null
|
|
1999
|
+
* ```
|
|
2000
|
+
*/
|
|
2001
|
+
const getFirstOrElse: <A, C>(defaultValue: () => C) => <B>(data: These<A, B>) => A | C;
|
|
2002
|
+
/**
|
|
2003
|
+
* Returns the second value, or a default if the These has no second value.
|
|
2004
|
+
* The default can be a different type, widening the result to `B | D`.
|
|
2005
|
+
*
|
|
2006
|
+
* @example
|
|
2007
|
+
* ```ts
|
|
2008
|
+
* pipe(These.second("warn"), These.getSecondOrElse(() => "none")); // "warn"
|
|
2009
|
+
* pipe(These.both(5, "warn"), These.getSecondOrElse(() => "none")); // "warn"
|
|
2010
|
+
* pipe(These.first(5), These.getSecondOrElse(() => "none")); // "none"
|
|
2011
|
+
* pipe(These.first(5), These.getSecondOrElse(() => null)); // null — typed as string | null
|
|
2012
|
+
* ```
|
|
2013
|
+
*/
|
|
2014
|
+
const getSecondOrElse: <B, D>(defaultValue: () => D) => <A>(data: These<A, B>) => B | D;
|
|
2015
|
+
/**
|
|
2016
|
+
* Executes a side effect on the first value without changing the These.
|
|
2017
|
+
* Useful for logging or debugging.
|
|
2018
|
+
*
|
|
2019
|
+
* @example
|
|
2020
|
+
* ```ts
|
|
2021
|
+
* pipe(These.first(5), These.tap(console.log)); // logs 5, returns First(5)
|
|
2022
|
+
* ```
|
|
2023
|
+
*/
|
|
2024
|
+
const tap: <A>(f: (a: A) => void) => <B>(data: These<A, B>) => These<A, B>;
|
|
2025
|
+
/**
|
|
2026
|
+
* Swaps the roles of first and second values.
|
|
2027
|
+
* - First(a) → Second(a)
|
|
2028
|
+
* - Second(b) → First(b)
|
|
2029
|
+
* - Both(a, b) → Both(b, a)
|
|
2030
|
+
*
|
|
2031
|
+
* @example
|
|
2032
|
+
* ```ts
|
|
2033
|
+
* These.swap(These.first(5)); // Second(5)
|
|
2034
|
+
* These.swap(These.second("warn")); // First("warn")
|
|
2035
|
+
* These.swap(These.both(5, "warn")); // Both("warn", 5)
|
|
2036
|
+
* ```
|
|
2037
|
+
*/
|
|
2038
|
+
const swap: <A, B>(data: These<A, B>) => These<B, A>;
|
|
2039
|
+
}
|
|
2040
|
+
|
|
2041
|
+
/**
|
|
2042
|
+
* Tuple<A, B> represents a pair of two values that are always both present.
|
|
2043
|
+
* It is a typed alias for `readonly [A, B]`.
|
|
2044
|
+
*
|
|
2045
|
+
* Use Tuple when two values always travel together through a pipeline and you
|
|
2046
|
+
* want to transform either or both sides without destructuring.
|
|
2047
|
+
*
|
|
2048
|
+
* @example
|
|
2049
|
+
* ```ts
|
|
2050
|
+
* import { Tuple } from "@nlozgachev/pipelined/core";
|
|
2051
|
+
* import { pipe } from "@nlozgachev/pipelined/composition";
|
|
2052
|
+
*
|
|
2053
|
+
* const entry = Tuple.make("alice", 42);
|
|
2054
|
+
*
|
|
2055
|
+
* pipe(
|
|
2056
|
+
* entry,
|
|
2057
|
+
* Tuple.mapFirst((name) => name.toUpperCase()),
|
|
2058
|
+
* Tuple.mapSecond((score) => score * 2),
|
|
2059
|
+
* Tuple.fold((name, score) => `${name}: ${score}`),
|
|
2060
|
+
* ); // "ALICE: 84"
|
|
2061
|
+
* ```
|
|
2062
|
+
*/
|
|
2063
|
+
type Tuple<A, B> = readonly [A, B];
|
|
2064
|
+
declare namespace Tuple {
|
|
2065
|
+
/**
|
|
2066
|
+
* Creates a pair from two values.
|
|
2067
|
+
*
|
|
2068
|
+
* @example
|
|
2069
|
+
* ```ts
|
|
2070
|
+
* Tuple.make("Paris", 2_161_000); // ["Paris", 2161000]
|
|
2071
|
+
* ```
|
|
2072
|
+
*/
|
|
2073
|
+
const make: <A, B>(first: A, second: B) => Tuple<A, B>;
|
|
2074
|
+
/**
|
|
2075
|
+
* Returns the first value from the pair.
|
|
2076
|
+
*
|
|
2077
|
+
* @example
|
|
2078
|
+
* ```ts
|
|
2079
|
+
* Tuple.first(Tuple.make("Paris", 2_161_000)); // "Paris"
|
|
2080
|
+
* ```
|
|
2081
|
+
*/
|
|
2082
|
+
const first: <A, B>(tuple: Tuple<A, B>) => A;
|
|
2083
|
+
/**
|
|
2084
|
+
* Returns the second value from the pair.
|
|
2085
|
+
*
|
|
2086
|
+
* @example
|
|
2087
|
+
* ```ts
|
|
2088
|
+
* Tuple.second(Tuple.make("Paris", 2_161_000)); // 2161000
|
|
2089
|
+
* ```
|
|
2090
|
+
*/
|
|
2091
|
+
const second: <A, B>(tuple: Tuple<A, B>) => B;
|
|
2092
|
+
/**
|
|
2093
|
+
* Transforms the first value, leaving the second unchanged.
|
|
2094
|
+
*
|
|
2095
|
+
* @example
|
|
2096
|
+
* ```ts
|
|
2097
|
+
* pipe(Tuple.make("alice", 42), Tuple.mapFirst((s) => s.toUpperCase())); // ["ALICE", 42]
|
|
2098
|
+
* ```
|
|
2099
|
+
*/
|
|
2100
|
+
const mapFirst: <A, C>(f: (a: A) => C) => <B>(tuple: Tuple<A, B>) => Tuple<C, B>;
|
|
2101
|
+
/**
|
|
2102
|
+
* Transforms the second value, leaving the first unchanged.
|
|
2103
|
+
*
|
|
2104
|
+
* @example
|
|
2105
|
+
* ```ts
|
|
2106
|
+
* pipe(Tuple.make("alice", 42), Tuple.mapSecond((n) => n * 2)); // ["alice", 84]
|
|
2107
|
+
* ```
|
|
2108
|
+
*/
|
|
2109
|
+
const mapSecond: <B, D>(f: (b: B) => D) => <A>(tuple: Tuple<A, B>) => Tuple<A, D>;
|
|
2110
|
+
/**
|
|
2111
|
+
* Transforms both values independently in a single step.
|
|
2112
|
+
*
|
|
2113
|
+
* @example
|
|
2114
|
+
* ```ts
|
|
2115
|
+
* pipe(
|
|
2116
|
+
* Tuple.make("alice", 42),
|
|
2117
|
+
* Tuple.mapBoth(
|
|
2118
|
+
* (name) => name.toUpperCase(),
|
|
2119
|
+
* (score) => score * 2,
|
|
2120
|
+
* ),
|
|
2121
|
+
* ); // ["ALICE", 84]
|
|
2122
|
+
* ```
|
|
2123
|
+
*/
|
|
2124
|
+
const mapBoth: <A, C, B, D>(onFirst: (a: A) => C, onSecond: (b: B) => D) => (tuple: Tuple<A, B>) => Tuple<C, D>;
|
|
2125
|
+
/**
|
|
2126
|
+
* Applies a binary function to both values, collapsing the pair into a single value.
|
|
2127
|
+
* Useful as the final step when consuming a pair in a pipeline.
|
|
2128
|
+
*
|
|
2129
|
+
* @example
|
|
2130
|
+
* ```ts
|
|
2131
|
+
* pipe(Tuple.make("Alice", 100), Tuple.fold((name, score) => `${name}: ${score}`));
|
|
2132
|
+
* // "Alice: 100"
|
|
2133
|
+
* ```
|
|
2134
|
+
*/
|
|
2135
|
+
const fold: <A, B, C>(f: (a: A, b: B) => C) => (tuple: Tuple<A, B>) => C;
|
|
2136
|
+
/**
|
|
2137
|
+
* Swaps the two values: `[A, B]` becomes `[B, A]`.
|
|
2138
|
+
*
|
|
2139
|
+
* @example
|
|
2140
|
+
* ```ts
|
|
2141
|
+
* Tuple.swap(Tuple.make("key", 1)); // [1, "key"]
|
|
2142
|
+
* ```
|
|
2143
|
+
*/
|
|
2144
|
+
const swap: <A, B>(tuple: Tuple<A, B>) => Tuple<B, A>;
|
|
2145
|
+
/**
|
|
2146
|
+
* Converts the pair to a heterogeneous readonly array `readonly (A | B)[]`.
|
|
2147
|
+
*
|
|
2148
|
+
* @example
|
|
2149
|
+
* ```ts
|
|
2150
|
+
* Tuple.toArray(Tuple.make("hello", 42)); // ["hello", 42]
|
|
2151
|
+
* ```
|
|
2152
|
+
*/
|
|
2153
|
+
const toArray: <A, B>(tuple: Tuple<A, B>) => readonly (A | B)[];
|
|
2154
|
+
/**
|
|
2155
|
+
* Runs a side effect with both values without changing the pair.
|
|
2156
|
+
* Useful for logging or debugging in the middle of a pipeline.
|
|
2157
|
+
*
|
|
2158
|
+
* @example
|
|
2159
|
+
* ```ts
|
|
2160
|
+
* pipe(
|
|
2161
|
+
* Tuple.make("Paris", 2_161_000),
|
|
2162
|
+
* Tuple.tap((city, pop) => console.log(`${city}: ${pop}`)),
|
|
2163
|
+
* Tuple.mapSecond((n) => n / 1_000_000),
|
|
2164
|
+
* ); // logs "Paris: 2161000", returns ["Paris", 2.161]
|
|
2165
|
+
* ```
|
|
2166
|
+
*/
|
|
2167
|
+
const tap: <A, B>(f: (a: A, b: B) => void) => (tuple: Tuple<A, B>) => Tuple<A, B>;
|
|
2168
|
+
}
|
|
2169
|
+
|
|
2170
|
+
export { type Failure, type Invalid, Lens, type Loading, Logged, type NotAsked, Option, Optional, Predicate, Reader, Refinement, RemoteData, Result, State, type Success, Task, TaskOption, TaskResult, TaskValidation, These, type TheseBoth, type TheseFirst, type TheseSecond, Tuple, type Valid, Validation };
|