@nlozgachev/pipekit 0.1.8 → 0.3.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 +18 -13
- package/esm/src/Composition/flip.js +2 -2
- package/esm/src/Composition/fn.js +1 -1
- package/esm/src/Composition/tap.js +1 -1
- package/esm/src/Core/Arr.js +16 -16
- package/esm/src/Core/Option.js +27 -36
- package/esm/src/Core/Rec.js +1 -1
- package/esm/src/Core/RemoteData.js +9 -13
- package/esm/src/Core/Result.js +23 -32
- package/esm/src/Core/Task.js +95 -24
- package/esm/src/Core/TaskOption.js +8 -6
- package/esm/src/Core/TaskResult.js +67 -6
- package/esm/src/Core/TaskValidation.js +12 -8
- package/esm/src/Core/These.js +42 -42
- package/esm/src/Core/Validation.js +42 -43
- package/esm/src/Types/Brand.js +3 -3
- package/package.json +1 -1
- package/script/src/Composition/flip.js +2 -2
- package/script/src/Composition/fn.js +1 -1
- package/script/src/Composition/tap.js +1 -1
- package/script/src/Core/Arr.js +16 -16
- package/script/src/Core/Option.js +27 -36
- package/script/src/Core/Rec.js +1 -1
- package/script/src/Core/RemoteData.js +9 -13
- package/script/src/Core/Result.js +23 -32
- package/script/src/Core/Task.js +95 -24
- package/script/src/Core/TaskOption.js +8 -6
- package/script/src/Core/TaskResult.js +67 -6
- package/script/src/Core/TaskValidation.js +12 -8
- package/script/src/Core/These.js +42 -42
- package/script/src/Core/Validation.js +42 -43
- package/script/src/Types/Brand.js +3 -3
- package/types/src/Composition/flip.d.ts +2 -2
- package/types/src/Composition/fn.d.ts +1 -1
- package/types/src/Composition/pipe.d.ts +1 -1
- package/types/src/Composition/tap.d.ts +1 -1
- package/types/src/Composition/uncurry.d.ts +3 -3
- package/types/src/Core/Arr.d.ts +5 -5
- package/types/src/Core/Arr.d.ts.map +1 -1
- package/types/src/Core/Option.d.ts +21 -30
- package/types/src/Core/Option.d.ts.map +1 -1
- package/types/src/Core/Rec.d.ts.map +1 -1
- package/types/src/Core/RemoteData.d.ts +9 -13
- package/types/src/Core/RemoteData.d.ts.map +1 -1
- package/types/src/Core/Result.d.ts +19 -28
- package/types/src/Core/Result.d.ts.map +1 -1
- package/types/src/Core/Task.d.ts +81 -34
- package/types/src/Core/Task.d.ts.map +1 -1
- package/types/src/Core/TaskOption.d.ts +1 -1
- package/types/src/Core/TaskOption.d.ts.map +1 -1
- package/types/src/Core/TaskResult.d.ts +41 -2
- package/types/src/Core/TaskResult.d.ts.map +1 -1
- package/types/src/Core/TaskValidation.d.ts +10 -6
- package/types/src/Core/TaskValidation.d.ts.map +1 -1
- package/types/src/Core/These.d.ts +33 -33
- package/types/src/Core/These.d.ts.map +1 -1
- package/types/src/Core/Validation.d.ts +38 -42
- package/types/src/Core/Validation.d.ts.map +1 -1
- package/types/src/Types/Brand.d.ts +5 -5
package/esm/src/Core/Task.js
CHANGED
|
@@ -1,26 +1,23 @@
|
|
|
1
|
+
import { Result } from "./Result.js";
|
|
1
2
|
export var Task;
|
|
2
3
|
(function (Task) {
|
|
3
4
|
/**
|
|
4
|
-
*
|
|
5
|
+
* Creates a Task that immediately resolves to the given value.
|
|
5
6
|
*
|
|
6
7
|
* @example
|
|
7
8
|
* ```ts
|
|
8
|
-
* const task = Task.
|
|
9
|
+
* const task = Task.resolve(42);
|
|
9
10
|
* task().then(console.log); // 42
|
|
10
11
|
* ```
|
|
11
12
|
*/
|
|
12
|
-
Task.
|
|
13
|
-
/**
|
|
14
|
-
* Creates a Task that will reject with the given error.
|
|
15
|
-
*/
|
|
16
|
-
Task.fail = (error) => () => Promise.reject(error);
|
|
13
|
+
Task.resolve = (value) => () => Promise.resolve(value);
|
|
17
14
|
/**
|
|
18
15
|
* Creates a Task from a function that returns a Promise.
|
|
19
16
|
* Alias for directly creating a Task.
|
|
20
17
|
*
|
|
21
18
|
* @example
|
|
22
19
|
* ```ts
|
|
23
|
-
* const
|
|
20
|
+
* const getTimestamp = Task.from(() => Promise.resolve(Date.now()));
|
|
24
21
|
* ```
|
|
25
22
|
*/
|
|
26
23
|
Task.from = (f) => f;
|
|
@@ -30,24 +27,24 @@ export var Task;
|
|
|
30
27
|
* @example
|
|
31
28
|
* ```ts
|
|
32
29
|
* pipe(
|
|
33
|
-
* Task.
|
|
30
|
+
* Task.resolve(5),
|
|
34
31
|
* Task.map(n => n * 2)
|
|
35
32
|
* )(); // Promise<10>
|
|
36
33
|
* ```
|
|
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
|
|
44
|
-
* const
|
|
45
|
-
* const
|
|
41
|
+
* const readUserId: Task<string> = () => Promise.resolve(session.userId);
|
|
42
|
+
* const loadPrefs = (id: string): Task<Preferences> => () => Promise.resolve(prefsCache.get(id));
|
|
46
43
|
*
|
|
47
44
|
* pipe(
|
|
48
|
-
*
|
|
49
|
-
* Task.chain(
|
|
50
|
-
* )(); // Promise<
|
|
45
|
+
* readUserId,
|
|
46
|
+
* Task.chain(loadPrefs)
|
|
47
|
+
* )(); // Promise<Preferences>
|
|
51
48
|
* ```
|
|
52
49
|
*/
|
|
53
50
|
Task.chain = (f) => (data) => () => data().then((a) => f(a)());
|
|
@@ -59,9 +56,9 @@ export var Task;
|
|
|
59
56
|
* ```ts
|
|
60
57
|
* const add = (a: number) => (b: number) => a + b;
|
|
61
58
|
* pipe(
|
|
62
|
-
* Task.
|
|
63
|
-
* Task.ap(Task.
|
|
64
|
-
* Task.ap(Task.
|
|
59
|
+
* Task.resolve(add),
|
|
60
|
+
* Task.ap(Task.resolve(5)),
|
|
61
|
+
* Task.ap(Task.resolve(3))
|
|
65
62
|
* )(); // Promise<8>
|
|
66
63
|
* ```
|
|
67
64
|
*/
|
|
@@ -73,9 +70,9 @@ export var Task;
|
|
|
73
70
|
* @example
|
|
74
71
|
* ```ts
|
|
75
72
|
* pipe(
|
|
76
|
-
*
|
|
77
|
-
* Task.tap(
|
|
78
|
-
* Task.map(
|
|
73
|
+
* loadConfig,
|
|
74
|
+
* Task.tap(cfg => console.log("Config:", cfg)),
|
|
75
|
+
* Task.map(buildReport)
|
|
79
76
|
* );
|
|
80
77
|
* ```
|
|
81
78
|
*/
|
|
@@ -88,21 +85,95 @@ export var Task;
|
|
|
88
85
|
*
|
|
89
86
|
* @example
|
|
90
87
|
* ```ts
|
|
91
|
-
* Task.all([
|
|
92
|
-
* // Promise<[
|
|
88
|
+
* Task.all([loadConfig, detectLocale, loadTheme])();
|
|
89
|
+
* // Promise<[Config, string, Theme]>
|
|
93
90
|
* ```
|
|
94
91
|
*/
|
|
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
|
|
101
99
|
* pipe(
|
|
102
|
-
* Task.
|
|
100
|
+
* Task.resolve(42),
|
|
103
101
|
* Task.delay(1000)
|
|
104
102
|
* )(); // Resolves after 1 second
|
|
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
|
+
* heavyComputation,
|
|
162
|
+
* Task.timeout(5000, () => "timed out"),
|
|
163
|
+
* TaskResult.chain(processResult)
|
|
164
|
+
* );
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
Task.timeout = (ms, onTimeout) => (task) => () => {
|
|
168
|
+
let timerId;
|
|
169
|
+
return Promise.race([
|
|
170
|
+
task().then((a) => {
|
|
171
|
+
clearTimeout(timerId);
|
|
172
|
+
return Result.ok(a);
|
|
173
|
+
}),
|
|
174
|
+
new Promise((resolve) => {
|
|
175
|
+
timerId = setTimeout(() => resolve(Result.err(onTimeout())), ms);
|
|
176
|
+
}),
|
|
177
|
+
]);
|
|
178
|
+
};
|
|
108
179
|
})(Task || (Task = {}));
|
|
@@ -5,19 +5,19 @@ export var TaskOption;
|
|
|
5
5
|
/**
|
|
6
6
|
* Wraps a value in a Some inside a Task.
|
|
7
7
|
*/
|
|
8
|
-
TaskOption.
|
|
8
|
+
TaskOption.some = (value) => Task.resolve(Option.some(value));
|
|
9
9
|
/**
|
|
10
10
|
* Creates a TaskOption that resolves to None.
|
|
11
11
|
*/
|
|
12
|
-
TaskOption.none = () => Task.
|
|
12
|
+
TaskOption.none = () => Task.resolve(Option.none());
|
|
13
13
|
/**
|
|
14
14
|
* Lifts an Option into a TaskOption.
|
|
15
15
|
*/
|
|
16
|
-
TaskOption.fromOption = (option) => Task.
|
|
16
|
+
TaskOption.fromOption = (option) => Task.resolve(option);
|
|
17
17
|
/**
|
|
18
18
|
* Lifts a Task into a TaskOption by wrapping its result in Some.
|
|
19
19
|
*/
|
|
20
|
-
TaskOption.fromTask = (task) => Task.map(Option.
|
|
20
|
+
TaskOption.fromTask = (task) => Task.map(Option.some)(task);
|
|
21
21
|
/**
|
|
22
22
|
* Creates a TaskOption from a Promise-returning function.
|
|
23
23
|
* Returns Some if the promise resolves, None if it rejects.
|
|
@@ -29,7 +29,9 @@ export var TaskOption;
|
|
|
29
29
|
* );
|
|
30
30
|
* ```
|
|
31
31
|
*/
|
|
32
|
-
TaskOption.tryCatch = (f) => () => f()
|
|
32
|
+
TaskOption.tryCatch = (f) => () => f()
|
|
33
|
+
.then(Option.some)
|
|
34
|
+
.catch(() => Option.none());
|
|
33
35
|
/**
|
|
34
36
|
* Transforms the value inside a TaskOption.
|
|
35
37
|
*/
|
|
@@ -46,7 +48,7 @@ export var TaskOption;
|
|
|
46
48
|
* )();
|
|
47
49
|
* ```
|
|
48
50
|
*/
|
|
49
|
-
TaskOption.chain = (f) => (data) => Task.chain((option) => Option.isSome(option) ? f(option.value) : Task.
|
|
51
|
+
TaskOption.chain = (f) => (data) => Task.chain((option) => Option.isSome(option) ? f(option.value) : Task.resolve(Option.none()))(data);
|
|
50
52
|
/**
|
|
51
53
|
* Applies a function wrapped in a TaskOption to a value wrapped in a TaskOption.
|
|
52
54
|
* 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.
|
|
8
|
+
TaskResult.ok = (value) => Task.resolve(Result.ok(value));
|
|
9
9
|
/**
|
|
10
10
|
* Creates a failed TaskResult with the given error.
|
|
11
11
|
*/
|
|
12
|
-
TaskResult.
|
|
12
|
+
TaskResult.err = (error) => Task.resolve(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.
|
|
41
|
+
TaskResult.chain = (f) => (data) => Task.chain((result) => Result.isOk(result) ? f(result.value) : Task.resolve(Result.err(result.error)))(data);
|
|
42
42
|
/**
|
|
43
43
|
* Extracts the value from a TaskResult by providing handlers for both cases.
|
|
44
44
|
*/
|
|
@@ -50,7 +50,7 @@ export var TaskResult;
|
|
|
50
50
|
/**
|
|
51
51
|
* Recovers from an error by providing a fallback TaskResult.
|
|
52
52
|
*/
|
|
53
|
-
TaskResult.recover = (fallback) => (data) => Task.chain((result) => Result.isErr(result) ? fallback(result.error) : Task.
|
|
53
|
+
TaskResult.recover = (fallback) => (data) => Task.chain((result) => Result.isErr(result) ? fallback(result.error) : Task.resolve(result))(data);
|
|
54
54
|
/**
|
|
55
55
|
* Returns the success value or a default value if the TaskResult is an error.
|
|
56
56
|
*/
|
|
@@ -60,4 +60,65 @@ 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
|
+
}
|
|
96
|
+
const ms = getDelay(attempts - left + 1);
|
|
97
|
+
return (ms > 0 ? new Promise((r) => setTimeout(r, ms)) : Promise.resolve()).then(() => run(left - 1));
|
|
98
|
+
});
|
|
99
|
+
return run(attempts);
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Fails a TaskResult with a typed error if it does not resolve within the given time.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* pipe(
|
|
107
|
+
* fetchUser,
|
|
108
|
+
* TaskResult.timeout(5000, () => new TimeoutError("fetch user timed out"))
|
|
109
|
+
* );
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
TaskResult.timeout = (ms, onTimeout) => (data) => () => {
|
|
113
|
+
let timerId;
|
|
114
|
+
return Promise.race([
|
|
115
|
+
data().then((result) => {
|
|
116
|
+
clearTimeout(timerId);
|
|
117
|
+
return result;
|
|
118
|
+
}),
|
|
119
|
+
new Promise((resolve) => {
|
|
120
|
+
timerId = setTimeout(() => resolve(Result.err(onTimeout())), ms);
|
|
121
|
+
}),
|
|
122
|
+
]);
|
|
123
|
+
};
|
|
63
124
|
})(TaskResult || (TaskResult = {}));
|
|
@@ -5,15 +5,19 @@ export var TaskValidation;
|
|
|
5
5
|
/**
|
|
6
6
|
* Wraps a value in a valid TaskValidation.
|
|
7
7
|
*/
|
|
8
|
-
TaskValidation.
|
|
8
|
+
TaskValidation.valid = (value) => Task.resolve(Validation.valid(value));
|
|
9
9
|
/**
|
|
10
10
|
* Creates a failed TaskValidation with a single error.
|
|
11
11
|
*/
|
|
12
|
-
TaskValidation.
|
|
12
|
+
TaskValidation.invalid = (error) => Task.resolve(Validation.invalid(error));
|
|
13
|
+
/**
|
|
14
|
+
* Creates an invalid TaskValidation from multiple errors.
|
|
15
|
+
*/
|
|
16
|
+
TaskValidation.invalidAll = (errors) => Task.resolve(Validation.invalidAll(errors));
|
|
13
17
|
/**
|
|
14
18
|
* Lifts a Validation into a TaskValidation.
|
|
15
19
|
*/
|
|
16
|
-
TaskValidation.fromValidation = (validation) => Task.
|
|
20
|
+
TaskValidation.fromValidation = (validation) => Task.resolve(validation);
|
|
17
21
|
/**
|
|
18
22
|
* Creates a TaskValidation from a Promise-returning function.
|
|
19
23
|
* Catches any errors and transforms them using the onError function.
|
|
@@ -28,8 +32,8 @@ export var TaskValidation;
|
|
|
28
32
|
* ```
|
|
29
33
|
*/
|
|
30
34
|
TaskValidation.tryCatch = (f, onError) => () => f()
|
|
31
|
-
.then((Validation.
|
|
32
|
-
.catch((e) => Validation.
|
|
35
|
+
.then((Validation.valid))
|
|
36
|
+
.catch((e) => Validation.invalid(onError(e)));
|
|
33
37
|
/**
|
|
34
38
|
* Transforms the success value inside a TaskValidation.
|
|
35
39
|
*/
|
|
@@ -42,7 +46,7 @@ export var TaskValidation;
|
|
|
42
46
|
*/
|
|
43
47
|
TaskValidation.chain = (f) => (data) => Task.chain((validation) => Validation.isValid(validation)
|
|
44
48
|
? f(validation.value)
|
|
45
|
-
: Task.
|
|
49
|
+
: Task.resolve(Validation.invalidAll(validation.errors)))(data);
|
|
46
50
|
/**
|
|
47
51
|
* Applies a function wrapped in a TaskValidation to a value wrapped in a
|
|
48
52
|
* TaskValidation. Both Tasks run in parallel and errors from both sides
|
|
@@ -51,7 +55,7 @@ export var TaskValidation;
|
|
|
51
55
|
* @example
|
|
52
56
|
* ```ts
|
|
53
57
|
* pipe(
|
|
54
|
-
* TaskValidation.
|
|
58
|
+
* TaskValidation.valid((name: string) => (age: number) => ({ name, age })),
|
|
55
59
|
* TaskValidation.ap(validateName(name)),
|
|
56
60
|
* TaskValidation.ap(validateAge(age))
|
|
57
61
|
* )();
|
|
@@ -89,5 +93,5 @@ export var TaskValidation;
|
|
|
89
93
|
/**
|
|
90
94
|
* Recovers from an Invalid state by providing a fallback TaskValidation.
|
|
91
95
|
*/
|
|
92
|
-
TaskValidation.recover = (fallback) => (data) => Task.chain((validation) => Validation.isValid(validation) ? Task.
|
|
96
|
+
TaskValidation.recover = (fallback) => (data) => Task.chain((validation) => Validation.isValid(validation) ? Task.resolve(validation) : fallback())(data);
|
|
93
97
|
})(TaskValidation || (TaskValidation = {}));
|
package/esm/src/Core/These.js
CHANGED
|
@@ -2,32 +2,32 @@ import { Result } from "./Result.js";
|
|
|
2
2
|
export var These;
|
|
3
3
|
(function (These) {
|
|
4
4
|
/**
|
|
5
|
-
* Creates a These holding only
|
|
5
|
+
* Creates a These holding only a success value (no error).
|
|
6
6
|
*
|
|
7
7
|
* @example
|
|
8
8
|
* ```ts
|
|
9
|
-
* These.
|
|
9
|
+
* These.ok(42);
|
|
10
10
|
* ```
|
|
11
11
|
*/
|
|
12
|
-
These.
|
|
12
|
+
These.ok = (value) => Result.ok(value);
|
|
13
13
|
/**
|
|
14
|
-
* Creates a These holding only
|
|
14
|
+
* Creates a These holding only an error/warning (no success value).
|
|
15
15
|
*
|
|
16
16
|
* @example
|
|
17
17
|
* ```ts
|
|
18
|
-
* These.
|
|
18
|
+
* These.err("Something went wrong");
|
|
19
19
|
* ```
|
|
20
20
|
*/
|
|
21
|
-
These.
|
|
21
|
+
These.err = (error) => Result.err(error);
|
|
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 = {}));
|