@nlozgachev/pipelined 0.6.4
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/README.md +85 -0
- package/esm/mod.js +3 -0
- package/esm/package.json +3 -0
- package/esm/src/Composition/compose.js +3 -0
- package/esm/src/Composition/converge.js +3 -0
- package/esm/src/Composition/curry.js +42 -0
- package/esm/src/Composition/flip.js +20 -0
- package/esm/src/Composition/flow.js +8 -0
- package/esm/src/Composition/fn.js +85 -0
- package/esm/src/Composition/index.js +13 -0
- package/esm/src/Composition/juxt.js +3 -0
- package/esm/src/Composition/memoize.js +66 -0
- package/esm/src/Composition/not.js +25 -0
- package/esm/src/Composition/on.js +12 -0
- package/esm/src/Composition/pipe.js +3 -0
- package/esm/src/Composition/tap.js +33 -0
- package/esm/src/Composition/uncurry.js +32 -0
- package/esm/src/Core/Arr.js +463 -0
- package/esm/src/Core/Deferred.js +26 -0
- package/esm/src/Core/InternalTypes.js +1 -0
- package/esm/src/Core/Lens.js +98 -0
- package/esm/src/Core/Option.js +186 -0
- package/esm/src/Core/Optional.js +160 -0
- package/esm/src/Core/Reader.js +134 -0
- package/esm/src/Core/Rec.js +167 -0
- package/esm/src/Core/RemoteData.js +206 -0
- package/esm/src/Core/Result.js +164 -0
- package/esm/src/Core/Task.js +187 -0
- package/esm/src/Core/TaskOption.js +105 -0
- package/esm/src/Core/TaskResult.js +125 -0
- package/esm/src/Core/TaskValidation.js +101 -0
- package/esm/src/Core/These.js +241 -0
- package/esm/src/Core/Validation.js +214 -0
- package/esm/src/Core/index.js +15 -0
- package/esm/src/Types/Brand.js +28 -0
- package/esm/src/Types/NonEmptyList.js +14 -0
- package/esm/src/Types/index.js +2 -0
- package/package.json +61 -0
- package/script/mod.js +19 -0
- package/script/package.json +3 -0
- package/script/src/Composition/compose.js +6 -0
- package/script/src/Composition/converge.js +6 -0
- package/script/src/Composition/curry.js +48 -0
- package/script/src/Composition/flip.js +24 -0
- package/script/src/Composition/flow.js +11 -0
- package/script/src/Composition/fn.js +98 -0
- package/script/src/Composition/index.js +29 -0
- package/script/src/Composition/juxt.js +6 -0
- package/script/src/Composition/memoize.js +71 -0
- package/script/src/Composition/not.js +29 -0
- package/script/src/Composition/on.js +16 -0
- package/script/src/Composition/pipe.js +6 -0
- package/script/src/Composition/tap.js +37 -0
- package/script/src/Composition/uncurry.js +38 -0
- package/script/src/Core/Arr.js +466 -0
- package/script/src/Core/Deferred.js +29 -0
- package/script/src/Core/InternalTypes.js +2 -0
- package/script/src/Core/Lens.js +101 -0
- package/script/src/Core/Option.js +189 -0
- package/script/src/Core/Optional.js +163 -0
- package/script/src/Core/Reader.js +137 -0
- package/script/src/Core/Rec.js +170 -0
- package/script/src/Core/RemoteData.js +209 -0
- package/script/src/Core/Result.js +167 -0
- package/script/src/Core/Task.js +190 -0
- package/script/src/Core/TaskOption.js +108 -0
- package/script/src/Core/TaskResult.js +128 -0
- package/script/src/Core/TaskValidation.js +104 -0
- package/script/src/Core/These.js +244 -0
- package/script/src/Core/Validation.js +217 -0
- package/script/src/Core/index.js +31 -0
- package/script/src/Types/Brand.js +31 -0
- package/script/src/Types/NonEmptyList.js +18 -0
- package/script/src/Types/index.js +18 -0
- package/types/mod.d.ts +4 -0
- package/types/mod.d.ts.map +1 -0
- package/types/src/Composition/compose.d.ts +33 -0
- package/types/src/Composition/compose.d.ts.map +1 -0
- package/types/src/Composition/converge.d.ts +21 -0
- package/types/src/Composition/converge.d.ts.map +1 -0
- package/types/src/Composition/curry.d.ts +43 -0
- package/types/src/Composition/curry.d.ts.map +1 -0
- package/types/src/Composition/flip.d.ts +21 -0
- package/types/src/Composition/flip.d.ts.map +1 -0
- package/types/src/Composition/flow.d.ts +56 -0
- package/types/src/Composition/flow.d.ts.map +1 -0
- package/types/src/Composition/fn.d.ts +76 -0
- package/types/src/Composition/fn.d.ts.map +1 -0
- package/types/src/Composition/index.d.ts +14 -0
- package/types/src/Composition/index.d.ts.map +1 -0
- package/types/src/Composition/juxt.d.ts +18 -0
- package/types/src/Composition/juxt.d.ts.map +1 -0
- package/types/src/Composition/memoize.d.ts +46 -0
- package/types/src/Composition/memoize.d.ts.map +1 -0
- package/types/src/Composition/not.d.ts +26 -0
- package/types/src/Composition/not.d.ts.map +1 -0
- package/types/src/Composition/on.d.ts +13 -0
- package/types/src/Composition/on.d.ts.map +1 -0
- package/types/src/Composition/pipe.d.ts +56 -0
- package/types/src/Composition/pipe.d.ts.map +1 -0
- package/types/src/Composition/tap.d.ts +31 -0
- package/types/src/Composition/tap.d.ts.map +1 -0
- package/types/src/Composition/uncurry.d.ts +54 -0
- package/types/src/Composition/uncurry.d.ts.map +1 -0
- package/types/src/Core/Arr.d.ts +355 -0
- package/types/src/Core/Arr.d.ts.map +1 -0
- package/types/src/Core/Deferred.d.ts +49 -0
- package/types/src/Core/Deferred.d.ts.map +1 -0
- package/types/src/Core/InternalTypes.d.ts +20 -0
- package/types/src/Core/InternalTypes.d.ts.map +1 -0
- package/types/src/Core/Lens.d.ts +118 -0
- package/types/src/Core/Lens.d.ts.map +1 -0
- package/types/src/Core/Option.d.ts +205 -0
- package/types/src/Core/Option.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/Reader.d.ts +156 -0
- package/types/src/Core/Reader.d.ts.map +1 -0
- package/types/src/Core/Rec.d.ts +121 -0
- package/types/src/Core/Rec.d.ts.map +1 -0
- package/types/src/Core/RemoteData.d.ts +192 -0
- package/types/src/Core/RemoteData.d.ts.map +1 -0
- package/types/src/Core/Result.d.ts +176 -0
- package/types/src/Core/Result.d.ts.map +1 -0
- package/types/src/Core/Task.d.ts +189 -0
- package/types/src/Core/Task.d.ts.map +1 -0
- package/types/src/Core/TaskOption.d.ts +120 -0
- package/types/src/Core/TaskOption.d.ts.map +1 -0
- package/types/src/Core/TaskResult.d.ts +117 -0
- package/types/src/Core/TaskResult.d.ts.map +1 -0
- package/types/src/Core/TaskValidation.d.ts +119 -0
- package/types/src/Core/TaskValidation.d.ts.map +1 -0
- package/types/src/Core/These.d.ts +221 -0
- package/types/src/Core/These.d.ts.map +1 -0
- package/types/src/Core/Validation.d.ts +213 -0
- package/types/src/Core/Validation.d.ts.map +1 -0
- package/types/src/Core/index.d.ts +16 -0
- package/types/src/Core/index.d.ts.map +1 -0
- package/types/src/Types/Brand.d.ts +52 -0
- package/types/src/Types/Brand.d.ts.map +1 -0
- package/types/src/Types/NonEmptyList.d.ts +29 -0
- package/types/src/Types/NonEmptyList.d.ts.map +1 -0
- package/types/src/Types/index.d.ts +3 -0
- package/types/src/Types/index.d.ts.map +1 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { Result } from "./Result.js";
|
|
2
|
+
export var Option;
|
|
3
|
+
(function (Option) {
|
|
4
|
+
/**
|
|
5
|
+
* Creates a Some containing the given value.
|
|
6
|
+
*/
|
|
7
|
+
Option.some = (value) => ({ kind: "Some", value });
|
|
8
|
+
/**
|
|
9
|
+
* Type guard that checks if a Option is Some.
|
|
10
|
+
*/
|
|
11
|
+
Option.isSome = (data) => data.kind === "Some";
|
|
12
|
+
/**
|
|
13
|
+
* Creates a None (empty Option).
|
|
14
|
+
*/
|
|
15
|
+
Option.none = () => ({ kind: "None" });
|
|
16
|
+
/**
|
|
17
|
+
* Type guard that checks if a Option is None.
|
|
18
|
+
*/
|
|
19
|
+
Option.isNone = (data) => data.kind === "None";
|
|
20
|
+
/**
|
|
21
|
+
* Creates a Option from a nullable value.
|
|
22
|
+
* Returns None if the value is null or undefined, Some otherwise.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* Option.fromNullable(null); // None
|
|
27
|
+
* Option.fromNullable(42); // Some(42)
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
Option.fromNullable = (value) => value === null || value === undefined ? Option.none() : Option.some(value);
|
|
31
|
+
/**
|
|
32
|
+
* Extracts the value from a Option, returning null if None.
|
|
33
|
+
*/
|
|
34
|
+
Option.toNullable = (data) => Option.isSome(data) ? data.value : null;
|
|
35
|
+
/**
|
|
36
|
+
* Extracts the value from a Option, returning undefined if None.
|
|
37
|
+
*/
|
|
38
|
+
Option.toUndefined = (data) => Option.isSome(data) ? data.value : undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Creates a Option from a possibly undefined value.
|
|
41
|
+
* Returns None if undefined, Some otherwise.
|
|
42
|
+
*/
|
|
43
|
+
Option.fromUndefined = (value) => value === undefined ? Option.none() : Option.some(value);
|
|
44
|
+
/**
|
|
45
|
+
* Converts an Option to a Result.
|
|
46
|
+
* Some becomes Ok, None becomes Err with the provided error.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* pipe(
|
|
51
|
+
* Option.some(42),
|
|
52
|
+
* Option.toResult(() => "Value was missing")
|
|
53
|
+
* ); // Ok(42)
|
|
54
|
+
*
|
|
55
|
+
* pipe(
|
|
56
|
+
* Option.none(),
|
|
57
|
+
* Option.toResult(() => "Value was missing")
|
|
58
|
+
* ); // Err("Value was missing")
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
Option.toResult = (onNone) => (data) => Option.isSome(data) ? Result.ok(data.value) : Result.err(onNone());
|
|
62
|
+
/**
|
|
63
|
+
* Creates an Option from a Result.
|
|
64
|
+
* Ok becomes Some, Err becomes None (the error is discarded).
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* Option.fromResult(Result.ok(42)); // Some(42)
|
|
69
|
+
* Option.fromResult(Result.err("oops")); // None
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
Option.fromResult = (data) => Result.isOk(data) ? Option.some(data.value) : Option.none();
|
|
73
|
+
/**
|
|
74
|
+
* Transforms the value inside a Option if it exists.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* pipe(Option.some(5), Option.map(n => n * 2)); // Some(10)
|
|
79
|
+
* pipe(Option.none(), Option.map(n => n * 2)); // None
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
Option.map = (f) => (data) => Option.isSome(data) ? Option.some(f(data.value)) : data;
|
|
83
|
+
/**
|
|
84
|
+
* Chains Option computations. If the first is Some, passes the value to f.
|
|
85
|
+
* If the first is None, propagates None.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* const parseNumber = (s: string): Option<number> => {
|
|
90
|
+
* const n = parseInt(s, 10);
|
|
91
|
+
* return isNaN(n) ? Option.none() : Option.some(n);
|
|
92
|
+
* };
|
|
93
|
+
*
|
|
94
|
+
* pipe(Option.some("42"), Option.chain(parseNumber)); // Some(42)
|
|
95
|
+
* pipe(Option.some("abc"), Option.chain(parseNumber)); // None
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
Option.chain = (f) => (data) => Option.isSome(data) ? f(data.value) : data;
|
|
99
|
+
/**
|
|
100
|
+
* Extracts the value from a Option by providing handlers for both cases.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* pipe(
|
|
105
|
+
* Option.some(5),
|
|
106
|
+
* Option.fold(
|
|
107
|
+
* () => "No value",
|
|
108
|
+
* n => `Value: ${n}`
|
|
109
|
+
* )
|
|
110
|
+
* ); // "Value: 5"
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
Option.fold = (onNone, onSome) => (data) => Option.isSome(data) ? onSome(data.value) : onNone();
|
|
114
|
+
/**
|
|
115
|
+
* Pattern matches on a Option, returning the result of the matching case.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* pipe(
|
|
120
|
+
* optionUser,
|
|
121
|
+
* Option.match({
|
|
122
|
+
* some: user => `Hello, ${user.name}`,
|
|
123
|
+
* none: () => "Hello, stranger"
|
|
124
|
+
* })
|
|
125
|
+
* );
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
Option.match = (cases) => (data) => Option.isSome(data) ? cases.some(data.value) : cases.none();
|
|
129
|
+
/**
|
|
130
|
+
* Returns the value inside a Option, or a default value if None.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```ts
|
|
134
|
+
* pipe(Option.some(5), Option.getOrElse(0)); // 5
|
|
135
|
+
* pipe(Option.none(), Option.getOrElse(0)); // 0
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
Option.getOrElse = (defaultValue) => (data) => Option.isSome(data) ? data.value : defaultValue;
|
|
139
|
+
/**
|
|
140
|
+
* Executes a side effect on the value without changing the Option.
|
|
141
|
+
* Useful for logging or debugging.
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```ts
|
|
145
|
+
* pipe(
|
|
146
|
+
* Option.some(5),
|
|
147
|
+
* Option.tap(n => console.log("Value:", n)),
|
|
148
|
+
* Option.map(n => n * 2)
|
|
149
|
+
* );
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
Option.tap = (f) => (data) => {
|
|
153
|
+
if (Option.isSome(data))
|
|
154
|
+
f(data.value);
|
|
155
|
+
return data;
|
|
156
|
+
};
|
|
157
|
+
/**
|
|
158
|
+
* Filters a Option based on a predicate.
|
|
159
|
+
* Returns None if the predicate returns false or if the Option is already None.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```ts
|
|
163
|
+
* pipe(Option.some(5), Option.filter(n => n > 3)); // Some(5)
|
|
164
|
+
* pipe(Option.some(2), Option.filter(n => n > 3)); // None
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
Option.filter = (predicate) => (data) => Option.isSome(data) && predicate(data.value) ? data : Option.none();
|
|
168
|
+
/**
|
|
169
|
+
* Recovers from a None by providing a fallback Option.
|
|
170
|
+
*/
|
|
171
|
+
Option.recover = (fallback) => (data) => Option.isSome(data) ? data : fallback();
|
|
172
|
+
/**
|
|
173
|
+
* Applies a function wrapped in a Option to a value wrapped in a Option.
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```ts
|
|
177
|
+
* const add = (a: number) => (b: number) => a + b;
|
|
178
|
+
* pipe(
|
|
179
|
+
* Option.some(add),
|
|
180
|
+
* Option.ap(Option.some(5)),
|
|
181
|
+
* Option.ap(Option.some(3))
|
|
182
|
+
* ); // Some(8)
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
Option.ap = (arg) => (data) => Option.isSome(data) && Option.isSome(arg) ? Option.some(data.value(arg.value)) : Option.none();
|
|
186
|
+
})(Option || (Option = {}));
|
|
@@ -0,0 +1,160 @@
|
|
|
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 ? Option.some(val) : Option.none();
|
|
30
|
+
}, (a) => (s) => ({ ...s, [key]: a }));
|
|
31
|
+
/**
|
|
32
|
+
* Creates an Optional that focuses on an element at a given index in an array.
|
|
33
|
+
* Returns None when the index is out of bounds; set is a no-op when out of bounds.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* const firstItem = Optional.index<string>(0);
|
|
38
|
+
*
|
|
39
|
+
* pipe(["a", "b"], Optional.get(firstItem)); // Some("a")
|
|
40
|
+
* pipe([], Optional.get(firstItem)); // None
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
Optional.index = (i) => Optional.make((arr) => i >= 0 && i < arr.length ? Option.some(arr[i]) : Option.none(), (a) => (arr) => {
|
|
44
|
+
if (i < 0 || i >= arr.length)
|
|
45
|
+
return arr;
|
|
46
|
+
const copy = [...arr];
|
|
47
|
+
copy[i] = a;
|
|
48
|
+
return copy;
|
|
49
|
+
});
|
|
50
|
+
/**
|
|
51
|
+
* Reads the focused value from a structure, returning Option<A>.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* pipe(profile, Optional.get(bioOpt)); // Some("...") or None
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
Optional.get = (opt) => (s) => opt.get(s);
|
|
59
|
+
/**
|
|
60
|
+
* Replaces the focused value within a structure.
|
|
61
|
+
* For indexed focuses, this is a no-op when the index is out of bounds.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* pipe(profile, Optional.set(bioOpt)("hello"));
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
Optional.set = (opt) => (a) => (s) => opt.set(a)(s);
|
|
69
|
+
/**
|
|
70
|
+
* Applies a function to the focused value if it is present; returns the
|
|
71
|
+
* structure unchanged if the focus is absent.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* pipe(profile, Optional.modify(bioOpt)(s => s.toUpperCase()));
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
Optional.modify = (opt) => (f) => (s) => {
|
|
79
|
+
const val = opt.get(s);
|
|
80
|
+
return val.kind === "None" ? s : opt.set(f(val.value))(s);
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Returns the focused value or a default when the focus is absent.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* pipe(profile, Optional.getOrElse(bioOpt)("no bio"));
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
Optional.getOrElse = (opt) => (defaultValue) => (s) => {
|
|
91
|
+
const val = opt.get(s);
|
|
92
|
+
return val.kind === "Some" ? val.value : defaultValue;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Extracts a value from an Optional focus using handlers for the present
|
|
96
|
+
* and absent cases.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* pipe(profile, Optional.fold(bioOpt)(() => "no bio", (bio) => bio.toUpperCase()));
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
Optional.fold = (opt) => (onNone, onSome) => (s) => {
|
|
104
|
+
const val = opt.get(s);
|
|
105
|
+
return val.kind === "Some" ? onSome(val.value) : onNone();
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Pattern matches on an Optional focus using a named-case object.
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```ts
|
|
112
|
+
* pipe(
|
|
113
|
+
* profile,
|
|
114
|
+
* Optional.match(bioOpt)({ none: () => "no bio", some: (bio) => bio }),
|
|
115
|
+
* );
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
Optional.match = (opt) => (cases) => (s) => {
|
|
119
|
+
const val = opt.get(s);
|
|
120
|
+
return val.kind === "Some" ? cases.some(val.value) : cases.none();
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* Composes two Optionals: focuses through the outer, then through the inner.
|
|
124
|
+
* Returns None if either focus is absent.
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* const deepOpt = pipe(
|
|
129
|
+
* Optional.prop<User>()("address"),
|
|
130
|
+
* Optional.andThen(Optional.prop<Address>()("landmark")),
|
|
131
|
+
* );
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
Optional.andThen = (inner) => (outer) => Optional.make((s) => {
|
|
135
|
+
const mid = outer.get(s);
|
|
136
|
+
return mid.kind === "None" ? Option.none() : inner.get(mid.value);
|
|
137
|
+
}, (b) => (s) => {
|
|
138
|
+
const mid = outer.get(s);
|
|
139
|
+
return mid.kind === "None" ? s : outer.set(inner.set(b)(mid.value))(s);
|
|
140
|
+
});
|
|
141
|
+
/**
|
|
142
|
+
* Composes an Optional with a Lens, producing an Optional.
|
|
143
|
+
* The Lens focuses within the value found by the Optional.
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```ts
|
|
147
|
+
* const cityOpt = pipe(
|
|
148
|
+
* Optional.prop<User>()("address"),
|
|
149
|
+
* Optional.andThenLens(Lens.prop<Address>()("city")),
|
|
150
|
+
* );
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
Optional.andThenLens = (inner) => (outer) => Optional.make((s) => {
|
|
154
|
+
const mid = outer.get(s);
|
|
155
|
+
return mid.kind === "None" ? Option.none() : Option.some(inner.get(mid.value));
|
|
156
|
+
}, (b) => (s) => {
|
|
157
|
+
const mid = outer.get(s);
|
|
158
|
+
return mid.kind === "None" ? s : outer.set(inner.set(b)(mid.value))(s);
|
|
159
|
+
});
|
|
160
|
+
})(Optional || (Optional = {}));
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
export var Reader;
|
|
2
|
+
(function (Reader) {
|
|
3
|
+
/**
|
|
4
|
+
* Lifts a pure value into a Reader. The environment is ignored.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* const always42: Reader<Config, number> = Reader.resolve(42);
|
|
9
|
+
* always42(anyConfig); // 42
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
Reader.resolve = (value) => (_env) => value;
|
|
13
|
+
/**
|
|
14
|
+
* Returns the full environment as the result.
|
|
15
|
+
* The fundamental way to access the environment in a pipeline.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* pipe(
|
|
20
|
+
* Reader.ask<Config>(),
|
|
21
|
+
* Reader.map(config => config.baseUrl)
|
|
22
|
+
* )(appConfig); // "https://api.example.com"
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
Reader.ask = () => (env) => env;
|
|
26
|
+
/**
|
|
27
|
+
* Projects a value from the environment using a selector function.
|
|
28
|
+
* Equivalent to `pipe(Reader.ask(), Reader.map(f))` but more direct.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const getBaseUrl: Reader<Config, string> = Reader.asks(c => c.baseUrl);
|
|
33
|
+
* getBaseUrl(appConfig); // "https://api.example.com"
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
Reader.asks = (f) => (env) => f(env);
|
|
37
|
+
/**
|
|
38
|
+
* Transforms the value produced by a Reader.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* pipe(
|
|
43
|
+
* Reader.asks((c: Config) => c.baseUrl),
|
|
44
|
+
* Reader.map(url => url.toUpperCase())
|
|
45
|
+
* )(appConfig); // "HTTPS://API.EXAMPLE.COM"
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
Reader.map = (f) => (data) => (env) => f(data(env));
|
|
49
|
+
/**
|
|
50
|
+
* Sequences two Readers. Both see the same environment.
|
|
51
|
+
* The output of the first is passed to `f`, which returns the next Reader.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* const buildUrl = (path: string): Reader<Config, string> =>
|
|
56
|
+
* Reader.asks(c => `${c.baseUrl}${path}`);
|
|
57
|
+
*
|
|
58
|
+
* const addAuth = (url: string): Reader<Config, string> =>
|
|
59
|
+
* Reader.asks(c => `${url}?key=${c.apiKey}`);
|
|
60
|
+
*
|
|
61
|
+
* pipe(
|
|
62
|
+
* buildUrl("/items"),
|
|
63
|
+
* Reader.chain(addAuth)
|
|
64
|
+
* )(appConfig); // "https://api.example.com/items?key=secret"
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
Reader.chain = (f) => (data) => (env) => f(data(env))(env);
|
|
68
|
+
/**
|
|
69
|
+
* Applies a function wrapped in a Reader to a value wrapped in a Reader.
|
|
70
|
+
* Both Readers see the same environment.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* const add = (a: number) => (b: number) => a + b;
|
|
75
|
+
* pipe(
|
|
76
|
+
* Reader.resolve<Config, typeof add>(add),
|
|
77
|
+
* Reader.ap(Reader.asks(c => c.timeout)),
|
|
78
|
+
* Reader.ap(Reader.resolve(5))
|
|
79
|
+
* )(appConfig);
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
Reader.ap = (arg) => (data) => (env) => data(env)(arg(env));
|
|
83
|
+
/**
|
|
84
|
+
* Executes a side effect on the produced value without changing the Reader.
|
|
85
|
+
* Useful for logging or debugging inside a pipeline.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* pipe(
|
|
90
|
+
* buildUrl("/users"),
|
|
91
|
+
* Reader.tap(url => console.log("Requesting:", url)),
|
|
92
|
+
* Reader.chain(addAuth)
|
|
93
|
+
* )(appConfig);
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
Reader.tap = (f) => (data) => (env) => {
|
|
97
|
+
const a = data(env);
|
|
98
|
+
f(a);
|
|
99
|
+
return a;
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Adapts a Reader to work with a different (typically wider) environment
|
|
103
|
+
* by transforming the environment before passing it to the Reader.
|
|
104
|
+
* This lets you compose Readers that expect different environments.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* type AppEnv = { db: DbPool; config: Config; logger: Logger };
|
|
109
|
+
*
|
|
110
|
+
* // buildUrl only needs Config
|
|
111
|
+
* const buildUrl: Reader<Config, string> = Reader.asks(c => c.baseUrl);
|
|
112
|
+
*
|
|
113
|
+
* // Zoom in from AppEnv to Config
|
|
114
|
+
* const buildUrlFromApp: Reader<AppEnv, string> =
|
|
115
|
+
* pipe(buildUrl, Reader.local((env: AppEnv) => env.config));
|
|
116
|
+
*
|
|
117
|
+
* buildUrlFromApp(appEnv); // works with the full AppEnv
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
Reader.local = (f) => (data) => (env) => data(f(env));
|
|
121
|
+
/**
|
|
122
|
+
* Runs a Reader by supplying the environment. Use this at the edge of your
|
|
123
|
+
* program where the environment is available.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```ts
|
|
127
|
+
* pipe(
|
|
128
|
+
* buildEndpoint("/users"),
|
|
129
|
+
* Reader.run(appConfig)
|
|
130
|
+
* ); // "https://api.example.com/users?key=secret"
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
Reader.run = (env) => (data) => data(env);
|
|
134
|
+
})(Reader || (Reader = {}));
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { Option } from "./Option.js";
|
|
2
|
+
/**
|
|
3
|
+
* Functional record/object utilities that compose well with pipe.
|
|
4
|
+
* All functions are data-last and curried where applicable.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* pipe(
|
|
9
|
+
* { a: 1, b: 2, c: 3 },
|
|
10
|
+
* Rec.filter(n => n > 1),
|
|
11
|
+
* Rec.map(n => n * 10)
|
|
12
|
+
* ); // { b: 20, c: 30 }
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export var Rec;
|
|
16
|
+
(function (Rec) {
|
|
17
|
+
/**
|
|
18
|
+
* Transforms each value in a record.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* pipe({ a: 1, b: 2 }, Rec.map(n => n * 2)); // { a: 2, b: 4 }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
Rec.map = (f) => (data) => {
|
|
26
|
+
const result = {};
|
|
27
|
+
for (const key of Object.keys(data)) {
|
|
28
|
+
result[key] = f(data[key]);
|
|
29
|
+
}
|
|
30
|
+
return result;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Transforms each value in a record, also receiving the key.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* pipe({ a: 1, b: 2 }, Rec.mapWithKey((k, v) => `${k}:${v}`));
|
|
38
|
+
* // { a: "a:1", b: "b:2" }
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
Rec.mapWithKey = (f) => (data) => {
|
|
42
|
+
const result = {};
|
|
43
|
+
for (const key of Object.keys(data)) {
|
|
44
|
+
result[key] = f(key, data[key]);
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Filters values in a record by a predicate.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* pipe({ a: 1, b: 2, c: 3 }, Rec.filter(n => n > 1)); // { b: 2, c: 3 }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
Rec.filter = (predicate) => (data) => {
|
|
57
|
+
const result = {};
|
|
58
|
+
for (const key of Object.keys(data)) {
|
|
59
|
+
if (predicate(data[key]))
|
|
60
|
+
result[key] = data[key];
|
|
61
|
+
}
|
|
62
|
+
return result;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Filters values in a record by a predicate that also receives the key.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* pipe({ a: 1, b: 2 }, Rec.filterWithKey((k, v) => k !== "a" && v > 0));
|
|
70
|
+
* // { b: 2 }
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
Rec.filterWithKey = (predicate) => (data) => {
|
|
74
|
+
const result = {};
|
|
75
|
+
for (const key of Object.keys(data)) {
|
|
76
|
+
if (predicate(key, data[key]))
|
|
77
|
+
result[key] = data[key];
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Looks up a value by key, returning Option.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```ts
|
|
86
|
+
* pipe({ a: 1, b: 2 }, Rec.lookup("a")); // Some(1)
|
|
87
|
+
* pipe({ a: 1, b: 2 }, Rec.lookup("c")); // None
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
Rec.lookup = (key) => (data) => Object.prototype.hasOwnProperty.call(data, key) ? Option.some(data[key]) : Option.none();
|
|
91
|
+
/**
|
|
92
|
+
* Returns all keys of a record.
|
|
93
|
+
*/
|
|
94
|
+
Rec.keys = (data) => Object.keys(data);
|
|
95
|
+
/**
|
|
96
|
+
* Returns all values of a record.
|
|
97
|
+
*/
|
|
98
|
+
Rec.values = (data) => Object.values(data);
|
|
99
|
+
/**
|
|
100
|
+
* Returns all key-value pairs of a record.
|
|
101
|
+
*/
|
|
102
|
+
Rec.entries = (data) => Object.entries(data);
|
|
103
|
+
/**
|
|
104
|
+
* Creates a record from key-value pairs.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* Rec.fromEntries([["a", 1], ["b", 2]]); // { a: 1, b: 2 }
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
Rec.fromEntries = (data) => Object.fromEntries(data);
|
|
112
|
+
/**
|
|
113
|
+
* Picks specific keys from a record.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* pipe({ a: 1, b: 2, c: 3 }, Rec.pick("a", "c")); // { a: 1, c: 3 }
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
Rec.pick = (...pickedKeys) => (data) => {
|
|
121
|
+
const result = {};
|
|
122
|
+
for (const key of pickedKeys) {
|
|
123
|
+
if (Object.prototype.hasOwnProperty.call(data, key)) {
|
|
124
|
+
result[key] = data[key];
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return result;
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* Omits specific keys from a record.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```ts
|
|
134
|
+
* pipe({ a: 1, b: 2, c: 3 }, Rec.omit("b")); // { a: 1, c: 3 }
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
Rec.omit = (...omittedKeys) => (data) => {
|
|
138
|
+
const omitSet = new Set(omittedKeys);
|
|
139
|
+
const result = {};
|
|
140
|
+
for (const key of Object.keys(data)) {
|
|
141
|
+
if (!omitSet.has(key)) {
|
|
142
|
+
result[key] = data[key];
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return result;
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* Merges two records. Values from the second record take precedence.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```ts
|
|
152
|
+
* pipe({ a: 1, b: 2 }, Rec.merge({ b: 3, c: 4 })); // { a: 1, b: 3, c: 4 }
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
Rec.merge = (other) => (data) => ({
|
|
156
|
+
...data,
|
|
157
|
+
...other,
|
|
158
|
+
});
|
|
159
|
+
/**
|
|
160
|
+
* Returns true if the record has no keys.
|
|
161
|
+
*/
|
|
162
|
+
Rec.isEmpty = (data) => Object.keys(data).length === 0;
|
|
163
|
+
/**
|
|
164
|
+
* Returns the number of keys in a record.
|
|
165
|
+
*/
|
|
166
|
+
Rec.size = (data) => Object.keys(data).length;
|
|
167
|
+
})(Rec || (Rec = {}));
|