lite-fp 0.1.0 → 0.4.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 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`, `Triple`
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.fromNullable("hello"); // Some("hello")
56
- const o2 = Option.fromNullable(null); // None
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
- // Convert
63
- import { done, fail } from "lite-fp";
64
- const asResult = Option.toResult(o1, new Error("missing")); // Done("hello")
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.fromNullable("data", "nope"); // Done("data")
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
- ### Pair / Triple
124
+
125
+ ### Pair
114
126
 
115
127
  ```ts
116
- import { Pair, Triple, pair, triple } from "lite-fp";
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
- - Pair / Triple
156
- - `pair`, `triple`, and utilities to map, zip, convert to/from arrays/objects.
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/package.json CHANGED
@@ -1,43 +1,49 @@
1
1
  {
2
2
  "name": "lite-fp",
3
- "version": "0.1.0",
3
+ "version": "0.4.0",
4
4
  "description": "Functional programming utilities.",
5
5
  "license": "MIT",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.mjs",
8
- "types": "./dist/index.d.ts",
9
- "bugs": {
10
- "url": "https://github.com/naoeosavio/lite-fp/issues"
11
- },
12
- "repository": {
13
- "type": "git",
14
- "url": "https://github.com/naoeosavio/lite-fp.git"
15
- },
16
- "author": "it is not savio",
17
8
  "exports": {
18
9
  ".": {
19
- "types": "./dist/index.d.ts",
20
10
  "import": "./dist/index.mjs",
21
11
  "require": "./dist/index.js"
22
12
  },
23
13
  "./package.json": "./package.json"
24
14
  },
25
- "sideEffects": false,
26
- "files": [
27
- "dist"
15
+ "bugs": {
16
+ "url": "https://github.com/naoeosavio/lite-fp/issues"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/naoeosavio/lite-fp.git"
21
+ },
22
+ "author": "it is not savio",
23
+ "keywords": [
24
+ "functional-programming",
25
+ "fp",
26
+ "typescript",
27
+ "either",
28
+ "option",
29
+ "maybe",
30
+ "result",
31
+ "adt",
32
+ "algebraic-data-types",
33
+ "utilities"
28
34
  ],
29
35
  "scripts": {
30
- "build": "tsup",
31
36
  "dev": "tsup --watch",
32
37
  "lint": "tsc",
33
- "format": "prettier --write .",
34
- "format:check": "prettier --check .",
35
- "ci": "npm run build && npm run lint && npm run format:check",
36
- "prepublishOnly": "npm run build"
38
+ "build": "tsup",
39
+ "format": "biome check --write src",
40
+ "check": "biome check src",
41
+ "ci": "npm run lint && npm run check && npm run build",
42
+ "prepublishOnly": "npm run ci"
37
43
  },
38
44
  "devDependencies": {
39
- "prettier": "^3.3.3",
40
- "tsup": "^8.0.1",
45
+ "@biomejs/biome": "2.2.6",
46
+ "tsup": "8.5.1",
41
47
  "typescript": "^5.4.5"
42
48
  }
43
49
  }
package/dist/index.d.mts DELETED
@@ -1,177 +0,0 @@
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 DELETED
@@ -1,177 +0,0 @@
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 };