@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,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 = {}));
@@ -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 success value (no error).
4
+ * Creates a These holding only a first value.
6
5
  *
7
6
  * @example
8
7
  * ```ts
9
- * These.ok(42);
8
+ * These.first(42); // { kind: "First", first: 42 }
10
9
  * ```
11
10
  */
12
- These.ok = (value) => Result.ok(value);
11
+ These.first = (value) => ({ kind: "First", first: value });
13
12
  /**
14
- * Creates a These holding only an error/warning (no success value).
13
+ * Creates a These holding only a second value.
15
14
  *
16
15
  * @example
17
16
  * ```ts
18
- * These.err("Something went wrong");
17
+ * These.second("warning"); // { kind: "Second", second: "warning" }
19
18
  * ```
20
19
  */
21
- These.err = (error) => Result.err(error);
20
+ These.second = (value) => ({ kind: "Second", second: value });
22
21
  /**
23
- * Creates a These holding both an error/warning and a success value.
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", result);
26
+ * These.both(42, "Deprecated API used"); // { kind: "Both", first: 42, second: "Deprecated API used" }
28
27
  * ```
29
28
  */
30
- These.both = (error, value) => ({
29
+ These.both = (first, second) => ({
31
30
  kind: "Both",
32
- error,
33
- value,
31
+ first,
32
+ second,
34
33
  });
35
34
  /**
36
- * Type guard — checks if a These holds only an error/warning.
35
+ * Type guard — checks if a These holds only a first value.
37
36
  */
38
- These.isErr = (data) => data.kind === "Error";
37
+ These.isFirst = (data) => data.kind === "First";
39
38
  /**
40
- * Type guard — checks if a These holds only a success value.
39
+ * Type guard — checks if a These holds only a second value.
41
40
  */
42
- These.isOk = (data) => data.kind === "Ok";
41
+ These.isSecond = (data) => data.kind === "Second";
43
42
  /**
44
- * Type guard — checks if a These holds both an error/warning and a success value.
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 success value (Ok or Both).
47
+ * Returns true if the These contains a first value (First or Both).
49
48
  */
50
- These.hasValue = (data) => data.kind === "Ok" || data.kind === "Both";
49
+ These.hasFirst = (data) => data.kind === "First" || data.kind === "Both";
51
50
  /**
52
- * Returns true if the These contains an error/warning (Err or Both).
51
+ * Returns true if the These contains a second value (Second or Both).
53
52
  */
54
- These.hasError = (data) => data.kind === "Error" || data.kind === "Both";
53
+ These.hasSecond = (data) => data.kind === "Second" || data.kind === "Both";
55
54
  /**
56
- * Transforms the success value, leaving the error unchanged.
55
+ * Transforms the first value, leaving the second unchanged.
57
56
  *
58
57
  * @example
59
58
  * ```ts
60
- * pipe(These.ok(5), These.map(n => n * 2)); // Ok(10)
61
- * pipe(These.both("warn", 5), These.map(n => n * 2)); // Both("warn", 10)
62
- * pipe(These.err("err"), These.map(n => n * 2)); // Err("err")
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.map = (f) => (data) => {
66
- if (These.isErr(data))
64
+ These.mapFirst = (f) => (data) => {
65
+ if (These.isSecond(data))
67
66
  return data;
68
- if (These.isOk(data))
69
- return These.ok(f(data.value));
70
- return These.both(data.error, f(data.value));
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 error/warning value, leaving the success value unchanged.
72
+ * Transforms the second value, leaving the first unchanged.
74
73
  *
75
74
  * @example
76
75
  * ```ts
77
- * pipe(These.err("err"), These.mapErr(e => e.toUpperCase())); // Err("ERR")
78
- * pipe(These.both("warn", 5), These.mapErr(e => e.toUpperCase())); // Both("WARN", 5)
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.mapErr = (f) => (data) => {
82
- if (These.isOk(data))
80
+ These.mapSecond = (f) => (data) => {
81
+ if (These.isFirst(data))
83
82
  return data;
84
- if (These.isErr(data))
85
- return These.err(f(data.error));
86
- return These.both(f(data.error), data.value);
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 error and success values independently.
88
+ * Transforms both the first and second values independently.
90
89
  *
91
90
  * @example
92
91
  * ```ts
93
92
  * pipe(
94
- * These.both("warn", 5),
95
- * These.bimap(e => e.toUpperCase(), n => n * 2)
96
- * ); // Both("WARN", 10)
93
+ * These.both(5, "warn"),
94
+ * These.mapBoth(n => n * 2, e => e.toUpperCase())
95
+ * ); // Both(10, "WARN")
97
96
  * ```
98
97
  */
99
- These.bimap = (onErr, onOk) => (data) => {
100
- if (These.isErr(data))
101
- return These.err(onErr(data.error));
102
- if (These.isOk(data))
103
- return These.ok(onOk(data.value));
104
- return These.both(onErr(data.error), onOk(data.value));
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 success value to f.
108
- * - Err propagates unchanged.
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<string, number> => These.ok(n * 2);
111
+ * const double = (n: number): These<number, string> => These.first(n * 2);
116
112
  *
117
- * pipe(These.ok(5), These.chain(double)); // Ok(10)
118
- * pipe(These.both("warn", 5), These.chain(double)); // Both("warn", 10)
119
- * pipe(These.err("err"), These.chain(double)); // Err("err")
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.chain = (f) => (data) => {
123
- if (These.isErr(data))
118
+ These.chainFirst = (f) => (data) => {
119
+ if (These.isSecond(data))
124
120
  return data;
125
- if (These.isOk(data))
126
- return f(data.value);
127
- const result = f(data.value);
128
- return These.isOk(result) ? These.both(data.error, result.value) : result;
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
- * e => `Error: ${e}`,
139
- * a => `Value: ${a}`,
140
- * (e, a) => `Both: ${e} / ${a}`
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 = (onErr, onOk, onBoth) => (data) => {
146
- if (These.isErr(data))
147
- return onErr(data.error);
148
- if (These.isOk(data))
149
- return onOk(data.value);
150
- return onBoth(data.error, data.value);
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
- * err: e => `Error: ${e}`,
161
- * ok: a => `Value: ${a}`,
162
- * both: (e, a) => `Both: ${e} / ${a}`
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.isErr(data))
169
- return cases.err(data.error);
170
- if (These.isOk(data))
171
- return cases.ok(data.value);
172
- return cases.both(data.error, data.value);
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 success value, or a default if the These has no success value.
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.ok(5), These.getOrElse(0)); // 5
180
- * pipe(These.both("warn", 5), These.getOrElse(0)); // 5
181
- * pipe(These.err("err"), These.getOrElse(0)); // 0
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.getOrElse = (defaultValue) => (data) => These.hasValue(data) ? data.value : defaultValue;
195
+ These.getFirstOrElse = (defaultValue) => (data) => These.hasFirst(data) ? data.first : defaultValue;
185
196
  /**
186
- * Executes a side effect on the success value without changing the These.
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.swap(These.err("err")); // Ok("err")
203
- * These.swap(These.ok(5)); // Err(5)
204
- * These.swap(These.both("warn", 5)); // Both(5, "warn")
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.swap = (data) => {
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
- * Converts a These to an Option.
216
- * Ok and Both produce Some; Err produces None.
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.toOption(These.ok(42)); // Some(42)
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.toOption = (data) => These.hasValue(data) ? { kind: "Some", value: data.value } : { kind: "None" };
216
+ These.tap = (f) => (data) => {
217
+ if (These.hasFirst(data))
218
+ f(data.first);
219
+ return data;
220
+ };
226
221
  /**
227
- * Converts a These to a Result, discarding any warning from Both.
228
- * Ok and Both produce Ok; Err produces Err.
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.toResult(These.ok(42)); // Ok(42)
233
- * These.toResult(These.both("warn", 42)); // Ok(42)
234
- * These.toResult(These.err("err")); // Err("err")
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.toResult = (data) => {
238
- if (These.hasValue(data))
239
- return Result.ok(data.value);
240
- return data;
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 = {}));
@@ -1,5 +1,7 @@
1
1
  export * from "./Arr.js";
2
+ export * from "./Lens.js";
2
3
  export * from "./Option.js";
4
+ export * from "./Optional.js";
3
5
  export * from "./Rec.js";
4
6
  export * from "./RemoteData.js";
5
7
  export * from "./Result.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nlozgachev/pipekit",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
4
4
  "description": "Simple functional programming toolkit for TypeScript",
5
5
  "keywords": [
6
6
  "functional",