@nlozgachev/pipekit 0.3.0 → 0.4.1
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/Lens.js +98 -0
- package/esm/src/Core/Optional.js +164 -0
- package/esm/src/Core/These.js +122 -123
- package/esm/src/Core/index.js +2 -0
- package/package.json +1 -1
- package/script/src/Core/Lens.js +101 -0
- package/script/src/Core/Optional.js +167 -0
- package/script/src/Core/These.js +122 -123
- package/script/src/Core/index.js +2 -0
- package/types/src/Core/InternalTypes.d.ts +6 -0
- package/types/src/Core/InternalTypes.d.ts.map +1 -1
- package/types/src/Core/Lens.d.ts +118 -0
- package/types/src/Core/Lens.d.ts.map +1 -0
- package/types/src/Core/Optional.d.ts +158 -0
- package/types/src/Core/Optional.d.ts.map +1 -0
- package/types/src/Core/These.d.ts +108 -100
- package/types/src/Core/These.d.ts.map +1 -1
- package/types/src/Core/index.d.ts +2 -0
- package/types/src/Core/index.d.ts.map +1 -1
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
export var Lens;
|
|
2
|
+
(function (Lens) {
|
|
3
|
+
/**
|
|
4
|
+
* Constructs a Lens from a getter and a setter.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* const nameLens = Lens.make(
|
|
9
|
+
* (user: User) => user.name,
|
|
10
|
+
* (name) => (user) => ({ ...user, name }),
|
|
11
|
+
* );
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
Lens.make = (get, set) => ({ get, set });
|
|
15
|
+
/**
|
|
16
|
+
* Creates a Lens that focuses on a property of an object.
|
|
17
|
+
* Call with the structure type first, then the key.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* const nameLens = Lens.prop<User>()("name");
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
Lens.prop = () => (key) => Lens.make((s) => s[key], (a) => (s) => ({ ...s, [key]: a }));
|
|
25
|
+
/**
|
|
26
|
+
* Reads the focused value from a structure.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* pipe(user, Lens.get(nameLens)); // "Alice"
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
Lens.get = (lens) => (s) => lens.get(s);
|
|
34
|
+
/**
|
|
35
|
+
* Replaces the focused value within a structure, returning a new structure.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* pipe(user, Lens.set(nameLens)("Bob")); // new User with name "Bob"
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
Lens.set = (lens) => (a) => (s) => lens.set(a)(s);
|
|
43
|
+
/**
|
|
44
|
+
* Applies a function to the focused value, returning a new structure.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* pipe(user, Lens.modify(nameLens)(n => n.toUpperCase())); // "ALICE"
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
Lens.modify = (lens) => (f) => (s) => lens.set(f(lens.get(s)))(s);
|
|
52
|
+
/**
|
|
53
|
+
* Composes two Lenses: focuses through the outer, then through the inner.
|
|
54
|
+
* Use in a pipe chain to build up a deep focus step by step.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* const userCityLens = pipe(
|
|
59
|
+
* Lens.prop<User>()("address"),
|
|
60
|
+
* Lens.andThen(Lens.prop<Address>()("city")),
|
|
61
|
+
* );
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
Lens.andThen = (inner) => (outer) => Lens.make((s) => inner.get(outer.get(s)), (b) => (s) => outer.set(inner.set(b)(outer.get(s)))(s));
|
|
65
|
+
/**
|
|
66
|
+
* Composes a Lens with an Optional, producing an Optional.
|
|
67
|
+
* Use when the next step in the focus is optional (may be absent).
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* const userBioOpt = pipe(
|
|
72
|
+
* Lens.prop<User>()("profile"),
|
|
73
|
+
* Lens.andThenOptional(Optional.prop<Profile>()("bio")),
|
|
74
|
+
* );
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
Lens.andThenOptional = (inner) => (outer) => ({
|
|
78
|
+
get: (s) => inner.get(outer.get(s)),
|
|
79
|
+
set: (b) => (s) => outer.set(inner.set(b)(outer.get(s)))(s),
|
|
80
|
+
});
|
|
81
|
+
/**
|
|
82
|
+
* Converts a Lens to an Optional. Every Lens is a valid Optional
|
|
83
|
+
* whose get always returns Some.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* pipe(
|
|
88
|
+
* Lens.prop<User>()("address"),
|
|
89
|
+
* Lens.toOptional,
|
|
90
|
+
* Optional.andThen(Optional.prop<Address>()("landmark")),
|
|
91
|
+
* );
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
Lens.toOptional = (lens) => ({
|
|
95
|
+
get: (s) => ({ kind: "Some", value: lens.get(s) }),
|
|
96
|
+
set: lens.set,
|
|
97
|
+
});
|
|
98
|
+
})(Lens || (Lens = {}));
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { Option } from "./Option.js";
|
|
2
|
+
export var Optional;
|
|
3
|
+
(function (Optional) {
|
|
4
|
+
/**
|
|
5
|
+
* Constructs an Optional from a getter (returning Option<A>) and a setter.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const firstChar = Optional.make(
|
|
10
|
+
* (s: string) => s.length > 0 ? Option.some(s[0]) : Option.none(),
|
|
11
|
+
* (c) => (s) => s.length > 0 ? c + s.slice(1) : s,
|
|
12
|
+
* );
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
Optional.make = (get, set) => ({ get, set });
|
|
16
|
+
/**
|
|
17
|
+
* Creates an Optional that focuses on an optional property of an object.
|
|
18
|
+
* Only keys whose type includes undefined (i.e. `field?: T`) are accepted.
|
|
19
|
+
* Call with the structure type first, then the key.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* type Profile = { username: string; bio?: string };
|
|
24
|
+
* const bioOpt = Optional.prop<Profile>()("bio");
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
Optional.prop = () => (key) => Optional.make((s) => {
|
|
28
|
+
const val = s[key];
|
|
29
|
+
return val != null
|
|
30
|
+
? Option.some(val)
|
|
31
|
+
: Option.none();
|
|
32
|
+
}, (a) => (s) => ({ ...s, [key]: a }));
|
|
33
|
+
/**
|
|
34
|
+
* Creates an Optional that focuses on an element at a given index in an array.
|
|
35
|
+
* Returns None when the index is out of bounds; set is a no-op when out of bounds.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* const firstItem = Optional.index<string>(0);
|
|
40
|
+
*
|
|
41
|
+
* pipe(["a", "b"], Optional.get(firstItem)); // Some("a")
|
|
42
|
+
* pipe([], Optional.get(firstItem)); // None
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
Optional.index = (i) => Optional.make((arr) => i >= 0 && i < arr.length ? Option.some(arr[i]) : Option.none(), (a) => (arr) => {
|
|
46
|
+
if (i < 0 || i >= arr.length)
|
|
47
|
+
return arr;
|
|
48
|
+
const copy = [...arr];
|
|
49
|
+
copy[i] = a;
|
|
50
|
+
return copy;
|
|
51
|
+
});
|
|
52
|
+
/**
|
|
53
|
+
* Reads the focused value from a structure, returning Option<A>.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* pipe(profile, Optional.get(bioOpt)); // Some("...") or None
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
Optional.get = (opt) => (s) => opt.get(s);
|
|
61
|
+
/**
|
|
62
|
+
* Replaces the focused value within a structure.
|
|
63
|
+
* For indexed focuses, this is a no-op when the index is out of bounds.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* pipe(profile, Optional.set(bioOpt)("hello"));
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
Optional.set = (opt) => (a) => (s) => opt.set(a)(s);
|
|
71
|
+
/**
|
|
72
|
+
* Applies a function to the focused value if it is present; returns the
|
|
73
|
+
* structure unchanged if the focus is absent.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* pipe(profile, Optional.modify(bioOpt)(s => s.toUpperCase()));
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
Optional.modify = (opt) => (f) => (s) => {
|
|
81
|
+
const val = opt.get(s);
|
|
82
|
+
return val.kind === "None" ? s : opt.set(f(val.value))(s);
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Returns the focused value or a default when the focus is absent.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* pipe(profile, Optional.getOrElse(bioOpt)("no bio"));
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
Optional.getOrElse = (opt) => (defaultValue) => (s) => {
|
|
93
|
+
const val = opt.get(s);
|
|
94
|
+
return val.kind === "Some" ? val.value : defaultValue;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Extracts a value from an Optional focus using handlers for the present
|
|
98
|
+
* and absent cases.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* pipe(profile, Optional.fold(bioOpt)(() => "no bio", (bio) => bio.toUpperCase()));
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
Optional.fold = (opt) => (onNone, onSome) => (s) => {
|
|
106
|
+
const val = opt.get(s);
|
|
107
|
+
return val.kind === "Some" ? onSome(val.value) : onNone();
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* Pattern matches on an Optional focus using a named-case object.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```ts
|
|
114
|
+
* pipe(
|
|
115
|
+
* profile,
|
|
116
|
+
* Optional.match(bioOpt)({ none: () => "no bio", some: (bio) => bio }),
|
|
117
|
+
* );
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
Optional.match = (opt) => (cases) => (s) => {
|
|
121
|
+
const val = opt.get(s);
|
|
122
|
+
return val.kind === "Some" ? cases.some(val.value) : cases.none();
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* Composes two Optionals: focuses through the outer, then through the inner.
|
|
126
|
+
* Returns None if either focus is absent.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```ts
|
|
130
|
+
* const deepOpt = pipe(
|
|
131
|
+
* Optional.prop<User>()("address"),
|
|
132
|
+
* Optional.andThen(Optional.prop<Address>()("landmark")),
|
|
133
|
+
* );
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
Optional.andThen = (inner) => (outer) => Optional.make((s) => {
|
|
137
|
+
const mid = outer.get(s);
|
|
138
|
+
return mid.kind === "None" ? Option.none() : inner.get(mid.value);
|
|
139
|
+
}, (b) => (s) => {
|
|
140
|
+
const mid = outer.get(s);
|
|
141
|
+
return mid.kind === "None" ? s : outer.set(inner.set(b)(mid.value))(s);
|
|
142
|
+
});
|
|
143
|
+
/**
|
|
144
|
+
* Composes an Optional with a Lens, producing an Optional.
|
|
145
|
+
* The Lens focuses within the value found by the Optional.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* const cityOpt = pipe(
|
|
150
|
+
* Optional.prop<User>()("address"),
|
|
151
|
+
* Optional.andThenLens(Lens.prop<Address>()("city")),
|
|
152
|
+
* );
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
Optional.andThenLens = (inner) => (outer) => Optional.make((s) => {
|
|
156
|
+
const mid = outer.get(s);
|
|
157
|
+
return mid.kind === "None"
|
|
158
|
+
? Option.none()
|
|
159
|
+
: Option.some(inner.get(mid.value));
|
|
160
|
+
}, (b) => (s) => {
|
|
161
|
+
const mid = outer.get(s);
|
|
162
|
+
return mid.kind === "None" ? s : outer.set(inner.set(b)(mid.value))(s);
|
|
163
|
+
});
|
|
164
|
+
})(Optional || (Optional = {}));
|
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/esm/src/Core/index.js
CHANGED