@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/script/src/Core/These.js
CHANGED
|
@@ -1,134 +1,145 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.These = void 0;
|
|
4
|
-
const Result_js_1 = require("./Result.js");
|
|
5
4
|
var These;
|
|
6
5
|
(function (These) {
|
|
7
6
|
/**
|
|
8
|
-
* Creates a These holding only
|
|
7
|
+
* Creates a These holding only a first value.
|
|
9
8
|
*
|
|
10
9
|
* @example
|
|
11
10
|
* ```ts
|
|
12
|
-
* These.
|
|
11
|
+
* These.first(42); // { kind: "First", first: 42 }
|
|
13
12
|
* ```
|
|
14
13
|
*/
|
|
15
|
-
These.
|
|
14
|
+
These.first = (value) => ({ kind: "First", first: value });
|
|
16
15
|
/**
|
|
17
|
-
* Creates a These holding only a
|
|
16
|
+
* Creates a These holding only a second value.
|
|
18
17
|
*
|
|
19
18
|
* @example
|
|
20
19
|
* ```ts
|
|
21
|
-
* These.
|
|
20
|
+
* These.second("warning"); // { kind: "Second", second: "warning" }
|
|
22
21
|
* ```
|
|
23
22
|
*/
|
|
24
|
-
These.
|
|
23
|
+
These.second = (value) => ({ kind: "Second", second: value });
|
|
25
24
|
/**
|
|
26
|
-
* Creates a These holding both
|
|
25
|
+
* Creates a These holding both a first and a second value simultaneously.
|
|
27
26
|
*
|
|
28
27
|
* @example
|
|
29
28
|
* ```ts
|
|
30
|
-
* These.both("Deprecated API used",
|
|
29
|
+
* These.both(42, "Deprecated API used"); // { kind: "Both", first: 42, second: "Deprecated API used" }
|
|
31
30
|
* ```
|
|
32
31
|
*/
|
|
33
|
-
These.both = (
|
|
32
|
+
These.both = (first, second) => ({
|
|
34
33
|
kind: "Both",
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
first,
|
|
35
|
+
second,
|
|
37
36
|
});
|
|
38
37
|
/**
|
|
39
|
-
* Type guard — checks if a These holds only
|
|
38
|
+
* Type guard — checks if a These holds only a first value.
|
|
40
39
|
*/
|
|
41
|
-
These.
|
|
40
|
+
These.isFirst = (data) => data.kind === "First";
|
|
42
41
|
/**
|
|
43
|
-
* Type guard — checks if a These holds only a
|
|
42
|
+
* Type guard — checks if a These holds only a second value.
|
|
44
43
|
*/
|
|
45
|
-
These.
|
|
44
|
+
These.isSecond = (data) => data.kind === "Second";
|
|
46
45
|
/**
|
|
47
|
-
* Type guard — checks if a These holds both
|
|
46
|
+
* Type guard — checks if a These holds both values simultaneously.
|
|
48
47
|
*/
|
|
49
48
|
These.isBoth = (data) => data.kind === "Both";
|
|
50
49
|
/**
|
|
51
|
-
* Returns true if the These contains a
|
|
50
|
+
* Returns true if the These contains a first value (First or Both).
|
|
52
51
|
*/
|
|
53
|
-
These.
|
|
52
|
+
These.hasFirst = (data) => data.kind === "First" || data.kind === "Both";
|
|
54
53
|
/**
|
|
55
|
-
* Returns true if the These contains
|
|
54
|
+
* Returns true if the These contains a second value (Second or Both).
|
|
56
55
|
*/
|
|
57
|
-
These.
|
|
56
|
+
These.hasSecond = (data) => data.kind === "Second" || data.kind === "Both";
|
|
58
57
|
/**
|
|
59
|
-
* Transforms the
|
|
58
|
+
* Transforms the first value, leaving the second unchanged.
|
|
60
59
|
*
|
|
61
60
|
* @example
|
|
62
61
|
* ```ts
|
|
63
|
-
* pipe(These.
|
|
64
|
-
* pipe(These.both("warn"
|
|
65
|
-
* pipe(These.
|
|
62
|
+
* pipe(These.first(5), These.mapFirst(n => n * 2)); // First(10)
|
|
63
|
+
* pipe(These.both(5, "warn"), These.mapFirst(n => n * 2)); // Both(10, "warn")
|
|
64
|
+
* pipe(These.second("warn"), These.mapFirst(n => n * 2)); // Second("warn")
|
|
66
65
|
* ```
|
|
67
66
|
*/
|
|
68
|
-
These.
|
|
69
|
-
if (These.
|
|
67
|
+
These.mapFirst = (f) => (data) => {
|
|
68
|
+
if (These.isSecond(data))
|
|
70
69
|
return data;
|
|
71
|
-
if (These.
|
|
72
|
-
return These.
|
|
73
|
-
return These.both(data.
|
|
70
|
+
if (These.isFirst(data))
|
|
71
|
+
return These.first(f(data.first));
|
|
72
|
+
return These.both(f(data.first), data.second);
|
|
74
73
|
};
|
|
75
74
|
/**
|
|
76
|
-
* Transforms the
|
|
75
|
+
* Transforms the second value, leaving the first unchanged.
|
|
77
76
|
*
|
|
78
77
|
* @example
|
|
79
78
|
* ```ts
|
|
80
|
-
* pipe(These.
|
|
81
|
-
* pipe(These.both("warn"
|
|
79
|
+
* pipe(These.second("warn"), These.mapSecond(e => e.toUpperCase())); // Second("WARN")
|
|
80
|
+
* pipe(These.both(5, "warn"), These.mapSecond(e => e.toUpperCase())); // Both(5, "WARN")
|
|
82
81
|
* ```
|
|
83
82
|
*/
|
|
84
|
-
These.
|
|
85
|
-
if (These.
|
|
83
|
+
These.mapSecond = (f) => (data) => {
|
|
84
|
+
if (These.isFirst(data))
|
|
86
85
|
return data;
|
|
87
|
-
if (These.
|
|
88
|
-
return These.
|
|
89
|
-
return These.both(
|
|
86
|
+
if (These.isSecond(data))
|
|
87
|
+
return These.second(f(data.second));
|
|
88
|
+
return These.both(data.first, f(data.second));
|
|
90
89
|
};
|
|
91
90
|
/**
|
|
92
|
-
* Transforms both the
|
|
91
|
+
* Transforms both the first and second values independently.
|
|
93
92
|
*
|
|
94
93
|
* @example
|
|
95
94
|
* ```ts
|
|
96
95
|
* pipe(
|
|
97
|
-
* These.both("warn"
|
|
98
|
-
* These.
|
|
99
|
-
* ); // Both("WARN"
|
|
96
|
+
* These.both(5, "warn"),
|
|
97
|
+
* These.mapBoth(n => n * 2, e => e.toUpperCase())
|
|
98
|
+
* ); // Both(10, "WARN")
|
|
100
99
|
* ```
|
|
101
100
|
*/
|
|
102
|
-
These.
|
|
103
|
-
if (These.
|
|
104
|
-
return These.
|
|
105
|
-
if (These.
|
|
106
|
-
return These.
|
|
107
|
-
return These.both(
|
|
101
|
+
These.mapBoth = (onFirst, onSecond) => (data) => {
|
|
102
|
+
if (These.isSecond(data))
|
|
103
|
+
return These.second(onSecond(data.second));
|
|
104
|
+
if (These.isFirst(data))
|
|
105
|
+
return These.first(onFirst(data.first));
|
|
106
|
+
return These.both(onFirst(data.first), onSecond(data.second));
|
|
108
107
|
};
|
|
109
108
|
/**
|
|
110
|
-
* Chains These computations by passing the
|
|
111
|
-
*
|
|
112
|
-
* - Ok(a) applies f(a) directly.
|
|
113
|
-
* - Both(e, a): applies f(a); if the result is Ok(b), returns Both(e, b)
|
|
114
|
-
* to preserve the warning. Otherwise returns f(a) as-is.
|
|
109
|
+
* Chains These computations by passing the first value to f.
|
|
110
|
+
* Second propagates unchanged; First and Both apply f to the first value.
|
|
115
111
|
*
|
|
116
112
|
* @example
|
|
117
113
|
* ```ts
|
|
118
|
-
* const double = (n: number): These<
|
|
114
|
+
* const double = (n: number): These<number, string> => These.first(n * 2);
|
|
119
115
|
*
|
|
120
|
-
* pipe(These.
|
|
121
|
-
* pipe(These.both("warn"
|
|
122
|
-
* pipe(These.
|
|
116
|
+
* pipe(These.first(5), These.chainFirst(double)); // First(10)
|
|
117
|
+
* pipe(These.both(5, "warn"), These.chainFirst(double)); // First(10)
|
|
118
|
+
* pipe(These.second("warn"), These.chainFirst(double)); // Second("warn")
|
|
123
119
|
* ```
|
|
124
120
|
*/
|
|
125
|
-
These.
|
|
126
|
-
if (These.
|
|
121
|
+
These.chainFirst = (f) => (data) => {
|
|
122
|
+
if (These.isSecond(data))
|
|
127
123
|
return data;
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
124
|
+
return f(data.first);
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* Chains These computations by passing the second value to f.
|
|
128
|
+
* First propagates unchanged; Second and Both apply f to the second value.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```ts
|
|
132
|
+
* const shout = (s: string): These<number, string> => These.second(s.toUpperCase());
|
|
133
|
+
*
|
|
134
|
+
* pipe(These.second("warn"), These.chainSecond(shout)); // Second("WARN")
|
|
135
|
+
* pipe(These.both(5, "warn"), These.chainSecond(shout)); // Second("WARN")
|
|
136
|
+
* pipe(These.first(5), These.chainSecond(shout)); // First(5)
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
These.chainSecond = (f) => (data) => {
|
|
140
|
+
if (These.isFirst(data))
|
|
141
|
+
return data;
|
|
142
|
+
return f(data.second);
|
|
132
143
|
};
|
|
133
144
|
/**
|
|
134
145
|
* Extracts a value from a These by providing handlers for all three cases.
|
|
@@ -138,19 +149,19 @@ var These;
|
|
|
138
149
|
* pipe(
|
|
139
150
|
* these,
|
|
140
151
|
* These.fold(
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
* (
|
|
152
|
+
* a => `First: ${a}`,
|
|
153
|
+
* b => `Second: ${b}`,
|
|
154
|
+
* (a, b) => `Both: ${a} / ${b}`
|
|
144
155
|
* )
|
|
145
156
|
* );
|
|
146
157
|
* ```
|
|
147
158
|
*/
|
|
148
|
-
These.fold = (
|
|
149
|
-
if (These.
|
|
150
|
-
return
|
|
151
|
-
if (These.
|
|
152
|
-
return
|
|
153
|
-
return onBoth(data.
|
|
159
|
+
These.fold = (onFirst, onSecond, onBoth) => (data) => {
|
|
160
|
+
if (These.isSecond(data))
|
|
161
|
+
return onSecond(data.second);
|
|
162
|
+
if (These.isFirst(data))
|
|
163
|
+
return onFirst(data.first);
|
|
164
|
+
return onBoth(data.first, data.second);
|
|
154
165
|
};
|
|
155
166
|
/**
|
|
156
167
|
* Pattern matches on a These, returning the result of the matching case.
|
|
@@ -160,86 +171,74 @@ var These;
|
|
|
160
171
|
* pipe(
|
|
161
172
|
* these,
|
|
162
173
|
* These.match({
|
|
163
|
-
*
|
|
164
|
-
*
|
|
165
|
-
* both: (
|
|
174
|
+
* first: a => `First: ${a}`,
|
|
175
|
+
* second: b => `Second: ${b}`,
|
|
176
|
+
* both: (a, b) => `Both: ${a} / ${b}`
|
|
166
177
|
* })
|
|
167
178
|
* );
|
|
168
179
|
* ```
|
|
169
180
|
*/
|
|
170
181
|
These.match = (cases) => (data) => {
|
|
171
|
-
if (These.
|
|
172
|
-
return cases.
|
|
173
|
-
if (These.
|
|
174
|
-
return cases.
|
|
175
|
-
return cases.both(data.
|
|
182
|
+
if (These.isSecond(data))
|
|
183
|
+
return cases.second(data.second);
|
|
184
|
+
if (These.isFirst(data))
|
|
185
|
+
return cases.first(data.first);
|
|
186
|
+
return cases.both(data.first, data.second);
|
|
176
187
|
};
|
|
177
188
|
/**
|
|
178
|
-
* Returns the
|
|
189
|
+
* Returns the first value, or a default if the These has no first value.
|
|
179
190
|
*
|
|
180
191
|
* @example
|
|
181
192
|
* ```ts
|
|
182
|
-
* pipe(These.
|
|
183
|
-
* pipe(These.both("warn"
|
|
184
|
-
* pipe(These.
|
|
193
|
+
* pipe(These.first(5), These.getFirstOrElse(0)); // 5
|
|
194
|
+
* pipe(These.both(5, "warn"), These.getFirstOrElse(0)); // 5
|
|
195
|
+
* pipe(These.second("warn"), These.getFirstOrElse(0)); // 0
|
|
185
196
|
* ```
|
|
186
197
|
*/
|
|
187
|
-
These.
|
|
198
|
+
These.getFirstOrElse = (defaultValue) => (data) => These.hasFirst(data) ? data.first : defaultValue;
|
|
188
199
|
/**
|
|
189
|
-
*
|
|
190
|
-
* Useful for logging or debugging.
|
|
191
|
-
*/
|
|
192
|
-
These.tap = (f) => (data) => {
|
|
193
|
-
if (These.hasValue(data))
|
|
194
|
-
f(data.value);
|
|
195
|
-
return data;
|
|
196
|
-
};
|
|
197
|
-
/**
|
|
198
|
-
* Swaps the roles of error and success values.
|
|
199
|
-
* - Err(e) → Ok(e)
|
|
200
|
-
* - Ok(a) → Err(a)
|
|
201
|
-
* - Both(e, a) → Both(a, e)
|
|
200
|
+
* Returns the second value, or a default if the These has no second value.
|
|
202
201
|
*
|
|
203
202
|
* @example
|
|
204
203
|
* ```ts
|
|
205
|
-
* These.
|
|
206
|
-
* These.
|
|
207
|
-
* These.
|
|
204
|
+
* pipe(These.second("warn"), These.getSecondOrElse("none")); // "warn"
|
|
205
|
+
* pipe(These.both(5, "warn"), These.getSecondOrElse("none")); // "warn"
|
|
206
|
+
* pipe(These.first(5), These.getSecondOrElse("none")); // "none"
|
|
208
207
|
* ```
|
|
209
208
|
*/
|
|
210
|
-
These.
|
|
211
|
-
if (These.isErr(data))
|
|
212
|
-
return These.ok(data.error);
|
|
213
|
-
if (These.isOk(data))
|
|
214
|
-
return These.err(data.value);
|
|
215
|
-
return These.both(data.value, data.error);
|
|
216
|
-
};
|
|
209
|
+
These.getSecondOrElse = (defaultValue) => (data) => These.hasSecond(data) ? data.second : defaultValue;
|
|
217
210
|
/**
|
|
218
|
-
*
|
|
219
|
-
*
|
|
211
|
+
* Executes a side effect on the first value without changing the These.
|
|
212
|
+
* Useful for logging or debugging.
|
|
220
213
|
*
|
|
221
214
|
* @example
|
|
222
215
|
* ```ts
|
|
223
|
-
* These.
|
|
224
|
-
* These.toOption(These.both("warn", 42)); // Some(42)
|
|
225
|
-
* These.toOption(These.err("err")); // None
|
|
216
|
+
* pipe(These.first(5), These.tap(console.log)); // logs 5, returns First(5)
|
|
226
217
|
* ```
|
|
227
218
|
*/
|
|
228
|
-
These.
|
|
219
|
+
These.tap = (f) => (data) => {
|
|
220
|
+
if (These.hasFirst(data))
|
|
221
|
+
f(data.first);
|
|
222
|
+
return data;
|
|
223
|
+
};
|
|
229
224
|
/**
|
|
230
|
-
*
|
|
231
|
-
*
|
|
225
|
+
* Swaps the roles of first and second values.
|
|
226
|
+
* - First(a) → Second(a)
|
|
227
|
+
* - Second(b) → First(b)
|
|
228
|
+
* - Both(a, b) → Both(b, a)
|
|
232
229
|
*
|
|
233
230
|
* @example
|
|
234
231
|
* ```ts
|
|
235
|
-
* These.
|
|
236
|
-
* These.
|
|
237
|
-
* These.
|
|
232
|
+
* These.swap(These.first(5)); // Second(5)
|
|
233
|
+
* These.swap(These.second("warn")); // First("warn")
|
|
234
|
+
* These.swap(These.both(5, "warn")); // Both("warn", 5)
|
|
238
235
|
* ```
|
|
239
236
|
*/
|
|
240
|
-
These.
|
|
241
|
-
if (These.
|
|
242
|
-
return
|
|
243
|
-
|
|
237
|
+
These.swap = (data) => {
|
|
238
|
+
if (These.isSecond(data))
|
|
239
|
+
return These.first(data.second);
|
|
240
|
+
if (These.isFirst(data))
|
|
241
|
+
return These.second(data.first);
|
|
242
|
+
return These.both(data.second, data.first);
|
|
244
243
|
};
|
|
245
244
|
})(These || (exports.These = These = {}));
|
|
@@ -9,56 +9,55 @@ var Validation;
|
|
|
9
9
|
*
|
|
10
10
|
* @example
|
|
11
11
|
* ```ts
|
|
12
|
-
* Validation.
|
|
12
|
+
* Validation.valid(42); // Valid(42)
|
|
13
13
|
* ```
|
|
14
14
|
*/
|
|
15
|
-
Validation.
|
|
15
|
+
Validation.valid = (value) => ({
|
|
16
16
|
kind: "Valid",
|
|
17
17
|
value,
|
|
18
18
|
});
|
|
19
19
|
/**
|
|
20
|
-
* Creates
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
*
|
|
20
|
+
* Creates an invalid Validation from a single error.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* Validation.invalid("Invalid input");
|
|
25
|
+
* ```
|
|
25
26
|
*/
|
|
26
|
-
Validation.
|
|
27
|
+
Validation.invalid = (error) => ({
|
|
28
|
+
kind: "Invalid",
|
|
29
|
+
errors: [error],
|
|
30
|
+
});
|
|
27
31
|
/**
|
|
28
|
-
* Creates an invalid Validation
|
|
32
|
+
* Creates an invalid Validation from multiple errors.
|
|
29
33
|
*
|
|
30
34
|
* @example
|
|
31
35
|
* ```ts
|
|
32
|
-
* Validation.
|
|
36
|
+
* Validation.invalidAll(["Invalid input"]);
|
|
33
37
|
* ```
|
|
34
38
|
*/
|
|
35
|
-
Validation.
|
|
39
|
+
Validation.invalidAll = (errors) => ({
|
|
36
40
|
kind: "Invalid",
|
|
37
41
|
errors,
|
|
38
42
|
});
|
|
39
43
|
/**
|
|
40
|
-
* Type guard that checks if a Validation is
|
|
44
|
+
* Type guard that checks if a Validation is valid.
|
|
41
45
|
*/
|
|
42
|
-
Validation.
|
|
46
|
+
Validation.isValid = (data) => data.kind === "Valid";
|
|
43
47
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* @example
|
|
47
|
-
* ```ts
|
|
48
|
-
* Validation.fail("Invalid input");
|
|
49
|
-
* ```
|
|
48
|
+
* Type guard that checks if a Validation is invalid.
|
|
50
49
|
*/
|
|
51
|
-
Validation.
|
|
50
|
+
Validation.isInvalid = (data) => data.kind === "Invalid";
|
|
52
51
|
/**
|
|
53
52
|
* Transforms the success value inside a Validation.
|
|
54
53
|
*
|
|
55
54
|
* @example
|
|
56
55
|
* ```ts
|
|
57
|
-
* pipe(Validation.
|
|
58
|
-
* pipe(Validation.
|
|
56
|
+
* pipe(Validation.valid(5), Validation.map(n => n * 2)); // Valid(10)
|
|
57
|
+
* pipe(Validation.invalid("oops"), Validation.map(n => n * 2)); // Invalid(["oops"])
|
|
59
58
|
* ```
|
|
60
59
|
*/
|
|
61
|
-
Validation.map = (f) => (data) => Validation.isValid(data) ? Validation.
|
|
60
|
+
Validation.map = (f) => (data) => Validation.isValid(data) ? Validation.valid(f(data.value)) : data;
|
|
62
61
|
/**
|
|
63
62
|
* Chains Validation computations. If the first is Valid, passes the value to f.
|
|
64
63
|
* If the first is Invalid, propagates the errors.
|
|
@@ -68,10 +67,10 @@ var Validation;
|
|
|
68
67
|
* @example
|
|
69
68
|
* ```ts
|
|
70
69
|
* const validatePositive = (n: number): Validation<string, number> =>
|
|
71
|
-
* n > 0 ? Validation.
|
|
70
|
+
* n > 0 ? Validation.valid(n) : Validation.invalid("Must be positive");
|
|
72
71
|
*
|
|
73
|
-
* pipe(Validation.
|
|
74
|
-
* pipe(Validation.
|
|
72
|
+
* pipe(Validation.valid(5), Validation.chain(validatePositive)); // Valid(5)
|
|
73
|
+
* pipe(Validation.valid(-1), Validation.chain(validatePositive)); // Invalid(["Must be positive"])
|
|
75
74
|
* ```
|
|
76
75
|
*/
|
|
77
76
|
Validation.chain = (f) => (data) => Validation.isValid(data) ? f(data.value) : data;
|
|
@@ -83,26 +82,26 @@ var Validation;
|
|
|
83
82
|
* ```ts
|
|
84
83
|
* const add = (a: number) => (b: number) => a + b;
|
|
85
84
|
* pipe(
|
|
86
|
-
* Validation.
|
|
87
|
-
* Validation.ap(Validation.
|
|
88
|
-
* Validation.ap(Validation.
|
|
85
|
+
* Validation.valid(add),
|
|
86
|
+
* Validation.ap(Validation.valid(5)),
|
|
87
|
+
* Validation.ap(Validation.valid(3))
|
|
89
88
|
* ); // Valid(8)
|
|
90
89
|
*
|
|
91
90
|
* pipe(
|
|
92
|
-
* Validation.
|
|
93
|
-
* Validation.ap(Validation.
|
|
94
|
-
* Validation.ap(Validation.
|
|
91
|
+
* Validation.valid(add),
|
|
92
|
+
* Validation.ap(Validation.invalid<string, number>("bad a")),
|
|
93
|
+
* Validation.ap(Validation.invalid<string, number>("bad b"))
|
|
95
94
|
* ); // Invalid(["bad a", "bad b"])
|
|
96
95
|
* ```
|
|
97
96
|
*/
|
|
98
97
|
Validation.ap = (arg) => (data) => {
|
|
99
98
|
if (Validation.isValid(data) && Validation.isValid(arg))
|
|
100
|
-
return Validation.
|
|
99
|
+
return Validation.valid(data.value(arg.value));
|
|
101
100
|
const errors = [
|
|
102
101
|
...(Validation.isInvalid(data) ? data.errors : []),
|
|
103
102
|
...(Validation.isInvalid(arg) ? arg.errors : []),
|
|
104
103
|
];
|
|
105
|
-
return (0, NonEmptyList_js_1.isNonEmptyList)(errors) ? Validation.
|
|
104
|
+
return (0, NonEmptyList_js_1.isNonEmptyList)(errors) ? Validation.invalidAll(errors) : Validation.valid(data);
|
|
106
105
|
};
|
|
107
106
|
/**
|
|
108
107
|
* Extracts the value from a Validation by providing handlers for both cases.
|
|
@@ -110,7 +109,7 @@ var Validation;
|
|
|
110
109
|
* @example
|
|
111
110
|
* ```ts
|
|
112
111
|
* pipe(
|
|
113
|
-
* Validation.
|
|
112
|
+
* Validation.valid(42),
|
|
114
113
|
* Validation.fold(
|
|
115
114
|
* errors => `Errors: ${errors.join(", ")}`,
|
|
116
115
|
* value => `Value: ${value}`
|
|
@@ -139,8 +138,8 @@ var Validation;
|
|
|
139
138
|
*
|
|
140
139
|
* @example
|
|
141
140
|
* ```ts
|
|
142
|
-
* pipe(Validation.
|
|
143
|
-
* pipe(Validation.
|
|
141
|
+
* pipe(Validation.valid(5), Validation.getOrElse(0)); // 5
|
|
142
|
+
* pipe(Validation.invalid("oops"), Validation.getOrElse(0)); // 0
|
|
144
143
|
* ```
|
|
145
144
|
*/
|
|
146
145
|
Validation.getOrElse = (defaultValue) => (data) => Validation.isValid(data) ? data.value : defaultValue;
|
|
@@ -150,7 +149,7 @@ var Validation;
|
|
|
150
149
|
* @example
|
|
151
150
|
* ```ts
|
|
152
151
|
* pipe(
|
|
153
|
-
* Validation.
|
|
152
|
+
* Validation.valid(5),
|
|
154
153
|
* Validation.tap(n => console.log("Value:", n)),
|
|
155
154
|
* Validation.map(n => n * 2)
|
|
156
155
|
* );
|
|
@@ -180,13 +179,13 @@ var Validation;
|
|
|
180
179
|
* @example
|
|
181
180
|
* ```ts
|
|
182
181
|
* Validation.combine(
|
|
183
|
-
* Validation.
|
|
184
|
-
* Validation.
|
|
182
|
+
* Validation.invalid("Error 1"),
|
|
183
|
+
* Validation.invalid("Error 2")
|
|
185
184
|
* ); // Invalid(["Error 1", "Error 2"])
|
|
186
185
|
*
|
|
187
186
|
* Validation.combine(
|
|
188
|
-
* Validation.
|
|
189
|
-
* Validation.
|
|
187
|
+
* Validation.valid("a"),
|
|
188
|
+
* Validation.valid("b")
|
|
190
189
|
* ); // Valid("b")
|
|
191
190
|
* ```
|
|
192
191
|
*/
|
|
@@ -198,7 +197,7 @@ var Validation;
|
|
|
198
197
|
...(Validation.isInvalid(first) ? first.errors : []),
|
|
199
198
|
...(Validation.isInvalid(second) ? second.errors : []),
|
|
200
199
|
];
|
|
201
|
-
return (0, NonEmptyList_js_1.isNonEmptyList)(errors) ? Validation.
|
|
200
|
+
return (0, NonEmptyList_js_1.isNonEmptyList)(errors) ? Validation.invalidAll(errors) : second;
|
|
202
201
|
};
|
|
203
202
|
/**
|
|
204
203
|
* Combines multiple Validation instances, accumulating all errors.
|
|
@@ -6,13 +6,13 @@
|
|
|
6
6
|
* ```ts
|
|
7
7
|
* // Original data-last (for pipe)
|
|
8
8
|
* pipe(
|
|
9
|
-
* Option.
|
|
9
|
+
* Option.some(5),
|
|
10
10
|
* Option.map(n => n * 2)
|
|
11
11
|
* ); // Some(10)
|
|
12
12
|
*
|
|
13
13
|
* // Flipped to data-first
|
|
14
14
|
* const mapFirst = flip(Option.map);
|
|
15
|
-
* mapFirst(Option.
|
|
15
|
+
* mapFirst(Option.some(5))(n => n * 2); // Some(10)
|
|
16
16
|
* ```
|
|
17
17
|
*
|
|
18
18
|
* @see {@link uncurry} for converting curried functions to multi-argument functions
|
|
@@ -11,15 +11,15 @@
|
|
|
11
11
|
* uncurry(nested)(); // 42
|
|
12
12
|
*
|
|
13
13
|
* // Original curried function
|
|
14
|
-
* Option.map(n => n * 2)(Option.
|
|
14
|
+
* Option.map(n => n * 2)(Option.some(5)); // Some(10)
|
|
15
15
|
*
|
|
16
16
|
* // Uncurried - all arguments at once
|
|
17
17
|
* const mapUncurried = uncurry(Option.map);
|
|
18
|
-
* mapUncurried(n => n * 2, Option.
|
|
18
|
+
* mapUncurried(n => n * 2, Option.some(5)); // Some(10)
|
|
19
19
|
*
|
|
20
20
|
* // Combined with flip for data-first uncurried
|
|
21
21
|
* const mapDataFirst = uncurry(flip(Option.map));
|
|
22
|
-
* mapDataFirst(Option.
|
|
22
|
+
* mapDataFirst(Option.some(5), n => n * 2); // Some(10)
|
|
23
23
|
* ```
|
|
24
24
|
*
|
|
25
25
|
* @see {@link flip} for reversing curried argument order
|
package/types/src/Core/Arr.d.ts
CHANGED
|
@@ -227,7 +227,7 @@ export declare namespace Arr {
|
|
|
227
227
|
* ```ts
|
|
228
228
|
* const parseNum = (s: string): Option<number> => {
|
|
229
229
|
* const n = Number(s);
|
|
230
|
-
* return isNaN(n) ? Option.none() : Option.
|
|
230
|
+
* return isNaN(n) ? Option.none() : Option.some(n);
|
|
231
231
|
* };
|
|
232
232
|
*
|
|
233
233
|
* pipe(["1", "2", "3"], Arr.traverse(parseNum)); // Some([1, 2, 3])
|
|
@@ -255,7 +255,7 @@ export declare namespace Arr {
|
|
|
255
255
|
* ```ts
|
|
256
256
|
* pipe(
|
|
257
257
|
* [1, 2, 3],
|
|
258
|
-
* Arr.traverseTask(n => Task.
|
|
258
|
+
* Arr.traverseTask(n => Task.resolve(n * 2))
|
|
259
259
|
* )(); // Promise<[2, 4, 6]>
|
|
260
260
|
* ```
|
|
261
261
|
*/
|
|
@@ -266,8 +266,8 @@ export declare namespace Arr {
|
|
|
266
266
|
*
|
|
267
267
|
* @example
|
|
268
268
|
* ```ts
|
|
269
|
-
* Arr.sequence([Option.
|
|
270
|
-
* Arr.sequence([Option.
|
|
269
|
+
* Arr.sequence([Option.some(1), Option.some(2)]); // Some([1, 2])
|
|
270
|
+
* Arr.sequence([Option.some(1), Option.none()]); // None
|
|
271
271
|
* ```
|
|
272
272
|
*/
|
|
273
273
|
const sequence: <A>(data: readonly Option<A>[]) => Option<readonly A[]>;
|
|
@@ -11,4 +11,10 @@ export type WithError<T> = {
|
|
|
11
11
|
export type WithErrors<T> = {
|
|
12
12
|
readonly errors: NonEmptyList<T>;
|
|
13
13
|
};
|
|
14
|
+
export type WithFirst<T> = {
|
|
15
|
+
readonly first: T;
|
|
16
|
+
};
|
|
17
|
+
export type WithSecond<T> = {
|
|
18
|
+
readonly second: T;
|
|
19
|
+
};
|
|
14
20
|
//# sourceMappingURL=InternalTypes.d.ts.map
|