lite-fp 0.1.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/LICENSE +22 -0
- package/README.md +178 -0
- package/dist/index.d.mts +177 -0
- package/dist/index.d.ts +177 -0
- package/dist/index.global.js +236 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +287 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +248 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 lite-fp Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# lite-fp
|
|
2
|
+
|
|
3
|
+
Tiny, zero‑dependency FP helpers for TypeScript.
|
|
4
|
+
|
|
5
|
+
- Algebraic data types: `Option`, `Either`, `Result`
|
|
6
|
+
- Product types: `Pair`, `Triple`
|
|
7
|
+
- Small, predictable API with discriminated unions
|
|
8
|
+
- ESM and CJS builds, first‑class TypeScript types
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm i lite-fp
|
|
14
|
+
# or
|
|
15
|
+
pnpm add lite-fp
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Import
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
// ESM
|
|
22
|
+
import {
|
|
23
|
+
Option,
|
|
24
|
+
Either,
|
|
25
|
+
Result,
|
|
26
|
+
Pair,
|
|
27
|
+
Triple,
|
|
28
|
+
some,
|
|
29
|
+
none,
|
|
30
|
+
right,
|
|
31
|
+
left,
|
|
32
|
+
} from "lite-fp";
|
|
33
|
+
|
|
34
|
+
// CJS
|
|
35
|
+
const {
|
|
36
|
+
Option,
|
|
37
|
+
Either,
|
|
38
|
+
Result,
|
|
39
|
+
Pair,
|
|
40
|
+
Triple,
|
|
41
|
+
some,
|
|
42
|
+
none,
|
|
43
|
+
right,
|
|
44
|
+
left,
|
|
45
|
+
} = require("lite-fp");
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Quick Start
|
|
49
|
+
|
|
50
|
+
### Option
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { Option, some, none } from "lite-fp";
|
|
54
|
+
|
|
55
|
+
const o1 = Option.fromNullable("hello"); // Some("hello")
|
|
56
|
+
const o2 = Option.fromNullable(null); // None
|
|
57
|
+
|
|
58
|
+
const upper = Option.map(o1, s => s.toUpperCase()); // Some("HELLO")
|
|
59
|
+
|
|
60
|
+
const value = Option.getOrElse(o2, "fallback"); // "fallback"
|
|
61
|
+
|
|
62
|
+
// Convert
|
|
63
|
+
import { done, fail } from "lite-fp";
|
|
64
|
+
const asResult = Option.toResult(o1, new Error("missing")); // Done("hello")
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Either (A | B)
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import { Either, left, right } from "lite-fp";
|
|
71
|
+
|
|
72
|
+
type Err = { message: string };
|
|
73
|
+
|
|
74
|
+
const parseJson = (s: string) =>
|
|
75
|
+
Either.fromThrowable(
|
|
76
|
+
() => JSON.parse(s),
|
|
77
|
+
e => ({ message: String(e) }) as Err,
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
const e1 = parseJson('{"a":1}'); // Right({ a: 1 })
|
|
81
|
+
const e2 = parseJson("invalid"); // Left({ message: "..." })
|
|
82
|
+
|
|
83
|
+
const msg = Either.fold(
|
|
84
|
+
e2,
|
|
85
|
+
l => `err: ${l.message}`,
|
|
86
|
+
r => `ok: ${Object.keys(r).length}`,
|
|
87
|
+
); // => "err: ..."
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Promise helper
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
// Adds Promise.prototype.toEither(onError)
|
|
94
|
+
const user = await fetch("/api/user")
|
|
95
|
+
.then(r => r.json())
|
|
96
|
+
.toEither(e => new Error(String(e))); // Either<Error, User>
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Result (Done | Fail)
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
import { Result } from "lite-fp";
|
|
103
|
+
|
|
104
|
+
const r1 = Result.fromNullable("data", "nope"); // Done("data")
|
|
105
|
+
const r2 = await Result.fromPromise(
|
|
106
|
+
Promise.reject("x"),
|
|
107
|
+
e => new Error(String(e)),
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
const safe = Result.recover(r2, () => "default"); // Done("default")
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Pair / Triple
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
import { Pair, Triple, pair, triple } from "lite-fp";
|
|
117
|
+
|
|
118
|
+
const p = pair(1, "a"); // Pair<number, string>
|
|
119
|
+
const p2 = Pair.map(
|
|
120
|
+
p,
|
|
121
|
+
x => x + 1,
|
|
122
|
+
s => s.toUpperCase(),
|
|
123
|
+
); // [2, "A"]
|
|
124
|
+
|
|
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
|
+
```
|
|
133
|
+
|
|
134
|
+
## API Overview
|
|
135
|
+
|
|
136
|
+
- Option
|
|
137
|
+
- Constructors: `none`, `some`, `new`, `fromNullable`, `fromPredicate`, `fromThrowable`, `fromPromise`
|
|
138
|
+
- Type guards: `isSome`, `isNone`
|
|
139
|
+
- Ops: `map`, `flatMap`, `filter`, `match`
|
|
140
|
+
- Extract: `getOrElse`, `getOrUndefined`, `getOrThrow`
|
|
141
|
+
- Combine: `zip`, `apply`, `orElse`
|
|
142
|
+
- Convert: `toResult`
|
|
143
|
+
|
|
144
|
+
- Either
|
|
145
|
+
- Constructors: `left`, `right`, `new`, `fromNullable`, `fromThrowable`, `fromPromise`
|
|
146
|
+
- Type guards: `isLeft`, `isRight`
|
|
147
|
+
- Ops: `map`, `mapLeft`, `bimap`, `flatMap`, `chain`, `fold`, `match`, `getOrElse`, `zip`, `apply`, `tap`, `tapLeft`
|
|
148
|
+
|
|
149
|
+
- Result
|
|
150
|
+
- Constructors: `done`, `fail`, `new`, `fromNullable`, `fromThrowable`, `fromPromise`
|
|
151
|
+
- Type guards: `isDone`, `isFail`
|
|
152
|
+
- Ops: `map`, `mapError`, `flatMap`, `match`, `recover`, `getOrElse`, `getOrThrow`, `zip`, `apply`
|
|
153
|
+
- Convert: `toOption`
|
|
154
|
+
|
|
155
|
+
- Pair / Triple
|
|
156
|
+
- `pair`, `triple`, and utilities to map, zip, convert to/from arrays/objects.
|
|
157
|
+
|
|
158
|
+
See `src/` for the full, well‑typed surface.
|
|
159
|
+
|
|
160
|
+
## Notes
|
|
161
|
+
|
|
162
|
+
- Prototype additions
|
|
163
|
+
- `Promise.prototype.toEither(onError)` is provided when `Either` is imported.
|
|
164
|
+
- `Promise.prototype.toResult(onError)` is provided when `Result` is imported.
|
|
165
|
+
- `Array.prototype.firstOption()` is provided when `Option` is imported.
|
|
166
|
+
- No runtime dependencies. Fully typed. Tree‑shakeable.
|
|
167
|
+
|
|
168
|
+
## Contributing
|
|
169
|
+
|
|
170
|
+
1. Fork the repository.
|
|
171
|
+
2. Create a new branch: `git checkout -b feature-name`.
|
|
172
|
+
3. Commit your changes: `git commit -m 'Add some feature'`.
|
|
173
|
+
4. Push to the branch: `git push origin feature-name`.
|
|
174
|
+
5. Open a pull request.
|
|
175
|
+
|
|
176
|
+
## License
|
|
177
|
+
|
|
178
|
+
Distributed under the MIT License. See the [LICENSE](LICENSE) file for more details.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
type Pair<A, B> = readonly [A, B];
|
|
2
|
+
declare const Pair: {
|
|
3
|
+
fst: <A, B>(p: Pair<A, B>) => A;
|
|
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;
|
|
35
|
+
};
|
|
36
|
+
type Fail<E> = {
|
|
37
|
+
readonly $: "Fail";
|
|
38
|
+
readonly error: E;
|
|
39
|
+
};
|
|
40
|
+
type Result<T, E> = Done<T> | Fail<E>;
|
|
41
|
+
declare const Result: {
|
|
42
|
+
done: <T>(value: T) => Done<T>;
|
|
43
|
+
fail: <E>(error: E) => Fail<E>;
|
|
44
|
+
new: <T, E = unknown>(value: T, error: E) => Result<T, E>;
|
|
45
|
+
isDone: <T, E>(result: Result<T, E>) => result is Done<T>;
|
|
46
|
+
isFail: <T, E>(result: Result<T, E>) => result is Fail<E>;
|
|
47
|
+
fromNullable: <T, E = unknown>(value: T | null | undefined, error: E) => Result<T, E>;
|
|
48
|
+
fromThrowable: <T, E = unknown>(fn: () => T, onError: (e: unknown) => E) => Result<T, E>;
|
|
49
|
+
fromPromise: <T, E = unknown>(promise: Promise<T>, onError: (e: unknown) => E) => Promise<Result<T, E>>;
|
|
50
|
+
map: <T, U, E>(result: Result<T, E>, fn: (value: T) => U) => Result<U, E>;
|
|
51
|
+
mapError: <T, E, F>(result: Result<T, E>, fn: (error: E) => F) => Result<T, F>;
|
|
52
|
+
flatMap: <T, U, E>(result: Result<T, E>, fn: (value: T) => Result<U, E>) => Result<U, E>;
|
|
53
|
+
match: <T, E, U>(result: Result<T, E>, matcher: {
|
|
54
|
+
done: (value: T) => U;
|
|
55
|
+
fail: (error: E) => U;
|
|
56
|
+
}) => U;
|
|
57
|
+
recover: <T, E>(result: Result<T, E>, fn: (error: E) => T) => Result<T, never>;
|
|
58
|
+
getOrElse: <T, E>(result: Result<T, E>, defaultValue: T) => T;
|
|
59
|
+
getOrThrow: <T, E>(result: Result<T, E>, onError: (error: E) => never) => T;
|
|
60
|
+
zip: <A, B, E>(a: Result<A, E>, b: Result<B, E>) => Result<Pair<A, B>, E>;
|
|
61
|
+
apply: <T, U, E>(fn: Result<(value: T) => U, E>, arg: Result<T, E>) => Result<U, E>;
|
|
62
|
+
toOption: <T, E>(result: Result<T, E>) => Option<T>;
|
|
63
|
+
};
|
|
64
|
+
declare const done: <T>(value: T) => Done<T>;
|
|
65
|
+
declare const fail: <E>(error: E) => Fail<E>;
|
|
66
|
+
declare global {
|
|
67
|
+
interface Promise<T> {
|
|
68
|
+
toResult<E = unknown>(onError?: (e: unknown) => E): Promise<Result<T, E>>;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
type None = {
|
|
73
|
+
readonly $: "None";
|
|
74
|
+
};
|
|
75
|
+
type Some<T> = {
|
|
76
|
+
readonly $: "Some";
|
|
77
|
+
readonly value: T;
|
|
78
|
+
};
|
|
79
|
+
type Option<T> = None | Some<T>;
|
|
80
|
+
declare const Option: {
|
|
81
|
+
none: <T>() => Option<T>;
|
|
82
|
+
some: <T>(value: T) => Option<T>;
|
|
83
|
+
new: <T>(value: T) => Option<T>;
|
|
84
|
+
isSome: <T>(option: Option<T>) => option is Some<T>;
|
|
85
|
+
isNone: <T>(option: Option<T>) => option is None;
|
|
86
|
+
fromNullable: <T>(value: T | null | undefined) => Option<T>;
|
|
87
|
+
fromPredicate: <T>(value: T, predicate: (value: T) => boolean) => Option<T>;
|
|
88
|
+
fromThrowable: <T>(fn: () => T) => Option<T>;
|
|
89
|
+
fromPromise: <T>(promise: Promise<T>) => Promise<Option<T>>;
|
|
90
|
+
map: <T, U>(option: Option<T>, fn: (value: T) => U) => Option<U>;
|
|
91
|
+
flatMap: <T, U>(option: Option<T>, fn: (value: T) => Option<U>) => Option<U>;
|
|
92
|
+
filter: <T>(option: Option<T>, predicate: (value: T) => boolean) => Option<T>;
|
|
93
|
+
match: <T, U>(option: Option<T>, matcher: {
|
|
94
|
+
some: (value: T) => U;
|
|
95
|
+
none: () => U;
|
|
96
|
+
}) => U;
|
|
97
|
+
getOrElse: <T>(option: Option<T>, defaultValue: T) => T;
|
|
98
|
+
getOrUndefined: <T>(option: Option<T>) => T | undefined;
|
|
99
|
+
getOrThrow: <T>(option: Option<T>, error: Error) => T;
|
|
100
|
+
zip: <T, U>(a: Option<T>, b: Option<U>) => Option<[T, U]>;
|
|
101
|
+
apply: <T, U>(fn: Option<(value: T) => U>, opt: Option<T>) => Option<U>;
|
|
102
|
+
orElse: <T>(opt: Option<T>, other: Option<T>) => Option<T>;
|
|
103
|
+
toResult: <T, E>(option: Option<T>, error: E) => Result<T, E>;
|
|
104
|
+
};
|
|
105
|
+
declare const none: <T>() => Option<T>;
|
|
106
|
+
declare const some: <T>(value: T) => Option<T>;
|
|
107
|
+
declare global {
|
|
108
|
+
interface Array<T> {
|
|
109
|
+
firstOption(): Option<T>;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
type Left<A> = {
|
|
114
|
+
readonly $: "Left";
|
|
115
|
+
readonly value: A;
|
|
116
|
+
};
|
|
117
|
+
type Right<B> = {
|
|
118
|
+
readonly $: "Right";
|
|
119
|
+
readonly value: B;
|
|
120
|
+
};
|
|
121
|
+
type Either<A, B> = Left<A> | Right<B>;
|
|
122
|
+
declare const Either: {
|
|
123
|
+
left: <A>(value: A) => Left<A>;
|
|
124
|
+
right: <B>(value: B) => Right<B>;
|
|
125
|
+
new: <A, B>(rgt: B, lft: A) => Either<A, B>;
|
|
126
|
+
isLeft: <A, B>(e: Either<A, B>) => e is Left<A>;
|
|
127
|
+
isRight: <A, B>(e: Either<A, B>) => e is Right<B>;
|
|
128
|
+
fromNullable: <A, B>(value: B | null | undefined, error: A) => Either<A, B>;
|
|
129
|
+
fromThrowable: <A, B>(fn: () => B, onError: (e: unknown) => A) => Either<A, B>;
|
|
130
|
+
fromPromise: <A, B>(promise: Promise<B>, onError: (e: unknown) => A) => Promise<Either<A, B>>;
|
|
131
|
+
map: <A, B, C>(e: Either<A, B>, fn: (r: B) => C) => Either<A, C>;
|
|
132
|
+
mapLeft: <A, B, C>(e: Either<A, B>, fn: (l: A) => C) => Either<C, B>;
|
|
133
|
+
bimap: <A, B, C, D>(e: Either<A, B>, fl: (l: A) => C, fr: (r: B) => D) => Either<C, D>;
|
|
134
|
+
flatMap: <A, B, C>(e: Either<A, B>, fn: (value: B) => Either<A, C>) => Either<A, C>;
|
|
135
|
+
chain: <A, B, C>(e: Either<A, B>, fn: (r: B) => Either<A, C>) => Either<A, C>;
|
|
136
|
+
fold: <A, B, C>(e: Either<A, B>, onLeft: (l: A) => C, onRight: (r: B) => C) => C;
|
|
137
|
+
match: <A, B, C>(e: Either<A, B>, matcher: {
|
|
138
|
+
right: (value: B) => C;
|
|
139
|
+
left: (value: A) => C;
|
|
140
|
+
}) => C;
|
|
141
|
+
swap: <A, B>(e: Either<A, B>) => Either<B, A>;
|
|
142
|
+
getOrElse: <A, B>(e: Either<A, B>, defaultValue: B) => B;
|
|
143
|
+
zip: <E, A, B>(a: Either<E, A>, b: Either<E, B>) => Either<E, Pair<A, B>>;
|
|
144
|
+
apply: <E, A, B>(fn: Either<E, (value: A) => B>, arg: Either<E, A>) => Either<E, B>;
|
|
145
|
+
tap: <A, B>(e: Either<A, B>, f: (r: B) => void) => Either<A, B>;
|
|
146
|
+
tapLeft: <A, B>(e: Either<A, B>, f: (l: A) => void) => Either<A, B>;
|
|
147
|
+
};
|
|
148
|
+
declare global {
|
|
149
|
+
interface Promise<T> {
|
|
150
|
+
toEither<A>(onError: (e: unknown) => A): Promise<Either<A, T>>;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
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
|
+
|
|
177
|
+
export { type Done, Either, type Fail, type Left, type None, Option, Pair, Result, type Right, type Some, Triple, done, fail, left, none, pair, right, some, triple };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
type Pair<A, B> = readonly [A, B];
|
|
2
|
+
declare const Pair: {
|
|
3
|
+
fst: <A, B>(p: Pair<A, B>) => A;
|
|
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;
|
|
35
|
+
};
|
|
36
|
+
type Fail<E> = {
|
|
37
|
+
readonly $: "Fail";
|
|
38
|
+
readonly error: E;
|
|
39
|
+
};
|
|
40
|
+
type Result<T, E> = Done<T> | Fail<E>;
|
|
41
|
+
declare const Result: {
|
|
42
|
+
done: <T>(value: T) => Done<T>;
|
|
43
|
+
fail: <E>(error: E) => Fail<E>;
|
|
44
|
+
new: <T, E = unknown>(value: T, error: E) => Result<T, E>;
|
|
45
|
+
isDone: <T, E>(result: Result<T, E>) => result is Done<T>;
|
|
46
|
+
isFail: <T, E>(result: Result<T, E>) => result is Fail<E>;
|
|
47
|
+
fromNullable: <T, E = unknown>(value: T | null | undefined, error: E) => Result<T, E>;
|
|
48
|
+
fromThrowable: <T, E = unknown>(fn: () => T, onError: (e: unknown) => E) => Result<T, E>;
|
|
49
|
+
fromPromise: <T, E = unknown>(promise: Promise<T>, onError: (e: unknown) => E) => Promise<Result<T, E>>;
|
|
50
|
+
map: <T, U, E>(result: Result<T, E>, fn: (value: T) => U) => Result<U, E>;
|
|
51
|
+
mapError: <T, E, F>(result: Result<T, E>, fn: (error: E) => F) => Result<T, F>;
|
|
52
|
+
flatMap: <T, U, E>(result: Result<T, E>, fn: (value: T) => Result<U, E>) => Result<U, E>;
|
|
53
|
+
match: <T, E, U>(result: Result<T, E>, matcher: {
|
|
54
|
+
done: (value: T) => U;
|
|
55
|
+
fail: (error: E) => U;
|
|
56
|
+
}) => U;
|
|
57
|
+
recover: <T, E>(result: Result<T, E>, fn: (error: E) => T) => Result<T, never>;
|
|
58
|
+
getOrElse: <T, E>(result: Result<T, E>, defaultValue: T) => T;
|
|
59
|
+
getOrThrow: <T, E>(result: Result<T, E>, onError: (error: E) => never) => T;
|
|
60
|
+
zip: <A, B, E>(a: Result<A, E>, b: Result<B, E>) => Result<Pair<A, B>, E>;
|
|
61
|
+
apply: <T, U, E>(fn: Result<(value: T) => U, E>, arg: Result<T, E>) => Result<U, E>;
|
|
62
|
+
toOption: <T, E>(result: Result<T, E>) => Option<T>;
|
|
63
|
+
};
|
|
64
|
+
declare const done: <T>(value: T) => Done<T>;
|
|
65
|
+
declare const fail: <E>(error: E) => Fail<E>;
|
|
66
|
+
declare global {
|
|
67
|
+
interface Promise<T> {
|
|
68
|
+
toResult<E = unknown>(onError?: (e: unknown) => E): Promise<Result<T, E>>;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
type None = {
|
|
73
|
+
readonly $: "None";
|
|
74
|
+
};
|
|
75
|
+
type Some<T> = {
|
|
76
|
+
readonly $: "Some";
|
|
77
|
+
readonly value: T;
|
|
78
|
+
};
|
|
79
|
+
type Option<T> = None | Some<T>;
|
|
80
|
+
declare const Option: {
|
|
81
|
+
none: <T>() => Option<T>;
|
|
82
|
+
some: <T>(value: T) => Option<T>;
|
|
83
|
+
new: <T>(value: T) => Option<T>;
|
|
84
|
+
isSome: <T>(option: Option<T>) => option is Some<T>;
|
|
85
|
+
isNone: <T>(option: Option<T>) => option is None;
|
|
86
|
+
fromNullable: <T>(value: T | null | undefined) => Option<T>;
|
|
87
|
+
fromPredicate: <T>(value: T, predicate: (value: T) => boolean) => Option<T>;
|
|
88
|
+
fromThrowable: <T>(fn: () => T) => Option<T>;
|
|
89
|
+
fromPromise: <T>(promise: Promise<T>) => Promise<Option<T>>;
|
|
90
|
+
map: <T, U>(option: Option<T>, fn: (value: T) => U) => Option<U>;
|
|
91
|
+
flatMap: <T, U>(option: Option<T>, fn: (value: T) => Option<U>) => Option<U>;
|
|
92
|
+
filter: <T>(option: Option<T>, predicate: (value: T) => boolean) => Option<T>;
|
|
93
|
+
match: <T, U>(option: Option<T>, matcher: {
|
|
94
|
+
some: (value: T) => U;
|
|
95
|
+
none: () => U;
|
|
96
|
+
}) => U;
|
|
97
|
+
getOrElse: <T>(option: Option<T>, defaultValue: T) => T;
|
|
98
|
+
getOrUndefined: <T>(option: Option<T>) => T | undefined;
|
|
99
|
+
getOrThrow: <T>(option: Option<T>, error: Error) => T;
|
|
100
|
+
zip: <T, U>(a: Option<T>, b: Option<U>) => Option<[T, U]>;
|
|
101
|
+
apply: <T, U>(fn: Option<(value: T) => U>, opt: Option<T>) => Option<U>;
|
|
102
|
+
orElse: <T>(opt: Option<T>, other: Option<T>) => Option<T>;
|
|
103
|
+
toResult: <T, E>(option: Option<T>, error: E) => Result<T, E>;
|
|
104
|
+
};
|
|
105
|
+
declare const none: <T>() => Option<T>;
|
|
106
|
+
declare const some: <T>(value: T) => Option<T>;
|
|
107
|
+
declare global {
|
|
108
|
+
interface Array<T> {
|
|
109
|
+
firstOption(): Option<T>;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
type Left<A> = {
|
|
114
|
+
readonly $: "Left";
|
|
115
|
+
readonly value: A;
|
|
116
|
+
};
|
|
117
|
+
type Right<B> = {
|
|
118
|
+
readonly $: "Right";
|
|
119
|
+
readonly value: B;
|
|
120
|
+
};
|
|
121
|
+
type Either<A, B> = Left<A> | Right<B>;
|
|
122
|
+
declare const Either: {
|
|
123
|
+
left: <A>(value: A) => Left<A>;
|
|
124
|
+
right: <B>(value: B) => Right<B>;
|
|
125
|
+
new: <A, B>(rgt: B, lft: A) => Either<A, B>;
|
|
126
|
+
isLeft: <A, B>(e: Either<A, B>) => e is Left<A>;
|
|
127
|
+
isRight: <A, B>(e: Either<A, B>) => e is Right<B>;
|
|
128
|
+
fromNullable: <A, B>(value: B | null | undefined, error: A) => Either<A, B>;
|
|
129
|
+
fromThrowable: <A, B>(fn: () => B, onError: (e: unknown) => A) => Either<A, B>;
|
|
130
|
+
fromPromise: <A, B>(promise: Promise<B>, onError: (e: unknown) => A) => Promise<Either<A, B>>;
|
|
131
|
+
map: <A, B, C>(e: Either<A, B>, fn: (r: B) => C) => Either<A, C>;
|
|
132
|
+
mapLeft: <A, B, C>(e: Either<A, B>, fn: (l: A) => C) => Either<C, B>;
|
|
133
|
+
bimap: <A, B, C, D>(e: Either<A, B>, fl: (l: A) => C, fr: (r: B) => D) => Either<C, D>;
|
|
134
|
+
flatMap: <A, B, C>(e: Either<A, B>, fn: (value: B) => Either<A, C>) => Either<A, C>;
|
|
135
|
+
chain: <A, B, C>(e: Either<A, B>, fn: (r: B) => Either<A, C>) => Either<A, C>;
|
|
136
|
+
fold: <A, B, C>(e: Either<A, B>, onLeft: (l: A) => C, onRight: (r: B) => C) => C;
|
|
137
|
+
match: <A, B, C>(e: Either<A, B>, matcher: {
|
|
138
|
+
right: (value: B) => C;
|
|
139
|
+
left: (value: A) => C;
|
|
140
|
+
}) => C;
|
|
141
|
+
swap: <A, B>(e: Either<A, B>) => Either<B, A>;
|
|
142
|
+
getOrElse: <A, B>(e: Either<A, B>, defaultValue: B) => B;
|
|
143
|
+
zip: <E, A, B>(a: Either<E, A>, b: Either<E, B>) => Either<E, Pair<A, B>>;
|
|
144
|
+
apply: <E, A, B>(fn: Either<E, (value: A) => B>, arg: Either<E, A>) => Either<E, B>;
|
|
145
|
+
tap: <A, B>(e: Either<A, B>, f: (r: B) => void) => Either<A, B>;
|
|
146
|
+
tapLeft: <A, B>(e: Either<A, B>, f: (l: A) => void) => Either<A, B>;
|
|
147
|
+
};
|
|
148
|
+
declare global {
|
|
149
|
+
interface Promise<T> {
|
|
150
|
+
toEither<A>(onError: (e: unknown) => A): Promise<Either<A, T>>;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
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
|
+
|
|
177
|
+
export { type Done, Either, type Fail, type Left, type None, Option, Pair, Result, type Right, type Some, Triple, done, fail, left, none, pair, right, some, triple };
|