ok-fp 0.2.0 → 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.
@@ -1,3 +1,5 @@
1
+ import { t as Either } from "./either-CWllQk7N.mjs";
2
+
1
3
  //#region src/option/model.d.ts
2
4
  type Some<T> = {
3
5
  some: T;
@@ -314,218 +316,6 @@ declare function map3$1<A, B, C, D>(optA: Option<A>, optB: Option<B>, optC: Opti
314
316
  */
315
317
  declare function sequence$1<T>(opts: Option<T>[]): Option<T[]>;
316
318
  //#endregion
317
- //#region src/either/model.d.ts
318
- type Left<E> = {
319
- readonly left: E;
320
- };
321
- type Right<T> = {
322
- readonly right: T;
323
- };
324
- type EitherValue<E, T> = Left<E> | Right<T>;
325
- type EitherResult<E, T> = {
326
- ok: true;
327
- value: T;
328
- } | {
329
- ok: false;
330
- error: E;
331
- };
332
- //#endregion
333
- //#region src/either/either.d.ts
334
- type Either<E, T> = {
335
- /**
336
- * Filters the Either based on a predicate function applied to the Right value.
337
- *
338
- * @param predicate - Function that tests the Right value
339
- * @param onLeft - Function that provides the error value when predicate fails
340
- * @returns The same Either if Left or predicate passes, otherwise Left with the provided error
341
- *
342
- * @example
343
- * ```typescript
344
- * const isPositive = (n: number) => n > 0;
345
- * right(5).filterOrElse(isPositive, () => "Must be positive") // Right(5)
346
- * right(-3).filterOrElse(isPositive, () => "Must be positive") // Left("Must be positive")
347
- * left("error").filterOrElse(isPositive, () => "Must be positive") // Left("error")
348
- * ```
349
- */
350
- filterOrElse: (predicate: (right: T) => boolean, onLeft: () => E) => Either<E, T>;
351
- /**
352
- * Transforms the Right value using a mapping function.
353
- *
354
- * @typeParam U - The type of the transformed value
355
- * @param mapper - Function to transform the Right value
356
- * @returns New Either with the transformed Right value, or the same Left if error
357
- *
358
- * @example
359
- * ```typescript
360
- * right(5).map(x => x * 2) // Right(10)
361
- * left("error").map(x => x * 2) // Left("error")
362
- * ```
363
- */
364
- map: <U>(mapper: (right: T) => U) => Either<E, U>;
365
- /**
366
- * Returns this Either if it's Right, otherwise returns the result of the fallback function.
367
- *
368
- * @typeParam EE - The type of the error in the fallback Either
369
- * @param fallback - Function that takes the Left value and returns an alternative Either
370
- * @returns This Either if Right, otherwise the Either returned by the fallback function
371
- *
372
- * @example
373
- * ```typescript
374
- * right(42).orElse((err) => right(0)) // Right(42)
375
- * left("error").orElse((err) => right(0)) // Right(0)
376
- * ```
377
- */
378
- orElse: <EE>(fallback: (left: E) => Either<EE, T>) => Either<E | EE, T>;
379
- /**
380
- * Applies a function wrapped in an Either to a value wrapped in an Either.
381
- *
382
- * @typeParam A - The type of the argument value
383
- * @typeParam U - The type of the function's return value
384
- * @param arg - Either containing the argument to apply the function to
385
- * @returns Either containing the function result, or the first Left if any Either is Left
386
- *
387
- * @example
388
- * ```typescript
389
- * const add = (x: number) => (y: number) => x + y;
390
- * right(add(5)).ap(right(3)) // Right(8)
391
- * right(add(5)).ap(left("err")) // Left("err")
392
- * left("err").ap(right(3)) // Left("err")
393
- * ```
394
- */
395
- ap: <EE, A, U>(this: Either<E, (a: A) => U>, arg: Either<EE, A>) => Either<E | EE, U>;
396
- /**
397
- * Swaps the Left and Right sides of the Either.
398
- *
399
- * @returns Either with Left and Right sides swapped
400
- *
401
- * @example
402
- * ```typescript
403
- * right(42).swap() // Left(42)
404
- * left("error").swap() // Right("error")
405
- * ```
406
- */
407
- swap: () => Either<T, E>;
408
- /**
409
- * Combines this Either with another Either into a tuple.
410
- *
411
- * @typeParam EE - Left type of the Either to combine with
412
- * @typeParam A - Right type of the Either to combine with
413
- * @param eitherA - The Either to combine with this one
414
- * @returns Either containing a tuple of both Right values, or the first Left if any Either is Left
415
- *
416
- * @example
417
- * ```typescript
418
- * right("Alice").zip(right(30)) // Right(["Alice", 30])
419
- * right("Alice").zip(left("No age")) // Left("No age")
420
- * left("No name").zip(right(30)) // Left("No name")
421
- * ```
422
- */
423
- zip: <EE, A>(eitherA: Either<EE, A>) => Either<E | EE, readonly [T, A]>;
424
- /**
425
- * Flattens a nested Either structure by removing one level of nesting.
426
- *
427
- * @typeParam EE - Left type of the inner Either
428
- * @typeParam U - Right type of the inner Either
429
- * @returns The inner Either if this Either is Right, otherwise this Either unchanged
430
- *
431
- * @example
432
- * ```typescript
433
- * right(right(42)).flatten() // Right(42)
434
- * right(left("inner error")).flatten() // Left("inner error")
435
- *
436
- * ```
437
- */
438
- flatten: <EE, U>(this: Either<E, Either<EE, U>>) => Either<E | EE, U>;
439
- /**
440
- * Chains Either-returning operations together (monadic bind).
441
- *
442
- * @typeParam EE - The error type of the Either returned by the mapper
443
- * @typeParam U - The success type of the Either returned by the mapper
444
- * @param mapper - Function that takes a Right value and returns an Either
445
- * @returns The Either returned by mapper if this Either is Right, otherwise this Either unchanged
446
- *
447
- * @example
448
- * ```typescript
449
- * const safeDivide = (x: number, y: number) =>
450
- * y === 0 ? left("Division by zero") : right(x / y);
451
- *
452
- * right(10).flatMap(x => safeDivide(x, 2)) // Right(5)
453
- * right(10).flatMap(x => safeDivide(x, 0)) // Left("Division by zero")
454
- * left("error").flatMap(x => safeDivide(x, 2)) // Left("error") - mapper not called
455
- * ```
456
- */
457
- flatMap: <EE, U>(mapper: (right: T) => Either<EE, U>) => Either<E | EE, U>;
458
- /**
459
- * Performs a side effect if this Either is Right, returning the original Either unchanged.
460
- * If this Either is Left, the side effect is not executed and the Either is returned as-is.
461
- *
462
- * @param sideEffect - Function to execute with the Right value (return value is ignored)
463
- * @returns The same Either instance unchanged
464
- *
465
- * @example
466
- * ```typescript
467
- * right(42)
468
- * .tap(value => console.log(`Got value: ${value}`)) // Logs: "Got value: 42"
469
- * left("error")
470
- * .tap(value => console.log(`Got value: ${value}`)) // No log output
471
- *
472
- * ```
473
- */
474
- tap: (sideEffect: (right: T) => void) => Either<E, T>;
475
- /**
476
- * Pattern matches on the Either, executing different functions based on its state.
477
- *
478
- * @typeParam U - The return type of both matcher functions
479
- * @param onLeft - Function to execute if Either is Left, receives the Left value
480
- * @param onRight - Function to execute if Either is Right, receives the Right value
481
- * @returns The result of the executed function
482
- *
483
- * @example
484
- * ```typescript
485
- * right(42).match(
486
- * (err) => `Error: ${err}`,
487
- * (val) => `Value: ${val}`
488
- * ) // "Value: 42"
489
- *
490
- * left("Not a number").match(
491
- * (err) => `Error: ${err}`,
492
- * (val) => `Value: ${val}`
493
- * ) // "Error: Not a number"
494
- * ```
495
- */
496
- match: <U>(onLeft: (left: E) => U, onRight: (right: T) => U) => U;
497
- /**
498
- * Extracts the Right value from the Either, or returns a fallback value if Left.
499
- *
500
- * @param fallback - Function that takes the Left value and returns a default value of type T
501
- * @returns The Right value if present, otherwise the result of the fallback function
502
- *
503
- * @example
504
- * ```typescript
505
- * right(42).getOrElse((err) => 0) // 42
506
- * left("error").getOrElse((err) => 0) // 0
507
- * ```
508
- */
509
- getOrElse: (fallback: (left: E) => T) => T;
510
- /**
511
- * Converts the Either to a Result type, which is an object with `ok` and `error` or `value` properties.
512
- *
513
- * @returns A Result object representing the Either's state
514
- *
515
- * @example
516
- * ```typescript
517
- * const result = right(42).toResult();
518
- * console.log(result.ok); // true
519
- * console.log(result.value); // 42
520
- *
521
- * const errorResult = left("error").toResult();
522
- * console.log(errorResult.ok); // false
523
- * console.log(errorResult.error); // "error"
524
- * ```
525
- */
526
- toResult: () => EitherResult<E, T>;
527
- };
528
- //#endregion
529
319
  //#region src/either/constructors.d.ts
530
320
  /**
531
321
  * Creates an Either containing an error value (Left).
@@ -662,4 +452,4 @@ declare function map3<EA, A, EB, B, EC, C, D>(eitherA: Either<EA, A>, eitherB: E
662
452
  */
663
453
  declare function sequence<E, T>(eithers: Either<E, T>[]): Either<E, T[]>;
664
454
  //#endregion
665
- export { None as C, NONE as S, Some as T, fromEither as _, fromOption as a, some as b, tryCatch as c, EitherValue as d, Left as f, sequence$1 as g, map3$1 as h, fromNullable as i, Either as l, map2$1 as m, map3 as n, left as o, Right as p, sequence as r, right as s, map2 as t, EitherResult as u, fromNullable$1 as v, OptionValue as w, Option as x, none as y };
455
+ export { NONE as _, fromOption as a, Some as b, tryCatch as c, sequence$1 as d, fromEither as f, Option as g, some as h, fromNullable as i, map2$1 as l, none as m, map3 as n, left as o, fromNullable$1 as p, sequence as r, right as s, map2 as t, map3$1 as u, None as v, OptionValue as y };
package/dist/option.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- import { C as None, S as NONE, T as Some, _ as fromEither, b as some, g as sequence, h as map3, m as map2, v as fromNullable, w as OptionValue, x as Option, y as none } from "./helpers-qdt9BxJz.mjs";
1
+ import { _ as NONE, b as Some, d as sequence, f as fromEither, g as Option, h as some, l as map2, m as none, p as fromNullable, u as map3, v as None, y as OptionValue } from "./helpers-Bw8McftR.mjs";
2
2
  export { NONE, None, type Option, OptionValue, Some, fromEither, fromNullable, map2, map3, none, sequence, some };
@@ -0,0 +1,100 @@
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 };
@@ -0,0 +1,23 @@
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 CHANGED
@@ -1,16 +1,49 @@
1
- //#region src/task.d.ts
2
- type TaskV<T> = () => Promise<T>;
3
- type Task<_T> = {
4
- map: never;
5
- ap: never;
6
- flatMap: never;
7
- tap: never;
8
- run: never;
9
- };
10
- declare function task<T>(): Task<T>;
11
- declare function fromPromise<T>(_thunk: () => Promise<T>): void;
12
- declare function all(): void;
13
- declare function traverse(): void;
14
- declare function sequence(): void;
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>;
15
33
  //#endregion
16
- export { Task, TaskV, all, fromPromise, sequence, task, traverse };
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 CHANGED
@@ -1,19 +1,57 @@
1
- //#region src/task.ts
2
- function task() {
3
- throw new Error("Not implemented");
4
- }
5
- function fromPromise(_thunk) {
6
- throw new Error("Not implemented");
7
- }
8
- function all() {
9
- throw new Error("Not implemented");
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));
10
19
  }
11
- function traverse() {
12
- throw new Error("Not implemented");
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);
13
36
  }
14
- function sequence() {
15
- throw new Error("Not implemented");
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())));
16
54
  }
17
55
 
18
56
  //#endregion
19
- export { all, fromPromise, sequence, task, traverse };
57
+ export { all, fromPromise, task };