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/index.mjs
ADDED
|
@@ -0,0 +1,636 @@
|
|
|
1
|
+
//#region src/either/either.ts
|
|
2
|
+
function createEither(value) {
|
|
3
|
+
const either = {
|
|
4
|
+
filterOrElse: (predicate, onLeft) => either.flatMap((right) => predicate(right) ? either : createEither({ left: onLeft() })),
|
|
5
|
+
map: (mapper) => either.flatMap((right) => createEither({ right: mapper(right) })),
|
|
6
|
+
orElse: (fallback) => either.match((left) => forceCast$2(fallback(left)), () => forceCast$2(either)),
|
|
7
|
+
ap: function(arg) {
|
|
8
|
+
return this.flatMap((fn) => arg.map((right) => fn(right)));
|
|
9
|
+
},
|
|
10
|
+
swap: () => either.match((left) => createEither({ right: left }), (right) => createEither({ left: right })),
|
|
11
|
+
zip: (eitherA) => either.map((value) => (a) => [value, a]).ap(eitherA),
|
|
12
|
+
flatten: function() {
|
|
13
|
+
return this.flatMap((value) => value);
|
|
14
|
+
},
|
|
15
|
+
flatMap: (mapper) => either.match(() => forceCast$2(either), (right) => forceCast$2(mapper(right))),
|
|
16
|
+
tap: (sideEffect) => {
|
|
17
|
+
either.match(() => {}, sideEffect);
|
|
18
|
+
return either;
|
|
19
|
+
},
|
|
20
|
+
match: (onLeft, onRight) => isLeft(value) ? onLeft(value.left) : onRight(value.right),
|
|
21
|
+
getOrElse: (fallback) => either.match((error) => fallback(error), (value) => value),
|
|
22
|
+
toResult: () => either.match((error) => ({
|
|
23
|
+
ok: false,
|
|
24
|
+
error
|
|
25
|
+
}), (value) => ({
|
|
26
|
+
ok: true,
|
|
27
|
+
value
|
|
28
|
+
}))
|
|
29
|
+
};
|
|
30
|
+
return either;
|
|
31
|
+
}
|
|
32
|
+
function isLeft(value) {
|
|
33
|
+
return typeof value === "object" && "left" in value;
|
|
34
|
+
}
|
|
35
|
+
function forceCast$2(either) {
|
|
36
|
+
return either;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/either/constructors.ts
|
|
41
|
+
/**
|
|
42
|
+
* Creates an Either containing an error value (Left).
|
|
43
|
+
*
|
|
44
|
+
* @typeParam E - The type of the error value
|
|
45
|
+
* @typeParam T - The type of the success value (defaults to never since this is an error)
|
|
46
|
+
* @param left - The error value to wrap
|
|
47
|
+
* @returns Either containing the error value on the Left side
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* const error = left("Something went wrong"); // Either<string, never>
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
function left(left) {
|
|
55
|
+
return createEither({ left });
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Creates an Either containing a success value (Right).
|
|
59
|
+
*
|
|
60
|
+
* @typeParam T - The type of the success value
|
|
61
|
+
* @typeParam E - The type of the error value (defaults to never since this is a success)
|
|
62
|
+
* @param right - The success value to wrap
|
|
63
|
+
* @returns Either containing the success value on the Right side
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* const success = right(42); // Either<never, number>
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
function right(right) {
|
|
71
|
+
return createEither({ right });
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Creates an Either from a potentially nullable value.
|
|
75
|
+
* If the value is null or undefined, creates a Left with the provided error.
|
|
76
|
+
* If the value is present, creates a Right with the value.
|
|
77
|
+
*
|
|
78
|
+
* @typeParam E - The type of the error value
|
|
79
|
+
* @typeParam T - The type of the success value
|
|
80
|
+
* @param nullable - The potentially null/undefined value
|
|
81
|
+
* @param onNullish - Function that provides the error value when nullable is null/undefined
|
|
82
|
+
* @returns Either containing the value if present, or the error if null/undefined
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```typescript
|
|
86
|
+
* fromNullable("hello", () => "empty") // Right("hello")
|
|
87
|
+
* fromNullable(null, () => "empty") // Left("empty")
|
|
88
|
+
* fromNullable(undefined, () => "empty") // Left("empty")
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
function fromNullable$1(nullable, onNullish) {
|
|
92
|
+
return nullable != null ? right(nullable) : left(onNullish());
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Safely executes a function that might throw, converting exceptions to Either.
|
|
96
|
+
*
|
|
97
|
+
* @typeParam E - The type of the processed error value
|
|
98
|
+
* @typeParam T - The type of the success value
|
|
99
|
+
* @param fn - The function to execute that might throw
|
|
100
|
+
* @param onThrow - Function that processes the caught error into the desired error type
|
|
101
|
+
* @returns Either containing the result if successful, or the processed error if thrown
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* const parseNumber = (str: string) =>
|
|
106
|
+
* tryCatch(
|
|
107
|
+
* () => JSON.parse(str),
|
|
108
|
+
* (err) => `Parse error: ${err.message}`
|
|
109
|
+
* );
|
|
110
|
+
*
|
|
111
|
+
* parseNumber("42") // Right(42)
|
|
112
|
+
* parseNumber("invalid") // Left("Parse error: ...")
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
function tryCatch$1(fn, onThrow) {
|
|
116
|
+
try {
|
|
117
|
+
return right(fn());
|
|
118
|
+
} catch (err) {
|
|
119
|
+
return left(onThrow(err));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Creates an Either from an Option by providing an error value for the None case.
|
|
124
|
+
*
|
|
125
|
+
* @typeParam E - The type of the error value
|
|
126
|
+
* @typeParam T - The type of the success value
|
|
127
|
+
* @param opt - The Option to convert
|
|
128
|
+
* @param onNone - Function that provides the error value when Option is None
|
|
129
|
+
* @returns Either containing the Option's value if Some, or the error if None
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```typescript
|
|
133
|
+
* fromOption(some(42), () => "empty") // Right(42)
|
|
134
|
+
* fromOption(none(), () => "empty") // Left("empty")
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
function fromOption$1(opt, onNone) {
|
|
138
|
+
return opt.match(() => left(onNone()), (value) => right(value));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
//#endregion
|
|
142
|
+
//#region src/either/helpers.ts
|
|
143
|
+
/**
|
|
144
|
+
* Converts an array of Eithers into an Either of array.
|
|
145
|
+
* Returns Right with all values if all Eithers are Right, otherwise the first Left.
|
|
146
|
+
*
|
|
147
|
+
* @param eithers - Array of Eithers to sequence
|
|
148
|
+
* @returns Either containing array of all values, or the first Left
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```typescript
|
|
152
|
+
* sequence([right(1), right(2), right(3)]) // Right([1, 2, 3])
|
|
153
|
+
* sequence([right(1), left("error")]) // Left("error")
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
function sequence$2(eithers) {
|
|
157
|
+
const out = [];
|
|
158
|
+
for (const either of eithers) {
|
|
159
|
+
const result = either.toResult();
|
|
160
|
+
if (!result.ok) return left(result.error);
|
|
161
|
+
out.push(result.value);
|
|
162
|
+
}
|
|
163
|
+
return right(out);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
//#endregion
|
|
167
|
+
//#region src/either/index.ts
|
|
168
|
+
const Either = {
|
|
169
|
+
fromNullable: fromNullable$1,
|
|
170
|
+
fromOption: fromOption$1,
|
|
171
|
+
tryCatch: tryCatch$1,
|
|
172
|
+
sequence: sequence$2
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
//#endregion
|
|
176
|
+
//#region src/helpers/map2.ts
|
|
177
|
+
function map2(first, second, mapper) {
|
|
178
|
+
const applicative = first;
|
|
179
|
+
const combine = mapper;
|
|
180
|
+
return applicative.map((firstValue) => (secondValue) => combine(firstValue, secondValue)).ap(second);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
//#endregion
|
|
184
|
+
//#region src/helpers/map3.ts
|
|
185
|
+
function map3(first, second, third, mapper) {
|
|
186
|
+
const applicative = first;
|
|
187
|
+
const combine = mapper;
|
|
188
|
+
return applicative.map((firstValue) => (secondValue) => (thirdValue) => combine(firstValue, secondValue, thirdValue)).ap(second).ap(third);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
//#endregion
|
|
192
|
+
//#region src/option/model.ts
|
|
193
|
+
const NONE = Symbol("None");
|
|
194
|
+
|
|
195
|
+
//#endregion
|
|
196
|
+
//#region src/option/option.ts
|
|
197
|
+
function createOption(optionValue) {
|
|
198
|
+
const option = {
|
|
199
|
+
filter: (predicate) => option.flatMap((value) => predicate(value) ? option : createOption(NONE)),
|
|
200
|
+
map: (mapper) => option.flatMap((value) => createOption({ some: mapper(value) })),
|
|
201
|
+
orElse: (fallback) => option.match(fallback, () => option),
|
|
202
|
+
ap: function(optA) {
|
|
203
|
+
return this.flatMap((fn) => optA.match(() => createOption(NONE), (arg) => createOption({ some: fn(arg) })));
|
|
204
|
+
},
|
|
205
|
+
zip: (optA) => option.map((value) => (a) => [value, a]).ap(optA),
|
|
206
|
+
flatten: function() {
|
|
207
|
+
return this.flatMap((value) => value);
|
|
208
|
+
},
|
|
209
|
+
flatMap: (mapper) => option.match(() => forceCast$1(option), (value) => mapper(value)),
|
|
210
|
+
tap: (sideEffect) => {
|
|
211
|
+
option.match(() => {}, sideEffect);
|
|
212
|
+
return option;
|
|
213
|
+
},
|
|
214
|
+
tapNone: (sideEffect) => {
|
|
215
|
+
option.match(sideEffect, () => {});
|
|
216
|
+
return option;
|
|
217
|
+
},
|
|
218
|
+
match: (onNone, onSome) => isSome(optionValue) ? onSome(optionValue.some) : onNone(),
|
|
219
|
+
getOrElse: (fallback) => option.match(fallback, (value) => value),
|
|
220
|
+
toNullable: () => option.match(() => null, (value) => value),
|
|
221
|
+
toArray: () => option.match(() => [], (val) => [val])
|
|
222
|
+
};
|
|
223
|
+
return option;
|
|
224
|
+
}
|
|
225
|
+
function isSome(option) {
|
|
226
|
+
return typeof option === "object" && "some" in option;
|
|
227
|
+
}
|
|
228
|
+
function forceCast$1(option) {
|
|
229
|
+
return option;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
//#endregion
|
|
233
|
+
//#region src/option/constructors.ts
|
|
234
|
+
/**
|
|
235
|
+
* Creates an {@link Option} that contains a value.
|
|
236
|
+
*
|
|
237
|
+
* Use this to wrap an existing value into an `Option<T>` representing presence
|
|
238
|
+
* (as opposed to `none`, which represents absence).
|
|
239
|
+
*
|
|
240
|
+
* @typeParam T - Type of the wrapped value.
|
|
241
|
+
* @param some - The value to store in the option.
|
|
242
|
+
* @returns An {@link Option} containing the provided value.
|
|
243
|
+
*/
|
|
244
|
+
function some(some) {
|
|
245
|
+
return createOption({ some });
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Creates an {@link Option} representing the absence of a value.
|
|
249
|
+
*
|
|
250
|
+
* @typeParam T - The type of the value that would be contained if present.
|
|
251
|
+
* @returns An {@link Option} in the `None` state.
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* ```ts
|
|
255
|
+
* const value = none<number>();
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
258
|
+
function none() {
|
|
259
|
+
return createOption(NONE);
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Creates an {@link Option} from a nullable value.
|
|
263
|
+
*
|
|
264
|
+
* If {@link nullable} is `null` or `undefined`, returns {@link none}.
|
|
265
|
+
* Otherwise, wraps the provided value in {@link some}.
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* fromNullable(0).getOrElse(() => 123); // 0
|
|
269
|
+
* fromNullable(null).toNullable(); // null
|
|
270
|
+
*
|
|
271
|
+
* @typeParam T - The non-null value type to wrap.
|
|
272
|
+
* @param nullable - A value that may be `null` or `undefined`.
|
|
273
|
+
* @returns An {@link Option} that is `Some` when the value is present, otherwise `None`.
|
|
274
|
+
*/
|
|
275
|
+
function fromNullable(nullable) {
|
|
276
|
+
return nullable == null ? none() : some(nullable);
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Creates an {@link Option} from an {@link Either} by extracting the Right value.
|
|
280
|
+
*
|
|
281
|
+
* If the Either is Right, returns Some containing the Right value.
|
|
282
|
+
* If the Either is Left, returns None (discarding the Left value).
|
|
283
|
+
*
|
|
284
|
+
* @typeParam L - The type of the Left value (error type) that will be discarded
|
|
285
|
+
* @typeParam R - The type of the Right value that will be preserved in the Option
|
|
286
|
+
* @param either - The Either to convert to an Option
|
|
287
|
+
* @returns Some containing the Right value, or None if the Either was Left
|
|
288
|
+
*
|
|
289
|
+
* @example
|
|
290
|
+
* ```typescript
|
|
291
|
+
* const success = right(42);
|
|
292
|
+
* fromEither(success) // Some(42)
|
|
293
|
+
*
|
|
294
|
+
* const failure = left("error message");
|
|
295
|
+
* fromEither(failure) // None
|
|
296
|
+
* ```
|
|
297
|
+
*/
|
|
298
|
+
function fromEither$2(either) {
|
|
299
|
+
return either.match(() => none(), (right) => some(right));
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
//#endregion
|
|
303
|
+
//#region src/option/helpers.ts
|
|
304
|
+
/**
|
|
305
|
+
* Converts an array of Options into an Option of array.
|
|
306
|
+
* If all Options in the array contain values, returns Some containing an array of all values.
|
|
307
|
+
* If any Option in the array is None, returns None.
|
|
308
|
+
*
|
|
309
|
+
* @param opts - Array of Options to sequence
|
|
310
|
+
* @returns Some containing array of all values if all Options are Some, otherwise None
|
|
311
|
+
*
|
|
312
|
+
* @example
|
|
313
|
+
* ```typescript
|
|
314
|
+
* sequence([some(1), some(2), some(3)]) // Some([1, 2, 3])
|
|
315
|
+
* sequence([some(1), none(), some(3)]) // None
|
|
316
|
+
* ```
|
|
317
|
+
*/
|
|
318
|
+
function sequence$1(opts) {
|
|
319
|
+
const out = [];
|
|
320
|
+
for (const opt of opts) {
|
|
321
|
+
const [value] = opt.toArray();
|
|
322
|
+
if (!value) return none();
|
|
323
|
+
out.push(value);
|
|
324
|
+
}
|
|
325
|
+
return some(out);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
//#endregion
|
|
329
|
+
//#region src/option/index.ts
|
|
330
|
+
const Option = {
|
|
331
|
+
fromNullable,
|
|
332
|
+
fromEither: fromEither$2,
|
|
333
|
+
sequence: sequence$1
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
//#endregion
|
|
337
|
+
//#region src/task/task.ts
|
|
338
|
+
function createTask(thunk) {
|
|
339
|
+
const t = {
|
|
340
|
+
map: (mapper) => createTask(() => thunk().then(mapper)),
|
|
341
|
+
ap: function(arg) {
|
|
342
|
+
return createTask(() => Promise.all([this.run(), arg.run()]).then(([fn, a]) => fn(a)));
|
|
343
|
+
},
|
|
344
|
+
zip: (taskA) => t.map((value) => (a) => [value, a]).ap(taskA),
|
|
345
|
+
flatten: function() {
|
|
346
|
+
return this.flatMap((value) => value);
|
|
347
|
+
},
|
|
348
|
+
flatMap: (mapper) => createTask(() => thunk().then((value) => mapper(value).run())),
|
|
349
|
+
tap: (sideEffect) => createTask(() => thunk().then((value) => {
|
|
350
|
+
sideEffect(value);
|
|
351
|
+
return value;
|
|
352
|
+
})),
|
|
353
|
+
run: () => thunk()
|
|
354
|
+
};
|
|
355
|
+
return t;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
//#endregion
|
|
359
|
+
//#region src/task/constructors.ts
|
|
360
|
+
/**
|
|
361
|
+
* Creates a Task that immediately resolves with the provided value.
|
|
362
|
+
*
|
|
363
|
+
* @typeParam T - The type of the value
|
|
364
|
+
* @param value - The value to wrap in a Task
|
|
365
|
+
* @returns Task that resolves with the given value
|
|
366
|
+
*
|
|
367
|
+
* @example
|
|
368
|
+
* ```typescript
|
|
369
|
+
* const t = task(42); // Task<number>
|
|
370
|
+
* await t.run(); // 42
|
|
371
|
+
* ```
|
|
372
|
+
*/
|
|
373
|
+
function task(value) {
|
|
374
|
+
return createTask(() => Promise.resolve(value));
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Creates a Task from a lazy Promise-returning thunk.
|
|
378
|
+
* The thunk is not called until `run()` is invoked.
|
|
379
|
+
*
|
|
380
|
+
* @typeParam T - The type of the resolved value
|
|
381
|
+
* @param thunk - A function that returns a Promise
|
|
382
|
+
* @returns Task wrapping the lazy Promise
|
|
383
|
+
*
|
|
384
|
+
* @example
|
|
385
|
+
* ```typescript
|
|
386
|
+
* const fetchUser = fromPromise(() => fetch("/api/user").then(r => r.json()));
|
|
387
|
+
* await fetchUser.run(); // fetches the user
|
|
388
|
+
* ```
|
|
389
|
+
*/
|
|
390
|
+
function fromPromise(thunk) {
|
|
391
|
+
return createTask(thunk);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
//#endregion
|
|
395
|
+
//#region src/task/helpers.ts
|
|
396
|
+
/**
|
|
397
|
+
* Runs all Tasks concurrently and collects their results into an array.
|
|
398
|
+
* Equivalent to `Promise.all` for Tasks.
|
|
399
|
+
*
|
|
400
|
+
* @param tasks - Array of Tasks to run concurrently
|
|
401
|
+
* @returns Task that resolves with an array of all results
|
|
402
|
+
*
|
|
403
|
+
* @example
|
|
404
|
+
* ```typescript
|
|
405
|
+
* await allTasks([task(1), task(2), task(3)]).run() // [1, 2, 3]
|
|
406
|
+
* ```
|
|
407
|
+
*/
|
|
408
|
+
function allTasks(tasks) {
|
|
409
|
+
return createTask(() => Promise.all(tasks.map((t) => t.run())));
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
//#endregion
|
|
413
|
+
//#region src/task/index.ts
|
|
414
|
+
const Task = {
|
|
415
|
+
fromPromise,
|
|
416
|
+
all: allTasks
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
//#endregion
|
|
420
|
+
//#region src/taskEither/taskEither.ts
|
|
421
|
+
function createTaskEither(thunk) {
|
|
422
|
+
const te = {
|
|
423
|
+
map: (mapper) => te.flatMap((rightVal) => createTaskEither(() => Promise.resolve(right(mapper(rightVal))))),
|
|
424
|
+
mapLeft: (mapper) => createTaskEither(() => thunk().then((either) => either.match((leftVal) => left(mapper(leftVal)), (rightVal) => right(rightVal)))),
|
|
425
|
+
ap: function(arg) {
|
|
426
|
+
return createTaskEither(() => Promise.all([this.run(), arg.run()]).then(([eitherFn, eitherA]) => eitherFn.ap(eitherA)));
|
|
427
|
+
},
|
|
428
|
+
zip: (other) => te.map((value) => (a) => [value, a]).ap(other),
|
|
429
|
+
flatten: function() {
|
|
430
|
+
return this.flatMap((value) => value);
|
|
431
|
+
},
|
|
432
|
+
flatMap: (mapper) => createTaskEither(() => thunk().then((either) => either.match((leftVal) => Promise.resolve(left(leftVal)), (rightVal) => mapper(rightVal).run()))),
|
|
433
|
+
tap: (sideEffect) => createTaskEither(() => thunk().then((either) => {
|
|
434
|
+
either.tap(sideEffect);
|
|
435
|
+
return either;
|
|
436
|
+
})),
|
|
437
|
+
tapLeft: (sideEffect) => createTaskEither(() => thunk().then((either) => {
|
|
438
|
+
either.match(sideEffect, () => {});
|
|
439
|
+
return either;
|
|
440
|
+
})),
|
|
441
|
+
match: (onLeft, onRight) => createTask(() => thunk().then((either) => either.match(onLeft, onRight))),
|
|
442
|
+
getOrElse: (fallback) => createTask(() => thunk().then((either) => either.getOrElse(fallback))),
|
|
443
|
+
orElse: (fallback) => createTaskEither(() => thunk().then((either) => either.match((leftVal) => fallback(leftVal).run(), () => Promise.resolve(either)))),
|
|
444
|
+
run: () => thunk()
|
|
445
|
+
};
|
|
446
|
+
return te;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
//#endregion
|
|
450
|
+
//#region src/taskEither/constructors.ts
|
|
451
|
+
function taskRight(value) {
|
|
452
|
+
return createTaskEither(() => Promise.resolve(right(value)));
|
|
453
|
+
}
|
|
454
|
+
function taskLeft(error) {
|
|
455
|
+
return createTaskEither(() => Promise.resolve(left(error)));
|
|
456
|
+
}
|
|
457
|
+
function fromEither$1(either) {
|
|
458
|
+
return createTaskEither(() => Promise.resolve(either));
|
|
459
|
+
}
|
|
460
|
+
function fromTask(t) {
|
|
461
|
+
return createTaskEither(() => t.run().then((value) => right(value)));
|
|
462
|
+
}
|
|
463
|
+
function tryCatch(thunk, onThrow) {
|
|
464
|
+
return createTaskEither(() => thunk().then((value) => right(value), (err) => left(onThrow(err))));
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
//#endregion
|
|
468
|
+
//#region src/taskEither/helpers.ts
|
|
469
|
+
/**
|
|
470
|
+
* Runs all TaskEithers concurrently and collects their Right values into an array.
|
|
471
|
+
* Short-circuits on the first Left, returning it immediately.
|
|
472
|
+
* Equivalent to `Promise.all` for TaskEithers.
|
|
473
|
+
*
|
|
474
|
+
* @param taskEithers - Array of TaskEithers to run concurrently
|
|
475
|
+
* @returns TaskEither that resolves with an array of all Right values, or the first Left
|
|
476
|
+
*
|
|
477
|
+
* @example
|
|
478
|
+
* ```typescript
|
|
479
|
+
* await allTaskEithers([taskRight(1), taskRight(2), taskRight(3)]).run() // Right([1, 2, 3])
|
|
480
|
+
* await allTaskEithers([taskRight(1), taskLeft("err"), taskRight(3)]).run() // Left("err")
|
|
481
|
+
* ```
|
|
482
|
+
*/
|
|
483
|
+
function allTaskEithers(taskEithers) {
|
|
484
|
+
return createTaskEither(() => Promise.all(taskEithers.map((te) => te.run())).then((eithers) => {
|
|
485
|
+
const out = [];
|
|
486
|
+
for (const either of eithers) {
|
|
487
|
+
const result = either.toResult();
|
|
488
|
+
if (!result.ok) return left(result.error);
|
|
489
|
+
out.push(result.value);
|
|
490
|
+
}
|
|
491
|
+
return right(out);
|
|
492
|
+
}));
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
//#endregion
|
|
496
|
+
//#region src/taskEither/index.ts
|
|
497
|
+
const TaskEither = {
|
|
498
|
+
fromEither: fromEither$1,
|
|
499
|
+
fromTask,
|
|
500
|
+
tryCatch,
|
|
501
|
+
all: allTaskEithers
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
//#endregion
|
|
505
|
+
//#region src/validation/validation.ts
|
|
506
|
+
function createValidation(value) {
|
|
507
|
+
const validation = {
|
|
508
|
+
filterOrElse: (predicate, onInvalid) => validation.match(() => validation, (v) => predicate(v) ? validation : createValidation({ invalid: [onInvalid()] })),
|
|
509
|
+
map: (mapper) => validation.match(() => forceCast(validation), (v) => createValidation({ valid: mapper(v) })),
|
|
510
|
+
ap: function(arg) {
|
|
511
|
+
return this.match((errors1) => arg.match((errors2) => createValidation({ invalid: [...errors1, ...errors2] }), () => createValidation({ invalid: errors1 })), (fn) => arg.match((errors2) => createValidation({ invalid: errors2 }), (v) => createValidation({ valid: fn(v) })));
|
|
512
|
+
},
|
|
513
|
+
zip: (valA) => validation.map((value) => (a) => [value, a]).ap(valA),
|
|
514
|
+
match: (onInvalid, onValid) => isValid(value) ? onValid(value.valid) : onInvalid(value.invalid),
|
|
515
|
+
getOrElse: (fallback) => validation.match((errors) => fallback(errors), (v) => v),
|
|
516
|
+
tap: (sideEffect) => {
|
|
517
|
+
validation.match(() => {}, sideEffect);
|
|
518
|
+
return validation;
|
|
519
|
+
},
|
|
520
|
+
tapInvalid: (sideEffect) => {
|
|
521
|
+
validation.match(sideEffect, () => {});
|
|
522
|
+
return validation;
|
|
523
|
+
},
|
|
524
|
+
toResult: () => validation.match((errors) => ({
|
|
525
|
+
ok: false,
|
|
526
|
+
errors
|
|
527
|
+
}), (value) => ({
|
|
528
|
+
ok: true,
|
|
529
|
+
value
|
|
530
|
+
}))
|
|
531
|
+
};
|
|
532
|
+
return validation;
|
|
533
|
+
}
|
|
534
|
+
function isValid(value) {
|
|
535
|
+
return typeof value === "object" && "valid" in value;
|
|
536
|
+
}
|
|
537
|
+
function forceCast(v) {
|
|
538
|
+
return v;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
//#endregion
|
|
542
|
+
//#region src/validation/constructors.ts
|
|
543
|
+
/**
|
|
544
|
+
* Creates a Validation containing a valid value.
|
|
545
|
+
*
|
|
546
|
+
* @param value - The valid value to wrap
|
|
547
|
+
* @returns Validation containing the valid value
|
|
548
|
+
*
|
|
549
|
+
* @example
|
|
550
|
+
* ```typescript
|
|
551
|
+
* const v = valid(42); // Validation<never, number>
|
|
552
|
+
* ```
|
|
553
|
+
*/
|
|
554
|
+
function valid(value) {
|
|
555
|
+
return createValidation({ valid: value });
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* Creates a Validation containing a single error.
|
|
559
|
+
*
|
|
560
|
+
* @param error - The error to wrap
|
|
561
|
+
* @returns Validation containing the error
|
|
562
|
+
*
|
|
563
|
+
* @example
|
|
564
|
+
* ```typescript
|
|
565
|
+
* const v = invalid("Something went wrong"); // Validation<string, never>
|
|
566
|
+
* ```
|
|
567
|
+
*/
|
|
568
|
+
function invalid(error) {
|
|
569
|
+
return createValidation({ invalid: [error] });
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* Creates a Validation from an Either.
|
|
573
|
+
* Right values become Valid, Left values become Invalid (with a single error).
|
|
574
|
+
*
|
|
575
|
+
* @param either - The Either to convert
|
|
576
|
+
* @returns Validation equivalent of the Either
|
|
577
|
+
*
|
|
578
|
+
* @example
|
|
579
|
+
* ```typescript
|
|
580
|
+
* fromEither(right(42)) // Valid(42)
|
|
581
|
+
* fromEither(left("error")) // Invalid(["error"])
|
|
582
|
+
* ```
|
|
583
|
+
*/
|
|
584
|
+
function fromEither(either) {
|
|
585
|
+
return either.match((error) => invalid(error), (value) => valid(value));
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* Creates a Validation from an Option.
|
|
589
|
+
* Some values become Valid, None becomes Invalid with the provided error.
|
|
590
|
+
*
|
|
591
|
+
* @param opt - The Option to convert
|
|
592
|
+
* @param onNone - Function that provides the error when Option is None
|
|
593
|
+
* @returns Validation equivalent of the Option
|
|
594
|
+
*
|
|
595
|
+
* @example
|
|
596
|
+
* ```typescript
|
|
597
|
+
* fromOption(some(42), () => "missing") // Valid(42)
|
|
598
|
+
* fromOption(none(), () => "missing") // Invalid(["missing"])
|
|
599
|
+
* ```
|
|
600
|
+
*/
|
|
601
|
+
function fromOption(opt, onNone) {
|
|
602
|
+
return opt.match(() => invalid(onNone()), (value) => valid(value));
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
//#endregion
|
|
606
|
+
//#region src/validation/helpers.ts
|
|
607
|
+
/**
|
|
608
|
+
* Converts an array of Validations into a Validation of array.
|
|
609
|
+
* Returns Valid with all values if all Validations are Valid, otherwise accumulates all errors.
|
|
610
|
+
*
|
|
611
|
+
* @param validations - Array of Validations to sequence
|
|
612
|
+
* @returns Validation containing array of all values, or all accumulated errors
|
|
613
|
+
*
|
|
614
|
+
* @example
|
|
615
|
+
* ```typescript
|
|
616
|
+
* sequence([valid(1), valid(2), valid(3)]) // Valid([1, 2, 3])
|
|
617
|
+
* sequence([valid(1), invalid("e1"), invalid("e2")]) // Invalid(["e1", "e2"])
|
|
618
|
+
* ```
|
|
619
|
+
*/
|
|
620
|
+
function sequence(validations) {
|
|
621
|
+
const errors = [];
|
|
622
|
+
const values = [];
|
|
623
|
+
for (const v of validations) v.match((errs) => errors.push(...errs), (value) => values.push(value));
|
|
624
|
+
return errors.length > 0 ? createValidation({ invalid: errors }) : createValidation({ valid: values });
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
//#endregion
|
|
628
|
+
//#region src/validation/index.ts
|
|
629
|
+
const Validation = {
|
|
630
|
+
fromEither,
|
|
631
|
+
fromOption,
|
|
632
|
+
sequence
|
|
633
|
+
};
|
|
634
|
+
|
|
635
|
+
//#endregion
|
|
636
|
+
export { Either, NONE, Option, Task, TaskEither, Validation, allTaskEithers, allTasks, invalid, left, map2, map3, none, right, some, task, taskLeft, taskRight, valid };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ok-fp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Essential Effect Data Types for TypeScript",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -26,27 +26,14 @@
|
|
|
26
26
|
"license": "MIT",
|
|
27
27
|
"author": "pwlmc",
|
|
28
28
|
"type": "module",
|
|
29
|
-
"
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=22"
|
|
31
|
+
},
|
|
32
|
+
"main": "./dist/index.mjs",
|
|
30
33
|
"exports": {
|
|
31
|
-
"
|
|
32
|
-
"types": "./dist/
|
|
33
|
-
"default": "./dist/
|
|
34
|
-
},
|
|
35
|
-
"./option": {
|
|
36
|
-
"types": "./dist/option.d.mts",
|
|
37
|
-
"default": "./dist/option.mjs"
|
|
38
|
-
},
|
|
39
|
-
"./validation": {
|
|
40
|
-
"types": "./dist/validation.d.mts",
|
|
41
|
-
"default": "./dist/validation.mjs"
|
|
42
|
-
},
|
|
43
|
-
"./task": {
|
|
44
|
-
"types": "./dist/task.d.mts",
|
|
45
|
-
"default": "./dist/task.mjs"
|
|
46
|
-
},
|
|
47
|
-
"./taskEither": {
|
|
48
|
-
"types": "./dist/taskEither.d.mts",
|
|
49
|
-
"default": "./dist/taskEither.mjs"
|
|
34
|
+
".": {
|
|
35
|
+
"types": "./dist/index.d.mts",
|
|
36
|
+
"default": "./dist/index.mjs"
|
|
50
37
|
}
|
|
51
38
|
},
|
|
52
39
|
"files": [
|