@nlozgachev/pipekit 0.4.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,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.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "Simple functional programming toolkit for TypeScript",
5
5
  "keywords": [
6
6
  "functional",
@@ -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 = {}));
@@ -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);
@@ -0,0 +1,118 @@
1
+ import type { Optional } from "./Optional.js";
2
+ /**
3
+ * Lens<S, A> focuses on a single value A inside a structure S, providing
4
+ * a composable way to read and immutably update nested data.
5
+ *
6
+ * A Lens always succeeds: the focused value is guaranteed to exist.
7
+ * For optional or indexed focuses, use Optional<S, A>.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * type Address = { city: string; zip: string };
12
+ * type User = { name: string; address: Address };
13
+ *
14
+ * const addressLens = Lens.prop<User>()("address");
15
+ * const cityLens = Lens.prop<Address>()("city");
16
+ * const userCityLens = pipe(addressLens, Lens.andThen(cityLens));
17
+ *
18
+ * pipe(user, Lens.get(userCityLens)); // "Berlin"
19
+ * pipe(user, Lens.set(userCityLens)("Hamburg")); // new User with city updated
20
+ * pipe(user, Lens.modify(userCityLens)(c => c.toUpperCase())); // "BERLIN"
21
+ * ```
22
+ */
23
+ export type Lens<S, A> = {
24
+ readonly get: (s: S) => A;
25
+ readonly set: (a: A) => (s: S) => S;
26
+ };
27
+ export declare namespace Lens {
28
+ /**
29
+ * Constructs a Lens from a getter and a setter.
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * const nameLens = Lens.make(
34
+ * (user: User) => user.name,
35
+ * (name) => (user) => ({ ...user, name }),
36
+ * );
37
+ * ```
38
+ */
39
+ const make: <S, A>(get: (s: S) => A, set: (a: A) => (s: S) => S) => Lens<S, A>;
40
+ /**
41
+ * Creates a Lens that focuses on a property of an object.
42
+ * Call with the structure type first, then the key.
43
+ *
44
+ * @example
45
+ * ```ts
46
+ * const nameLens = Lens.prop<User>()("name");
47
+ * ```
48
+ */
49
+ const prop: <S>() => <K extends keyof S>(key: K) => Lens<S, S[K]>;
50
+ /**
51
+ * Reads the focused value from a structure.
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * pipe(user, Lens.get(nameLens)); // "Alice"
56
+ * ```
57
+ */
58
+ const get: <S, A>(lens: Lens<S, A>) => (s: S) => A;
59
+ /**
60
+ * Replaces the focused value within a structure, returning a new structure.
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * pipe(user, Lens.set(nameLens)("Bob")); // new User with name "Bob"
65
+ * ```
66
+ */
67
+ const set: <S, A>(lens: Lens<S, A>) => (a: A) => (s: S) => S;
68
+ /**
69
+ * Applies a function to the focused value, returning a new structure.
70
+ *
71
+ * @example
72
+ * ```ts
73
+ * pipe(user, Lens.modify(nameLens)(n => n.toUpperCase())); // "ALICE"
74
+ * ```
75
+ */
76
+ const modify: <S, A>(lens: Lens<S, A>) => (f: (a: A) => A) => (s: S) => S;
77
+ /**
78
+ * Composes two Lenses: focuses through the outer, then through the inner.
79
+ * Use in a pipe chain to build up a deep focus step by step.
80
+ *
81
+ * @example
82
+ * ```ts
83
+ * const userCityLens = pipe(
84
+ * Lens.prop<User>()("address"),
85
+ * Lens.andThen(Lens.prop<Address>()("city")),
86
+ * );
87
+ * ```
88
+ */
89
+ const andThen: <A, B>(inner: Lens<A, B>) => <S>(outer: Lens<S, A>) => Lens<S, B>;
90
+ /**
91
+ * Composes a Lens with an Optional, producing an Optional.
92
+ * Use when the next step in the focus is optional (may be absent).
93
+ *
94
+ * @example
95
+ * ```ts
96
+ * const userBioOpt = pipe(
97
+ * Lens.prop<User>()("profile"),
98
+ * Lens.andThenOptional(Optional.prop<Profile>()("bio")),
99
+ * );
100
+ * ```
101
+ */
102
+ const andThenOptional: <A, B>(inner: Optional<A, B>) => <S>(outer: Lens<S, A>) => Optional<S, B>;
103
+ /**
104
+ * Converts a Lens to an Optional. Every Lens is a valid Optional
105
+ * whose get always returns Some.
106
+ *
107
+ * @example
108
+ * ```ts
109
+ * pipe(
110
+ * Lens.prop<User>()("address"),
111
+ * Lens.toOptional,
112
+ * Optional.andThen(Optional.prop<Address>()("landmark")),
113
+ * );
114
+ * ```
115
+ */
116
+ const toOptional: <S, A>(lens: Lens<S, A>) => Optional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OAOG;IACI,MAAM,MAAM,GAAI,CAAC,EAAE,CAAC,EAAE,MAAM,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,KAAG,CACjD,CAAC;IAE9B;;;;;;;;;;;OAWG;IACI,MAAM,OAAO,GACjB,CAAC,EAAE,CAAC,EAAE,OAAO,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,KAAG,IAAI,CAAC,CAAC,EAAE,CAAC,CAI3D,CAAC;IAEN;;;;;;;;;;;OAWG;IACI,MAAM,eAAe,GACzB,CAAC,EAAE,CAAC,EAAE,OAAO,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,KAAG,QAAQ,CAAC,CAAC,EAAE,CAAC,CAGpE,CAAC;IAEL;;;;;;;;;;;;OAYG;IACI,MAAM,UAAU,GAAI,CAAC,EAAE,CAAC,EAAE,MAAM,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,KAAG,QAAQ,CAAC,CAAC,EAAE,CAAC,CAG/D,CAAC;CACJ"}
@@ -0,0 +1,158 @@
1
+ import { Option } from "./Option.js";
2
+ import type { Lens } from "./Lens.js";
3
+ /** Keys of T for which undefined is assignable (i.e. optional fields). */
4
+ type OptionalKeys<T> = {
5
+ [K in keyof T]-?: undefined extends T[K] ? K : never;
6
+ }[keyof T];
7
+ /**
8
+ * Optional<S, A> focuses on a value A inside a structure S that may or may
9
+ * not be present. Like a Lens, but get returns Option<A>.
10
+ *
11
+ * Compose with other Optionals via `andThen`, or with a Lens via `andThenLens`.
12
+ * Convert a Lens to an Optional with `Lens.toOptional`.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * type Profile = { username: string; bio?: string };
17
+ *
18
+ * const bioOpt = Optional.prop<Profile>()("bio");
19
+ *
20
+ * pipe(profile, Optional.get(bioOpt)); // Some("hello") or None
21
+ * pipe(profile, Optional.set(bioOpt)("hello")); // new Profile with bio set
22
+ * pipe(profile, Optional.modify(bioOpt)(s => s + "!")); // appends if present
23
+ * ```
24
+ */
25
+ export type Optional<S, A> = {
26
+ readonly get: (s: S) => Option<A>;
27
+ readonly set: (a: A) => (s: S) => S;
28
+ };
29
+ export declare namespace Optional {
30
+ /**
31
+ * Constructs an Optional from a getter (returning Option<A>) and a setter.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * const firstChar = Optional.make(
36
+ * (s: string) => s.length > 0 ? Option.some(s[0]) : Option.none(),
37
+ * (c) => (s) => s.length > 0 ? c + s.slice(1) : s,
38
+ * );
39
+ * ```
40
+ */
41
+ const make: <S, A>(get: (s: S) => Option<A>, set: (a: A) => (s: S) => S) => Optional<S, A>;
42
+ /**
43
+ * Creates an Optional that focuses on an optional property of an object.
44
+ * Only keys whose type includes undefined (i.e. `field?: T`) are accepted.
45
+ * Call with the structure type first, then the key.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * type Profile = { username: string; bio?: string };
50
+ * const bioOpt = Optional.prop<Profile>()("bio");
51
+ * ```
52
+ */
53
+ const prop: <S>() => <K extends OptionalKeys<S>>(key: K) => Optional<S, NonNullable<S[K]>>;
54
+ /**
55
+ * Creates an Optional that focuses on an element at a given index in an array.
56
+ * Returns None when the index is out of bounds; set is a no-op when out of bounds.
57
+ *
58
+ * @example
59
+ * ```ts
60
+ * const firstItem = Optional.index<string>(0);
61
+ *
62
+ * pipe(["a", "b"], Optional.get(firstItem)); // Some("a")
63
+ * pipe([], Optional.get(firstItem)); // None
64
+ * ```
65
+ */
66
+ const index: <A>(i: number) => Optional<A[], A>;
67
+ /**
68
+ * Reads the focused value from a structure, returning Option<A>.
69
+ *
70
+ * @example
71
+ * ```ts
72
+ * pipe(profile, Optional.get(bioOpt)); // Some("...") or None
73
+ * ```
74
+ */
75
+ const get: <S, A>(opt: Optional<S, A>) => (s: S) => Option<A>;
76
+ /**
77
+ * Replaces the focused value within a structure.
78
+ * For indexed focuses, this is a no-op when the index is out of bounds.
79
+ *
80
+ * @example
81
+ * ```ts
82
+ * pipe(profile, Optional.set(bioOpt)("hello"));
83
+ * ```
84
+ */
85
+ const set: <S, A>(opt: Optional<S, A>) => (a: A) => (s: S) => S;
86
+ /**
87
+ * Applies a function to the focused value if it is present; returns the
88
+ * structure unchanged if the focus is absent.
89
+ *
90
+ * @example
91
+ * ```ts
92
+ * pipe(profile, Optional.modify(bioOpt)(s => s.toUpperCase()));
93
+ * ```
94
+ */
95
+ const modify: <S, A>(opt: Optional<S, A>) => (f: (a: A) => A) => (s: S) => S;
96
+ /**
97
+ * Returns the focused value or a default when the focus is absent.
98
+ *
99
+ * @example
100
+ * ```ts
101
+ * pipe(profile, Optional.getOrElse(bioOpt)("no bio"));
102
+ * ```
103
+ */
104
+ const getOrElse: <S, A>(opt: Optional<S, A>) => (defaultValue: A) => (s: S) => A;
105
+ /**
106
+ * Extracts a value from an Optional focus using handlers for the present
107
+ * and absent cases.
108
+ *
109
+ * @example
110
+ * ```ts
111
+ * pipe(profile, Optional.fold(bioOpt)(() => "no bio", (bio) => bio.toUpperCase()));
112
+ * ```
113
+ */
114
+ const fold: <S, A>(opt: Optional<S, A>) => <B>(onNone: () => B, onSome: (a: A) => B) => (s: S) => B;
115
+ /**
116
+ * Pattern matches on an Optional focus using a named-case object.
117
+ *
118
+ * @example
119
+ * ```ts
120
+ * pipe(
121
+ * profile,
122
+ * Optional.match(bioOpt)({ none: () => "no bio", some: (bio) => bio }),
123
+ * );
124
+ * ```
125
+ */
126
+ const match: <S, A>(opt: Optional<S, A>) => <B>(cases: {
127
+ none: () => B;
128
+ some: (a: A) => B;
129
+ }) => (s: S) => B;
130
+ /**
131
+ * Composes two Optionals: focuses through the outer, then through the inner.
132
+ * Returns None if either focus is absent.
133
+ *
134
+ * @example
135
+ * ```ts
136
+ * const deepOpt = pipe(
137
+ * Optional.prop<User>()("address"),
138
+ * Optional.andThen(Optional.prop<Address>()("landmark")),
139
+ * );
140
+ * ```
141
+ */
142
+ const andThen: <A, B>(inner: Optional<A, B>) => <S>(outer: Optional<S, A>) => Optional<S, B>;
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
+ const andThenLens: <A, B>(inner: Lens<A, B>) => <S>(outer: Optional<S, A>) => Optional<S, B>;
156
+ }
157
+ export {};
158
+ //# sourceMappingURL=Optional.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Optional.d.ts","sourceRoot":"","sources":["../../../src/src/Core/Optional.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEtC,0EAA0E;AAC1E,KAAK,YAAY,CAAC,CAAC,IAAI;KACpB,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,GAAG,SAAS,SAAS,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK;CACrD,CAAC,MAAM,CAAC,CAAC,CAAC;AAEX;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,EAAE,CAAC,IAAI;IAC3B,QAAQ,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,MAAM,CAAC,CAAC,CAAC,CAAC;IAClC,QAAQ,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC;CACrC,CAAC;AAEF,yBAAiB,QAAQ,CAAC;IACxB;;;;;;;;;;OAUG;IACI,MAAM,IAAI,G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CAAC;IAEJ;;;;;;;;OAQG;IACI,MAAM,IAAI,GACd,CAAC,EAAE,CAAC,EAAE,KAAK,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,QAAQ,MAAM,CAAC,EAAE,QAAQ,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,KAAG,CAGnF,CAAC;IAEJ;;;;;;;;;;OAUG;IACI,MAAM,KAAK,GACf,CAAC,EAAE,CAAC,EAAE,KAAK,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO;QAAE,IAAI,EAAE,MAAM,CAAC,CAAC;QAAC,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAA;KAAE,MAC7E,GAAG,CAAC,KAAG,CAGP,CAAC;IAEJ;;;;;;;;;;;OAWG;IACI,MAAM,OAAO,GACjB,CAAC,EAAE,CAAC,EAAE,OAAO,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,KAAG,QAAQ,CAAC,CAAC,EAAE,CAAC,CAUvE,CAAC;IAEN;;;;;;;;;;;OAWG;IACI,MAAM,WAAW,GACrB,CAAC,EAAE,CAAC,EAAE,OAAO,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,KAAG,QAAQ,CAAC,CAAC,EAAE,CAAC,CAYnE,CAAC;CACP"}
@@ -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";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/src/Core/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,iBAAiB,CAAC;AAChC,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC;AACpC,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/src/Core/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,UAAU,CAAC;AACzB,cAAc,iBAAiB,CAAC;AAChC,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC;AACpC,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC"}