ok-fp 0.2.1 → 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 +15 -14
- package/dist/index.d.mts +1328 -0
- package/dist/index.mjs +636 -0
- package/package.json +8 -17
- package/dist/either.d.mts +0 -2
- package/dist/either.mjs +0 -204
- package/dist/helpers-qdt9BxJz.d.mts +0 -665
- package/dist/option.d.mts +0 -2
- package/dist/option.mjs +0 -177
- package/dist/task.d.mts +0 -146
- package/dist/task.mjs +0 -77
- package/dist/validation.d.mts +0 -232
- package/dist/validation.mjs +0 -165
package/README.md
CHANGED
|
@@ -8,14 +8,15 @@ OK-FP is a small, focused functional programming toolkit for TypeScript.
|
|
|
8
8
|
|
|
9
9
|
It provides a minimal set of **typed effects**: composable, type-safe wrappers for optional values, errors, and async computations.
|
|
10
10
|
|
|
11
|
-
##
|
|
11
|
+
## Effects
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
| Effect | Description |
|
|
14
|
+
| ------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
|
|
15
|
+
| [`Option<T>`](https://pwlmc.github.io/ok-fp/option) | A value that may or may not be present. Use instead of `null`/`undefined`. |
|
|
16
|
+
| [`Either<E, T>`](https://pwlmc.github.io/ok-fp/either) | A computation that succeeds with `T` or fails with a typed error `E`. Errors are explicit and must be handled. |
|
|
17
|
+
| [`Validation<E, T>`](https://pwlmc.github.io/ok-fp/validation) | Like `Either`, but accumulates **all** errors instead of stopping at the first one. Ideal for form and config validation. |
|
|
18
|
+
| [`Task<T>`](https://pwlmc.github.io/ok-fp/task) | A lazy async computation that always succeeds. Executes only when `.run()` is called - unlike Promises, which are eager. |
|
|
19
|
+
| [`TaskEither<E, T>`](https://pwlmc.github.io/ok-fp/task-either) | A lazy async computation that can succeed with `T` or fail with `E`. Combines `Task`'s laziness with `Either`'s typed errors. |
|
|
19
20
|
|
|
20
21
|
## Installation
|
|
21
22
|
|
|
@@ -33,7 +34,7 @@ npm install ok-fp
|
|
|
33
34
|
This example shows a small pipeline using `Option`.
|
|
34
35
|
|
|
35
36
|
```ts
|
|
36
|
-
import { type Option, some, none } from "ok-fp
|
|
37
|
+
import { type Option, some, none } from "ok-fp";
|
|
37
38
|
|
|
38
39
|
const parseNumber = (input: string): Option<number> => {
|
|
39
40
|
const n = Number(input);
|
|
@@ -46,15 +47,15 @@ const positive = (n: number): Option<number> => (n > 0 ? some(n) : none());
|
|
|
46
47
|
|
|
47
48
|
const compute = (input: string): Option<number> =>
|
|
48
49
|
parseNumber(input)
|
|
49
|
-
.flatMap(nonZero)
|
|
50
|
-
.flatMap(positive)
|
|
51
|
-
.map((n) => 1 / n)
|
|
50
|
+
.flatMap(nonZero) // must not be 0
|
|
51
|
+
.flatMap(positive) // must be > 0
|
|
52
|
+
.map((n) => 1 / n) // reciprocal (no need for flatMap)
|
|
52
53
|
.map((n) => n * 100); // scale
|
|
53
54
|
|
|
54
55
|
// Usage (returns Option instances)
|
|
55
|
-
compute("4");
|
|
56
|
-
compute("0");
|
|
57
|
-
compute("-3");
|
|
56
|
+
compute("4"); // returns some(25)
|
|
57
|
+
compute("0"); // returns none() (fails nonZero)
|
|
58
|
+
compute("-3"); // returns none() (fails positive)
|
|
58
59
|
compute("abc"); // returns none() (fails parsing)
|
|
59
60
|
```
|
|
60
61
|
|