@nlozgachev/pipekit 0.3.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/esm/src/Core/These.js +122 -123
- package/package.json +1 -1
- package/script/src/Core/These.js +122 -123
- package/types/src/Core/InternalTypes.d.ts +6 -0
- package/types/src/Core/InternalTypes.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/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 a
|
|
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
|
|
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 = {}));
|
package/package.json
CHANGED
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 a
|
|
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
|
|
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 = {}));
|
|
@@ -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
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"InternalTypes.d.ts","sourceRoot":"","sources":["../../../src/src/Core/InternalTypes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAExD,MAAM,MAAM,QAAQ,CAA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, WithValue } from "./InternalTypes.js";
|
|
1
|
+
import { WithFirst, WithKind, WithSecond } from "./InternalTypes.js";
|
|
3
2
|
/**
|
|
4
|
-
* These<
|
|
5
|
-
* value (
|
|
6
|
-
*
|
|
3
|
+
* These<A, B> is an inclusive-OR type: it holds a first value (A), a second
|
|
4
|
+
* value (B), or both simultaneously. Neither side carries a success/failure
|
|
5
|
+
* connotation — it is a neutral pair where any combination is valid.
|
|
7
6
|
*
|
|
8
|
-
* -
|
|
9
|
-
* -
|
|
10
|
-
* - Both(
|
|
7
|
+
* - First(a) — only a first value
|
|
8
|
+
* - Second(b) — only a second value
|
|
9
|
+
* - Both(a, b) — first and second values simultaneously
|
|
10
|
+
*
|
|
11
|
+
* A common use: lenient parsers or processors that carry a diagnostic note
|
|
12
|
+
* alongside a result, without losing either piece of information.
|
|
11
13
|
*
|
|
12
14
|
* @example
|
|
13
15
|
* ```ts
|
|
14
|
-
* const parse = (s: string): These<
|
|
15
|
-
* const
|
|
16
|
-
*
|
|
17
|
-
* if (
|
|
18
|
-
* return These.
|
|
16
|
+
* const parse = (s: string): These<number, string> => {
|
|
17
|
+
* const trimmed = s.trim();
|
|
18
|
+
* const n = parseFloat(trimmed);
|
|
19
|
+
* if (isNaN(n)) return These.second("Not a number");
|
|
20
|
+
* if (s !== trimmed) return These.both(n, "Leading/trailing whitespace trimmed");
|
|
21
|
+
* return These.first(n);
|
|
19
22
|
* };
|
|
20
23
|
* ```
|
|
21
24
|
*/
|
|
22
|
-
export type These<
|
|
23
|
-
export type
|
|
25
|
+
export type These<A, B> = TheseFirst<A> | TheseSecond<B> | TheseBoth<A, B>;
|
|
26
|
+
export type TheseFirst<T> = WithKind<"First"> & WithFirst<T>;
|
|
27
|
+
export type TheseSecond<T> = WithKind<"Second"> & WithSecond<T>;
|
|
28
|
+
export type TheseBoth<First, Second> = WithKind<"Both"> & WithFirst<First> & WithSecond<Second>;
|
|
24
29
|
export declare namespace These {
|
|
25
30
|
/**
|
|
26
|
-
* Creates a These holding only a
|
|
31
|
+
* Creates a These holding only a first value.
|
|
27
32
|
*
|
|
28
33
|
* @example
|
|
29
34
|
* ```ts
|
|
30
|
-
* These.
|
|
35
|
+
* These.first(42); // { kind: "First", first: 42 }
|
|
31
36
|
* ```
|
|
32
37
|
*/
|
|
33
|
-
const
|
|
38
|
+
const first: <A>(value: A) => TheseFirst<A>;
|
|
34
39
|
/**
|
|
35
|
-
* Creates a These holding only
|
|
40
|
+
* Creates a These holding only a second value.
|
|
36
41
|
*
|
|
37
42
|
* @example
|
|
38
43
|
* ```ts
|
|
39
|
-
* These.
|
|
44
|
+
* These.second("warning"); // { kind: "Second", second: "warning" }
|
|
40
45
|
* ```
|
|
41
46
|
*/
|
|
42
|
-
const
|
|
47
|
+
const second: <B>(value: B) => TheseSecond<B>;
|
|
43
48
|
/**
|
|
44
|
-
* Creates a These holding both
|
|
49
|
+
* Creates a These holding both a first and a second value simultaneously.
|
|
45
50
|
*
|
|
46
51
|
* @example
|
|
47
52
|
* ```ts
|
|
48
|
-
* These.both("Deprecated API used",
|
|
53
|
+
* These.both(42, "Deprecated API used"); // { kind: "Both", first: 42, second: "Deprecated API used" }
|
|
49
54
|
* ```
|
|
50
55
|
*/
|
|
51
|
-
const both: <
|
|
56
|
+
const both: <A, B>(first: A, second: B) => TheseBoth<A, B>;
|
|
52
57
|
/**
|
|
53
|
-
* Type guard — checks if a These holds only
|
|
58
|
+
* Type guard — checks if a These holds only a first value.
|
|
54
59
|
*/
|
|
55
|
-
const
|
|
60
|
+
const isFirst: <A, B>(data: These<A, B>) => data is TheseFirst<A>;
|
|
56
61
|
/**
|
|
57
|
-
* Type guard — checks if a These holds only a
|
|
62
|
+
* Type guard — checks if a These holds only a second value.
|
|
58
63
|
*/
|
|
59
|
-
const
|
|
64
|
+
const isSecond: <A, B>(data: These<A, B>) => data is TheseSecond<B>;
|
|
60
65
|
/**
|
|
61
|
-
* Type guard — checks if a These holds both
|
|
66
|
+
* Type guard — checks if a These holds both values simultaneously.
|
|
62
67
|
*/
|
|
63
|
-
const isBoth: <
|
|
68
|
+
const isBoth: <A, B>(data: These<A, B>) => data is TheseBoth<A, B>;
|
|
64
69
|
/**
|
|
65
|
-
* Returns true if the These contains a
|
|
70
|
+
* Returns true if the These contains a first value (First or Both).
|
|
66
71
|
*/
|
|
67
|
-
const
|
|
72
|
+
const hasFirst: <A, B>(data: These<A, B>) => data is TheseFirst<A> | TheseBoth<A, B>;
|
|
68
73
|
/**
|
|
69
|
-
* Returns true if the These contains
|
|
74
|
+
* Returns true if the These contains a second value (Second or Both).
|
|
70
75
|
*/
|
|
71
|
-
const
|
|
76
|
+
const hasSecond: <A, B>(data: These<A, B>) => data is TheseSecond<B> | TheseBoth<A, B>;
|
|
72
77
|
/**
|
|
73
|
-
* Transforms the
|
|
78
|
+
* Transforms the first value, leaving the second unchanged.
|
|
74
79
|
*
|
|
75
80
|
* @example
|
|
76
81
|
* ```ts
|
|
77
|
-
* pipe(These.
|
|
78
|
-
* pipe(These.both("warn"
|
|
79
|
-
* pipe(These.
|
|
82
|
+
* pipe(These.first(5), These.mapFirst(n => n * 2)); // First(10)
|
|
83
|
+
* pipe(These.both(5, "warn"), These.mapFirst(n => n * 2)); // Both(10, "warn")
|
|
84
|
+
* pipe(These.second("warn"), These.mapFirst(n => n * 2)); // Second("warn")
|
|
80
85
|
* ```
|
|
81
86
|
*/
|
|
82
|
-
const
|
|
87
|
+
const mapFirst: <A, C>(f: (a: A) => C) => <B>(data: These<A, B>) => These<C, B>;
|
|
83
88
|
/**
|
|
84
|
-
* Transforms the
|
|
89
|
+
* Transforms the second value, leaving the first unchanged.
|
|
85
90
|
*
|
|
86
91
|
* @example
|
|
87
92
|
* ```ts
|
|
88
|
-
* pipe(These.
|
|
89
|
-
* pipe(These.both("warn"
|
|
93
|
+
* pipe(These.second("warn"), These.mapSecond(e => e.toUpperCase())); // Second("WARN")
|
|
94
|
+
* pipe(These.both(5, "warn"), These.mapSecond(e => e.toUpperCase())); // Both(5, "WARN")
|
|
90
95
|
* ```
|
|
91
96
|
*/
|
|
92
|
-
const
|
|
97
|
+
const mapSecond: <B, D>(f: (b: B) => D) => <A>(data: These<A, B>) => These<A, D>;
|
|
93
98
|
/**
|
|
94
|
-
* Transforms both the
|
|
99
|
+
* Transforms both the first and second values independently.
|
|
95
100
|
*
|
|
96
101
|
* @example
|
|
97
102
|
* ```ts
|
|
98
103
|
* pipe(
|
|
99
|
-
* These.both("warn"
|
|
100
|
-
* These.
|
|
101
|
-
* ); // Both("WARN"
|
|
104
|
+
* These.both(5, "warn"),
|
|
105
|
+
* These.mapBoth(n => n * 2, e => e.toUpperCase())
|
|
106
|
+
* ); // Both(10, "WARN")
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
const mapBoth: <A, C, B, D>(onFirst: (a: A) => C, onSecond: (b: B) => D) => (data: These<A, B>) => These<C, D>;
|
|
110
|
+
/**
|
|
111
|
+
* Chains These computations by passing the first value to f.
|
|
112
|
+
* Second propagates unchanged; First and Both apply f to the first value.
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```ts
|
|
116
|
+
* const double = (n: number): These<number, string> => These.first(n * 2);
|
|
117
|
+
*
|
|
118
|
+
* pipe(These.first(5), These.chainFirst(double)); // First(10)
|
|
119
|
+
* pipe(These.both(5, "warn"), These.chainFirst(double)); // First(10)
|
|
120
|
+
* pipe(These.second("warn"), These.chainFirst(double)); // Second("warn")
|
|
102
121
|
* ```
|
|
103
122
|
*/
|
|
104
|
-
const
|
|
123
|
+
const chainFirst: <A, B, C>(f: (a: A) => These<C, B>) => (data: These<A, B>) => These<C, B>;
|
|
105
124
|
/**
|
|
106
|
-
* Chains These computations by passing the
|
|
107
|
-
*
|
|
108
|
-
* - Ok(a) applies f(a) directly.
|
|
109
|
-
* - Both(e, a): applies f(a); if the result is Ok(b), returns Both(e, b)
|
|
110
|
-
* to preserve the warning. Otherwise returns f(a) as-is.
|
|
125
|
+
* Chains These computations by passing the second value to f.
|
|
126
|
+
* First propagates unchanged; Second and Both apply f to the second value.
|
|
111
127
|
*
|
|
112
128
|
* @example
|
|
113
129
|
* ```ts
|
|
114
|
-
* const
|
|
130
|
+
* const shout = (s: string): These<number, string> => These.second(s.toUpperCase());
|
|
115
131
|
*
|
|
116
|
-
* pipe(These.
|
|
117
|
-
* pipe(These.both("warn"
|
|
118
|
-
* pipe(These.
|
|
132
|
+
* pipe(These.second("warn"), These.chainSecond(shout)); // Second("WARN")
|
|
133
|
+
* pipe(These.both(5, "warn"), These.chainSecond(shout)); // Second("WARN")
|
|
134
|
+
* pipe(These.first(5), These.chainSecond(shout)); // First(5)
|
|
119
135
|
* ```
|
|
120
136
|
*/
|
|
121
|
-
const
|
|
137
|
+
const chainSecond: <A, B, D>(f: (b: B) => These<A, D>) => (data: These<A, B>) => These<A, D>;
|
|
122
138
|
/**
|
|
123
139
|
* Extracts a value from a These by providing handlers for all three cases.
|
|
124
140
|
*
|
|
@@ -127,14 +143,14 @@ export declare namespace These {
|
|
|
127
143
|
* pipe(
|
|
128
144
|
* these,
|
|
129
145
|
* These.fold(
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
* (
|
|
146
|
+
* a => `First: ${a}`,
|
|
147
|
+
* b => `Second: ${b}`,
|
|
148
|
+
* (a, b) => `Both: ${a} / ${b}`
|
|
133
149
|
* )
|
|
134
150
|
* );
|
|
135
151
|
* ```
|
|
136
152
|
*/
|
|
137
|
-
const fold: <
|
|
153
|
+
const fold: <A, B, C>(onFirst: (a: A) => C, onSecond: (b: B) => C, onBoth: (a: A, b: B) => C) => (data: These<A, B>) => C;
|
|
138
154
|
/**
|
|
139
155
|
* Pattern matches on a These, returning the result of the matching case.
|
|
140
156
|
*
|
|
@@ -143,71 +159,63 @@ export declare namespace These {
|
|
|
143
159
|
* pipe(
|
|
144
160
|
* these,
|
|
145
161
|
* These.match({
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
* both: (
|
|
162
|
+
* first: a => `First: ${a}`,
|
|
163
|
+
* second: b => `Second: ${b}`,
|
|
164
|
+
* both: (a, b) => `Both: ${a} / ${b}`
|
|
149
165
|
* })
|
|
150
166
|
* );
|
|
151
167
|
* ```
|
|
152
168
|
*/
|
|
153
|
-
const match: <
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
both: (
|
|
157
|
-
}) => (data: These<
|
|
169
|
+
const match: <A, B, C>(cases: {
|
|
170
|
+
first: (a: A) => C;
|
|
171
|
+
second: (b: B) => C;
|
|
172
|
+
both: (a: A, b: B) => C;
|
|
173
|
+
}) => (data: These<A, B>) => C;
|
|
158
174
|
/**
|
|
159
|
-
* Returns the
|
|
175
|
+
* Returns the first value, or a default if the These has no first value.
|
|
160
176
|
*
|
|
161
177
|
* @example
|
|
162
178
|
* ```ts
|
|
163
|
-
* pipe(These.
|
|
164
|
-
* pipe(These.both("warn"
|
|
165
|
-
* pipe(These.
|
|
179
|
+
* pipe(These.first(5), These.getFirstOrElse(0)); // 5
|
|
180
|
+
* pipe(These.both(5, "warn"), These.getFirstOrElse(0)); // 5
|
|
181
|
+
* pipe(These.second("warn"), These.getFirstOrElse(0)); // 0
|
|
166
182
|
* ```
|
|
167
183
|
*/
|
|
168
|
-
const
|
|
169
|
-
/**
|
|
170
|
-
* Executes a side effect on the success value without changing the These.
|
|
171
|
-
* Useful for logging or debugging.
|
|
172
|
-
*/
|
|
173
|
-
const tap: <A>(f: (a: A) => void) => <E>(data: These<E, A>) => These<E, A>;
|
|
184
|
+
const getFirstOrElse: <A>(defaultValue: A) => <B>(data: These<A, B>) => A;
|
|
174
185
|
/**
|
|
175
|
-
*
|
|
176
|
-
* - Err(e) → Ok(e)
|
|
177
|
-
* - Ok(a) → Err(a)
|
|
178
|
-
* - Both(e, a) → Both(a, e)
|
|
186
|
+
* Returns the second value, or a default if the These has no second value.
|
|
179
187
|
*
|
|
180
188
|
* @example
|
|
181
189
|
* ```ts
|
|
182
|
-
* These.
|
|
183
|
-
* These.
|
|
184
|
-
* These.
|
|
190
|
+
* pipe(These.second("warn"), These.getSecondOrElse("none")); // "warn"
|
|
191
|
+
* pipe(These.both(5, "warn"), These.getSecondOrElse("none")); // "warn"
|
|
192
|
+
* pipe(These.first(5), These.getSecondOrElse("none")); // "none"
|
|
185
193
|
* ```
|
|
186
194
|
*/
|
|
187
|
-
const
|
|
195
|
+
const getSecondOrElse: <B>(defaultValue: B) => <A>(data: These<A, B>) => B;
|
|
188
196
|
/**
|
|
189
|
-
*
|
|
190
|
-
*
|
|
197
|
+
* Executes a side effect on the first value without changing the These.
|
|
198
|
+
* Useful for logging or debugging.
|
|
191
199
|
*
|
|
192
200
|
* @example
|
|
193
201
|
* ```ts
|
|
194
|
-
* These.
|
|
195
|
-
* These.toOption(These.both("warn", 42)); // Some(42)
|
|
196
|
-
* These.toOption(These.err("err")); // None
|
|
202
|
+
* pipe(These.first(5), These.tap(console.log)); // logs 5, returns First(5)
|
|
197
203
|
* ```
|
|
198
204
|
*/
|
|
199
|
-
const
|
|
205
|
+
const tap: <A>(f: (a: A) => void) => <B>(data: These<A, B>) => These<A, B>;
|
|
200
206
|
/**
|
|
201
|
-
*
|
|
202
|
-
*
|
|
207
|
+
* Swaps the roles of first and second values.
|
|
208
|
+
* - First(a) → Second(a)
|
|
209
|
+
* - Second(b) → First(b)
|
|
210
|
+
* - Both(a, b) → Both(b, a)
|
|
203
211
|
*
|
|
204
212
|
* @example
|
|
205
213
|
* ```ts
|
|
206
|
-
* These.
|
|
207
|
-
* These.
|
|
208
|
-
* These.
|
|
214
|
+
* These.swap(These.first(5)); // Second(5)
|
|
215
|
+
* These.swap(These.second("warn")); // First("warn")
|
|
216
|
+
* These.swap(These.both(5, "warn")); // Both("warn", 5)
|
|
209
217
|
* ```
|
|
210
218
|
*/
|
|
211
|
-
const
|
|
219
|
+
const swap: <A, B>(data: These<A, B>) => These<B, A>;
|
|
212
220
|
}
|
|
213
221
|
//# sourceMappingURL=These.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"These.d.ts","sourceRoot":"","sources":["../../../src/src/Core/These.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"These.d.ts","sourceRoot":"","sources":["../../../src/src/Core/These.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAErE;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,EAAE,CAAC,IAAI,UAAU,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAE3E,MAAM,MAAM,UAAU,CAAC,CAAC,IAAI,QAAQ,CAAC,OAAO,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC;AAC7D,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI,QAAQ,CAAC,QAAQ,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;AAChE,MAAM,MAAM,SAAS,CAAC,KAAK,EAAE,MAAM,IAAI,QAAQ,CAAC,MAAM,CAAC,GAAG,SAAS,CAAC,KAAK,CAAC,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC;AAEhG,yBAAiB,KAAK,CAAC;IACrB;;;;;;;OAOG;IACI,MAAM,KAAK,GAAI,CAAC,EAAE,OAAO,CAAC,KAAG,UAAU,CAAC,CAAC,CAAsC,CAAC;IAEvF;;;;;;;OAOG;IACI,MAAM,MAAM,GAAI,CAAC,EAAE,OAAO,CAAC,KAAG,WAAW,CAAC,CAAC,CAAwC,CAAC;IAE3F;;;;;;;OAOG;IACI,MAAM,IAAI,GAAI,CAAC,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,QAAQ,CAAC,KAAG,SAAS,CAAC,CAAC,EAAE,CAAC,CAI7D,CAAC;IAEH;;OAEG;IACI,MAAM,OAAO,GAAI,CAAC,EAAE,CAAC,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