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