lite-fp 0.1.0 → 0.4.1
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 +48 -27
- package/dist/index.d.mts +146 -124
- package/dist/index.d.ts +146 -124
- package/dist/index.js +1 -287
- package/dist/index.mjs +1 -248
- package/package.json +27 -21
- package/dist/index.global.js +0 -236
- package/dist/index.global.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Tiny, zero‑dependency FP helpers for TypeScript.
|
|
4
4
|
|
|
5
|
-
- Algebraic data types: `Option`, `Either`, `Result`
|
|
6
|
-
- Product types: `Pair
|
|
5
|
+
- Algebraic data types: `Option`, `Either`, `Result`, `Maybe`
|
|
6
|
+
- Product types: `Pair`
|
|
7
7
|
- Small, predictable API with discriminated unions
|
|
8
8
|
- ESM and CJS builds, first‑class TypeScript types
|
|
9
9
|
|
|
@@ -24,7 +24,6 @@ import {
|
|
|
24
24
|
Either,
|
|
25
25
|
Result,
|
|
26
26
|
Pair,
|
|
27
|
-
Triple,
|
|
28
27
|
some,
|
|
29
28
|
none,
|
|
30
29
|
right,
|
|
@@ -37,7 +36,6 @@ const {
|
|
|
37
36
|
Either,
|
|
38
37
|
Result,
|
|
39
38
|
Pair,
|
|
40
|
-
Triple,
|
|
41
39
|
some,
|
|
42
40
|
none,
|
|
43
41
|
right,
|
|
@@ -52,16 +50,29 @@ const {
|
|
|
52
50
|
```ts
|
|
53
51
|
import { Option, some, none } from "lite-fp";
|
|
54
52
|
|
|
55
|
-
const o1 = Option.
|
|
56
|
-
const o2 = Option.
|
|
53
|
+
const o1 = Option.new("hello"); // Some("hello")
|
|
54
|
+
const o2 = Option.new(null); // None
|
|
57
55
|
|
|
58
56
|
const upper = Option.map(o1, s => s.toUpperCase()); // Some("HELLO")
|
|
59
57
|
|
|
60
58
|
const value = Option.getOrElse(o2, "fallback"); // "fallback"
|
|
61
59
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Maybe (T | null | undefined)
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { Maybe } from "lite-fp";
|
|
66
|
+
|
|
67
|
+
const m1 = Maybe.new(42); // 42
|
|
68
|
+
const m2 = Maybe.new(null); // undefined (None)
|
|
69
|
+
|
|
70
|
+
const doubled = Maybe.map(m1, x => x * 2); // 84
|
|
71
|
+
const value = Maybe.getOrElse(m2, 0); // 0
|
|
72
|
+
|
|
73
|
+
if (Maybe.isSome(m1)) {
|
|
74
|
+
// m1 is number here
|
|
75
|
+
}
|
|
65
76
|
```
|
|
66
77
|
|
|
67
78
|
### Either (A | B)
|
|
@@ -101,7 +112,7 @@ const user = await fetch("/api/user")
|
|
|
101
112
|
```ts
|
|
102
113
|
import { Result } from "lite-fp";
|
|
103
114
|
|
|
104
|
-
const r1 = Result.
|
|
115
|
+
const r1 = Result.new("data", "nope"); // Done("data")
|
|
105
116
|
const r2 = await Result.fromPromise(
|
|
106
117
|
Promise.reject("x"),
|
|
107
118
|
e => new Error(String(e)),
|
|
@@ -110,10 +121,11 @@ const r2 = await Result.fromPromise(
|
|
|
110
121
|
const safe = Result.recover(r2, () => "default"); // Done("default")
|
|
111
122
|
```
|
|
112
123
|
|
|
113
|
-
|
|
124
|
+
|
|
125
|
+
### Pair
|
|
114
126
|
|
|
115
127
|
```ts
|
|
116
|
-
import { Pair,
|
|
128
|
+
import { Pair, pair } from "lite-fp";
|
|
117
129
|
|
|
118
130
|
const p = pair(1, "a"); // Pair<number, string>
|
|
119
131
|
const p2 = Pair.map(
|
|
@@ -122,13 +134,6 @@ const p2 = Pair.map(
|
|
|
122
134
|
s => s.toUpperCase(),
|
|
123
135
|
); // [2, "A"]
|
|
124
136
|
|
|
125
|
-
const t = triple(1, 2, 3);
|
|
126
|
-
const t2 = Triple.map(
|
|
127
|
-
t,
|
|
128
|
-
x => x + 1,
|
|
129
|
-
y => y * 2,
|
|
130
|
-
z => z - 1,
|
|
131
|
-
); // [2, 4, 2]
|
|
132
137
|
```
|
|
133
138
|
|
|
134
139
|
## API Overview
|
|
@@ -136,34 +141,50 @@ const t2 = Triple.map(
|
|
|
136
141
|
- Option
|
|
137
142
|
- Constructors: `none`, `some`, `new`, `fromNullable`, `fromPredicate`, `fromThrowable`, `fromPromise`
|
|
138
143
|
- Type guards: `isSome`, `isNone`
|
|
139
|
-
- Ops: `map`, `flatMap`, `filter`, `match`
|
|
140
|
-
- Extract: `getOrElse`, `getOrUndefined`, `getOrThrow`
|
|
144
|
+
- Ops: `map`, `flatMap`, `filter`, `match`, `getOrElse`, `getOrUndefined`, `getOrThrow`
|
|
141
145
|
- Combine: `zip`, `apply`, `orElse`
|
|
142
|
-
- Convert: `toResult`
|
|
143
146
|
|
|
144
147
|
- Either
|
|
145
148
|
- Constructors: `left`, `right`, `new`, `fromNullable`, `fromThrowable`, `fromPromise`
|
|
146
149
|
- Type guards: `isLeft`, `isRight`
|
|
147
|
-
- Ops: `map`, `mapLeft`, `bimap`, `flatMap`, `chain`, `fold`, `match`, `getOrElse`, `zip`, `apply`, `tap`, `tapLeft`
|
|
150
|
+
- Ops: `map`, `mapLeft`, `bimap`, `flatMap`, `chain`, `fold`, `match`, `swap`, `getOrElse`, `zip`, `apply`, `tap`, `tapLeft`
|
|
148
151
|
|
|
149
152
|
- Result
|
|
150
153
|
- Constructors: `done`, `fail`, `new`, `fromNullable`, `fromThrowable`, `fromPromise`
|
|
151
154
|
- Type guards: `isDone`, `isFail`
|
|
152
155
|
- Ops: `map`, `mapError`, `flatMap`, `match`, `recover`, `getOrElse`, `getOrThrow`, `zip`, `apply`
|
|
153
|
-
- Convert: `toOption`
|
|
154
156
|
|
|
155
|
-
-
|
|
156
|
-
- `
|
|
157
|
+
- Maybe (T | null | undefined)
|
|
158
|
+
- Constructors: `new`, `just`, `nothing`/`nothingNull`/`nothingUndefined`, `fromNullable`, `fromPredicate`, `fromThrowable`, `fromPromise`
|
|
159
|
+
- Type guards: `isSome`, `isNothing`
|
|
160
|
+
- Ops: `map`, `flatMap`, `filter`, `match`, `zip`, `apply`, `orElse`
|
|
161
|
+
- Extract: `getOrElse`, `getOrUndefined`, `getOrThrow`
|
|
162
|
+
|
|
163
|
+
- Pair
|
|
164
|
+
- `pair`, and utilities to map, zip, convert to/from arrays/objects.
|
|
157
165
|
|
|
158
166
|
See `src/` for the full, well‑typed surface.
|
|
159
167
|
|
|
168
|
+
## Differences: Result/Either vs Maybe/Option
|
|
169
|
+
|
|
170
|
+
- Either vs Result
|
|
171
|
+
- Both are discriminated unions with rich operations.
|
|
172
|
+
- Either represents two generic branches (Left/Right) and is often used for domain errors (Left) vs success (Right).
|
|
173
|
+
- Result represents success (`Done`) or failure (`Fail`) explicitly and includes helpers like `recover` and `getOrUndefined`.
|
|
174
|
+
- Choose based on semantics and style; the APIs are similar.
|
|
175
|
+
|
|
176
|
+
- Maybe vs Option
|
|
177
|
+
- Maybe is `T | null | undefined` (lightweight and idiomatic in JS). No runtime tag; use `== null` or the provided guards to narrow.
|
|
178
|
+
- Option is a discriminated union (`{ $: "Some" } | { $: "None" }`) with stable guards (`isSome`, `isNone`) and utility functions.
|
|
179
|
+
- Use Maybe when interoperating with APIs that naturally return `null/undefined`; use Option when you want an explicit ADT and consistent operations.
|
|
180
|
+
|
|
160
181
|
## Notes
|
|
161
182
|
|
|
162
183
|
- Prototype additions
|
|
163
184
|
- `Promise.prototype.toEither(onError)` is provided when `Either` is imported.
|
|
164
185
|
- `Promise.prototype.toResult(onError)` is provided when `Result` is imported.
|
|
165
186
|
- `Array.prototype.firstOption()` is provided when `Option` is imported.
|
|
166
|
-
- No runtime dependencies. Fully typed. Tree‑shakeable.
|
|
187
|
+
- No runtime dependencies. Fully typed. Tree‑shakeable.
|
|
167
188
|
|
|
168
189
|
## Contributing
|
|
169
190
|
|
package/dist/index.d.mts
CHANGED
|
@@ -1,74 +1,82 @@
|
|
|
1
|
-
type
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
snd: <A, B>(p: Pair<A, B>) => B;
|
|
5
|
-
new: <A, B>(fst: A, snd: B) => Pair<A, B>;
|
|
6
|
-
fromArray: <A, B>([fst, snd]: [A, B]) => Pair<A, B>;
|
|
7
|
-
fromObject: <A, B>(obj: {
|
|
8
|
-
fst: A;
|
|
9
|
-
snd: B;
|
|
10
|
-
}) => Pair<A, B>;
|
|
11
|
-
curry: <A>(fst: A) => <B>(snd: B) => Pair<A, B>;
|
|
12
|
-
mapFirst: <A, B, C>(p: Pair<A, B>, fn: (a: A) => C) => Pair<C, B>;
|
|
13
|
-
mapSecond: <A, B, C>(p: Pair<A, B>, fn: (b: B) => C) => Pair<A, C>;
|
|
14
|
-
map: <A, B, C, D>(p: Pair<A, B>, fnA: (a: A) => C, fnB: (b: B) => D) => Pair<C, D>;
|
|
15
|
-
swap: <A, B>(p: Pair<A, B>) => Pair<B, A>;
|
|
16
|
-
apply: <A, B, C>(p: Pair<(a: A) => B, C>, value: A) => Pair<B, C>;
|
|
17
|
-
apply2: <A, B, C>(fnPair: Pair<(a: A) => B, (b: B) => C>, vPair: Pair<A, B>) => Pair<A, C>;
|
|
18
|
-
reduce: <A, B, C>(p: Pair<A, B>, fn: (a: A, b: B) => C) => C;
|
|
19
|
-
toArray: <A, B>(p: Pair<A, B>) => [A, B];
|
|
20
|
-
toObject: <A, B>(p: Pair<A, B>) => {
|
|
21
|
-
fst: A;
|
|
22
|
-
snd: B;
|
|
23
|
-
};
|
|
24
|
-
eq: <A, B>(p1: Pair<A, B>, p2: Pair<A, B>) => boolean;
|
|
25
|
-
equals: <A, B>(p1: Pair<A, B>, p2: Pair<A, B>, eqA: (a1: A, a2: A) => boolean, eqB: (b1: B, b2: B) => boolean) => boolean;
|
|
26
|
-
traverseOption: <A, B, C>(p: Pair<A, Option<B>>, fn: (a: A, b: B) => C) => Option<Pair<A, C>>;
|
|
27
|
-
traverseResult: <A, B, C, E>(p: Pair<A, Result<B, E>>, fn: (a: A, b: B) => C) => Result<Pair<A, C>, E>;
|
|
28
|
-
zip: <A, B, C, D>(p1: Pair<A, B>, p2: Pair<C, D>) => Pair<A & C, Pair<B, D>>;
|
|
29
|
-
};
|
|
30
|
-
declare const pair: <A, B>(fst: A, snd: B) => Pair<A, B>;
|
|
31
|
-
|
|
32
|
-
type Done<T> = {
|
|
33
|
-
readonly $: "Done";
|
|
34
|
-
readonly value: T;
|
|
1
|
+
type Left<A> = {
|
|
2
|
+
readonly $: "Left";
|
|
3
|
+
readonly value: A;
|
|
35
4
|
};
|
|
36
|
-
type
|
|
37
|
-
readonly $: "
|
|
38
|
-
readonly
|
|
5
|
+
type Right<B> = {
|
|
6
|
+
readonly $: "Right";
|
|
7
|
+
readonly value: B;
|
|
39
8
|
};
|
|
40
|
-
|
|
41
|
-
declare const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
9
|
+
declare const left: <A>(value: A) => Left<A>;
|
|
10
|
+
declare const right: <B>(value: B) => Right<B>;
|
|
11
|
+
type Either<A, B> = Left<A> | Right<B>;
|
|
12
|
+
declare const Either: {
|
|
13
|
+
left: <A>(value: A) => Left<A>;
|
|
14
|
+
right: <B>(value: B) => Right<B>;
|
|
15
|
+
new: <A, B>(value: B | null | undefined, error: A) => Either<A, B>;
|
|
16
|
+
isLeft: <A, B>(e: Either<A, B>) => e is Left<A>;
|
|
17
|
+
isRight: <A, B>(e: Either<A, B>) => e is Right<B>;
|
|
18
|
+
fromNullable: <A, B>(value: B | null | undefined, error: A) => Either<A, B>;
|
|
19
|
+
fromThrowable: <A, B>(fn: () => B, onError: (e: unknown) => A) => Either<A, B>;
|
|
20
|
+
fromPromise: <A, B>(promise: Promise<B>, onError: (e: unknown) => A) => Promise<Either<A, B>>;
|
|
21
|
+
map: <A, B, C>(e: Either<A, B>, fn: (r: B) => C) => Either<A, C>;
|
|
22
|
+
mapLeft: <A, B, C>(e: Either<A, B>, fn: (l: A) => C) => Either<C, B>;
|
|
23
|
+
bimap: <A, B, C, D>(e: Either<A, B>, fl: (l: A) => C, fr: (r: B) => D) => Either<C, D>;
|
|
24
|
+
flatMap: <A, B, C>(e: Either<A, B>, fn: (value: B) => Either<A, C>) => Either<A, C>;
|
|
25
|
+
chain: <A, B, C>(e: Either<A, B>, fn: (r: B) => Either<A, C>) => Either<A, C>;
|
|
26
|
+
fold: <A, B, C>(e: Either<A, B>, onLeft: (l: A) => C, onRight: (r: B) => C) => C;
|
|
27
|
+
match: <A, B, C>(e: Either<A, B>, matcher: {
|
|
28
|
+
right: (value: B) => C;
|
|
29
|
+
left: (value: A) => C;
|
|
30
|
+
}) => C;
|
|
31
|
+
swap: <A, B>(e: Either<A, B>) => Either<B, A>;
|
|
32
|
+
getOrElse: <A, B>(e: Either<A, B>, defaultValue: B) => B;
|
|
33
|
+
zip: <E, A, B>(a: Either<E, A>, b: Either<E, B>) => Either<E, [A, B]>;
|
|
34
|
+
apply: <E, A, B>(fn: Either<E, (value: A) => B>, arg: Either<E, A>) => Either<E, B>;
|
|
35
|
+
tap: <A, B>(e: Either<A, B>, f: (r: B) => void) => Either<A, B>;
|
|
36
|
+
tapLeft: <A, B>(e: Either<A, B>, f: (l: A) => void) => Either<A, B>;
|
|
63
37
|
};
|
|
64
|
-
declare const done: <T>(value: T) => Done<T>;
|
|
65
|
-
declare const fail: <E>(error: E) => Fail<E>;
|
|
66
38
|
declare global {
|
|
67
39
|
interface Promise<T> {
|
|
68
|
-
|
|
40
|
+
toEither<A>(onError: (e: unknown) => A): Promise<Either<A, T>>;
|
|
69
41
|
}
|
|
70
42
|
}
|
|
71
43
|
|
|
44
|
+
type NothingNull = null;
|
|
45
|
+
type NothingUndefined = undefined;
|
|
46
|
+
type Nothing = NothingNull | NothingUndefined;
|
|
47
|
+
declare const just: <T>(value: T) => Maybe<T>;
|
|
48
|
+
declare const nothing: () => Nothing;
|
|
49
|
+
type Maybe<T> = T | Nothing;
|
|
50
|
+
declare const Maybe: {
|
|
51
|
+
just: <T>(value: T) => Maybe<T>;
|
|
52
|
+
nothing: () => Nothing;
|
|
53
|
+
new: <T>(v: T | null | undefined) => Maybe<T>;
|
|
54
|
+
nothingNull: () => NothingNull;
|
|
55
|
+
nothingUndefined: () => NothingUndefined;
|
|
56
|
+
isSome: <T>(m: Maybe<T>) => m is T;
|
|
57
|
+
isNothing: <T>(m: Maybe<T>) => m is Nothing;
|
|
58
|
+
isNothingNull: <T>(m: Maybe<T>) => m is NothingNull;
|
|
59
|
+
isNothingUndefined: <T>(m: Maybe<T>) => m is NothingUndefined;
|
|
60
|
+
fromNullable: <T>(v: T | null | undefined) => Maybe<T>;
|
|
61
|
+
toNullable: <T>(m: Maybe<T>) => T | null;
|
|
62
|
+
toUndefined: <T>(m: Maybe<T>) => T | undefined;
|
|
63
|
+
map: <T, U>(m: Maybe<T>, fn: (v: T) => U) => Maybe<U>;
|
|
64
|
+
flatMap: <T, U>(m: Maybe<T>, fn: (v: T) => Maybe<U>) => Maybe<U>;
|
|
65
|
+
filter: <T>(m: Maybe<T>, predicate: (value: T) => boolean) => Maybe<T>;
|
|
66
|
+
match: <T, U>(m: Maybe<T>, matcher: {
|
|
67
|
+
some: (v: T) => U;
|
|
68
|
+
nothing: () => U;
|
|
69
|
+
}) => U;
|
|
70
|
+
getOrElse: <T>(m: Maybe<T>, d: T) => T;
|
|
71
|
+
getOrUndefined: <T>(m: Maybe<T>) => T | undefined;
|
|
72
|
+
getOrThrow: <T>(m: Maybe<T>, error: Error) => T;
|
|
73
|
+
fromThrowable: <T>(fn: () => T) => Maybe<T>;
|
|
74
|
+
fromPromise: <T>(promise: Promise<T>) => Promise<Maybe<T>>;
|
|
75
|
+
zip: <T, U>(a: Maybe<T>, b: Maybe<U>) => Maybe<[T, U]>;
|
|
76
|
+
apply: <T, U>(fn: Maybe<(value: T) => U>, opt: Maybe<T>) => Maybe<U>;
|
|
77
|
+
orElse: <T>(opt: Maybe<T>, other: Maybe<T>) => Maybe<T>;
|
|
78
|
+
};
|
|
79
|
+
|
|
72
80
|
type None = {
|
|
73
81
|
readonly $: "None";
|
|
74
82
|
};
|
|
@@ -76,11 +84,13 @@ type Some<T> = {
|
|
|
76
84
|
readonly $: "Some";
|
|
77
85
|
readonly value: T;
|
|
78
86
|
};
|
|
87
|
+
declare const none: <T>() => Option<T>;
|
|
88
|
+
declare const some: <T>(value: T) => Option<T>;
|
|
79
89
|
type Option<T> = None | Some<T>;
|
|
80
90
|
declare const Option: {
|
|
81
91
|
none: <T>() => Option<T>;
|
|
82
92
|
some: <T>(value: T) => Option<T>;
|
|
83
|
-
new: <T>(value: T) => Option<T>;
|
|
93
|
+
new: <T>(value: T | null | undefined) => Option<T>;
|
|
84
94
|
isSome: <T>(option: Option<T>) => option is Some<T>;
|
|
85
95
|
isNone: <T>(option: Option<T>) => option is None;
|
|
86
96
|
fromNullable: <T>(value: T | null | undefined) => Option<T>;
|
|
@@ -100,78 +110,90 @@ declare const Option: {
|
|
|
100
110
|
zip: <T, U>(a: Option<T>, b: Option<U>) => Option<[T, U]>;
|
|
101
111
|
apply: <T, U>(fn: Option<(value: T) => U>, opt: Option<T>) => Option<U>;
|
|
102
112
|
orElse: <T>(opt: Option<T>, other: Option<T>) => Option<T>;
|
|
103
|
-
toResult: <T, E>(option: Option<T>, error: E) => Result<T, E>;
|
|
104
113
|
};
|
|
105
|
-
declare const none: <T>() => Option<T>;
|
|
106
|
-
declare const some: <T>(value: T) => Option<T>;
|
|
107
114
|
declare global {
|
|
108
115
|
interface Array<T> {
|
|
109
116
|
firstOption(): Option<T>;
|
|
110
117
|
}
|
|
118
|
+
interface Promise<T> {
|
|
119
|
+
toOption(): Promise<Option<T>>;
|
|
120
|
+
}
|
|
111
121
|
}
|
|
112
122
|
|
|
113
|
-
type
|
|
114
|
-
|
|
115
|
-
|
|
123
|
+
type Pair<A, B> = readonly [A, B];
|
|
124
|
+
declare const Pair: {
|
|
125
|
+
fst: <A, B>(p: Pair<A, B>) => A;
|
|
126
|
+
snd: <A, B>(p: Pair<A, B>) => B;
|
|
127
|
+
new: <A, B>(a: A, b: B) => Pair<A, B>;
|
|
128
|
+
fromArray: <A, B>([a, b]: [A, B]) => Pair<A, B>;
|
|
129
|
+
fromObject: <A, B>(obj: {
|
|
130
|
+
fst: A;
|
|
131
|
+
snd: B;
|
|
132
|
+
}) => Pair<A, B>;
|
|
133
|
+
curry: <A>(a: A) => <B>(b: B) => Pair<A, B>;
|
|
134
|
+
mapFirst: <A, B, C>(p: Pair<A, B>, fn: (a: A) => C) => Pair<C, B>;
|
|
135
|
+
mapSecond: <A, B, C>(p: Pair<A, B>, fn: (b: B) => C) => Pair<A, C>;
|
|
136
|
+
map: <A, B, C, D>(p: Pair<A, B>, fnA: (a: A) => C, fnB: (b: B) => D) => Pair<C, D>;
|
|
137
|
+
swap: <A, B>(p: Pair<A, B>) => Pair<B, A>;
|
|
138
|
+
apply: <A, B, C>(p: Pair<(a: A) => B, C>, value: A) => Pair<B, C>;
|
|
139
|
+
apply2: <A, B, C>(fnPair: Pair<(a: A) => B, (b: B) => C>, vPair: Pair<A, B>) => Pair<A, C>;
|
|
140
|
+
reduce: <A, B, C>(p: Pair<A, B>, fn: (a: A, b: B) => C) => C;
|
|
141
|
+
toArray: <A, B>(p: Pair<A, B>) => [A, B];
|
|
142
|
+
toObject: <A, B>(p: Pair<A, B>) => {
|
|
143
|
+
fst: A;
|
|
144
|
+
snd: B;
|
|
145
|
+
};
|
|
146
|
+
eq: <A, B>(p1: Pair<A, B>, p2: Pair<A, B>) => boolean;
|
|
147
|
+
equals: <A, B>(p1: Pair<A, B>, p2: Pair<A, B>, eqA: (a1: A, a2: A) => boolean, eqB: (b1: B, b2: B) => boolean) => boolean;
|
|
148
|
+
zip: <A, B, C, D>(p1: Pair<A, B>, p2: Pair<C, D>) => Pair<Pair<A, C>, Pair<B, D>>;
|
|
116
149
|
};
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
150
|
+
declare const pair: <A, B>(a: A, b: B) => Pair<A, B>;
|
|
151
|
+
|
|
152
|
+
type Done<T> = {
|
|
153
|
+
readonly v: T;
|
|
120
154
|
};
|
|
121
|
-
type
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
155
|
+
type Fail<E> = {
|
|
156
|
+
readonly e: E;
|
|
157
|
+
};
|
|
158
|
+
declare const done: <T>(value: T) => Done<T>;
|
|
159
|
+
declare const fail: <E>(error: E) => Fail<E>;
|
|
160
|
+
type Result<T, E> = Done<T> | Fail<E>;
|
|
161
|
+
declare const Result: {
|
|
162
|
+
done: <T>(value: T) => Done<T>;
|
|
163
|
+
fail: <E>(error: E) => Fail<E>;
|
|
164
|
+
OK: <T>(value: T) => Done<T>;
|
|
165
|
+
Err: <E>(error: E) => Fail<E>;
|
|
166
|
+
new: <T, E>(value: T | null | undefined, error: E) => Result<T, E>;
|
|
167
|
+
isDone: <T, E>(r: Result<T, E>) => r is Done<T>;
|
|
168
|
+
isFail: <T, E>(r: Result<T, E>) => r is Fail<E>;
|
|
169
|
+
isOk: <T, E>(r: Result<T, E>) => r is Done<T>;
|
|
170
|
+
isErr: <T, E>(r: Result<T, E>) => r is Fail<E>;
|
|
171
|
+
fromNullable: <T, E>(value: T | null | undefined, error: E) => Result<T, E>;
|
|
172
|
+
fromThrowable: <T, E>(fn: () => T, onError: (e: unknown) => E) => Result<T, E>;
|
|
173
|
+
fromPromise: <T, E>(promise: Promise<T>, onError: (e: unknown) => E) => Promise<Result<T, E>>;
|
|
174
|
+
map: <T, E, U>(r: Result<T, E>, fn: (a: T) => U) => Result<U, E>;
|
|
175
|
+
mapErr: <T, E, F>(r: Result<T, E>, fn: (b: E) => F) => Result<T, F>;
|
|
176
|
+
bimap: <T, E, U, F>(r: Result<T, E>, onDone: (a: T) => U, onFail: (b: E) => F) => Result<U, F>;
|
|
177
|
+
flatMap: <T, E, C>(r: Result<T, E>, fn: (a: T) => Result<C, E>) => Result<C, E>;
|
|
178
|
+
match: <T, E, U>(r: Result<T, E>, matcher: {
|
|
179
|
+
done: (a: T) => U;
|
|
180
|
+
fail: (b: E) => U;
|
|
181
|
+
}) => U;
|
|
182
|
+
fold: <T, E, U>(r: Result<T, E>, onFail: (error: E) => U, onDone: (value: T) => U) => U;
|
|
183
|
+
recover: <T, E>(r: Result<T, E>, fn: (error: E) => T) => Result<T, E>;
|
|
184
|
+
getOrElse: <T, E>(r: Result<T, E>, d: T) => T;
|
|
185
|
+
getOrThrow: <T, E>(r: Result<T, E>, onError: (b: E) => never) => T;
|
|
186
|
+
zip: <T, U, E>(a: Result<T, E>, b: Result<U, E>) => Result<[T, U], E>;
|
|
187
|
+
apply: <T, U, E>(fn: Result<(a: T) => U, E>, arg: Result<T, E>) => Result<U, E>;
|
|
188
|
+
orElse: <T, E>(a: Result<T, E>, b: Result<T, E>) => Result<T, E>;
|
|
189
|
+
filter: <T, E>(r: Result<T, E>, predicate: (a: T) => boolean, onFalse: E) => Result<T, E>;
|
|
190
|
+
tap: <T, E>(r: Result<T, E>, f: (a: T) => void) => Result<T, E>;
|
|
191
|
+
tapErr: <T, E>(r: Result<T, E>, f: (b: E) => void) => Result<T, E>;
|
|
147
192
|
};
|
|
148
193
|
declare global {
|
|
149
194
|
interface Promise<T> {
|
|
150
|
-
|
|
195
|
+
toResult<E = unknown>(onError: (e: unknown) => E): Promise<Result<T, E>>;
|
|
151
196
|
}
|
|
152
197
|
}
|
|
153
|
-
declare const left: <A>(value: A) => Left<A>;
|
|
154
|
-
declare const right: <B>(value: B) => Right<B>;
|
|
155
|
-
|
|
156
|
-
type Triple<A, B, C> = readonly [A, B, C];
|
|
157
|
-
declare const Triple: {
|
|
158
|
-
fst: <A, B, C>(t: Triple<A, B, C>) => A;
|
|
159
|
-
snd: <A, B, C>(t: Triple<A, B, C>) => B;
|
|
160
|
-
thd: <A, B, C>(t: Triple<A, B, C>) => C;
|
|
161
|
-
new: <A, B, C>(fst: A, snd: B, thd: C) => Triple<A, B, C>;
|
|
162
|
-
fromArray: <A, B, C>([fst, snd, thd]: [A, B, C]) => Triple<A, B, C>;
|
|
163
|
-
toArray: <A, B, C>(t: Triple<A, B, C>) => [A, B, C];
|
|
164
|
-
fromObject: <A, B, C>(obj: {
|
|
165
|
-
fst: A;
|
|
166
|
-
snd: B;
|
|
167
|
-
thd: C;
|
|
168
|
-
}) => Triple<A, B, C>;
|
|
169
|
-
curry: <A>(fst: A) => <B>(snd: B) => <C>(thd: C) => Triple<A, B, C>;
|
|
170
|
-
equals: <A, B, C>(t1: Triple<A, B, C>, t2: Triple<A, B, C>, eqA: (a1: A, a2: A) => boolean, eqB: (b1: B, b2: B) => boolean, eqC: (c1: C, c2: C) => boolean) => boolean;
|
|
171
|
-
map: <A, B, C, D, E, F>(t: Triple<A, B, C>, fnA: (a: A) => D, fnB: (b: B) => E, fnC: (c: C) => F) => Triple<D, E, F>;
|
|
172
|
-
zip: <A, B, C, D, E, F>(t1: Triple<A, B, C>, t2: Triple<D, E, F>) => Triple<A & D, B & E, C & F>;
|
|
173
|
-
apply: <A, B, C, D>(t: Triple<(a: A) => B, (b: B) => C, (c: C) => D>, value: A) => Triple<B, C, D>;
|
|
174
|
-
};
|
|
175
|
-
declare const triple: <A, B, C>(fst: A, snd: B, thd: C) => Triple<A, B, C>;
|
|
176
198
|
|
|
177
|
-
export { type Done, Either, type Fail, type Left, type None, Option, Pair, Result, type Right, type Some,
|
|
199
|
+
export { type Done, Either, type Fail, type Left, Maybe, type None, type Nothing, type NothingNull, type NothingUndefined, Option, Pair, Result, type Right, type Some, done, fail, just, left, none, nothing, pair, right, some };
|