@nlozgachev/pipekit 0.1.8 → 0.2.0
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 +1 -1
- package/esm/src/Core/Arr.js +14 -14
- package/esm/src/Core/Option.js +16 -16
- package/esm/src/Core/Rec.js +1 -1
- package/esm/src/Core/Result.js +14 -14
- package/esm/src/Core/Task.js +73 -5
- package/esm/src/Core/TaskOption.js +3 -3
- package/esm/src/Core/TaskResult.js +62 -5
- package/esm/src/Core/These.js +40 -40
- package/esm/src/Types/Brand.js +3 -3
- package/package.json +1 -1
- package/script/src/Core/Arr.js +14 -14
- package/script/src/Core/Option.js +16 -16
- package/script/src/Core/Rec.js +1 -1
- package/script/src/Core/Result.js +14 -14
- package/script/src/Core/Task.js +73 -5
- package/script/src/Core/TaskOption.js +3 -3
- package/script/src/Core/TaskResult.js +62 -5
- package/script/src/Core/These.js +40 -40
- package/script/src/Types/Brand.js +3 -3
- package/types/src/Core/Arr.d.ts +3 -3
- package/types/src/Core/Arr.d.ts.map +1 -1
- package/types/src/Core/Option.d.ts +9 -9
- package/types/src/Core/Option.d.ts.map +1 -1
- package/types/src/Core/Rec.d.ts.map +1 -1
- package/types/src/Core/Result.d.ts +9 -9
- package/types/src/Core/Result.d.ts.map +1 -1
- package/types/src/Core/Task.d.ts +49 -5
- package/types/src/Core/Task.d.ts.map +1 -1
- package/types/src/Core/TaskOption.d.ts.map +1 -1
- package/types/src/Core/TaskResult.d.ts +40 -1
- package/types/src/Core/TaskResult.d.ts.map +1 -1
- package/types/src/Core/These.d.ts +31 -31
- package/types/src/Core/These.d.ts.map +1 -1
- package/types/src/Types/Brand.d.ts +5 -5
package/esm/src/Core/These.js
CHANGED
|
@@ -6,28 +6,28 @@ export var These;
|
|
|
6
6
|
*
|
|
7
7
|
* @example
|
|
8
8
|
* ```ts
|
|
9
|
-
* These.
|
|
9
|
+
* These.err("Something went wrong");
|
|
10
10
|
* ```
|
|
11
11
|
*/
|
|
12
|
-
These.
|
|
12
|
+
These.err = (error) => Result.err(error);
|
|
13
13
|
/**
|
|
14
14
|
* Creates a These holding only a success value (no error).
|
|
15
15
|
*
|
|
16
16
|
* @example
|
|
17
17
|
* ```ts
|
|
18
|
-
* These.
|
|
18
|
+
* These.ok(42);
|
|
19
19
|
* ```
|
|
20
20
|
*/
|
|
21
|
-
These.
|
|
21
|
+
These.ok = (value) => Result.ok(value);
|
|
22
22
|
/**
|
|
23
23
|
* Creates a These holding both an error/warning and a success value.
|
|
24
24
|
*
|
|
25
25
|
* @example
|
|
26
26
|
* ```ts
|
|
27
|
-
* These.
|
|
27
|
+
* These.both("Deprecated API used", result);
|
|
28
28
|
* ```
|
|
29
29
|
*/
|
|
30
|
-
These.
|
|
30
|
+
These.both = (error, value) => ({
|
|
31
31
|
kind: "Both",
|
|
32
32
|
error,
|
|
33
33
|
value,
|
|
@@ -57,33 +57,33 @@ export var These;
|
|
|
57
57
|
*
|
|
58
58
|
* @example
|
|
59
59
|
* ```ts
|
|
60
|
-
* pipe(These.
|
|
61
|
-
* pipe(These.
|
|
62
|
-
* pipe(These.
|
|
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")
|
|
63
63
|
* ```
|
|
64
64
|
*/
|
|
65
65
|
These.map = (f) => (data) => {
|
|
66
66
|
if (These.isErr(data))
|
|
67
67
|
return data;
|
|
68
68
|
if (These.isOk(data))
|
|
69
|
-
return These.
|
|
70
|
-
return These.
|
|
69
|
+
return These.ok(f(data.value));
|
|
70
|
+
return These.both(data.error, f(data.value));
|
|
71
71
|
};
|
|
72
72
|
/**
|
|
73
73
|
* Transforms the error/warning value, leaving the success value unchanged.
|
|
74
74
|
*
|
|
75
75
|
* @example
|
|
76
76
|
* ```ts
|
|
77
|
-
* pipe(These.
|
|
78
|
-
* pipe(These.
|
|
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)
|
|
79
79
|
* ```
|
|
80
80
|
*/
|
|
81
81
|
These.mapErr = (f) => (data) => {
|
|
82
82
|
if (These.isOk(data))
|
|
83
83
|
return data;
|
|
84
84
|
if (These.isErr(data))
|
|
85
|
-
return These.
|
|
86
|
-
return These.
|
|
85
|
+
return These.err(f(data.error));
|
|
86
|
+
return These.both(f(data.error), data.value);
|
|
87
87
|
};
|
|
88
88
|
/**
|
|
89
89
|
* Transforms both the error and success values independently.
|
|
@@ -91,17 +91,17 @@ export var These;
|
|
|
91
91
|
* @example
|
|
92
92
|
* ```ts
|
|
93
93
|
* pipe(
|
|
94
|
-
* These.
|
|
94
|
+
* These.both("warn", 5),
|
|
95
95
|
* These.bimap(e => e.toUpperCase(), n => n * 2)
|
|
96
96
|
* ); // Both("WARN", 10)
|
|
97
97
|
* ```
|
|
98
98
|
*/
|
|
99
99
|
These.bimap = (onErr, onOk) => (data) => {
|
|
100
100
|
if (These.isErr(data))
|
|
101
|
-
return These.
|
|
101
|
+
return These.err(onErr(data.error));
|
|
102
102
|
if (These.isOk(data))
|
|
103
|
-
return These.
|
|
104
|
-
return These.
|
|
103
|
+
return These.ok(onOk(data.value));
|
|
104
|
+
return These.both(onErr(data.error), onOk(data.value));
|
|
105
105
|
};
|
|
106
106
|
/**
|
|
107
107
|
* Chains These computations by passing the success value to f.
|
|
@@ -112,11 +112,11 @@ export var These;
|
|
|
112
112
|
*
|
|
113
113
|
* @example
|
|
114
114
|
* ```ts
|
|
115
|
-
* const double = (n: number): These<string, number> => These.
|
|
115
|
+
* const double = (n: number): These<string, number> => These.ok(n * 2);
|
|
116
116
|
*
|
|
117
|
-
* pipe(These.
|
|
118
|
-
* pipe(These.
|
|
119
|
-
* pipe(These.
|
|
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")
|
|
120
120
|
* ```
|
|
121
121
|
*/
|
|
122
122
|
These.chain = (f) => (data) => {
|
|
@@ -125,7 +125,7 @@ export var These;
|
|
|
125
125
|
if (These.isOk(data))
|
|
126
126
|
return f(data.value);
|
|
127
127
|
const result = f(data.value);
|
|
128
|
-
return These.isOk(result) ? These.
|
|
128
|
+
return These.isOk(result) ? These.both(data.error, result.value) : result;
|
|
129
129
|
};
|
|
130
130
|
/**
|
|
131
131
|
* Extracts a value from a These by providing handlers for all three cases.
|
|
@@ -176,9 +176,9 @@ export var These;
|
|
|
176
176
|
*
|
|
177
177
|
* @example
|
|
178
178
|
* ```ts
|
|
179
|
-
* pipe(These.
|
|
180
|
-
* pipe(These.
|
|
181
|
-
* pipe(These.
|
|
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
|
|
182
182
|
* ```
|
|
183
183
|
*/
|
|
184
184
|
These.getOrElse = (defaultValue) => (data) => These.hasValue(data) ? data.value : defaultValue;
|
|
@@ -199,17 +199,17 @@ export var These;
|
|
|
199
199
|
*
|
|
200
200
|
* @example
|
|
201
201
|
* ```ts
|
|
202
|
-
* These.swap(These.
|
|
203
|
-
* These.swap(These.
|
|
204
|
-
* These.swap(These.
|
|
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")
|
|
205
205
|
* ```
|
|
206
206
|
*/
|
|
207
207
|
These.swap = (data) => {
|
|
208
208
|
if (These.isErr(data))
|
|
209
|
-
return These.
|
|
209
|
+
return These.ok(data.error);
|
|
210
210
|
if (These.isOk(data))
|
|
211
|
-
return These.
|
|
212
|
-
return These.
|
|
211
|
+
return These.err(data.value);
|
|
212
|
+
return These.both(data.value, data.error);
|
|
213
213
|
};
|
|
214
214
|
/**
|
|
215
215
|
* Converts a These to an Option.
|
|
@@ -217,9 +217,9 @@ export var These;
|
|
|
217
217
|
*
|
|
218
218
|
* @example
|
|
219
219
|
* ```ts
|
|
220
|
-
* These.toOption(These.
|
|
221
|
-
* These.toOption(These.
|
|
222
|
-
* These.toOption(These.
|
|
220
|
+
* These.toOption(These.ok(42)); // Some(42)
|
|
221
|
+
* These.toOption(These.both("warn", 42)); // Some(42)
|
|
222
|
+
* These.toOption(These.err("err")); // None
|
|
223
223
|
* ```
|
|
224
224
|
*/
|
|
225
225
|
These.toOption = (data) => These.hasValue(data) ? { kind: "Some", value: data.value } : { kind: "None" };
|
|
@@ -229,14 +229,14 @@ export var These;
|
|
|
229
229
|
*
|
|
230
230
|
* @example
|
|
231
231
|
* ```ts
|
|
232
|
-
* These.toResult(These.
|
|
233
|
-
* These.toResult(These.
|
|
234
|
-
* These.toResult(These.
|
|
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")
|
|
235
235
|
* ```
|
|
236
236
|
*/
|
|
237
237
|
These.toResult = (data) => {
|
|
238
238
|
if (These.hasValue(data))
|
|
239
|
-
return Result.
|
|
239
|
+
return Result.ok(data.value);
|
|
240
240
|
return data;
|
|
241
241
|
};
|
|
242
242
|
})(These || (These = {}));
|
package/esm/src/Types/Brand.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
export var Brand;
|
|
2
2
|
(function (Brand) {
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Returns a constructor that wraps a value of type T in brand K.
|
|
5
5
|
* The resulting function performs an unchecked cast — only use when the raw
|
|
6
6
|
* value is known to satisfy the brand's invariants.
|
|
7
7
|
*
|
|
8
8
|
* @example
|
|
9
9
|
* ```ts
|
|
10
10
|
* type PositiveNumber = Brand<"PositiveNumber", number>;
|
|
11
|
-
* const toPositiveNumber = Brand.
|
|
11
|
+
* const toPositiveNumber = Brand.wrap<"PositiveNumber", number>();
|
|
12
12
|
*
|
|
13
13
|
* const n: PositiveNumber = toPositiveNumber(42);
|
|
14
14
|
* ```
|
|
15
15
|
*/
|
|
16
|
-
Brand.
|
|
16
|
+
Brand.wrap = () => (value) => value;
|
|
17
17
|
/**
|
|
18
18
|
* Strips the brand and returns the underlying value.
|
|
19
19
|
* Since Brand<K, T> extends T this is rarely needed, but can improve readability.
|
package/package.json
CHANGED
package/script/src/Core/Arr.js
CHANGED
|
@@ -31,7 +31,7 @@ var Arr;
|
|
|
31
31
|
* Arr.head([]); // None
|
|
32
32
|
* ```
|
|
33
33
|
*/
|
|
34
|
-
Arr.head = (data) => data.length > 0 ? Option_js_1.Option.
|
|
34
|
+
Arr.head = (data) => data.length > 0 ? Option_js_1.Option.some(data[0]) : Option_js_1.Option.none();
|
|
35
35
|
/**
|
|
36
36
|
* Returns the last element of an array, or None if the array is empty.
|
|
37
37
|
*
|
|
@@ -41,7 +41,7 @@ var Arr;
|
|
|
41
41
|
* Arr.last([]); // None
|
|
42
42
|
* ```
|
|
43
43
|
*/
|
|
44
|
-
Arr.last = (data) => data.length > 0 ? Option_js_1.Option.
|
|
44
|
+
Arr.last = (data) => data.length > 0 ? Option_js_1.Option.some(data[data.length - 1]) : Option_js_1.Option.none();
|
|
45
45
|
/**
|
|
46
46
|
* Returns all elements except the first, or None if the array is empty.
|
|
47
47
|
*
|
|
@@ -51,7 +51,7 @@ var Arr;
|
|
|
51
51
|
* Arr.tail([]); // None
|
|
52
52
|
* ```
|
|
53
53
|
*/
|
|
54
|
-
Arr.tail = (data) => data.length > 0 ? Option_js_1.Option.
|
|
54
|
+
Arr.tail = (data) => data.length > 0 ? Option_js_1.Option.some(data.slice(1)) : Option_js_1.Option.none();
|
|
55
55
|
/**
|
|
56
56
|
* Returns all elements except the last, or None if the array is empty.
|
|
57
57
|
*
|
|
@@ -61,7 +61,7 @@ var Arr;
|
|
|
61
61
|
* Arr.init([]); // None
|
|
62
62
|
* ```
|
|
63
63
|
*/
|
|
64
|
-
Arr.init = (data) => data.length > 0 ? Option_js_1.Option.
|
|
64
|
+
Arr.init = (data) => data.length > 0 ? Option_js_1.Option.some(data.slice(0, -1)) : Option_js_1.Option.none();
|
|
65
65
|
// --- Search ---
|
|
66
66
|
/**
|
|
67
67
|
* Returns the first element matching the predicate, or None.
|
|
@@ -73,7 +73,7 @@ var Arr;
|
|
|
73
73
|
*/
|
|
74
74
|
Arr.findFirst = (predicate) => (data) => {
|
|
75
75
|
const idx = data.findIndex(predicate);
|
|
76
|
-
return idx >= 0 ? Option_js_1.Option.
|
|
76
|
+
return idx >= 0 ? Option_js_1.Option.some(data[idx]) : Option_js_1.Option.none();
|
|
77
77
|
};
|
|
78
78
|
/**
|
|
79
79
|
* Returns the last element matching the predicate, or None.
|
|
@@ -86,9 +86,9 @@ var Arr;
|
|
|
86
86
|
Arr.findLast = (predicate) => (data) => {
|
|
87
87
|
for (let i = data.length - 1; i >= 0; i--) {
|
|
88
88
|
if (predicate(data[i]))
|
|
89
|
-
return Option_js_1.Option.
|
|
89
|
+
return Option_js_1.Option.some(data[i]);
|
|
90
90
|
}
|
|
91
|
-
return Option_js_1.Option.
|
|
91
|
+
return Option_js_1.Option.none();
|
|
92
92
|
};
|
|
93
93
|
/**
|
|
94
94
|
* Returns the index of the first element matching the predicate, or None.
|
|
@@ -100,7 +100,7 @@ var Arr;
|
|
|
100
100
|
*/
|
|
101
101
|
Arr.findIndex = (predicate) => (data) => {
|
|
102
102
|
const idx = data.findIndex(predicate);
|
|
103
|
-
return idx >= 0 ? Option_js_1.Option.
|
|
103
|
+
return idx >= 0 ? Option_js_1.Option.some(idx) : Option_js_1.Option.none();
|
|
104
104
|
};
|
|
105
105
|
// --- Transform ---
|
|
106
106
|
/**
|
|
@@ -306,7 +306,7 @@ var Arr;
|
|
|
306
306
|
* ```ts
|
|
307
307
|
* const parseNum = (s: string): Option<number> => {
|
|
308
308
|
* const n = Number(s);
|
|
309
|
-
* return isNaN(n) ? Option.
|
|
309
|
+
* return isNaN(n) ? Option.none() : Option.of(n);
|
|
310
310
|
* };
|
|
311
311
|
*
|
|
312
312
|
* pipe(["1", "2", "3"], Arr.traverse(parseNum)); // Some([1, 2, 3])
|
|
@@ -318,10 +318,10 @@ var Arr;
|
|
|
318
318
|
for (const a of data) {
|
|
319
319
|
const mapped = f(a);
|
|
320
320
|
if (Option_js_1.Option.isNone(mapped))
|
|
321
|
-
return Option_js_1.Option.
|
|
321
|
+
return Option_js_1.Option.none();
|
|
322
322
|
result.push(mapped.value);
|
|
323
323
|
}
|
|
324
|
-
return Option_js_1.Option.
|
|
324
|
+
return Option_js_1.Option.some(result);
|
|
325
325
|
};
|
|
326
326
|
/**
|
|
327
327
|
* Maps each element to a Result and collects the results.
|
|
@@ -331,7 +331,7 @@ var Arr;
|
|
|
331
331
|
* ```ts
|
|
332
332
|
* pipe(
|
|
333
333
|
* [1, 2, 3],
|
|
334
|
-
* Arr.traverseResult(n => n > 0 ? Result.
|
|
334
|
+
* Arr.traverseResult(n => n > 0 ? Result.ok(n) : Result.err("negative"))
|
|
335
335
|
* ); // Ok([1, 2, 3])
|
|
336
336
|
* ```
|
|
337
337
|
*/
|
|
@@ -343,7 +343,7 @@ var Arr;
|
|
|
343
343
|
return mapped;
|
|
344
344
|
result.push(mapped.value);
|
|
345
345
|
}
|
|
346
|
-
return Result_js_1.Result.
|
|
346
|
+
return Result_js_1.Result.ok(result);
|
|
347
347
|
};
|
|
348
348
|
/**
|
|
349
349
|
* Maps each element to a Task and runs all in parallel.
|
|
@@ -364,7 +364,7 @@ var Arr;
|
|
|
364
364
|
* @example
|
|
365
365
|
* ```ts
|
|
366
366
|
* Arr.sequence([Option.of(1), Option.of(2)]); // Some([1, 2])
|
|
367
|
-
* Arr.sequence([Option.of(1), Option.
|
|
367
|
+
* Arr.sequence([Option.of(1), Option.none()]); // None
|
|
368
368
|
* ```
|
|
369
369
|
*/
|
|
370
370
|
Arr.sequence = (data) => Arr.traverse((a) => a)(data);
|
|
@@ -12,11 +12,11 @@ var Option;
|
|
|
12
12
|
* Option.of(42); // Some(42)
|
|
13
13
|
* ```
|
|
14
14
|
*/
|
|
15
|
-
Option.of = (value) => Option.
|
|
15
|
+
Option.of = (value) => Option.some(value);
|
|
16
16
|
/**
|
|
17
17
|
* Creates a Some containing the given value.
|
|
18
18
|
*/
|
|
19
|
-
Option.
|
|
19
|
+
Option.some = (value) => ({ kind: "Some", value });
|
|
20
20
|
/**
|
|
21
21
|
* Type guard that checks if a Option is Some.
|
|
22
22
|
*/
|
|
@@ -24,7 +24,7 @@ var Option;
|
|
|
24
24
|
/**
|
|
25
25
|
* Creates a None (empty Option).
|
|
26
26
|
*/
|
|
27
|
-
Option.
|
|
27
|
+
Option.none = () => ({ kind: "None" });
|
|
28
28
|
/**
|
|
29
29
|
* Type guard that checks if a Option is None.
|
|
30
30
|
*/
|
|
@@ -39,7 +39,7 @@ var Option;
|
|
|
39
39
|
* Option.fromNullable(42); // Some(42)
|
|
40
40
|
* ```
|
|
41
41
|
*/
|
|
42
|
-
Option.fromNullable = (value) => value === null || value === undefined ? Option.
|
|
42
|
+
Option.fromNullable = (value) => value === null || value === undefined ? Option.none() : Option.some(value);
|
|
43
43
|
/**
|
|
44
44
|
* Extracts the value from a Option, returning null if None.
|
|
45
45
|
*/
|
|
@@ -52,7 +52,7 @@ var Option;
|
|
|
52
52
|
* Creates a Option from a possibly undefined value.
|
|
53
53
|
* Returns None if undefined, Some otherwise.
|
|
54
54
|
*/
|
|
55
|
-
Option.fromUndefined = (value) => value === undefined ? Option.
|
|
55
|
+
Option.fromUndefined = (value) => value === undefined ? Option.none() : Option.some(value);
|
|
56
56
|
/**
|
|
57
57
|
* Converts an Option to a Result.
|
|
58
58
|
* Some becomes Ok, None becomes Err with the provided error.
|
|
@@ -65,33 +65,33 @@ var Option;
|
|
|
65
65
|
* ); // Ok(42)
|
|
66
66
|
*
|
|
67
67
|
* pipe(
|
|
68
|
-
* Option.
|
|
68
|
+
* Option.none(),
|
|
69
69
|
* Option.toResult(() => "Value was missing")
|
|
70
70
|
* ); // Err("Value was missing")
|
|
71
71
|
* ```
|
|
72
72
|
*/
|
|
73
|
-
Option.toResult = (onNone) => (data) => Option.isSome(data) ? Result_js_1.Result.
|
|
73
|
+
Option.toResult = (onNone) => (data) => Option.isSome(data) ? Result_js_1.Result.ok(data.value) : Result_js_1.Result.err(onNone());
|
|
74
74
|
/**
|
|
75
75
|
* Creates an Option from a Result.
|
|
76
76
|
* Ok becomes Some, Err becomes None (the error is discarded).
|
|
77
77
|
*
|
|
78
78
|
* @example
|
|
79
79
|
* ```ts
|
|
80
|
-
* Option.fromResult(Result.
|
|
81
|
-
* Option.fromResult(Result.
|
|
80
|
+
* Option.fromResult(Result.ok(42)); // Some(42)
|
|
81
|
+
* Option.fromResult(Result.err("oops")); // None
|
|
82
82
|
* ```
|
|
83
83
|
*/
|
|
84
|
-
Option.fromResult = (data) => Result_js_1.Result.isOk(data) ? Option.
|
|
84
|
+
Option.fromResult = (data) => Result_js_1.Result.isOk(data) ? Option.some(data.value) : Option.none();
|
|
85
85
|
/**
|
|
86
86
|
* Transforms the value inside a Option if it exists.
|
|
87
87
|
*
|
|
88
88
|
* @example
|
|
89
89
|
* ```ts
|
|
90
90
|
* pipe(Option.of(5), Option.map(n => n * 2)); // Some(10)
|
|
91
|
-
* pipe(Option.
|
|
91
|
+
* pipe(Option.none(), Option.map(n => n * 2)); // None
|
|
92
92
|
* ```
|
|
93
93
|
*/
|
|
94
|
-
Option.map = (f) => (data) => Option.isSome(data) ? Option.
|
|
94
|
+
Option.map = (f) => (data) => Option.isSome(data) ? Option.some(f(data.value)) : data;
|
|
95
95
|
/**
|
|
96
96
|
* Chains Option computations. If the first is Some, passes the value to f.
|
|
97
97
|
* If the first is None, propagates None.
|
|
@@ -100,7 +100,7 @@ var Option;
|
|
|
100
100
|
* ```ts
|
|
101
101
|
* const parseNumber = (s: string): Option<number> => {
|
|
102
102
|
* const n = parseInt(s, 10);
|
|
103
|
-
* return isNaN(n) ? Option.
|
|
103
|
+
* return isNaN(n) ? Option.none() : Option.of(n);
|
|
104
104
|
* };
|
|
105
105
|
*
|
|
106
106
|
* pipe(Option.of("42"), Option.chain(parseNumber)); // Some(42)
|
|
@@ -144,7 +144,7 @@ var Option;
|
|
|
144
144
|
* @example
|
|
145
145
|
* ```ts
|
|
146
146
|
* pipe(Option.of(5), Option.getOrElse(0)); // 5
|
|
147
|
-
* pipe(Option.
|
|
147
|
+
* pipe(Option.none(), Option.getOrElse(0)); // 0
|
|
148
148
|
* ```
|
|
149
149
|
*/
|
|
150
150
|
Option.getOrElse = (defaultValue) => (data) => Option.isSome(data) ? data.value : defaultValue;
|
|
@@ -176,7 +176,7 @@ var Option;
|
|
|
176
176
|
* pipe(Option.of(2), Option.filter(n => n > 3)); // None
|
|
177
177
|
* ```
|
|
178
178
|
*/
|
|
179
|
-
Option.filter = (predicate) => (data) => Option.isSome(data) && predicate(data.value) ? data : Option.
|
|
179
|
+
Option.filter = (predicate) => (data) => Option.isSome(data) && predicate(data.value) ? data : Option.none();
|
|
180
180
|
/**
|
|
181
181
|
* Recovers from a None by providing a fallback Option.
|
|
182
182
|
*/
|
|
@@ -194,5 +194,5 @@ var Option;
|
|
|
194
194
|
* ); // Some(8)
|
|
195
195
|
* ```
|
|
196
196
|
*/
|
|
197
|
-
Option.ap = (arg) => (data) => Option.isSome(data) && Option.isSome(arg) ? Option.
|
|
197
|
+
Option.ap = (arg) => (data) => Option.isSome(data) && Option.isSome(arg) ? Option.some(data.value(arg.value)) : Option.none();
|
|
198
198
|
})(Option || (exports.Option = Option = {}));
|
package/script/src/Core/Rec.js
CHANGED
|
@@ -90,7 +90,7 @@ var Rec;
|
|
|
90
90
|
* pipe({ a: 1, b: 2 }, Rec.lookup("c")); // None
|
|
91
91
|
* ```
|
|
92
92
|
*/
|
|
93
|
-
Rec.lookup = (key) => (data) => Object.prototype.hasOwnProperty.call(data, key) ? Option_js_1.Option.
|
|
93
|
+
Rec.lookup = (key) => (data) => Object.prototype.hasOwnProperty.call(data, key) ? Option_js_1.Option.some(data[key]) : Option_js_1.Option.none();
|
|
94
94
|
/**
|
|
95
95
|
* Returns all keys of a record.
|
|
96
96
|
*/
|
|
@@ -11,15 +11,15 @@ var Result;
|
|
|
11
11
|
* Result.of(42); // Ok(42)
|
|
12
12
|
* ```
|
|
13
13
|
*/
|
|
14
|
-
Result.of = (value) => Result.
|
|
14
|
+
Result.of = (value) => Result.ok(value);
|
|
15
15
|
/**
|
|
16
16
|
* Creates a failed Result with the given error.
|
|
17
17
|
*/
|
|
18
|
-
Result.
|
|
18
|
+
Result.err = (error) => ({ kind: "Error", error });
|
|
19
19
|
/**
|
|
20
20
|
* Creates a successful Result with the given value.
|
|
21
21
|
*/
|
|
22
|
-
Result.
|
|
22
|
+
Result.ok = (value) => ({ kind: "Ok", value });
|
|
23
23
|
/**
|
|
24
24
|
* Type guard that checks if an Result is Ok.
|
|
25
25
|
*/
|
|
@@ -43,10 +43,10 @@ var Result;
|
|
|
43
43
|
*/
|
|
44
44
|
Result.tryCatch = (f, onError) => {
|
|
45
45
|
try {
|
|
46
|
-
return Result.
|
|
46
|
+
return Result.ok(f());
|
|
47
47
|
}
|
|
48
48
|
catch (e) {
|
|
49
|
-
return Result.
|
|
49
|
+
return Result.err(onError(e));
|
|
50
50
|
}
|
|
51
51
|
};
|
|
52
52
|
/**
|
|
@@ -55,19 +55,19 @@ var Result;
|
|
|
55
55
|
* @example
|
|
56
56
|
* ```ts
|
|
57
57
|
* pipe(Result.of(5), Result.map(n => n * 2)); // Ok(10)
|
|
58
|
-
* pipe(Result.
|
|
58
|
+
* pipe(Result.err("error"), Result.map(n => n * 2)); // Err("error")
|
|
59
59
|
* ```
|
|
60
60
|
*/
|
|
61
|
-
Result.map = (f) => (data) => Result.isOk(data) ? Result.
|
|
61
|
+
Result.map = (f) => (data) => Result.isOk(data) ? Result.ok(f(data.value)) : data;
|
|
62
62
|
/**
|
|
63
63
|
* Transforms the error value inside an Result.
|
|
64
64
|
*
|
|
65
65
|
* @example
|
|
66
66
|
* ```ts
|
|
67
|
-
* pipe(Result.
|
|
67
|
+
* pipe(Result.err("oops"), Result.mapError(e => e.toUpperCase())); // Err("OOPS")
|
|
68
68
|
* ```
|
|
69
69
|
*/
|
|
70
|
-
Result.mapError = (f) => (data) => Result.isErr(data) ? Result.
|
|
70
|
+
Result.mapError = (f) => (data) => Result.isErr(data) ? Result.err(f(data.error)) : data;
|
|
71
71
|
/**
|
|
72
72
|
* Chains Result computations. If the first is Ok, passes the value to f.
|
|
73
73
|
* If the first is Err, propagates the error.
|
|
@@ -75,7 +75,7 @@ var Result;
|
|
|
75
75
|
* @example
|
|
76
76
|
* ```ts
|
|
77
77
|
* const validatePositive = (n: number): Result<string, number> =>
|
|
78
|
-
* n > 0 ? Result.of(n) : Result.
|
|
78
|
+
* n > 0 ? Result.of(n) : Result.err("Must be positive");
|
|
79
79
|
*
|
|
80
80
|
* pipe(Result.of(5), Result.chain(validatePositive)); // Ok(5)
|
|
81
81
|
* pipe(Result.of(-1), Result.chain(validatePositive)); // Err("Must be positive")
|
|
@@ -118,7 +118,7 @@ var Result;
|
|
|
118
118
|
* @example
|
|
119
119
|
* ```ts
|
|
120
120
|
* pipe(Result.of(5), Result.getOrElse(0)); // 5
|
|
121
|
-
* pipe(Result.
|
|
121
|
+
* pipe(Result.err("error"), Result.getOrElse(0)); // 0
|
|
122
122
|
* ```
|
|
123
123
|
*/
|
|
124
124
|
Result.getOrElse = (defaultValue) => (data) => Result.isOk(data) ? data.value : defaultValue;
|
|
@@ -154,8 +154,8 @@ var Result;
|
|
|
154
154
|
*
|
|
155
155
|
* @example
|
|
156
156
|
* ```ts
|
|
157
|
-
* Result.toOption(Result.
|
|
158
|
-
* Result.toOption(Result.
|
|
157
|
+
* Result.toOption(Result.ok(42)); // Some(42)
|
|
158
|
+
* Result.toOption(Result.err("oops")); // None
|
|
159
159
|
* ```
|
|
160
160
|
*/
|
|
161
161
|
Result.toOption = (data) => Result.isOk(data) ? { kind: "Some", value: data.value } : { kind: "None" };
|
|
@@ -172,5 +172,5 @@ var Result;
|
|
|
172
172
|
* ); // Ok(8)
|
|
173
173
|
* ```
|
|
174
174
|
*/
|
|
175
|
-
Result.ap = (arg) => (data) => Result.isOk(data) && Result.isOk(arg) ? Result.
|
|
175
|
+
Result.ap = (arg) => (data) => Result.isOk(data) && Result.isOk(arg) ? Result.ok(data.value(arg.value)) : Result.isErr(data) ? data : arg;
|
|
176
176
|
})(Result || (exports.Result = Result = {}));
|
package/script/src/Core/Task.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Task = void 0;
|
|
4
|
+
const Result_js_1 = require("./Result.js");
|
|
4
5
|
var Task;
|
|
5
6
|
(function (Task) {
|
|
6
7
|
/**
|
|
@@ -13,10 +14,6 @@ var Task;
|
|
|
13
14
|
* ```
|
|
14
15
|
*/
|
|
15
16
|
Task.of = (value) => () => Promise.resolve(value);
|
|
16
|
-
/**
|
|
17
|
-
* Creates a Task that will reject with the given error.
|
|
18
|
-
*/
|
|
19
|
-
Task.fail = (error) => () => Promise.reject(error);
|
|
20
17
|
/**
|
|
21
18
|
* Creates a Task from a function that returns a Promise.
|
|
22
19
|
* Alias for directly creating a Task.
|
|
@@ -40,7 +37,7 @@ var Task;
|
|
|
40
37
|
*/
|
|
41
38
|
Task.map = (f) => (data) => () => data().then(f);
|
|
42
39
|
/**
|
|
43
|
-
* Chains Task computations.
|
|
40
|
+
* Chains Task computations. Passes the resolved value of the first Task to f.
|
|
44
41
|
*
|
|
45
42
|
* @example
|
|
46
43
|
* ```ts
|
|
@@ -98,6 +95,7 @@ var Task;
|
|
|
98
95
|
Task.all = (tasks) => () => Promise.all(tasks.map((t) => t()));
|
|
99
96
|
/**
|
|
100
97
|
* Delays the execution of a Task by the specified milliseconds.
|
|
98
|
+
* Useful for debouncing or rate limiting.
|
|
101
99
|
*
|
|
102
100
|
* @example
|
|
103
101
|
* ```ts
|
|
@@ -108,4 +106,74 @@ var Task;
|
|
|
108
106
|
* ```
|
|
109
107
|
*/
|
|
110
108
|
Task.delay = (ms) => (data) => () => new Promise((resolve, reject) => setTimeout(() => data().then(resolve, reject), ms));
|
|
109
|
+
/**
|
|
110
|
+
* Runs a Task a fixed number of times sequentially, collecting all results into an array.
|
|
111
|
+
* An optional delay (ms) can be inserted between runs.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```ts
|
|
115
|
+
* pipe(
|
|
116
|
+
* pollSensor,
|
|
117
|
+
* Task.repeat({ times: 5, delay: 1000 })
|
|
118
|
+
* )(); // Task<Reading[]> — 5 readings, one per second
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
Task.repeat = (options) => (task) => () => {
|
|
122
|
+
const { times, delay: ms } = options;
|
|
123
|
+
if (times <= 0)
|
|
124
|
+
return Promise.resolve([]);
|
|
125
|
+
const results = [];
|
|
126
|
+
const wait = () => ms !== undefined && ms > 0 ? new Promise((r) => setTimeout(r, ms)) : Promise.resolve();
|
|
127
|
+
const run = (left) => task().then((a) => {
|
|
128
|
+
results.push(a);
|
|
129
|
+
if (left <= 1)
|
|
130
|
+
return results;
|
|
131
|
+
return wait().then(() => run(left - 1));
|
|
132
|
+
});
|
|
133
|
+
return run(times);
|
|
134
|
+
};
|
|
135
|
+
/**
|
|
136
|
+
* Runs a Task repeatedly until the result satisfies a predicate, returning that result.
|
|
137
|
+
* An optional delay (ms) can be inserted between runs.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```ts
|
|
141
|
+
* pipe(
|
|
142
|
+
* checkStatus,
|
|
143
|
+
* Task.repeatUntil({ when: (s) => s === "ready", delay: 500 })
|
|
144
|
+
* )(); // polls every 500ms until status is "ready"
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
Task.repeatUntil = (options) => (task) => () => {
|
|
148
|
+
const { when: predicate, delay: ms } = options;
|
|
149
|
+
const wait = () => ms !== undefined && ms > 0 ? new Promise((r) => setTimeout(r, ms)) : Promise.resolve();
|
|
150
|
+
const run = () => task().then((a) => {
|
|
151
|
+
if (predicate(a))
|
|
152
|
+
return a;
|
|
153
|
+
return wait().then(run);
|
|
154
|
+
});
|
|
155
|
+
return run();
|
|
156
|
+
};
|
|
157
|
+
/**
|
|
158
|
+
* Converts a `Task<A>` into a `Task<Result<E, A>>`, resolving to `Err` if the
|
|
159
|
+
* Task does not complete within the given time.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```ts
|
|
163
|
+
* pipe(
|
|
164
|
+
* fetchUser,
|
|
165
|
+
* Task.timeout(5000, () => new TimeoutError("fetch user timed out")),
|
|
166
|
+
* TaskResult.chain(processUser)
|
|
167
|
+
* );
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
Task.timeout = (ms, onTimeout) => (task) => () => {
|
|
171
|
+
let timerId;
|
|
172
|
+
return Promise.race([
|
|
173
|
+
task().then((a) => { clearTimeout(timerId); return Result_js_1.Result.ok(a); }),
|
|
174
|
+
new Promise((resolve) => {
|
|
175
|
+
timerId = setTimeout(() => resolve(Result_js_1.Result.err(onTimeout())), ms);
|
|
176
|
+
}),
|
|
177
|
+
]);
|
|
178
|
+
};
|
|
111
179
|
})(Task || (exports.Task = Task = {}));
|