@nlozgachev/pipekit 0.1.7 → 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 +41 -218
- 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 +2 -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/Task.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Result } from "./Result.js";
|
|
1
2
|
export var Task;
|
|
2
3
|
(function (Task) {
|
|
3
4
|
/**
|
|
@@ -10,10 +11,6 @@ export var Task;
|
|
|
10
11
|
* ```
|
|
11
12
|
*/
|
|
12
13
|
Task.of = (value) => () => Promise.resolve(value);
|
|
13
|
-
/**
|
|
14
|
-
* Creates a Task that will reject with the given error.
|
|
15
|
-
*/
|
|
16
|
-
Task.fail = (error) => () => Promise.reject(error);
|
|
17
14
|
/**
|
|
18
15
|
* Creates a Task from a function that returns a Promise.
|
|
19
16
|
* Alias for directly creating a Task.
|
|
@@ -37,7 +34,7 @@ export var Task;
|
|
|
37
34
|
*/
|
|
38
35
|
Task.map = (f) => (data) => () => data().then(f);
|
|
39
36
|
/**
|
|
40
|
-
* Chains Task computations.
|
|
37
|
+
* Chains Task computations. Passes the resolved value of the first Task to f.
|
|
41
38
|
*
|
|
42
39
|
* @example
|
|
43
40
|
* ```ts
|
|
@@ -95,6 +92,7 @@ export var Task;
|
|
|
95
92
|
Task.all = (tasks) => () => Promise.all(tasks.map((t) => t()));
|
|
96
93
|
/**
|
|
97
94
|
* Delays the execution of a Task by the specified milliseconds.
|
|
95
|
+
* Useful for debouncing or rate limiting.
|
|
98
96
|
*
|
|
99
97
|
* @example
|
|
100
98
|
* ```ts
|
|
@@ -105,4 +103,74 @@ export var Task;
|
|
|
105
103
|
* ```
|
|
106
104
|
*/
|
|
107
105
|
Task.delay = (ms) => (data) => () => new Promise((resolve, reject) => setTimeout(() => data().then(resolve, reject), ms));
|
|
106
|
+
/**
|
|
107
|
+
* Runs a Task a fixed number of times sequentially, collecting all results into an array.
|
|
108
|
+
* An optional delay (ms) can be inserted between runs.
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```ts
|
|
112
|
+
* pipe(
|
|
113
|
+
* pollSensor,
|
|
114
|
+
* Task.repeat({ times: 5, delay: 1000 })
|
|
115
|
+
* )(); // Task<Reading[]> — 5 readings, one per second
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
Task.repeat = (options) => (task) => () => {
|
|
119
|
+
const { times, delay: ms } = options;
|
|
120
|
+
if (times <= 0)
|
|
121
|
+
return Promise.resolve([]);
|
|
122
|
+
const results = [];
|
|
123
|
+
const wait = () => ms !== undefined && ms > 0 ? new Promise((r) => setTimeout(r, ms)) : Promise.resolve();
|
|
124
|
+
const run = (left) => task().then((a) => {
|
|
125
|
+
results.push(a);
|
|
126
|
+
if (left <= 1)
|
|
127
|
+
return results;
|
|
128
|
+
return wait().then(() => run(left - 1));
|
|
129
|
+
});
|
|
130
|
+
return run(times);
|
|
131
|
+
};
|
|
132
|
+
/**
|
|
133
|
+
* Runs a Task repeatedly until the result satisfies a predicate, returning that result.
|
|
134
|
+
* An optional delay (ms) can be inserted between runs.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* pipe(
|
|
139
|
+
* checkStatus,
|
|
140
|
+
* Task.repeatUntil({ when: (s) => s === "ready", delay: 500 })
|
|
141
|
+
* )(); // polls every 500ms until status is "ready"
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
Task.repeatUntil = (options) => (task) => () => {
|
|
145
|
+
const { when: predicate, delay: ms } = options;
|
|
146
|
+
const wait = () => ms !== undefined && ms > 0 ? new Promise((r) => setTimeout(r, ms)) : Promise.resolve();
|
|
147
|
+
const run = () => task().then((a) => {
|
|
148
|
+
if (predicate(a))
|
|
149
|
+
return a;
|
|
150
|
+
return wait().then(run);
|
|
151
|
+
});
|
|
152
|
+
return run();
|
|
153
|
+
};
|
|
154
|
+
/**
|
|
155
|
+
* Converts a `Task<A>` into a `Task<Result<E, A>>`, resolving to `Err` if the
|
|
156
|
+
* Task does not complete within the given time.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```ts
|
|
160
|
+
* pipe(
|
|
161
|
+
* fetchUser,
|
|
162
|
+
* Task.timeout(5000, () => new TimeoutError("fetch user timed out")),
|
|
163
|
+
* TaskResult.chain(processUser)
|
|
164
|
+
* );
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
Task.timeout = (ms, onTimeout) => (task) => () => {
|
|
168
|
+
let timerId;
|
|
169
|
+
return Promise.race([
|
|
170
|
+
task().then((a) => { clearTimeout(timerId); return Result.ok(a); }),
|
|
171
|
+
new Promise((resolve) => {
|
|
172
|
+
timerId = setTimeout(() => resolve(Result.err(onTimeout())), ms);
|
|
173
|
+
}),
|
|
174
|
+
]);
|
|
175
|
+
};
|
|
108
176
|
})(Task || (Task = {}));
|
|
@@ -9,7 +9,7 @@ export var TaskOption;
|
|
|
9
9
|
/**
|
|
10
10
|
* Creates a TaskOption that resolves to None.
|
|
11
11
|
*/
|
|
12
|
-
TaskOption.none = () => Task.of(Option.
|
|
12
|
+
TaskOption.none = () => Task.of(Option.none());
|
|
13
13
|
/**
|
|
14
14
|
* Lifts an Option into a TaskOption.
|
|
15
15
|
*/
|
|
@@ -29,7 +29,7 @@ export var TaskOption;
|
|
|
29
29
|
* );
|
|
30
30
|
* ```
|
|
31
31
|
*/
|
|
32
|
-
TaskOption.tryCatch = (f) => () => f().then(Option.of).catch(() => Option.
|
|
32
|
+
TaskOption.tryCatch = (f) => () => f().then(Option.of).catch(() => Option.none());
|
|
33
33
|
/**
|
|
34
34
|
* Transforms the value inside a TaskOption.
|
|
35
35
|
*/
|
|
@@ -46,7 +46,7 @@ export var TaskOption;
|
|
|
46
46
|
* )();
|
|
47
47
|
* ```
|
|
48
48
|
*/
|
|
49
|
-
TaskOption.chain = (f) => (data) => Task.chain((option) => Option.isSome(option) ? f(option.value) : Task.of(Option.
|
|
49
|
+
TaskOption.chain = (f) => (data) => Task.chain((option) => Option.isSome(option) ? f(option.value) : Task.of(Option.none()))(data);
|
|
50
50
|
/**
|
|
51
51
|
* Applies a function wrapped in a TaskOption to a value wrapped in a TaskOption.
|
|
52
52
|
* Both Tasks run in parallel.
|
|
@@ -5,11 +5,11 @@ export var TaskResult;
|
|
|
5
5
|
/**
|
|
6
6
|
* Wraps a value in a successful TaskResult.
|
|
7
7
|
*/
|
|
8
|
-
TaskResult.of = (value) => Task.of(Result.
|
|
8
|
+
TaskResult.of = (value) => Task.of(Result.ok(value));
|
|
9
9
|
/**
|
|
10
10
|
* Creates a failed TaskResult with the given error.
|
|
11
11
|
*/
|
|
12
|
-
TaskResult.
|
|
12
|
+
TaskResult.err = (error) => Task.of(Result.err(error));
|
|
13
13
|
/**
|
|
14
14
|
* Creates a TaskResult from a function that may throw.
|
|
15
15
|
* Catches any errors and transforms them using the onError function.
|
|
@@ -24,8 +24,8 @@ export var TaskResult;
|
|
|
24
24
|
* ```
|
|
25
25
|
*/
|
|
26
26
|
TaskResult.tryCatch = (f, onError) => () => f()
|
|
27
|
-
.then(Result.
|
|
28
|
-
.catch((e) => Result.
|
|
27
|
+
.then(Result.ok)
|
|
28
|
+
.catch((e) => Result.err(onError(e)));
|
|
29
29
|
/**
|
|
30
30
|
* Transforms the success value inside a TaskResult.
|
|
31
31
|
*/
|
|
@@ -38,7 +38,7 @@ export var TaskResult;
|
|
|
38
38
|
* Chains TaskResult computations. If the first succeeds, passes the value to f.
|
|
39
39
|
* If the first fails, propagates the error.
|
|
40
40
|
*/
|
|
41
|
-
TaskResult.chain = (f) => (data) => Task.chain((result) => Result.isOk(result) ? f(result.value) : Task.of(Result.
|
|
41
|
+
TaskResult.chain = (f) => (data) => Task.chain((result) => Result.isOk(result) ? f(result.value) : Task.of(Result.err(result.error)))(data);
|
|
42
42
|
/**
|
|
43
43
|
* Extracts the value from a TaskResult by providing handlers for both cases.
|
|
44
44
|
*/
|
|
@@ -60,4 +60,61 @@ export var TaskResult;
|
|
|
60
60
|
* Useful for logging or debugging.
|
|
61
61
|
*/
|
|
62
62
|
TaskResult.tap = (f) => (data) => Task.map(Result.tap(f))(data);
|
|
63
|
+
/**
|
|
64
|
+
* Re-runs a TaskResult on `Err` with configurable attempts, backoff, and retry condition.
|
|
65
|
+
*
|
|
66
|
+
* @param options.attempts - Total number of attempts (1 = no retry, 3 = up to 3 tries)
|
|
67
|
+
* @param options.backoff - Fixed delay in ms, or a function `(attempt) => ms` for computed delay
|
|
68
|
+
* @param options.when - Only retry when this returns true; defaults to always retry on Err
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* // Retry up to 3 times with exponential backoff
|
|
73
|
+
* pipe(
|
|
74
|
+
* fetchUser,
|
|
75
|
+
* TaskResult.retry({ attempts: 3, backoff: n => n * 1000 })
|
|
76
|
+
* );
|
|
77
|
+
*
|
|
78
|
+
* // Only retry on network errors, not auth errors
|
|
79
|
+
* pipe(
|
|
80
|
+
* fetchUser,
|
|
81
|
+
* TaskResult.retry({ attempts: 3, when: e => e instanceof NetworkError })
|
|
82
|
+
* );
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
TaskResult.retry = (options) => (data) => () => {
|
|
86
|
+
const { attempts, backoff, when: shouldRetry } = options;
|
|
87
|
+
const getDelay = (n) => backoff === undefined ? 0 : typeof backoff === "function" ? backoff(n) : backoff;
|
|
88
|
+
const run = (left) => data().then((result) => {
|
|
89
|
+
if (Result.isOk(result))
|
|
90
|
+
return result;
|
|
91
|
+
if (left <= 1)
|
|
92
|
+
return result;
|
|
93
|
+
if (shouldRetry !== undefined && !shouldRetry(result.error))
|
|
94
|
+
return result;
|
|
95
|
+
const ms = getDelay(attempts - left + 1);
|
|
96
|
+
return (ms > 0 ? new Promise((r) => setTimeout(r, ms)) : Promise.resolve()).then(() => run(left - 1));
|
|
97
|
+
});
|
|
98
|
+
return run(attempts);
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Fails a TaskResult with a typed error if it does not resolve within the given time.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* pipe(
|
|
106
|
+
* fetchUser,
|
|
107
|
+
* TaskResult.timeout(5000, () => new TimeoutError("fetch user timed out"))
|
|
108
|
+
* );
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
TaskResult.timeout = (ms, onTimeout) => (data) => () => {
|
|
112
|
+
let timerId;
|
|
113
|
+
return Promise.race([
|
|
114
|
+
data().then((result) => { clearTimeout(timerId); return result; }),
|
|
115
|
+
new Promise((resolve) => {
|
|
116
|
+
timerId = setTimeout(() => resolve(Result.err(onTimeout())), ms);
|
|
117
|
+
}),
|
|
118
|
+
]);
|
|
119
|
+
};
|
|
63
120
|
})(TaskResult || (TaskResult = {}));
|
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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nlozgachev/pipekit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Simple functional programming toolkit for TypeScript",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"functional",
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"composition",
|
|
10
10
|
"pipe"
|
|
11
11
|
],
|
|
12
|
+
"homepage": "https://pipekit.lozgachev.dev",
|
|
12
13
|
"repository": {
|
|
13
14
|
"type": "git",
|
|
14
15
|
"url": "https://github.com/nlozgachev/pipekit"
|
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);
|