@nlozgachev/pipekit 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -13
- package/esm/src/Composition/flip.js +2 -2
- package/esm/src/Composition/fn.js +1 -1
- package/esm/src/Composition/tap.js +1 -1
- package/esm/src/Core/Arr.js +4 -4
- package/esm/src/Core/Option.js +13 -22
- package/esm/src/Core/RemoteData.js +9 -13
- package/esm/src/Core/Result.js +12 -21
- package/esm/src/Core/Task.js +26 -23
- package/esm/src/Core/TaskOption.js +8 -6
- package/esm/src/Core/TaskResult.js +10 -6
- package/esm/src/Core/TaskValidation.js +12 -8
- package/esm/src/Core/These.js +122 -123
- package/esm/src/Core/Validation.js +42 -43
- package/package.json +1 -1
- package/script/src/Composition/flip.js +2 -2
- package/script/src/Composition/fn.js +1 -1
- package/script/src/Composition/tap.js +1 -1
- package/script/src/Core/Arr.js +4 -4
- package/script/src/Core/Option.js +13 -22
- package/script/src/Core/RemoteData.js +9 -13
- package/script/src/Core/Result.js +12 -21
- package/script/src/Core/Task.js +26 -23
- package/script/src/Core/TaskOption.js +8 -6
- package/script/src/Core/TaskResult.js +10 -6
- package/script/src/Core/TaskValidation.js +12 -8
- package/script/src/Core/These.js +122 -123
- package/script/src/Core/Validation.js +42 -43
- package/types/src/Composition/flip.d.ts +2 -2
- package/types/src/Composition/fn.d.ts +1 -1
- package/types/src/Composition/pipe.d.ts +1 -1
- package/types/src/Composition/tap.d.ts +1 -1
- package/types/src/Composition/uncurry.d.ts +3 -3
- package/types/src/Core/Arr.d.ts +4 -4
- package/types/src/Core/InternalTypes.d.ts +6 -0
- package/types/src/Core/InternalTypes.d.ts.map +1 -1
- package/types/src/Core/Option.d.ts +14 -23
- package/types/src/Core/Option.d.ts.map +1 -1
- package/types/src/Core/RemoteData.d.ts +9 -13
- package/types/src/Core/RemoteData.d.ts.map +1 -1
- package/types/src/Core/Result.d.ts +12 -21
- package/types/src/Core/Result.d.ts.map +1 -1
- package/types/src/Core/Task.d.ts +35 -32
- package/types/src/Core/Task.d.ts.map +1 -1
- package/types/src/Core/TaskOption.d.ts +1 -1
- package/types/src/Core/TaskOption.d.ts.map +1 -1
- package/types/src/Core/TaskResult.d.ts +1 -1
- package/types/src/Core/TaskResult.d.ts.map +1 -1
- package/types/src/Core/TaskValidation.d.ts +10 -6
- package/types/src/Core/TaskValidation.d.ts.map +1 -1
- package/types/src/Core/These.d.ts +108 -100
- package/types/src/Core/These.d.ts.map +1 -1
- package/types/src/Core/Validation.d.ts +38 -42
- package/types/src/Core/Validation.d.ts.map +1 -1
package/esm/src/Core/These.js
CHANGED
|
@@ -1,131 +1,142 @@
|
|
|
1
|
-
import { Result } from "./Result.js";
|
|
2
1
|
export var These;
|
|
3
2
|
(function (These) {
|
|
4
3
|
/**
|
|
5
|
-
* Creates a These holding only
|
|
4
|
+
* Creates a These holding only a first value.
|
|
6
5
|
*
|
|
7
6
|
* @example
|
|
8
7
|
* ```ts
|
|
9
|
-
* These.
|
|
8
|
+
* These.first(42); // { kind: "First", first: 42 }
|
|
10
9
|
* ```
|
|
11
10
|
*/
|
|
12
|
-
These.
|
|
11
|
+
These.first = (value) => ({ kind: "First", first: value });
|
|
13
12
|
/**
|
|
14
|
-
* Creates a These holding only a
|
|
13
|
+
* Creates a These holding only a second value.
|
|
15
14
|
*
|
|
16
15
|
* @example
|
|
17
16
|
* ```ts
|
|
18
|
-
* These.
|
|
17
|
+
* These.second("warning"); // { kind: "Second", second: "warning" }
|
|
19
18
|
* ```
|
|
20
19
|
*/
|
|
21
|
-
These.
|
|
20
|
+
These.second = (value) => ({ kind: "Second", second: value });
|
|
22
21
|
/**
|
|
23
|
-
* Creates a These holding both
|
|
22
|
+
* Creates a These holding both a first and a second value simultaneously.
|
|
24
23
|
*
|
|
25
24
|
* @example
|
|
26
25
|
* ```ts
|
|
27
|
-
* These.both("Deprecated API used",
|
|
26
|
+
* These.both(42, "Deprecated API used"); // { kind: "Both", first: 42, second: "Deprecated API used" }
|
|
28
27
|
* ```
|
|
29
28
|
*/
|
|
30
|
-
These.both = (
|
|
29
|
+
These.both = (first, second) => ({
|
|
31
30
|
kind: "Both",
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
first,
|
|
32
|
+
second,
|
|
34
33
|
});
|
|
35
34
|
/**
|
|
36
|
-
* Type guard — checks if a These holds only
|
|
35
|
+
* Type guard — checks if a These holds only a first value.
|
|
37
36
|
*/
|
|
38
|
-
These.
|
|
37
|
+
These.isFirst = (data) => data.kind === "First";
|
|
39
38
|
/**
|
|
40
|
-
* Type guard — checks if a These holds only a
|
|
39
|
+
* Type guard — checks if a These holds only a second value.
|
|
41
40
|
*/
|
|
42
|
-
These.
|
|
41
|
+
These.isSecond = (data) => data.kind === "Second";
|
|
43
42
|
/**
|
|
44
|
-
* Type guard — checks if a These holds both
|
|
43
|
+
* Type guard — checks if a These holds both values simultaneously.
|
|
45
44
|
*/
|
|
46
45
|
These.isBoth = (data) => data.kind === "Both";
|
|
47
46
|
/**
|
|
48
|
-
* Returns true if the These contains a
|
|
47
|
+
* Returns true if the These contains a first value (First or Both).
|
|
49
48
|
*/
|
|
50
|
-
These.
|
|
49
|
+
These.hasFirst = (data) => data.kind === "First" || data.kind === "Both";
|
|
51
50
|
/**
|
|
52
|
-
* Returns true if the These contains
|
|
51
|
+
* Returns true if the These contains a second value (Second or Both).
|
|
53
52
|
*/
|
|
54
|
-
These.
|
|
53
|
+
These.hasSecond = (data) => data.kind === "Second" || data.kind === "Both";
|
|
55
54
|
/**
|
|
56
|
-
* Transforms the
|
|
55
|
+
* Transforms the first value, leaving the second unchanged.
|
|
57
56
|
*
|
|
58
57
|
* @example
|
|
59
58
|
* ```ts
|
|
60
|
-
* pipe(These.
|
|
61
|
-
* pipe(These.both("warn"
|
|
62
|
-
* pipe(These.
|
|
59
|
+
* pipe(These.first(5), These.mapFirst(n => n * 2)); // First(10)
|
|
60
|
+
* pipe(These.both(5, "warn"), These.mapFirst(n => n * 2)); // Both(10, "warn")
|
|
61
|
+
* pipe(These.second("warn"), These.mapFirst(n => n * 2)); // Second("warn")
|
|
63
62
|
* ```
|
|
64
63
|
*/
|
|
65
|
-
These.
|
|
66
|
-
if (These.
|
|
64
|
+
These.mapFirst = (f) => (data) => {
|
|
65
|
+
if (These.isSecond(data))
|
|
67
66
|
return data;
|
|
68
|
-
if (These.
|
|
69
|
-
return These.
|
|
70
|
-
return These.both(data.
|
|
67
|
+
if (These.isFirst(data))
|
|
68
|
+
return These.first(f(data.first));
|
|
69
|
+
return These.both(f(data.first), data.second);
|
|
71
70
|
};
|
|
72
71
|
/**
|
|
73
|
-
* Transforms the
|
|
72
|
+
* Transforms the second value, leaving the first unchanged.
|
|
74
73
|
*
|
|
75
74
|
* @example
|
|
76
75
|
* ```ts
|
|
77
|
-
* pipe(These.
|
|
78
|
-
* pipe(These.both("warn"
|
|
76
|
+
* pipe(These.second("warn"), These.mapSecond(e => e.toUpperCase())); // Second("WARN")
|
|
77
|
+
* pipe(These.both(5, "warn"), These.mapSecond(e => e.toUpperCase())); // Both(5, "WARN")
|
|
79
78
|
* ```
|
|
80
79
|
*/
|
|
81
|
-
These.
|
|
82
|
-
if (These.
|
|
80
|
+
These.mapSecond = (f) => (data) => {
|
|
81
|
+
if (These.isFirst(data))
|
|
83
82
|
return data;
|
|
84
|
-
if (These.
|
|
85
|
-
return These.
|
|
86
|
-
return These.both(
|
|
83
|
+
if (These.isSecond(data))
|
|
84
|
+
return These.second(f(data.second));
|
|
85
|
+
return These.both(data.first, f(data.second));
|
|
87
86
|
};
|
|
88
87
|
/**
|
|
89
|
-
* Transforms both the
|
|
88
|
+
* Transforms both the first and second values independently.
|
|
90
89
|
*
|
|
91
90
|
* @example
|
|
92
91
|
* ```ts
|
|
93
92
|
* pipe(
|
|
94
|
-
* These.both("warn"
|
|
95
|
-
* These.
|
|
96
|
-
* ); // Both("WARN"
|
|
93
|
+
* These.both(5, "warn"),
|
|
94
|
+
* These.mapBoth(n => n * 2, e => e.toUpperCase())
|
|
95
|
+
* ); // Both(10, "WARN")
|
|
97
96
|
* ```
|
|
98
97
|
*/
|
|
99
|
-
These.
|
|
100
|
-
if (These.
|
|
101
|
-
return These.
|
|
102
|
-
if (These.
|
|
103
|
-
return These.
|
|
104
|
-
return These.both(
|
|
98
|
+
These.mapBoth = (onFirst, onSecond) => (data) => {
|
|
99
|
+
if (These.isSecond(data))
|
|
100
|
+
return These.second(onSecond(data.second));
|
|
101
|
+
if (These.isFirst(data))
|
|
102
|
+
return These.first(onFirst(data.first));
|
|
103
|
+
return These.both(onFirst(data.first), onSecond(data.second));
|
|
105
104
|
};
|
|
106
105
|
/**
|
|
107
|
-
* Chains These computations by passing the
|
|
108
|
-
*
|
|
109
|
-
* - Ok(a) applies f(a) directly.
|
|
110
|
-
* - Both(e, a): applies f(a); if the result is Ok(b), returns Both(e, b)
|
|
111
|
-
* to preserve the warning. Otherwise returns f(a) as-is.
|
|
106
|
+
* Chains These computations by passing the first value to f.
|
|
107
|
+
* Second propagates unchanged; First and Both apply f to the first value.
|
|
112
108
|
*
|
|
113
109
|
* @example
|
|
114
110
|
* ```ts
|
|
115
|
-
* const double = (n: number): These<
|
|
111
|
+
* const double = (n: number): These<number, string> => These.first(n * 2);
|
|
116
112
|
*
|
|
117
|
-
* pipe(These.
|
|
118
|
-
* pipe(These.both("warn"
|
|
119
|
-
* pipe(These.
|
|
113
|
+
* pipe(These.first(5), These.chainFirst(double)); // First(10)
|
|
114
|
+
* pipe(These.both(5, "warn"), These.chainFirst(double)); // First(10)
|
|
115
|
+
* pipe(These.second("warn"), These.chainFirst(double)); // Second("warn")
|
|
120
116
|
* ```
|
|
121
117
|
*/
|
|
122
|
-
These.
|
|
123
|
-
if (These.
|
|
118
|
+
These.chainFirst = (f) => (data) => {
|
|
119
|
+
if (These.isSecond(data))
|
|
124
120
|
return data;
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
121
|
+
return f(data.first);
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* Chains These computations by passing the second value to f.
|
|
125
|
+
* First propagates unchanged; Second and Both apply f to the second value.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```ts
|
|
129
|
+
* const shout = (s: string): These<number, string> => These.second(s.toUpperCase());
|
|
130
|
+
*
|
|
131
|
+
* pipe(These.second("warn"), These.chainSecond(shout)); // Second("WARN")
|
|
132
|
+
* pipe(These.both(5, "warn"), These.chainSecond(shout)); // Second("WARN")
|
|
133
|
+
* pipe(These.first(5), These.chainSecond(shout)); // First(5)
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
These.chainSecond = (f) => (data) => {
|
|
137
|
+
if (These.isFirst(data))
|
|
138
|
+
return data;
|
|
139
|
+
return f(data.second);
|
|
129
140
|
};
|
|
130
141
|
/**
|
|
131
142
|
* Extracts a value from a These by providing handlers for all three cases.
|
|
@@ -135,19 +146,19 @@ export var These;
|
|
|
135
146
|
* pipe(
|
|
136
147
|
* these,
|
|
137
148
|
* These.fold(
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
* (
|
|
149
|
+
* a => `First: ${a}`,
|
|
150
|
+
* b => `Second: ${b}`,
|
|
151
|
+
* (a, b) => `Both: ${a} / ${b}`
|
|
141
152
|
* )
|
|
142
153
|
* );
|
|
143
154
|
* ```
|
|
144
155
|
*/
|
|
145
|
-
These.fold = (
|
|
146
|
-
if (These.
|
|
147
|
-
return
|
|
148
|
-
if (These.
|
|
149
|
-
return
|
|
150
|
-
return onBoth(data.
|
|
156
|
+
These.fold = (onFirst, onSecond, onBoth) => (data) => {
|
|
157
|
+
if (These.isSecond(data))
|
|
158
|
+
return onSecond(data.second);
|
|
159
|
+
if (These.isFirst(data))
|
|
160
|
+
return onFirst(data.first);
|
|
161
|
+
return onBoth(data.first, data.second);
|
|
151
162
|
};
|
|
152
163
|
/**
|
|
153
164
|
* Pattern matches on a These, returning the result of the matching case.
|
|
@@ -157,86 +168,74 @@ export var These;
|
|
|
157
168
|
* pipe(
|
|
158
169
|
* these,
|
|
159
170
|
* These.match({
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
* both: (
|
|
171
|
+
* first: a => `First: ${a}`,
|
|
172
|
+
* second: b => `Second: ${b}`,
|
|
173
|
+
* both: (a, b) => `Both: ${a} / ${b}`
|
|
163
174
|
* })
|
|
164
175
|
* );
|
|
165
176
|
* ```
|
|
166
177
|
*/
|
|
167
178
|
These.match = (cases) => (data) => {
|
|
168
|
-
if (These.
|
|
169
|
-
return cases.
|
|
170
|
-
if (These.
|
|
171
|
-
return cases.
|
|
172
|
-
return cases.both(data.
|
|
179
|
+
if (These.isSecond(data))
|
|
180
|
+
return cases.second(data.second);
|
|
181
|
+
if (These.isFirst(data))
|
|
182
|
+
return cases.first(data.first);
|
|
183
|
+
return cases.both(data.first, data.second);
|
|
173
184
|
};
|
|
174
185
|
/**
|
|
175
|
-
* Returns the
|
|
186
|
+
* Returns the first value, or a default if the These has no first value.
|
|
176
187
|
*
|
|
177
188
|
* @example
|
|
178
189
|
* ```ts
|
|
179
|
-
* pipe(These.
|
|
180
|
-
* pipe(These.both("warn"
|
|
181
|
-
* pipe(These.
|
|
190
|
+
* pipe(These.first(5), These.getFirstOrElse(0)); // 5
|
|
191
|
+
* pipe(These.both(5, "warn"), These.getFirstOrElse(0)); // 5
|
|
192
|
+
* pipe(These.second("warn"), These.getFirstOrElse(0)); // 0
|
|
182
193
|
* ```
|
|
183
194
|
*/
|
|
184
|
-
These.
|
|
195
|
+
These.getFirstOrElse = (defaultValue) => (data) => These.hasFirst(data) ? data.first : defaultValue;
|
|
185
196
|
/**
|
|
186
|
-
*
|
|
187
|
-
* Useful for logging or debugging.
|
|
188
|
-
*/
|
|
189
|
-
These.tap = (f) => (data) => {
|
|
190
|
-
if (These.hasValue(data))
|
|
191
|
-
f(data.value);
|
|
192
|
-
return data;
|
|
193
|
-
};
|
|
194
|
-
/**
|
|
195
|
-
* Swaps the roles of error and success values.
|
|
196
|
-
* - Err(e) → Ok(e)
|
|
197
|
-
* - Ok(a) → Err(a)
|
|
198
|
-
* - Both(e, a) → Both(a, e)
|
|
197
|
+
* Returns the second value, or a default if the These has no second value.
|
|
199
198
|
*
|
|
200
199
|
* @example
|
|
201
200
|
* ```ts
|
|
202
|
-
* These.
|
|
203
|
-
* These.
|
|
204
|
-
* These.
|
|
201
|
+
* pipe(These.second("warn"), These.getSecondOrElse("none")); // "warn"
|
|
202
|
+
* pipe(These.both(5, "warn"), These.getSecondOrElse("none")); // "warn"
|
|
203
|
+
* pipe(These.first(5), These.getSecondOrElse("none")); // "none"
|
|
205
204
|
* ```
|
|
206
205
|
*/
|
|
207
|
-
These.
|
|
208
|
-
if (These.isErr(data))
|
|
209
|
-
return These.ok(data.error);
|
|
210
|
-
if (These.isOk(data))
|
|
211
|
-
return These.err(data.value);
|
|
212
|
-
return These.both(data.value, data.error);
|
|
213
|
-
};
|
|
206
|
+
These.getSecondOrElse = (defaultValue) => (data) => These.hasSecond(data) ? data.second : defaultValue;
|
|
214
207
|
/**
|
|
215
|
-
*
|
|
216
|
-
*
|
|
208
|
+
* Executes a side effect on the first value without changing the These.
|
|
209
|
+
* Useful for logging or debugging.
|
|
217
210
|
*
|
|
218
211
|
* @example
|
|
219
212
|
* ```ts
|
|
220
|
-
* These.
|
|
221
|
-
* These.toOption(These.both("warn", 42)); // Some(42)
|
|
222
|
-
* These.toOption(These.err("err")); // None
|
|
213
|
+
* pipe(These.first(5), These.tap(console.log)); // logs 5, returns First(5)
|
|
223
214
|
* ```
|
|
224
215
|
*/
|
|
225
|
-
These.
|
|
216
|
+
These.tap = (f) => (data) => {
|
|
217
|
+
if (These.hasFirst(data))
|
|
218
|
+
f(data.first);
|
|
219
|
+
return data;
|
|
220
|
+
};
|
|
226
221
|
/**
|
|
227
|
-
*
|
|
228
|
-
*
|
|
222
|
+
* Swaps the roles of first and second values.
|
|
223
|
+
* - First(a) → Second(a)
|
|
224
|
+
* - Second(b) → First(b)
|
|
225
|
+
* - Both(a, b) → Both(b, a)
|
|
229
226
|
*
|
|
230
227
|
* @example
|
|
231
228
|
* ```ts
|
|
232
|
-
* These.
|
|
233
|
-
* These.
|
|
234
|
-
* These.
|
|
229
|
+
* These.swap(These.first(5)); // Second(5)
|
|
230
|
+
* These.swap(These.second("warn")); // First("warn")
|
|
231
|
+
* These.swap(These.both(5, "warn")); // Both("warn", 5)
|
|
235
232
|
* ```
|
|
236
233
|
*/
|
|
237
|
-
These.
|
|
238
|
-
if (These.
|
|
239
|
-
return
|
|
240
|
-
|
|
234
|
+
These.swap = (data) => {
|
|
235
|
+
if (These.isSecond(data))
|
|
236
|
+
return These.first(data.second);
|
|
237
|
+
if (These.isFirst(data))
|
|
238
|
+
return These.second(data.first);
|
|
239
|
+
return These.both(data.second, data.first);
|
|
241
240
|
};
|
|
242
241
|
})(These || (These = {}));
|
|
@@ -6,56 +6,55 @@ export var Validation;
|
|
|
6
6
|
*
|
|
7
7
|
* @example
|
|
8
8
|
* ```ts
|
|
9
|
-
* Validation.
|
|
9
|
+
* Validation.valid(42); // Valid(42)
|
|
10
10
|
* ```
|
|
11
11
|
*/
|
|
12
|
-
Validation.
|
|
12
|
+
Validation.valid = (value) => ({
|
|
13
13
|
kind: "Valid",
|
|
14
14
|
value,
|
|
15
15
|
});
|
|
16
16
|
/**
|
|
17
|
-
* Creates
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
*
|
|
17
|
+
* Creates an invalid Validation from a single error.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* Validation.invalid("Invalid input");
|
|
22
|
+
* ```
|
|
22
23
|
*/
|
|
23
|
-
Validation.
|
|
24
|
+
Validation.invalid = (error) => ({
|
|
25
|
+
kind: "Invalid",
|
|
26
|
+
errors: [error],
|
|
27
|
+
});
|
|
24
28
|
/**
|
|
25
|
-
* Creates an invalid Validation
|
|
29
|
+
* Creates an invalid Validation from multiple errors.
|
|
26
30
|
*
|
|
27
31
|
* @example
|
|
28
32
|
* ```ts
|
|
29
|
-
* Validation.
|
|
33
|
+
* Validation.invalidAll(["Invalid input"]);
|
|
30
34
|
* ```
|
|
31
35
|
*/
|
|
32
|
-
Validation.
|
|
36
|
+
Validation.invalidAll = (errors) => ({
|
|
33
37
|
kind: "Invalid",
|
|
34
38
|
errors,
|
|
35
39
|
});
|
|
36
40
|
/**
|
|
37
|
-
* Type guard that checks if a Validation is
|
|
41
|
+
* Type guard that checks if a Validation is valid.
|
|
38
42
|
*/
|
|
39
|
-
Validation.
|
|
43
|
+
Validation.isValid = (data) => data.kind === "Valid";
|
|
40
44
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* @example
|
|
44
|
-
* ```ts
|
|
45
|
-
* Validation.fail("Invalid input");
|
|
46
|
-
* ```
|
|
45
|
+
* Type guard that checks if a Validation is invalid.
|
|
47
46
|
*/
|
|
48
|
-
Validation.
|
|
47
|
+
Validation.isInvalid = (data) => data.kind === "Invalid";
|
|
49
48
|
/**
|
|
50
49
|
* Transforms the success value inside a Validation.
|
|
51
50
|
*
|
|
52
51
|
* @example
|
|
53
52
|
* ```ts
|
|
54
|
-
* pipe(Validation.
|
|
55
|
-
* pipe(Validation.
|
|
53
|
+
* pipe(Validation.valid(5), Validation.map(n => n * 2)); // Valid(10)
|
|
54
|
+
* pipe(Validation.invalid("oops"), Validation.map(n => n * 2)); // Invalid(["oops"])
|
|
56
55
|
* ```
|
|
57
56
|
*/
|
|
58
|
-
Validation.map = (f) => (data) => Validation.isValid(data) ? Validation.
|
|
57
|
+
Validation.map = (f) => (data) => Validation.isValid(data) ? Validation.valid(f(data.value)) : data;
|
|
59
58
|
/**
|
|
60
59
|
* Chains Validation computations. If the first is Valid, passes the value to f.
|
|
61
60
|
* If the first is Invalid, propagates the errors.
|
|
@@ -65,10 +64,10 @@ export var Validation;
|
|
|
65
64
|
* @example
|
|
66
65
|
* ```ts
|
|
67
66
|
* const validatePositive = (n: number): Validation<string, number> =>
|
|
68
|
-
* n > 0 ? Validation.
|
|
67
|
+
* n > 0 ? Validation.valid(n) : Validation.invalid("Must be positive");
|
|
69
68
|
*
|
|
70
|
-
* pipe(Validation.
|
|
71
|
-
* pipe(Validation.
|
|
69
|
+
* pipe(Validation.valid(5), Validation.chain(validatePositive)); // Valid(5)
|
|
70
|
+
* pipe(Validation.valid(-1), Validation.chain(validatePositive)); // Invalid(["Must be positive"])
|
|
72
71
|
* ```
|
|
73
72
|
*/
|
|
74
73
|
Validation.chain = (f) => (data) => Validation.isValid(data) ? f(data.value) : data;
|
|
@@ -80,26 +79,26 @@ export var Validation;
|
|
|
80
79
|
* ```ts
|
|
81
80
|
* const add = (a: number) => (b: number) => a + b;
|
|
82
81
|
* pipe(
|
|
83
|
-
* Validation.
|
|
84
|
-
* Validation.ap(Validation.
|
|
85
|
-
* Validation.ap(Validation.
|
|
82
|
+
* Validation.valid(add),
|
|
83
|
+
* Validation.ap(Validation.valid(5)),
|
|
84
|
+
* Validation.ap(Validation.valid(3))
|
|
86
85
|
* ); // Valid(8)
|
|
87
86
|
*
|
|
88
87
|
* pipe(
|
|
89
|
-
* Validation.
|
|
90
|
-
* Validation.ap(Validation.
|
|
91
|
-
* Validation.ap(Validation.
|
|
88
|
+
* Validation.valid(add),
|
|
89
|
+
* Validation.ap(Validation.invalid<string, number>("bad a")),
|
|
90
|
+
* Validation.ap(Validation.invalid<string, number>("bad b"))
|
|
92
91
|
* ); // Invalid(["bad a", "bad b"])
|
|
93
92
|
* ```
|
|
94
93
|
*/
|
|
95
94
|
Validation.ap = (arg) => (data) => {
|
|
96
95
|
if (Validation.isValid(data) && Validation.isValid(arg))
|
|
97
|
-
return Validation.
|
|
96
|
+
return Validation.valid(data.value(arg.value));
|
|
98
97
|
const errors = [
|
|
99
98
|
...(Validation.isInvalid(data) ? data.errors : []),
|
|
100
99
|
...(Validation.isInvalid(arg) ? arg.errors : []),
|
|
101
100
|
];
|
|
102
|
-
return isNonEmptyList(errors) ? Validation.
|
|
101
|
+
return isNonEmptyList(errors) ? Validation.invalidAll(errors) : Validation.valid(data);
|
|
103
102
|
};
|
|
104
103
|
/**
|
|
105
104
|
* Extracts the value from a Validation by providing handlers for both cases.
|
|
@@ -107,7 +106,7 @@ export var Validation;
|
|
|
107
106
|
* @example
|
|
108
107
|
* ```ts
|
|
109
108
|
* pipe(
|
|
110
|
-
* Validation.
|
|
109
|
+
* Validation.valid(42),
|
|
111
110
|
* Validation.fold(
|
|
112
111
|
* errors => `Errors: ${errors.join(", ")}`,
|
|
113
112
|
* value => `Value: ${value}`
|
|
@@ -136,8 +135,8 @@ export var Validation;
|
|
|
136
135
|
*
|
|
137
136
|
* @example
|
|
138
137
|
* ```ts
|
|
139
|
-
* pipe(Validation.
|
|
140
|
-
* pipe(Validation.
|
|
138
|
+
* pipe(Validation.valid(5), Validation.getOrElse(0)); // 5
|
|
139
|
+
* pipe(Validation.invalid("oops"), Validation.getOrElse(0)); // 0
|
|
141
140
|
* ```
|
|
142
141
|
*/
|
|
143
142
|
Validation.getOrElse = (defaultValue) => (data) => Validation.isValid(data) ? data.value : defaultValue;
|
|
@@ -147,7 +146,7 @@ export var Validation;
|
|
|
147
146
|
* @example
|
|
148
147
|
* ```ts
|
|
149
148
|
* pipe(
|
|
150
|
-
* Validation.
|
|
149
|
+
* Validation.valid(5),
|
|
151
150
|
* Validation.tap(n => console.log("Value:", n)),
|
|
152
151
|
* Validation.map(n => n * 2)
|
|
153
152
|
* );
|
|
@@ -177,13 +176,13 @@ export var Validation;
|
|
|
177
176
|
* @example
|
|
178
177
|
* ```ts
|
|
179
178
|
* Validation.combine(
|
|
180
|
-
* Validation.
|
|
181
|
-
* Validation.
|
|
179
|
+
* Validation.invalid("Error 1"),
|
|
180
|
+
* Validation.invalid("Error 2")
|
|
182
181
|
* ); // Invalid(["Error 1", "Error 2"])
|
|
183
182
|
*
|
|
184
183
|
* Validation.combine(
|
|
185
|
-
* Validation.
|
|
186
|
-
* Validation.
|
|
184
|
+
* Validation.valid("a"),
|
|
185
|
+
* Validation.valid("b")
|
|
187
186
|
* ); // Valid("b")
|
|
188
187
|
* ```
|
|
189
188
|
*/
|
|
@@ -195,7 +194,7 @@ export var Validation;
|
|
|
195
194
|
...(Validation.isInvalid(first) ? first.errors : []),
|
|
196
195
|
...(Validation.isInvalid(second) ? second.errors : []),
|
|
197
196
|
];
|
|
198
|
-
return isNonEmptyList(errors) ? Validation.
|
|
197
|
+
return isNonEmptyList(errors) ? Validation.invalidAll(errors) : second;
|
|
199
198
|
};
|
|
200
199
|
/**
|
|
201
200
|
* Combines multiple Validation instances, accumulating all errors.
|
package/package.json
CHANGED
|
@@ -9,13 +9,13 @@ exports.flip = void 0;
|
|
|
9
9
|
* ```ts
|
|
10
10
|
* // Original data-last (for pipe)
|
|
11
11
|
* pipe(
|
|
12
|
-
* Option.
|
|
12
|
+
* Option.some(5),
|
|
13
13
|
* Option.map(n => n * 2)
|
|
14
14
|
* ); // Some(10)
|
|
15
15
|
*
|
|
16
16
|
* // Flipped to data-first
|
|
17
17
|
* const mapFirst = flip(Option.map);
|
|
18
|
-
* mapFirst(Option.
|
|
18
|
+
* mapFirst(Option.some(5))(n => n * 2); // Some(10)
|
|
19
19
|
* ```
|
|
20
20
|
*
|
|
21
21
|
* @see {@link uncurry} for converting curried functions to multi-argument functions
|
|
@@ -7,7 +7,7 @@ exports.once = exports.or = exports.and = exports.constVoid = exports.constUndef
|
|
|
7
7
|
* @example
|
|
8
8
|
* ```ts
|
|
9
9
|
* identity(42); // 42
|
|
10
|
-
* pipe(Option.
|
|
10
|
+
* pipe(Option.some(5), Option.fold(() => 0, identity)); // 5
|
|
11
11
|
* ```
|
|
12
12
|
*/
|
|
13
13
|
const identity = (a) => a;
|
package/script/src/Core/Arr.js
CHANGED
|
@@ -306,7 +306,7 @@ var Arr;
|
|
|
306
306
|
* ```ts
|
|
307
307
|
* const parseNum = (s: string): Option<number> => {
|
|
308
308
|
* const n = Number(s);
|
|
309
|
-
* return isNaN(n) ? Option.none() : Option.
|
|
309
|
+
* return isNaN(n) ? Option.none() : Option.some(n);
|
|
310
310
|
* };
|
|
311
311
|
*
|
|
312
312
|
* pipe(["1", "2", "3"], Arr.traverse(parseNum)); // Some([1, 2, 3])
|
|
@@ -352,7 +352,7 @@ var Arr;
|
|
|
352
352
|
* ```ts
|
|
353
353
|
* pipe(
|
|
354
354
|
* [1, 2, 3],
|
|
355
|
-
* Arr.traverseTask(n => Task.
|
|
355
|
+
* Arr.traverseTask(n => Task.resolve(n * 2))
|
|
356
356
|
* )(); // Promise<[2, 4, 6]>
|
|
357
357
|
* ```
|
|
358
358
|
*/
|
|
@@ -363,8 +363,8 @@ var Arr;
|
|
|
363
363
|
*
|
|
364
364
|
* @example
|
|
365
365
|
* ```ts
|
|
366
|
-
* Arr.sequence([Option.
|
|
367
|
-
* Arr.sequence([Option.
|
|
366
|
+
* Arr.sequence([Option.some(1), Option.some(2)]); // Some([1, 2])
|
|
367
|
+
* Arr.sequence([Option.some(1), Option.none()]); // None
|
|
368
368
|
* ```
|
|
369
369
|
*/
|
|
370
370
|
Arr.sequence = (data) => Arr.traverse((a) => a)(data);
|