@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.
@@ -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 = {}));
@@ -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 success value (no error).
7
+ * Creates a These holding only a first value.
9
8
  *
10
9
  * @example
11
10
  * ```ts
12
- * These.ok(42);
11
+ * These.first(42); // { kind: "First", first: 42 }
13
12
  * ```
14
13
  */
15
- These.ok = (value) => Result_js_1.Result.ok(value);
14
+ These.first = (value) => ({ kind: "First", first: value });
16
15
  /**
17
- * Creates a These holding only an error/warning (no success value).
16
+ * Creates a These holding only a second value.
18
17
  *
19
18
  * @example
20
19
  * ```ts
21
- * These.err("Something went wrong");
20
+ * These.second("warning"); // { kind: "Second", second: "warning" }
22
21
  * ```
23
22
  */
24
- These.err = (error) => Result_js_1.Result.err(error);
23
+ These.second = (value) => ({ kind: "Second", second: value });
25
24
  /**
26
- * Creates a These holding both an error/warning and a success value.
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", result);
29
+ * These.both(42, "Deprecated API used"); // { kind: "Both", first: 42, second: "Deprecated API used" }
31
30
  * ```
32
31
  */
33
- These.both = (error, value) => ({
32
+ These.both = (first, second) => ({
34
33
  kind: "Both",
35
- error,
36
- value,
34
+ first,
35
+ second,
37
36
  });
38
37
  /**
39
- * Type guard — checks if a These holds only an error/warning.
38
+ * Type guard — checks if a These holds only a first value.
40
39
  */
41
- These.isErr = (data) => data.kind === "Error";
40
+ These.isFirst = (data) => data.kind === "First";
42
41
  /**
43
- * Type guard — checks if a These holds only a success value.
42
+ * Type guard — checks if a These holds only a second value.
44
43
  */
45
- These.isOk = (data) => data.kind === "Ok";
44
+ These.isSecond = (data) => data.kind === "Second";
46
45
  /**
47
- * Type guard — checks if a These holds both an error/warning and a success value.
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 success value (Ok or Both).
50
+ * Returns true if the These contains a first value (First or Both).
52
51
  */
53
- These.hasValue = (data) => data.kind === "Ok" || data.kind === "Both";
52
+ These.hasFirst = (data) => data.kind === "First" || data.kind === "Both";
54
53
  /**
55
- * Returns true if the These contains an error/warning (Err or Both).
54
+ * Returns true if the These contains a second value (Second or Both).
56
55
  */
57
- These.hasError = (data) => data.kind === "Error" || data.kind === "Both";
56
+ These.hasSecond = (data) => data.kind === "Second" || data.kind === "Both";
58
57
  /**
59
- * Transforms the success value, leaving the error unchanged.
58
+ * Transforms the first value, leaving the second unchanged.
60
59
  *
61
60
  * @example
62
61
  * ```ts
63
- * pipe(These.ok(5), These.map(n => n * 2)); // Ok(10)
64
- * pipe(These.both("warn", 5), These.map(n => n * 2)); // Both("warn", 10)
65
- * pipe(These.err("err"), These.map(n => n * 2)); // Err("err")
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.map = (f) => (data) => {
69
- if (These.isErr(data))
67
+ These.mapFirst = (f) => (data) => {
68
+ if (These.isSecond(data))
70
69
  return data;
71
- if (These.isOk(data))
72
- return These.ok(f(data.value));
73
- return These.both(data.error, f(data.value));
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 error/warning value, leaving the success value unchanged.
75
+ * Transforms the second value, leaving the first unchanged.
77
76
  *
78
77
  * @example
79
78
  * ```ts
80
- * pipe(These.err("err"), These.mapErr(e => e.toUpperCase())); // Err("ERR")
81
- * pipe(These.both("warn", 5), These.mapErr(e => e.toUpperCase())); // Both("WARN", 5)
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.mapErr = (f) => (data) => {
85
- if (These.isOk(data))
83
+ These.mapSecond = (f) => (data) => {
84
+ if (These.isFirst(data))
86
85
  return data;
87
- if (These.isErr(data))
88
- return These.err(f(data.error));
89
- return These.both(f(data.error), data.value);
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 error and success values independently.
91
+ * Transforms both the first and second values independently.
93
92
  *
94
93
  * @example
95
94
  * ```ts
96
95
  * pipe(
97
- * These.both("warn", 5),
98
- * These.bimap(e => e.toUpperCase(), n => n * 2)
99
- * ); // Both("WARN", 10)
96
+ * These.both(5, "warn"),
97
+ * These.mapBoth(n => n * 2, e => e.toUpperCase())
98
+ * ); // Both(10, "WARN")
100
99
  * ```
101
100
  */
102
- These.bimap = (onErr, onOk) => (data) => {
103
- if (These.isErr(data))
104
- return These.err(onErr(data.error));
105
- if (These.isOk(data))
106
- return These.ok(onOk(data.value));
107
- return These.both(onErr(data.error), onOk(data.value));
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 success value to f.
111
- * - Err propagates unchanged.
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<string, number> => These.ok(n * 2);
114
+ * const double = (n: number): These<number, string> => These.first(n * 2);
119
115
  *
120
- * pipe(These.ok(5), These.chain(double)); // Ok(10)
121
- * pipe(These.both("warn", 5), These.chain(double)); // Both("warn", 10)
122
- * pipe(These.err("err"), These.chain(double)); // Err("err")
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.chain = (f) => (data) => {
126
- if (These.isErr(data))
121
+ These.chainFirst = (f) => (data) => {
122
+ if (These.isSecond(data))
127
123
  return data;
128
- if (These.isOk(data))
129
- return f(data.value);
130
- const result = f(data.value);
131
- return These.isOk(result) ? These.both(data.error, result.value) : result;
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
- * e => `Error: ${e}`,
142
- * a => `Value: ${a}`,
143
- * (e, a) => `Both: ${e} / ${a}`
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 = (onErr, onOk, onBoth) => (data) => {
149
- if (These.isErr(data))
150
- return onErr(data.error);
151
- if (These.isOk(data))
152
- return onOk(data.value);
153
- return onBoth(data.error, data.value);
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
- * err: e => `Error: ${e}`,
164
- * ok: a => `Value: ${a}`,
165
- * both: (e, a) => `Both: ${e} / ${a}`
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.isErr(data))
172
- return cases.err(data.error);
173
- if (These.isOk(data))
174
- return cases.ok(data.value);
175
- return cases.both(data.error, data.value);
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 success value, or a default if the These has no success value.
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.ok(5), These.getOrElse(0)); // 5
183
- * pipe(These.both("warn", 5), These.getOrElse(0)); // 5
184
- * pipe(These.err("err"), These.getOrElse(0)); // 0
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.getOrElse = (defaultValue) => (data) => These.hasValue(data) ? data.value : defaultValue;
198
+ These.getFirstOrElse = (defaultValue) => (data) => These.hasFirst(data) ? data.first : defaultValue;
188
199
  /**
189
- * Executes a side effect on the success value without changing the These.
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.swap(These.err("err")); // Ok("err")
206
- * These.swap(These.ok(5)); // Err(5)
207
- * These.swap(These.both("warn", 5)); // Both(5, "warn")
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.swap = (data) => {
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
- * Converts a These to an Option.
219
- * Ok and Both produce Some; Err produces None.
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.toOption(These.ok(42)); // Some(42)
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.toOption = (data) => These.hasValue(data) ? { kind: "Some", value: data.value } : { kind: "None" };
219
+ These.tap = (f) => (data) => {
220
+ if (These.hasFirst(data))
221
+ f(data.first);
222
+ return data;
223
+ };
229
224
  /**
230
- * Converts a These to a Result, discarding any warning from Both.
231
- * Ok and Both produce Ok; Err produces Err.
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.toResult(These.ok(42)); // Ok(42)
236
- * These.toResult(These.both("warn", 42)); // Ok(42)
237
- * These.toResult(These.err("err")); // Err("err")
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.toResult = (data) => {
241
- if (These.hasValue(data))
242
- return Result_js_1.Result.ok(data.value);
243
- return data;
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 = {}));
@@ -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"}