ok-fp 0.2.1 → 0.2.2
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 +14 -13
- package/dist/constructors-pI0lvrQE.mjs +142 -0
- package/dist/either-CWllQk7N.d.mts +213 -0
- package/dist/either.d.mts +2 -1
- package/dist/either.mjs +1 -140
- package/dist/{helpers-qdt9BxJz.d.mts → helpers-Bw8McftR.d.mts} +3 -213
- package/dist/option.d.mts +1 -1
- package/dist/task-D9kWv4yq.d.mts +100 -0
- package/dist/task-DQTXV7xv.mjs +23 -0
- package/dist/task.d.mts +2 -99
- package/dist/task.mjs +1 -21
- package/dist/taskEither.d.mts +212 -0
- package/dist/taskEither.mjs +80 -0
- package/dist/validation.d.mts +2 -1
- package/package.json +5 -1
|
@@ -0,0 +1,212 @@
|
|
|
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 };
|
|
@@ -0,0 +1,80 @@
|
|
|
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 };
|
package/dist/validation.d.mts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ok-fp",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "Essential Effect Data Types for TypeScript",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -43,6 +43,10 @@
|
|
|
43
43
|
"./task": {
|
|
44
44
|
"types": "./dist/task.d.mts",
|
|
45
45
|
"default": "./dist/task.mjs"
|
|
46
|
+
},
|
|
47
|
+
"./taskEither": {
|
|
48
|
+
"types": "./dist/taskEither.d.mts",
|
|
49
|
+
"default": "./dist/taskEither.mjs"
|
|
46
50
|
}
|
|
47
51
|
},
|
|
48
52
|
"files": [
|