ok-fp 0.2.2 → 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 +6 -6
- package/dist/index.d.mts +1328 -0
- package/dist/index.mjs +636 -0
- package/package.json +8 -21
- package/dist/constructors-pI0lvrQE.mjs +0 -142
- package/dist/either-CWllQk7N.d.mts +0 -213
- package/dist/either.d.mts +0 -3
- package/dist/either.mjs +0 -65
- package/dist/helpers-Bw8McftR.d.mts +0 -455
- package/dist/option.d.mts +0 -2
- package/dist/option.mjs +0 -177
- package/dist/task-D9kWv4yq.d.mts +0 -100
- package/dist/task-DQTXV7xv.mjs +0 -23
- package/dist/task.d.mts +0 -49
- package/dist/task.mjs +0 -57
- package/dist/taskEither.d.mts +0 -212
- package/dist/taskEither.mjs +0 -80
- package/dist/validation.d.mts +0 -233
- package/dist/validation.mjs +0 -165
package/dist/task-D9kWv4yq.d.mts
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
//#region src/task/model.d.ts
|
|
2
|
-
type TaskValue<T> = () => Promise<T>;
|
|
3
|
-
//#endregion
|
|
4
|
-
//#region src/task/task.d.ts
|
|
5
|
-
type Task<T> = {
|
|
6
|
-
/**
|
|
7
|
-
* Transforms the value produced by the Task using a mapping function.
|
|
8
|
-
*
|
|
9
|
-
* @typeParam U - The type of the transformed value
|
|
10
|
-
* @param mapper - Function to transform the resolved value
|
|
11
|
-
* @returns A new Task that resolves with the transformed value
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```typescript
|
|
15
|
-
* task(5).map(x => x * 2).run() // Promise<10>
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
map: <U>(mapper: (value: T) => U) => Task<U>;
|
|
19
|
-
/**
|
|
20
|
-
* Applies a function wrapped in a Task to a value wrapped in a Task.
|
|
21
|
-
* Both Tasks are run concurrently.
|
|
22
|
-
*
|
|
23
|
-
* @typeParam A - The type of the argument value
|
|
24
|
-
* @typeParam U - The type of the function's return value
|
|
25
|
-
* @param arg - Task containing the argument to apply the function to
|
|
26
|
-
* @returns Task containing the function result
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* ```typescript
|
|
30
|
-
* const add = (x: number) => (y: number) => x + y;
|
|
31
|
-
* task(add(5)).ap(task(3)).run() // Promise<8>
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
ap: <A, U>(this: Task<(a: A) => U>, arg: Task<A>) => Task<U>;
|
|
35
|
-
/**
|
|
36
|
-
* Combines this Task with another Task into a tuple.
|
|
37
|
-
* Both Tasks are run concurrently.
|
|
38
|
-
*
|
|
39
|
-
* @typeParam A - The type of the value in the other Task
|
|
40
|
-
* @param taskA - The Task to combine with this one
|
|
41
|
-
* @returns Task containing a tuple of both values
|
|
42
|
-
*
|
|
43
|
-
* @example
|
|
44
|
-
* ```typescript
|
|
45
|
-
* task("Alice").zip(task(30)).run() // Promise<["Alice", 30]>
|
|
46
|
-
* ```
|
|
47
|
-
*/
|
|
48
|
-
zip: <A>(taskA: Task<A>) => Task<readonly [T, A]>;
|
|
49
|
-
/**
|
|
50
|
-
* Flattens a nested Task structure by removing one level of nesting.
|
|
51
|
-
*
|
|
52
|
-
* @returns The inner Task result
|
|
53
|
-
*
|
|
54
|
-
* @example
|
|
55
|
-
* ```typescript
|
|
56
|
-
* task(task(42)).flatten().run() // Promise<42>
|
|
57
|
-
* ```
|
|
58
|
-
*/
|
|
59
|
-
flatten: <U>(this: Task<Task<U>>) => Task<U>;
|
|
60
|
-
/**
|
|
61
|
-
* Chains Task-returning operations together (monadic bind).
|
|
62
|
-
*
|
|
63
|
-
* @typeParam U - The type of the value in the returned Task
|
|
64
|
-
* @param mapper - Function that takes a resolved value and returns a Task
|
|
65
|
-
* @returns The Task returned by mapper
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```typescript
|
|
69
|
-
* task(10).flatMap(x => task(x * 2)).run() // Promise<20>
|
|
70
|
-
* ```
|
|
71
|
-
*/
|
|
72
|
-
flatMap: <U>(mapper: (value: T) => Task<U>) => Task<U>;
|
|
73
|
-
/**
|
|
74
|
-
* Performs a side effect with the resolved value, returning the original Task unchanged.
|
|
75
|
-
*
|
|
76
|
-
* @param sideEffect - Function to execute with the resolved value (return value is ignored)
|
|
77
|
-
* @returns The same Task instance unchanged
|
|
78
|
-
*
|
|
79
|
-
* @example
|
|
80
|
-
* ```typescript
|
|
81
|
-
* task(42)
|
|
82
|
-
* .tap(value => console.log(`Got value: ${value}`))
|
|
83
|
-
* .run() // Logs: "Got value: 42", resolves to 42
|
|
84
|
-
* ```
|
|
85
|
-
*/
|
|
86
|
-
tap: (sideEffect: (value: T) => unknown) => Task<T>;
|
|
87
|
-
/**
|
|
88
|
-
* Executes the Task and returns the resulting Promise.
|
|
89
|
-
*
|
|
90
|
-
* @returns A Promise that resolves with the Task's value
|
|
91
|
-
*
|
|
92
|
-
* @example
|
|
93
|
-
* ```typescript
|
|
94
|
-
* const result = await task(42).run(); // 42
|
|
95
|
-
* ```
|
|
96
|
-
*/
|
|
97
|
-
run: () => Promise<T>;
|
|
98
|
-
};
|
|
99
|
-
//#endregion
|
|
100
|
-
export { TaskValue as n, Task as t };
|
package/dist/task-DQTXV7xv.mjs
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
//#region src/task/task.ts
|
|
2
|
-
function createTask(thunk) {
|
|
3
|
-
const t = {
|
|
4
|
-
map: (mapper) => createTask(() => thunk().then(mapper)),
|
|
5
|
-
ap: function(arg) {
|
|
6
|
-
return createTask(() => Promise.all([this.run(), arg.run()]).then(([fn, a]) => fn(a)));
|
|
7
|
-
},
|
|
8
|
-
zip: (taskA) => t.map((value) => (a) => [value, a]).ap(taskA),
|
|
9
|
-
flatten: function() {
|
|
10
|
-
return this.flatMap((value) => value);
|
|
11
|
-
},
|
|
12
|
-
flatMap: (mapper) => createTask(() => thunk().then((value) => mapper(value).run())),
|
|
13
|
-
tap: (sideEffect) => createTask(() => thunk().then((value) => {
|
|
14
|
-
sideEffect(value);
|
|
15
|
-
return value;
|
|
16
|
-
})),
|
|
17
|
-
run: () => thunk()
|
|
18
|
-
};
|
|
19
|
-
return t;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
//#endregion
|
|
23
|
-
export { createTask as t };
|
package/dist/task.d.mts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { n as TaskValue, t as Task } from "./task-D9kWv4yq.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/task/constructors.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* Creates a Task that immediately resolves with the provided value.
|
|
6
|
-
*
|
|
7
|
-
* @typeParam T - The type of the value
|
|
8
|
-
* @param value - The value to wrap in a Task
|
|
9
|
-
* @returns Task that resolves with the given value
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```typescript
|
|
13
|
-
* const t = task(42); // Task<number>
|
|
14
|
-
* await t.run(); // 42
|
|
15
|
-
* ```
|
|
16
|
-
*/
|
|
17
|
-
declare function task<T>(value: T): Task<T>;
|
|
18
|
-
/**
|
|
19
|
-
* Creates a Task from a lazy Promise-returning thunk.
|
|
20
|
-
* The thunk is not called until `run()` is invoked.
|
|
21
|
-
*
|
|
22
|
-
* @typeParam T - The type of the resolved value
|
|
23
|
-
* @param thunk - A function that returns a Promise
|
|
24
|
-
* @returns Task wrapping the lazy Promise
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* ```typescript
|
|
28
|
-
* const fetchUser = fromPromise(() => fetch("/api/user").then(r => r.json()));
|
|
29
|
-
* await fetchUser.run(); // fetches the user
|
|
30
|
-
* ```
|
|
31
|
-
*/
|
|
32
|
-
declare function fromPromise<T>(thunk: () => Promise<T>): Task<T>;
|
|
33
|
-
//#endregion
|
|
34
|
-
//#region src/task/helpers.d.ts
|
|
35
|
-
/**
|
|
36
|
-
* Runs all Tasks concurrently and collects their results into an array.
|
|
37
|
-
* Equivalent to `Promise.all` for Tasks.
|
|
38
|
-
*
|
|
39
|
-
* @param tasks - Array of Tasks to run concurrently
|
|
40
|
-
* @returns Task that resolves with an array of all results
|
|
41
|
-
*
|
|
42
|
-
* @example
|
|
43
|
-
* ```typescript
|
|
44
|
-
* await all([task(1), task(2), task(3)]).run() // [1, 2, 3]
|
|
45
|
-
* ```
|
|
46
|
-
*/
|
|
47
|
-
declare function all<T>(tasks: Task<T>[]): Task<T[]>;
|
|
48
|
-
//#endregion
|
|
49
|
-
export { type Task, type TaskValue, all, fromPromise, task };
|
package/dist/task.mjs
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { t as createTask } from "./task-DQTXV7xv.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/task/constructors.ts
|
|
4
|
-
/**
|
|
5
|
-
* Creates a Task that immediately resolves with the provided value.
|
|
6
|
-
*
|
|
7
|
-
* @typeParam T - The type of the value
|
|
8
|
-
* @param value - The value to wrap in a Task
|
|
9
|
-
* @returns Task that resolves with the given value
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```typescript
|
|
13
|
-
* const t = task(42); // Task<number>
|
|
14
|
-
* await t.run(); // 42
|
|
15
|
-
* ```
|
|
16
|
-
*/
|
|
17
|
-
function task(value) {
|
|
18
|
-
return createTask(() => Promise.resolve(value));
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Creates a Task from a lazy Promise-returning thunk.
|
|
22
|
-
* The thunk is not called until `run()` is invoked.
|
|
23
|
-
*
|
|
24
|
-
* @typeParam T - The type of the resolved value
|
|
25
|
-
* @param thunk - A function that returns a Promise
|
|
26
|
-
* @returns Task wrapping the lazy Promise
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* ```typescript
|
|
30
|
-
* const fetchUser = fromPromise(() => fetch("/api/user").then(r => r.json()));
|
|
31
|
-
* await fetchUser.run(); // fetches the user
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
function fromPromise(thunk) {
|
|
35
|
-
return createTask(thunk);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
//#endregion
|
|
39
|
-
//#region src/task/helpers.ts
|
|
40
|
-
/**
|
|
41
|
-
* Runs all Tasks concurrently and collects their results into an array.
|
|
42
|
-
* Equivalent to `Promise.all` for Tasks.
|
|
43
|
-
*
|
|
44
|
-
* @param tasks - Array of Tasks to run concurrently
|
|
45
|
-
* @returns Task that resolves with an array of all results
|
|
46
|
-
*
|
|
47
|
-
* @example
|
|
48
|
-
* ```typescript
|
|
49
|
-
* await all([task(1), task(2), task(3)]).run() // [1, 2, 3]
|
|
50
|
-
* ```
|
|
51
|
-
*/
|
|
52
|
-
function all(tasks) {
|
|
53
|
-
return createTask(() => Promise.all(tasks.map((t) => t.run())));
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
//#endregion
|
|
57
|
-
export { all, fromPromise, task };
|
package/dist/taskEither.d.mts
DELETED
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
import { t as Either } from "./either-CWllQk7N.mjs";
|
|
2
|
-
import { t as Task } from "./task-D9kWv4yq.mjs";
|
|
3
|
-
|
|
4
|
-
//#region src/taskEither/model.d.ts
|
|
5
|
-
type TaskEitherValue<E, T> = () => Promise<Either<E, T>>;
|
|
6
|
-
//#endregion
|
|
7
|
-
//#region src/taskEither/taskEither.d.ts
|
|
8
|
-
type TaskEither<E, T> = {
|
|
9
|
-
/**
|
|
10
|
-
* Transforms the Right value using a mapping function.
|
|
11
|
-
* If this TaskEither resolves to Left, the mapper is not called.
|
|
12
|
-
*
|
|
13
|
-
* @typeParam U - The type of the transformed value
|
|
14
|
-
* @param mapper - Function to transform the Right value
|
|
15
|
-
* @returns New TaskEither with the transformed Right value, or the same Left if error
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* ```typescript
|
|
19
|
-
* taskEither(5).map(x => x * 2).run() // Promise<Right(10)>
|
|
20
|
-
* taskLeft("error").map(x => x * 2).run() // Promise<Left("error")>
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
map: <U>(mapper: (right: T) => U) => TaskEither<E, U>;
|
|
24
|
-
/**
|
|
25
|
-
* Transforms the Left value using a mapping function.
|
|
26
|
-
* If this TaskEither resolves to Right, the mapper is not called.
|
|
27
|
-
*
|
|
28
|
-
* @typeParam F - The type of the transformed error
|
|
29
|
-
* @param mapper - Function to transform the Left value
|
|
30
|
-
* @returns New TaskEither with the transformed Left value, or the same Right if success
|
|
31
|
-
*
|
|
32
|
-
* @example
|
|
33
|
-
* ```typescript
|
|
34
|
-
* taskLeft("error").mapLeft(e => `mapped: ${e}`).run() // Promise<Left("mapped: error")>
|
|
35
|
-
* taskEither(42).mapLeft(e => `mapped: ${e}`).run() // Promise<Right(42)>
|
|
36
|
-
* ```
|
|
37
|
-
*/
|
|
38
|
-
mapLeft: <F>(mapper: (left: E) => F) => TaskEither<F, T>;
|
|
39
|
-
/**
|
|
40
|
-
* Applies a function wrapped in a TaskEither to a value wrapped in a TaskEither.
|
|
41
|
-
* Both TaskEithers are run concurrently.
|
|
42
|
-
*
|
|
43
|
-
* @typeParam EE - Left type of the argument TaskEither
|
|
44
|
-
* @typeParam A - The type of the argument value
|
|
45
|
-
* @typeParam U - The type of the function's return value
|
|
46
|
-
* @param arg - TaskEither containing the argument to apply the function to
|
|
47
|
-
* @returns TaskEither containing the function result, or the first Left if any is Left
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* ```typescript
|
|
51
|
-
* const add = (x: number) => (y: number) => x + y;
|
|
52
|
-
* taskEither(add(5)).ap(taskEither(3)).run() // Promise<Right(8)>
|
|
53
|
-
* taskEither(add(5)).ap(taskLeft("err")).run() // Promise<Left("err")>
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
ap: <EE, A, U>(this: TaskEither<E, (a: A) => U>, arg: TaskEither<EE, A>) => TaskEither<E | EE, U>;
|
|
57
|
-
/**
|
|
58
|
-
* Combines this TaskEither with another TaskEither into a tuple.
|
|
59
|
-
* Both TaskEithers are run concurrently.
|
|
60
|
-
*
|
|
61
|
-
* @typeParam EE - Left type of the other TaskEither
|
|
62
|
-
* @typeParam A - Right type of the other TaskEither
|
|
63
|
-
* @param other - The TaskEither to combine with this one
|
|
64
|
-
* @returns TaskEither containing a tuple of both Right values, or the first Left
|
|
65
|
-
*
|
|
66
|
-
* @example
|
|
67
|
-
* ```typescript
|
|
68
|
-
* taskEither("Alice").zip(taskEither(30)).run() // Promise<Right(["Alice", 30])>
|
|
69
|
-
* taskEither("Alice").zip(taskLeft("No age")).run() // Promise<Left("No age")>
|
|
70
|
-
* ```
|
|
71
|
-
*/
|
|
72
|
-
zip: <EE, A>(other: TaskEither<EE, A>) => TaskEither<E | EE, readonly [T, A]>;
|
|
73
|
-
/**
|
|
74
|
-
* Flattens a nested TaskEither structure by removing one level of nesting.
|
|
75
|
-
*
|
|
76
|
-
* @typeParam EE - Left type of the inner TaskEither
|
|
77
|
-
* @typeParam U - Right type of the inner TaskEither
|
|
78
|
-
* @returns The inner TaskEither if this TaskEither is Right, otherwise this Left unchanged
|
|
79
|
-
*
|
|
80
|
-
* @example
|
|
81
|
-
* ```typescript
|
|
82
|
-
* taskEither(taskEither(42)).flatten().run() // Promise<Right(42)>
|
|
83
|
-
* taskEither(taskLeft("inner error")).flatten().run() // Promise<Left("inner error")>
|
|
84
|
-
* ```
|
|
85
|
-
*/
|
|
86
|
-
flatten: <EE, U>(this: TaskEither<E, TaskEither<EE, U>>) => TaskEither<E | EE, U>;
|
|
87
|
-
/**
|
|
88
|
-
* Chains TaskEither-returning operations together (monadic bind).
|
|
89
|
-
* If this TaskEither resolves to Left, the mapper is not called.
|
|
90
|
-
*
|
|
91
|
-
* @typeParam EE - The error type of the TaskEither returned by the mapper
|
|
92
|
-
* @typeParam U - The success type of the TaskEither returned by the mapper
|
|
93
|
-
* @param mapper - Function that takes a Right value and returns a TaskEither
|
|
94
|
-
* @returns The TaskEither returned by mapper if this is Right, otherwise this Left unchanged
|
|
95
|
-
*
|
|
96
|
-
* @example
|
|
97
|
-
* ```typescript
|
|
98
|
-
* taskEither(10).flatMap(x => taskEither(x * 2)).run() // Promise<Right(20)>
|
|
99
|
-
* taskLeft("error").flatMap(x => taskEither(x)).run() // Promise<Left("error")>
|
|
100
|
-
* ```
|
|
101
|
-
*/
|
|
102
|
-
flatMap: <EE, U>(mapper: (right: T) => TaskEither<EE, U>) => TaskEither<E | EE, U>;
|
|
103
|
-
/**
|
|
104
|
-
* Performs a side effect with the Right value, returning the original TaskEither unchanged.
|
|
105
|
-
* If this TaskEither resolves to Left, the side effect is not executed.
|
|
106
|
-
*
|
|
107
|
-
* @param sideEffect - Function to execute with the Right value (return value is ignored)
|
|
108
|
-
* @returns The same TaskEither instance unchanged
|
|
109
|
-
*
|
|
110
|
-
* @example
|
|
111
|
-
* ```typescript
|
|
112
|
-
* taskEither(42).tap(v => console.log(v)).run() // Logs: 42, resolves to Right(42)
|
|
113
|
-
* taskLeft("error").tap(v => console.log(v)).run() // No log, resolves to Left("error")
|
|
114
|
-
* ```
|
|
115
|
-
*/
|
|
116
|
-
tap: (sideEffect: (right: T) => unknown) => TaskEither<E, T>;
|
|
117
|
-
/**
|
|
118
|
-
* Performs a side effect with the Left value, returning the original TaskEither unchanged.
|
|
119
|
-
* If this TaskEither resolves to Right, the side effect is not executed.
|
|
120
|
-
*
|
|
121
|
-
* @param sideEffect - Function to execute with the Left value (return value is ignored)
|
|
122
|
-
* @returns The same TaskEither instance unchanged
|
|
123
|
-
*
|
|
124
|
-
* @example
|
|
125
|
-
* ```typescript
|
|
126
|
-
* taskLeft("error").tapLeft(e => console.log(e)).run() // Logs: "error", resolves to Left("error")
|
|
127
|
-
* taskEither(42).tapLeft(e => console.log(e)).run() // No log, resolves to Right(42)
|
|
128
|
-
* ```
|
|
129
|
-
*/
|
|
130
|
-
tapLeft: (sideEffect: (left: E) => unknown) => TaskEither<E, T>;
|
|
131
|
-
/**
|
|
132
|
-
* Pattern matches on the resolved Either, executing different functions based on its state.
|
|
133
|
-
* Returns the result wrapped in a Task.
|
|
134
|
-
*
|
|
135
|
-
* @typeParam U - The return type of both matcher functions
|
|
136
|
-
* @param onLeft - Function to execute if resolved Either is Left
|
|
137
|
-
* @param onRight - Function to execute if resolved Either is Right
|
|
138
|
-
* @returns Task that resolves with the result of the executed function
|
|
139
|
-
*
|
|
140
|
-
* @example
|
|
141
|
-
* ```typescript
|
|
142
|
-
* await taskEither(5).match(() => 0, x => x * 2).run() // 10
|
|
143
|
-
* await taskLeft("error").match(() => 0, x => x * 2).run() // 0
|
|
144
|
-
* ```
|
|
145
|
-
*/
|
|
146
|
-
match: <U>(onLeft: (left: E) => U, onRight: (right: T) => U) => Task<U>;
|
|
147
|
-
/**
|
|
148
|
-
* Extracts the Right value, or returns a fallback value if Left.
|
|
149
|
-
* Returns the result wrapped in a Task.
|
|
150
|
-
*
|
|
151
|
-
* @param fallback - Function that takes the Left value and returns a default value of type T
|
|
152
|
-
* @returns Task that resolves with the Right value or the fallback
|
|
153
|
-
*
|
|
154
|
-
* @example
|
|
155
|
-
* ```typescript
|
|
156
|
-
* await taskEither(42).getOrElse(() => 0).run() // 42
|
|
157
|
-
* await taskLeft("error").getOrElse(() => 0).run() // 0
|
|
158
|
-
* ```
|
|
159
|
-
*/
|
|
160
|
-
getOrElse: (fallback: (left: E) => T) => Task<T>;
|
|
161
|
-
/**
|
|
162
|
-
* Returns this TaskEither if it resolves to Right, otherwise returns the result of the fallback function.
|
|
163
|
-
*
|
|
164
|
-
* @typeParam EE - The type of the error in the fallback TaskEither
|
|
165
|
-
* @param fallback - Function that takes the Left value and returns an alternative TaskEither
|
|
166
|
-
* @returns This TaskEither if Right, otherwise the TaskEither returned by the fallback function
|
|
167
|
-
*
|
|
168
|
-
* @example
|
|
169
|
-
* ```typescript
|
|
170
|
-
* taskEither(42).orElse(() => taskEither(0)).run() // Promise<Right(42)>
|
|
171
|
-
* taskLeft("error").orElse(() => taskEither(0)).run() // Promise<Right(0)>
|
|
172
|
-
* ```
|
|
173
|
-
*/
|
|
174
|
-
orElse: <EE>(fallback: (left: E) => TaskEither<EE, T>) => TaskEither<E | EE, T>;
|
|
175
|
-
/**
|
|
176
|
-
* Executes the TaskEither and returns the resulting Promise.
|
|
177
|
-
*
|
|
178
|
-
* @returns A Promise that resolves with the Either value
|
|
179
|
-
*
|
|
180
|
-
* @example
|
|
181
|
-
* ```typescript
|
|
182
|
-
* const result = await taskEither(42).run(); // Either<never, number>
|
|
183
|
-
* ```
|
|
184
|
-
*/
|
|
185
|
-
run: () => Promise<Either<E, T>>;
|
|
186
|
-
};
|
|
187
|
-
//#endregion
|
|
188
|
-
//#region src/taskEither/constructors.d.ts
|
|
189
|
-
declare function taskEither<T, E = never>(value: T): TaskEither<E, T>;
|
|
190
|
-
declare function taskLeft<E, T = never>(error: E): TaskEither<E, T>;
|
|
191
|
-
declare function fromEither<E, T>(either: Either<E, T>): TaskEither<E, T>;
|
|
192
|
-
declare function fromTask<T, E = never>(t: Task<T>): TaskEither<E, T>;
|
|
193
|
-
declare function tryCatch<T, E>(thunk: () => Promise<T>, onThrow: (err: unknown) => E): TaskEither<E, T>;
|
|
194
|
-
//#endregion
|
|
195
|
-
//#region src/taskEither/helpers.d.ts
|
|
196
|
-
/**
|
|
197
|
-
* Runs all TaskEithers concurrently and collects their Right values into an array.
|
|
198
|
-
* Short-circuits on the first Left, returning it immediately.
|
|
199
|
-
* Equivalent to `Promise.all` for TaskEithers.
|
|
200
|
-
*
|
|
201
|
-
* @param taskEithers - Array of TaskEithers to run concurrently
|
|
202
|
-
* @returns TaskEither that resolves with an array of all Right values, or the first Left
|
|
203
|
-
*
|
|
204
|
-
* @example
|
|
205
|
-
* ```typescript
|
|
206
|
-
* await all([taskEither(1), taskEither(2), taskEither(3)]).run() // Right([1, 2, 3])
|
|
207
|
-
* await all([taskEither(1), taskLeft("err"), taskEither(3)]).run() // Left("err")
|
|
208
|
-
* ```
|
|
209
|
-
*/
|
|
210
|
-
declare function all<E, T>(taskEithers: TaskEither<E, T>[]): TaskEither<E, T[]>;
|
|
211
|
-
//#endregion
|
|
212
|
-
export { type TaskEither, type TaskEitherValue, all, fromEither, fromTask, taskEither, taskLeft, tryCatch };
|
package/dist/taskEither.mjs
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { i as right, r as left } from "./constructors-pI0lvrQE.mjs";
|
|
2
|
-
import { t as createTask } from "./task-DQTXV7xv.mjs";
|
|
3
|
-
|
|
4
|
-
//#region src/taskEither/taskEither.ts
|
|
5
|
-
function createTaskEither(thunk) {
|
|
6
|
-
const te = {
|
|
7
|
-
map: (mapper) => te.flatMap((rightVal) => createTaskEither(() => Promise.resolve(right(mapper(rightVal))))),
|
|
8
|
-
mapLeft: (mapper) => createTaskEither(() => thunk().then((either) => either.match((leftVal) => left(mapper(leftVal)), (rightVal) => right(rightVal)))),
|
|
9
|
-
ap: function(arg) {
|
|
10
|
-
return createTaskEither(() => Promise.all([this.run(), arg.run()]).then(([eitherFn, eitherA]) => eitherFn.ap(eitherA)));
|
|
11
|
-
},
|
|
12
|
-
zip: (other) => te.map((value) => (a) => [value, a]).ap(other),
|
|
13
|
-
flatten: function() {
|
|
14
|
-
return this.flatMap((value) => value);
|
|
15
|
-
},
|
|
16
|
-
flatMap: (mapper) => createTaskEither(() => thunk().then((either) => either.match((leftVal) => Promise.resolve(left(leftVal)), (rightVal) => mapper(rightVal).run()))),
|
|
17
|
-
tap: (sideEffect) => createTaskEither(() => thunk().then((either) => {
|
|
18
|
-
either.tap(sideEffect);
|
|
19
|
-
return either;
|
|
20
|
-
})),
|
|
21
|
-
tapLeft: (sideEffect) => createTaskEither(() => thunk().then((either) => {
|
|
22
|
-
either.match(sideEffect, () => {});
|
|
23
|
-
return either;
|
|
24
|
-
})),
|
|
25
|
-
match: (onLeft, onRight) => createTask(() => thunk().then((either) => either.match(onLeft, onRight))),
|
|
26
|
-
getOrElse: (fallback) => createTask(() => thunk().then((either) => either.getOrElse(fallback))),
|
|
27
|
-
orElse: (fallback) => createTaskEither(() => thunk().then((either) => either.match((leftVal) => fallback(leftVal).run(), () => Promise.resolve(either)))),
|
|
28
|
-
run: () => thunk()
|
|
29
|
-
};
|
|
30
|
-
return te;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
//#endregion
|
|
34
|
-
//#region src/taskEither/constructors.ts
|
|
35
|
-
function taskEither(value) {
|
|
36
|
-
return createTaskEither(() => Promise.resolve(right(value)));
|
|
37
|
-
}
|
|
38
|
-
function taskLeft(error) {
|
|
39
|
-
return createTaskEither(() => Promise.resolve(left(error)));
|
|
40
|
-
}
|
|
41
|
-
function fromEither(either) {
|
|
42
|
-
return createTaskEither(() => Promise.resolve(either));
|
|
43
|
-
}
|
|
44
|
-
function fromTask(t) {
|
|
45
|
-
return createTaskEither(() => t.run().then((value) => right(value)));
|
|
46
|
-
}
|
|
47
|
-
function tryCatch(thunk, onThrow) {
|
|
48
|
-
return createTaskEither(() => thunk().then((value) => right(value), (err) => left(onThrow(err))));
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
//#endregion
|
|
52
|
-
//#region src/taskEither/helpers.ts
|
|
53
|
-
/**
|
|
54
|
-
* Runs all TaskEithers concurrently and collects their Right values into an array.
|
|
55
|
-
* Short-circuits on the first Left, returning it immediately.
|
|
56
|
-
* Equivalent to `Promise.all` for TaskEithers.
|
|
57
|
-
*
|
|
58
|
-
* @param taskEithers - Array of TaskEithers to run concurrently
|
|
59
|
-
* @returns TaskEither that resolves with an array of all Right values, or the first Left
|
|
60
|
-
*
|
|
61
|
-
* @example
|
|
62
|
-
* ```typescript
|
|
63
|
-
* await all([taskEither(1), taskEither(2), taskEither(3)]).run() // Right([1, 2, 3])
|
|
64
|
-
* await all([taskEither(1), taskLeft("err"), taskEither(3)]).run() // Left("err")
|
|
65
|
-
* ```
|
|
66
|
-
*/
|
|
67
|
-
function all(taskEithers) {
|
|
68
|
-
return createTaskEither(() => Promise.all(taskEithers.map((te) => te.run())).then((eithers) => {
|
|
69
|
-
const out = [];
|
|
70
|
-
for (const either of eithers) {
|
|
71
|
-
const result = either.toResult();
|
|
72
|
-
if (!result.ok) return left(result.error);
|
|
73
|
-
out.push(result.value);
|
|
74
|
-
}
|
|
75
|
-
return right(out);
|
|
76
|
-
}));
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
//#endregion
|
|
80
|
-
export { all, fromEither, fromTask, taskEither, taskLeft, tryCatch };
|