lite-fp 0.5.0 → 0.7.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
@@ -121,7 +121,57 @@ const r2 = await Result.fromPromise(
121
121
  const safe = Result.recover(r2, () => "default"); // Done("default")
122
122
  ```
123
123
 
124
-
124
+ ### Callback-based conversions
125
+
126
+ For callback-driven code — `fromPromiseCallback` fires **once** and is gone (like `setTimeout`,
127
+ not `setInterval` or `EventEmitter.on`). After the Promise settles, the callback executes and
128
+ becomes eligible for garbage collection — no cleanup, no leak.
129
+
130
+ ```ts
131
+ import { Either, fromPromiseCallback, flatMapCallback, match } from "lite-fp";
132
+
133
+ // Promise → Either delivered via callback (fires exactly once)
134
+ fromPromiseCallback(
135
+ fetch("/api/user").then(r => r.json()),
136
+ e => new Error(String(e)),
137
+ result => {
138
+ // result is Either<Error, User> — callback is done after this tick
139
+ flatMapCallback(result, user => fetchPosts(user.id), postsResult => {
140
+ match(postsResult, {
141
+ right: posts => render(posts),
142
+ left: err => showError(err),
143
+ });
144
+ });
145
+ },
146
+ );
147
+ ```
148
+
149
+ Contrast with persistent subscriptions (`setInterval`, `emitter.on`, `addEventListener`,
150
+ `subscribe`) where the callback stays registered until explicitly removed.
151
+
152
+ ### Collection operations
153
+
154
+ `all`/`collect`, `flatten`, and `partition` work on arrays of `Result` or `Either`:
155
+
156
+ ```ts
157
+ import { Result, Either } from "lite-fp";
158
+
159
+ // all — fail-fast: returns the first Fail/Left, or Done/Right with all values
160
+ const ok = Result.all([Result.done(1), Result.done(2)]); // Done([1, 2])
161
+ const err = Result.all([Result.done(1), Result.fail("nope")]); // Fail("nope")
162
+
163
+ // partition — split into done/fail (or right/left) arrays
164
+ const { done, fail } = Result.partition([
165
+ Result.done(1), Result.fail("a"), Result.done(2), Result.fail("b"),
166
+ ]); // done = [1, 2], fail = ["a", "b"]
167
+
168
+ // flatten — unwrap nested Result/Either
169
+ const nested = Result.done(Result.done(42));
170
+ Result.flatten(nested); // Done(42)
171
+ ```
172
+
173
+ Also available: `Either.all`, `Either.collect`, `Either.flatten`, `Either.partition`.
174
+
125
175
  ### Pair
126
176
 
127
177
  ```ts
@@ -145,14 +195,23 @@ const p2 = Pair.map(
145
195
  - Combine: `zip`, `apply`, `orElse`
146
196
 
147
197
  - Either
148
- - Constructors: `left`, `right`, `new`, `fromNullable`, `fromThrowable`, `fromPromise`
198
+ - Constructors: `left`, `right`, `new`, `fromNullable`, `fromThrowable`, `fromPromise`, `fromPromiseSettled`, `fromPromiseCallback`
149
199
  - Type guards: `isLeft`, `isRight`
150
- - Ops: `map`, `mapLeft`, `bimap`, `flatMap`, `chain`, `fold`, `match`, `swap`, `getOrElse`, `zip`, `apply`, `tap`, `tapLeft`
200
+ - Ops: `map`, `mapLeft`, `bimap`, `flatMap`, `chain`, `fold`, `match`, `swap`, `recover`, `getOrElse`, `getOrNull`, `getOrUndefined`, `getOrThrow`
201
+ - Combine: `zip`, `apply`, `orElse`, `tap`, `tapLeft`
202
+ - Collection: `all`, `collect`, `flatten`, `partition`
203
+ - Callback: `flatMapCallback`
204
+ - Conversions: `toPromise`
151
205
 
152
206
  - Result
153
- - Constructors: `done`, `fail`, `new`, `fromNullable`, `fromThrowable`, `fromPromise`
154
- - Type guards: `isDone`, `isFail`
155
- - Ops: `map`, `mapError`, `flatMap`, `match`, `recover`, `getOrElse`, `getOrThrow`, `zip`, `apply`
207
+ - Constructors: `done`, `fail`, `Ok`, `Err`, `new`, `fromNullable`, `fromThrowable`, `fromPromise`, `fromPromiseSettled`, `fromPromiseCallback`
208
+ - Type guards: `isDone`, `isFail`, `isOk`, `isErr`
209
+ - Ops: `map`, `mapFail`, `mapErr`, `bimap`, `flatMap`, `chain`, `fold`, `match`, `filter`, `recover`, `swap`
210
+ - Extract: `val`, `err`, `getOrElse`, `getOrUndefined`, `getOrNull`, `getOrThrow`
211
+ - Combine: `zip`, `apply`, `orElse`, `tap`, `tapFail`, `tapErr`
212
+ - Collection: `all`, `collect`, `flatten`, `partition`
213
+ - Callback: `flatMapCallback`
214
+ - Conversions: `toPromise`
156
215
 
157
216
  - Maybe (T | null | undefined)
158
217
  - Constructors: `new`, `just`, `nothing`/`nothingNull`/`nothingUndefined`, `fromNullable`, `fromPredicate`, `fromThrowable`, `fromPromise`
package/dist/index.d.mts CHANGED
@@ -1,45 +1,60 @@
1
- type Left<A> = {
1
+ interface Left<A> {
2
2
  readonly $: "Left";
3
3
  readonly value: A;
4
- };
5
- type Right<B> = {
4
+ }
5
+ interface Right<B> {
6
6
  readonly $: "Right";
7
7
  readonly value: B;
8
- };
8
+ }
9
9
  declare const left: <A>(value: A) => Left<A>;
10
10
  declare const right: <B>(value: B) => Right<B>;
11
11
  declare const isLeft: <A, B>(e: Either<A, B>) => e is Left<A>;
12
12
  declare const isRight: <A, B>(e: Either<A, B>) => e is Right<B>;
13
- declare const getLeft: <A>(e: Left<A>) => A;
14
- declare const getRight: <B>(e: Right<B>) => B;
13
+ declare const mapLeft: <A, B, C>(e: Either<A, B>, fn: (l: A) => C) => Either<C, B>;
14
+ declare const lft: <A>(e: Left<A>) => A;
15
+ declare const rgt: <B>(e: Right<B>) => B;
16
+ declare const tapLeft: <A, B>(e: Either<A, B>, f: (l: A) => void) => Either<A, B>;
15
17
  type Either<A, B> = Left<A> | Right<B>;
16
18
  declare const Either: {
19
+ new: <A, B>(value: B | null | undefined, error: A) => Either<A, B>;
17
20
  left: <A>(value: A) => Left<A>;
18
21
  right: <B>(value: B) => Right<B>;
19
- new: <A, B>(value: B | null | undefined, error: A) => Either<A, B>;
20
22
  isLeft: <A, B>(e: Either<A, B>) => e is Left<A>;
21
23
  isRight: <A, B>(e: Either<A, B>) => e is Right<B>;
22
24
  fromNullable: <A, B>(value: B | null | undefined, error: A) => Either<A, B>;
23
25
  fromThrowable: <A, B>(fn: () => B, onError: (e: unknown) => A) => Either<A, B>;
24
26
  fromPromise: <A, B>(promise: Promise<B>, onError: (e: unknown) => A) => Promise<Either<A, B>>;
27
+ fromPromiseCallback: <A, B>(promise: Promise<B>, onError: (e: unknown) => A, callback: (result: Either<A, B>) => void) => void;
28
+ toPromise: <A, B>(e: Either<A, B>) => Promise<B>;
25
29
  map: <A, B, C>(e: Either<A, B>, fn: (r: B) => C) => Either<A, C>;
26
30
  mapLeft: <A, B, C>(e: Either<A, B>, fn: (l: A) => C) => Either<C, B>;
27
31
  bimap: <A, B, C, D>(e: Either<A, B>, fl: (l: A) => C, fr: (r: B) => D) => Either<C, D>;
28
32
  flatMap: <A, B, C>(e: Either<A, B>, fn: (value: B) => Either<A, C>) => Either<A, C>;
29
- chain: <A, B, C>(e: Either<A, B>, fn: (r: B) => Either<A, C>) => Either<A, C>;
33
+ filter: <A, B>(e: Either<A, B>, predicate: (value: B) => boolean, onFalse: A) => Either<A, B>;
34
+ chain: <A, B, C>(e: Either<A, B>, fn: (value: B) => Either<A, C>) => Either<A, C>;
30
35
  fold: <A, B, C>(e: Either<A, B>, onLeft: (l: A) => C, onRight: (r: B) => C) => C;
31
36
  match: <A, B, C>(e: Either<A, B>, matcher: {
32
37
  right: (value: B) => C;
33
38
  left: (value: A) => C;
34
39
  }) => C;
35
40
  swap: <A, B>(e: Either<A, B>) => Either<B, A>;
36
- getLeft: <A>(e: Left<A>) => A;
37
- getRight: <B>(e: Right<B>) => B;
41
+ flatMapCallback: <A, B, C>(e: Either<A, B>, fn: (value: B) => Promise<Either<A, C>>, callback: (result: Either<A, C>) => void) => void;
42
+ lft: <A>(e: Left<A>) => A;
43
+ rgt: <B>(e: Right<B>) => B;
38
44
  getOrElse: <A, B>(e: Either<A, B>, defaultValue: B) => B;
45
+ getOrNull: <A, B>(e: Either<A, B>) => B | null;
39
46
  getOrUndefined: <A, B>(e: Either<A, B>) => B | undefined;
40
47
  getOrThrow: <A, B>(e: Either<A, B>) => B;
48
+ all: <A, B>(eithers: Either<A, B>[]) => Either<A, B[]>;
49
+ collect: <A, B>(eithers: Either<A, B>[]) => Either<A, B[]>;
50
+ flatten: <A, B>(e: Either<A, Either<A, B>>) => Either<A, B>;
51
+ partition: <A, B>(eithers: Either<A, B>[]) => {
52
+ right: B[];
53
+ left: A[];
54
+ };
41
55
  zip: <E, A, B>(a: Either<E, A>, b: Either<E, B>) => Either<E, [A, B]>;
42
56
  apply: <E, A, B>(fn: Either<E, (value: A) => B>, arg: Either<E, A>) => Either<E, B>;
57
+ orElse: <E, A, B>(a: Either<E, A>, b: Either<E, B>) => Either<E, A | B>;
43
58
  tap: <A, B>(e: Either<A, B>, f: (r: B) => void) => Either<A, B>;
44
59
  tapLeft: <A, B>(e: Either<A, B>, f: (l: A) => void) => Either<A, B>;
45
60
  };
@@ -49,52 +64,55 @@ declare global {
49
64
  }
50
65
  }
51
66
 
52
- type NothingNull = null;
53
- type NothingUndefined = undefined;
54
- type Nothing = NothingNull | NothingUndefined;
55
- declare const just: <T>(value: T) => Maybe<T>;
67
+ type Nothing = null | undefined;
68
+ declare const just: <T>(value: T) => NonNullable<T>;
56
69
  declare const nothing: () => Nothing;
70
+ declare const nothingNull: () => null;
71
+ declare const nothingUndefined: () => undefined;
57
72
  declare const isJust: <T>(m: Maybe<T>) => m is T;
58
73
  declare const isNothing: <T>(m: Maybe<T>) => m is Nothing;
74
+ declare const isNull: <T>(m: Maybe<T>) => m is null;
75
+ declare const isUndefined: <T>(m: Maybe<T>) => m is undefined;
59
76
  type Maybe<T> = T | Nothing;
60
77
  declare const Maybe: {
61
- just: <T>(value: T) => Maybe<T>;
62
- nothing: () => Nothing;
63
78
  new: <T>(m: T | null | undefined) => Maybe<T>;
64
- nothingNull: () => NothingNull;
65
- nothingUndefined: () => NothingUndefined;
79
+ just: <T>(value: T) => NonNullable<T>;
80
+ nothing: () => Nothing;
81
+ nothingNull: () => null;
82
+ nothingUndefined: () => undefined;
66
83
  isJust: <T>(m: Maybe<T>) => m is T;
67
84
  isNothing: <T>(m: Maybe<T>) => m is Nothing;
68
- isNothingNull: <T>(m: Maybe<T>) => m is NothingNull;
69
- isNothingUndefined: <T>(m: Maybe<T>) => m is NothingUndefined;
85
+ isNull: <T>(m: Maybe<T>) => m is null;
86
+ isUndefined: <T>(m: Maybe<T>) => m is undefined;
70
87
  fromNullable: <T>(m: T | null | undefined) => Maybe<T>;
71
88
  fromThrowable: <T>(fn: () => T) => Maybe<T>;
89
+ fromPredicate: <T>(value: T, predicate: (value: T) => boolean) => Maybe<T>;
72
90
  fromPromise: <T>(promise: Promise<T>) => Promise<Maybe<T>>;
73
- toNullable: <T>(m: Maybe<T>) => T | null;
74
- toUndefined: <T>(m: Maybe<T>) => T | undefined;
75
91
  map: <T, U>(m: Maybe<T>, fn: (v: T) => U) => Maybe<U>;
76
92
  flatMap: <T, U>(m: Maybe<T>, fn: (v: T) => Maybe<U>) => Maybe<U>;
77
93
  filter: <T>(m: Maybe<T>, predicate: (value: T) => boolean) => Maybe<T>;
94
+ fold: <T, U>(m: Maybe<T>, onNothing: () => U, onJust: (v: T) => U) => U;
78
95
  match: <T, U>(m: Maybe<T>, matcher: {
79
96
  some: (v: T) => U;
80
97
  nothing: () => U;
81
98
  }) => U;
82
- fold: <T, U>(m: Maybe<T>, onNothing: () => U, onJust: (v: T) => U) => U;
83
- getOrElse: <T>(m: Maybe<T>, d: T) => T;
99
+ getOrElse: <T>(m: Maybe<T>, defaultValue: T) => T;
84
100
  getOrUndefined: <T>(m: Maybe<T>) => T | undefined;
101
+ getOrNull: <T>(m: Maybe<T>) => T | null;
85
102
  getOrThrow: <T>(m: Maybe<T>) => T;
86
103
  zip: <T, U>(a: Maybe<T>, b: Maybe<U>) => Maybe<[T, U]>;
87
104
  apply: <T, U>(fn: Maybe<(value: T) => U>, opt: Maybe<T>) => Maybe<U>;
88
105
  orElse: <T>(opt: Maybe<T>, other: Maybe<T>) => Maybe<T>;
106
+ tap: <T>(m: Maybe<T>, fn: (v: T) => void) => Maybe<T>;
89
107
  };
90
108
 
91
- type None = {
109
+ interface None {
92
110
  readonly $: "None";
93
- };
94
- type Some<T> = {
111
+ }
112
+ interface Some<T> {
95
113
  readonly $: "Some";
96
114
  readonly value: T;
97
- };
115
+ }
98
116
  declare const none: <T>() => Option<T>;
99
117
  declare const some: <T>(value: T) => Option<T>;
100
118
  declare const isSome: <T>(option: Option<T>) => option is Some<T>;
@@ -102,9 +120,9 @@ declare const isNone: <T>(option: Option<T>) => option is None;
102
120
  declare const unwrap: <T>(option: Some<T>) => T;
103
121
  type Option<T> = None | Some<T>;
104
122
  declare const Option: {
123
+ new: <T>(value: T | null | undefined) => Option<T>;
105
124
  none: <T>() => Option<T>;
106
125
  some: <T>(value: T) => Option<T>;
107
- new: <T>(value: T | null | undefined) => Option<T>;
108
126
  isSome: <T>(option: Option<T>) => option is Some<T>;
109
127
  isNone: <T>(option: Option<T>) => option is None;
110
128
  fromNullable: <T>(value: T | null | undefined) => Option<T>;
@@ -114,6 +132,7 @@ declare const Option: {
114
132
  map: <T, U>(option: Option<T>, fn: (value: T) => U) => Option<U>;
115
133
  flatMap: <T, U>(option: Option<T>, fn: (value: T) => Option<U>) => Option<U>;
116
134
  filter: <T>(option: Option<T>, predicate: (value: T) => boolean) => Option<T>;
135
+ fold: <T, U>(option: Option<T>, onNone: () => U, onSome: (value: T) => U) => U;
117
136
  match: <T, U>(option: Option<T>, matcher: {
118
137
  some: (value: T) => U;
119
138
  none: () => U;
@@ -121,10 +140,12 @@ declare const Option: {
121
140
  unwrap: <T>(option: Some<T>) => T;
122
141
  getOrElse: <T>(option: Option<T>, defaultValue: T) => T;
123
142
  getOrUndefined: <T>(option: Option<T>) => T | undefined;
143
+ getOrNull: <T>(option: Option<T>) => T | null;
124
144
  getOrThrow: <T>(option: Option<T>) => T;
125
145
  zip: <T, U>(a: Option<T>, b: Option<U>) => Option<[T, U]>;
126
146
  apply: <T, U>(fn: Option<(value: T) => U>, opt: Option<T>) => Option<U>;
127
147
  orElse: <T>(opt: Option<T>, other: Option<T>) => Option<T>;
148
+ tap: <T>(option: Option<T>, f: (value: T) => void) => Option<T>;
128
149
  };
129
150
  declare global {
130
151
  interface Array<T> {
@@ -135,62 +156,79 @@ declare global {
135
156
  }
136
157
  }
137
158
 
159
+ declare const pair: <A, B>(a: A, b: B) => Pair<A, B>;
160
+ declare const curry: <A>(a: A) => <B>(b: B) => Pair<A, B>;
161
+ declare const fromArray: <A, B>([a, b]: [A, B]) => Pair<A, B>;
162
+ declare const fromObject: <A, B>(obj: {
163
+ fst: A;
164
+ snd: B;
165
+ }) => Pair<A, B>;
166
+ declare const toArray: <A, B>(p: Pair<A, B>) => [A, B];
167
+ declare const toObject: <A, B>(p: Pair<A, B>) => {
168
+ fst: A;
169
+ snd: B;
170
+ };
171
+ declare const mapFirst: <A, B, C>(p: Pair<A, B>, fn: (a: A) => C) => Pair<C, B>;
172
+ declare const mapSecond: <A, B, C>(p: Pair<A, B>, fn: (b: B) => C) => Pair<A, C>;
173
+ declare const eq: <A, B>(p1: Pair<A, B>, p2: Pair<A, B>) => boolean;
174
+ declare const eql: <A, B>(p1: Pair<A, B>, p2: Pair<A, B>, eqA: (a1: A, a2: A) => boolean, eqB: (b1: B, b2: B) => boolean) => boolean;
138
175
  declare const fst: <A, B>(p: Pair<A, B>) => A;
139
176
  declare const snd: <A, B>(p: Pair<A, B>) => B;
140
- declare const pair: <A, B>(a: A, b: B) => Pair<A, B>;
177
+ declare const app: <A, B, C>(p: Pair<(a: A) => B, C>, value: A) => Pair<B, C>;
141
178
  type Pair<A, B> = readonly [A, B];
142
179
  declare const Pair: {
143
- fst: <A, B>(p: Pair<A, B>) => A;
144
- snd: <A, B>(p: Pair<A, B>) => B;
145
180
  new: <A, B>(a: A, b: B) => Pair<A, B>;
181
+ curry: <A>(a: A) => <B>(b: B) => Pair<A, B>;
146
182
  fromArray: <A, B>([a, b]: [A, B]) => Pair<A, B>;
147
183
  fromObject: <A, B>(obj: {
148
184
  fst: A;
149
185
  snd: B;
150
186
  }) => Pair<A, B>;
151
- curry: <A>(a: A) => <B>(b: B) => Pair<A, B>;
152
- mapFirst: <A, B, C>(p: Pair<A, B>, fn: (a: A) => C) => Pair<C, B>;
153
- mapSecond: <A, B, C>(p: Pair<A, B>, fn: (b: B) => C) => Pair<A, C>;
154
- map: <A, B, C, D>(p: Pair<A, B>, fnA: (a: A) => C, fnB: (b: B) => D) => Pair<C, D>;
155
- swap: <A, B>(p: Pair<A, B>) => Pair<B, A>;
156
- apply: <A, B, C>(p: Pair<(a: A) => B, C>, value: A) => Pair<B, C>;
157
- apply2: <A, B, C>(fnPair: Pair<(a: A) => B, (b: B) => C>, vPair: Pair<A, B>) => Pair<A, C>;
158
- reduce: <A, B, C>(p: Pair<A, B>, fn: (a: A, b: B) => C) => C;
159
187
  toArray: <A, B>(p: Pair<A, B>) => [A, B];
160
188
  toObject: <A, B>(p: Pair<A, B>) => {
161
189
  fst: A;
162
190
  snd: B;
163
191
  };
192
+ mapFirst: <A, B, C>(p: Pair<A, B>, fn: (a: A) => C) => Pair<C, B>;
193
+ mapSecond: <A, B, C>(p: Pair<A, B>, fn: (b: B) => C) => Pair<A, C>;
194
+ map: <A, B, C, D>(p: Pair<A, B>, fnA: (a: A) => C, fnB: (b: B) => D) => Pair<C, D>;
195
+ swap: <A, B>(p: Pair<A, B>) => Pair<B, A>;
196
+ fold: <A, B, C>(p: Pair<A, B>, fn: (first: A, second: B) => C) => C;
197
+ match: <A, B, C>(p: Pair<A, B>, matcher: {
198
+ new: (first: A, second: B) => C;
199
+ }) => C;
164
200
  eq: <A, B>(p1: Pair<A, B>, p2: Pair<A, B>) => boolean;
165
- equals: <A, B>(p1: Pair<A, B>, p2: Pair<A, B>, eqA: (a1: A, a2: A) => boolean, eqB: (b1: B, b2: B) => boolean) => boolean;
201
+ eql: <A, B>(p1: Pair<A, B>, p2: Pair<A, B>, eqA: (a1: A, a2: A) => boolean, eqB: (b1: B, b2: B) => boolean) => boolean;
202
+ fst: <A, B>(p: Pair<A, B>) => A;
203
+ snd: <A, B>(p: Pair<A, B>) => B;
204
+ app: <A, B, C>(p: Pair<(a: A) => B, C>, value: A) => Pair<B, C>;
205
+ apply: <A, B, C>(fnPair: Pair<(a: A) => B, (b: B) => C>, vPair: Pair<A, B>) => Pair<A, C>;
166
206
  zip: <A, B, C, D>(p1: Pair<A, B>, p2: Pair<C, D>) => Pair<Pair<A, C>, Pair<B, D>>;
167
207
  };
168
208
 
169
- type Done<T> = {
209
+ interface Done<T> {
170
210
  readonly v: T;
171
- };
172
- type Fail<E> = {
211
+ }
212
+ interface Fail<E> {
173
213
  readonly e: E;
174
- };
175
- declare const done: <T>(v: T) => Done<T>;
176
- declare const fail: <E>(e: E) => Fail<E>;
177
- declare const Ok: <T>(v: T) => Done<T>;
178
- declare const Err: <E>(e: E) => Fail<E>;
214
+ }
215
+ declare const done: <T>(value: T) => Done<T>;
216
+ declare const fail: <E>(value: E) => Fail<E>;
217
+ declare const Ok: <T>(value: T) => Done<T>;
218
+ declare const Err: <E>(value: E) => Fail<E>;
179
219
  declare const isDone: <T, E>(r: Result<T, E>) => r is Done<T>;
180
220
  declare const isFail: <T, E>(r: Result<T, E>) => r is Fail<E>;
181
221
  declare const isOk: <T, E>(r: Result<T, E>) => r is Done<T>;
182
222
  declare const isErr: <T, E>(r: Result<T, E>) => r is Fail<E>;
223
+ declare const mapFail: <T, E, F>(r: Result<T, E>, fn: (b: E) => F) => Result<T, F>;
183
224
  declare const val: <T>(r: Done<T>) => T;
184
225
  declare const err: <E>(r: Fail<E>) => E;
226
+ declare const tapFail: <T, E>(r: Result<T, E>, f: (b: E) => void) => Result<T, E>;
185
227
  type Result<T, E> = Done<T> | Fail<E>;
186
228
  declare const Result: {
187
- done: <T>(v: T) => Done<T>;
188
- fail: <E>(e: E) => Fail<E>;
189
- Ok: <T>(v: T) => Done<T>;
190
- Err: <E>(e: E) => Fail<E>;
191
- val: <T>(r: Done<T>) => T;
192
- err: <E>(r: Fail<E>) => E;
193
229
  new: <T, E>(value: T | null | undefined, error: E) => Result<T, E>;
230
+ done: <T>(value: T) => Done<T>;
231
+ fail: <E>(value: E) => Fail<E>;
194
232
  isDone: <T, E>(r: Result<T, E>) => r is Done<T>;
195
233
  isFail: <T, E>(r: Result<T, E>) => r is Fail<E>;
196
234
  isOk: <T, E>(r: Result<T, E>) => r is Done<T>;
@@ -198,24 +236,40 @@ declare const Result: {
198
236
  fromNullable: <T, E>(value: T | null | undefined, error: E) => Result<T, E>;
199
237
  fromThrowable: <T, E>(fn: () => T, onError: (e: unknown) => E) => Result<T, E>;
200
238
  fromPromise: <T, E>(promise: Promise<T>, onError: (e: unknown) => E) => Promise<Result<T, E>>;
239
+ fromPromiseCallback: <T, E>(promise: Promise<T>, onError: (e: unknown) => E, callback: (result: Result<T, E>) => void) => void;
240
+ toPromise: <T, E>(r: Result<T, E>) => Promise<T>;
201
241
  map: <T, E, U>(r: Result<T, E>, fn: (a: T) => U) => Result<U, E>;
242
+ mapFail: <T, E, F>(r: Result<T, E>, fn: (b: E) => F) => Result<T, F>;
202
243
  mapErr: <T, E, F>(r: Result<T, E>, fn: (b: E) => F) => Result<T, F>;
203
244
  bimap: <T, E, U, F>(r: Result<T, E>, onDone: (a: T) => U, onFail: (b: E) => F) => Result<U, F>;
204
245
  flatMap: <T, E, C>(r: Result<T, E>, fn: (a: T) => Result<C, E>) => Result<C, E>;
246
+ filter: <T, E>(r: Result<T, E>, predicate: (a: T) => boolean, onFalse: E) => Result<T, E>;
205
247
  match: <T, E, U>(r: Result<T, E>, matcher: {
206
248
  done: (a: T) => U;
207
249
  fail: (b: E) => U;
208
250
  }) => U;
209
- fold: <T, E, U>(r: Result<T, E>, onFail: (error: E) => U, onDone: (value: T) => U) => U;
210
- recover: <T, E>(r: Result<T, E>, fn: (error: E) => T) => Result<T, E>;
211
- getOrElse: <T, E>(r: Result<T, E>, d: T) => T;
251
+ fold: <T, E, U>(r: Result<T, E>, onFail: (e: E) => U, onDone: (v: T) => U) => U;
252
+ chain: <T, E, C>(r: Result<T, E>, fn: (a: T) => Result<C, E>) => Result<C, E>;
253
+ recover: <T, E>(r: Result<T, E>, fn: (value: E) => T) => Result<T, E>;
254
+ flatMapCallback: <T, E, C>(r: Result<T, E>, fn: (a: T) => Promise<Result<C, E>>, callback: (result: Result<C, E>) => void) => void;
255
+ val: <T>(r: Done<T>) => T;
256
+ err: <E>(r: Fail<E>) => E;
257
+ getOrElse: <T, E>(r: Result<T, E>, defaultValue: T) => T;
212
258
  getOrUndefined: <T, E>(r: Result<T, E>) => T | undefined;
259
+ getOrNull: <T, E>(r: Result<T, E>) => T | null;
213
260
  getOrThrow: <T, E>(r: Result<T, E>) => T;
261
+ all: <T, E>(results: Result<T, E>[]) => Result<T[], E>;
262
+ collect: <T, E>(results: Result<T, E>[]) => Result<T[], E>;
263
+ flatten: <T, E>(r: Result<Result<T, E>, E>) => Result<T, E>;
264
+ partition: <T, E>(results: Result<T, E>[]) => {
265
+ done: T[];
266
+ fail: E[];
267
+ };
214
268
  zip: <T, U, E>(a: Result<T, E>, b: Result<U, E>) => Result<[T, U], E>;
215
269
  apply: <T, U, E>(fn: Result<(a: T) => U, E>, arg: Result<T, E>) => Result<U, E>;
216
270
  orElse: <T, E>(a: Result<T, E>, b: Result<T, E>) => Result<T, E>;
217
- filter: <T, E>(r: Result<T, E>, predicate: (a: T) => boolean, onFalse: E) => Result<T, E>;
218
271
  tap: <T, E>(r: Result<T, E>, f: (a: T) => void) => Result<T, E>;
272
+ tapFail: <T, E>(r: Result<T, E>, f: (b: E) => void) => Result<T, E>;
219
273
  tapErr: <T, E>(r: Result<T, E>, f: (b: E) => void) => Result<T, E>;
220
274
  };
221
275
  declare global {
@@ -224,4 +278,4 @@ declare global {
224
278
  }
225
279
  }
226
280
 
227
- export { type Done, Either, Err, type Fail, type Left, Maybe, type None, type Nothing, type NothingNull, type NothingUndefined, Ok, Option, Pair, Result, type Right, type Some, done, err, fail, fst, getLeft, getRight, isDone, isErr, isFail, isJust, isLeft, isNone, isNothing, isOk, isRight, isSome, just, left, none, nothing, pair, right, snd, some, unwrap, val };
281
+ export { type Done, Either, Err, type Fail, type Left, Maybe, type None, type Nothing, Ok, Option, Pair, Result, type Right, type Some, app, curry, done, eq, eql, err, fail, fromArray, fromObject, fst, isDone, isErr, isFail, isJust, isLeft, isNone, isNothing, isNull, isOk, isRight, isSome, isUndefined, just, left, lft, mapFail, mapFirst, mapLeft, mapSecond, none, nothing, nothingNull, nothingUndefined, pair, rgt, right, snd, some, tapFail, tapLeft, toArray, toObject, unwrap, val };
package/dist/index.d.ts CHANGED
@@ -1,45 +1,60 @@
1
- type Left<A> = {
1
+ interface Left<A> {
2
2
  readonly $: "Left";
3
3
  readonly value: A;
4
- };
5
- type Right<B> = {
4
+ }
5
+ interface Right<B> {
6
6
  readonly $: "Right";
7
7
  readonly value: B;
8
- };
8
+ }
9
9
  declare const left: <A>(value: A) => Left<A>;
10
10
  declare const right: <B>(value: B) => Right<B>;
11
11
  declare const isLeft: <A, B>(e: Either<A, B>) => e is Left<A>;
12
12
  declare const isRight: <A, B>(e: Either<A, B>) => e is Right<B>;
13
- declare const getLeft: <A>(e: Left<A>) => A;
14
- declare const getRight: <B>(e: Right<B>) => B;
13
+ declare const mapLeft: <A, B, C>(e: Either<A, B>, fn: (l: A) => C) => Either<C, B>;
14
+ declare const lft: <A>(e: Left<A>) => A;
15
+ declare const rgt: <B>(e: Right<B>) => B;
16
+ declare const tapLeft: <A, B>(e: Either<A, B>, f: (l: A) => void) => Either<A, B>;
15
17
  type Either<A, B> = Left<A> | Right<B>;
16
18
  declare const Either: {
19
+ new: <A, B>(value: B | null | undefined, error: A) => Either<A, B>;
17
20
  left: <A>(value: A) => Left<A>;
18
21
  right: <B>(value: B) => Right<B>;
19
- new: <A, B>(value: B | null | undefined, error: A) => Either<A, B>;
20
22
  isLeft: <A, B>(e: Either<A, B>) => e is Left<A>;
21
23
  isRight: <A, B>(e: Either<A, B>) => e is Right<B>;
22
24
  fromNullable: <A, B>(value: B | null | undefined, error: A) => Either<A, B>;
23
25
  fromThrowable: <A, B>(fn: () => B, onError: (e: unknown) => A) => Either<A, B>;
24
26
  fromPromise: <A, B>(promise: Promise<B>, onError: (e: unknown) => A) => Promise<Either<A, B>>;
27
+ fromPromiseCallback: <A, B>(promise: Promise<B>, onError: (e: unknown) => A, callback: (result: Either<A, B>) => void) => void;
28
+ toPromise: <A, B>(e: Either<A, B>) => Promise<B>;
25
29
  map: <A, B, C>(e: Either<A, B>, fn: (r: B) => C) => Either<A, C>;
26
30
  mapLeft: <A, B, C>(e: Either<A, B>, fn: (l: A) => C) => Either<C, B>;
27
31
  bimap: <A, B, C, D>(e: Either<A, B>, fl: (l: A) => C, fr: (r: B) => D) => Either<C, D>;
28
32
  flatMap: <A, B, C>(e: Either<A, B>, fn: (value: B) => Either<A, C>) => Either<A, C>;
29
- chain: <A, B, C>(e: Either<A, B>, fn: (r: B) => Either<A, C>) => Either<A, C>;
33
+ filter: <A, B>(e: Either<A, B>, predicate: (value: B) => boolean, onFalse: A) => Either<A, B>;
34
+ chain: <A, B, C>(e: Either<A, B>, fn: (value: B) => Either<A, C>) => Either<A, C>;
30
35
  fold: <A, B, C>(e: Either<A, B>, onLeft: (l: A) => C, onRight: (r: B) => C) => C;
31
36
  match: <A, B, C>(e: Either<A, B>, matcher: {
32
37
  right: (value: B) => C;
33
38
  left: (value: A) => C;
34
39
  }) => C;
35
40
  swap: <A, B>(e: Either<A, B>) => Either<B, A>;
36
- getLeft: <A>(e: Left<A>) => A;
37
- getRight: <B>(e: Right<B>) => B;
41
+ flatMapCallback: <A, B, C>(e: Either<A, B>, fn: (value: B) => Promise<Either<A, C>>, callback: (result: Either<A, C>) => void) => void;
42
+ lft: <A>(e: Left<A>) => A;
43
+ rgt: <B>(e: Right<B>) => B;
38
44
  getOrElse: <A, B>(e: Either<A, B>, defaultValue: B) => B;
45
+ getOrNull: <A, B>(e: Either<A, B>) => B | null;
39
46
  getOrUndefined: <A, B>(e: Either<A, B>) => B | undefined;
40
47
  getOrThrow: <A, B>(e: Either<A, B>) => B;
48
+ all: <A, B>(eithers: Either<A, B>[]) => Either<A, B[]>;
49
+ collect: <A, B>(eithers: Either<A, B>[]) => Either<A, B[]>;
50
+ flatten: <A, B>(e: Either<A, Either<A, B>>) => Either<A, B>;
51
+ partition: <A, B>(eithers: Either<A, B>[]) => {
52
+ right: B[];
53
+ left: A[];
54
+ };
41
55
  zip: <E, A, B>(a: Either<E, A>, b: Either<E, B>) => Either<E, [A, B]>;
42
56
  apply: <E, A, B>(fn: Either<E, (value: A) => B>, arg: Either<E, A>) => Either<E, B>;
57
+ orElse: <E, A, B>(a: Either<E, A>, b: Either<E, B>) => Either<E, A | B>;
43
58
  tap: <A, B>(e: Either<A, B>, f: (r: B) => void) => Either<A, B>;
44
59
  tapLeft: <A, B>(e: Either<A, B>, f: (l: A) => void) => Either<A, B>;
45
60
  };
@@ -49,52 +64,55 @@ declare global {
49
64
  }
50
65
  }
51
66
 
52
- type NothingNull = null;
53
- type NothingUndefined = undefined;
54
- type Nothing = NothingNull | NothingUndefined;
55
- declare const just: <T>(value: T) => Maybe<T>;
67
+ type Nothing = null | undefined;
68
+ declare const just: <T>(value: T) => NonNullable<T>;
56
69
  declare const nothing: () => Nothing;
70
+ declare const nothingNull: () => null;
71
+ declare const nothingUndefined: () => undefined;
57
72
  declare const isJust: <T>(m: Maybe<T>) => m is T;
58
73
  declare const isNothing: <T>(m: Maybe<T>) => m is Nothing;
74
+ declare const isNull: <T>(m: Maybe<T>) => m is null;
75
+ declare const isUndefined: <T>(m: Maybe<T>) => m is undefined;
59
76
  type Maybe<T> = T | Nothing;
60
77
  declare const Maybe: {
61
- just: <T>(value: T) => Maybe<T>;
62
- nothing: () => Nothing;
63
78
  new: <T>(m: T | null | undefined) => Maybe<T>;
64
- nothingNull: () => NothingNull;
65
- nothingUndefined: () => NothingUndefined;
79
+ just: <T>(value: T) => NonNullable<T>;
80
+ nothing: () => Nothing;
81
+ nothingNull: () => null;
82
+ nothingUndefined: () => undefined;
66
83
  isJust: <T>(m: Maybe<T>) => m is T;
67
84
  isNothing: <T>(m: Maybe<T>) => m is Nothing;
68
- isNothingNull: <T>(m: Maybe<T>) => m is NothingNull;
69
- isNothingUndefined: <T>(m: Maybe<T>) => m is NothingUndefined;
85
+ isNull: <T>(m: Maybe<T>) => m is null;
86
+ isUndefined: <T>(m: Maybe<T>) => m is undefined;
70
87
  fromNullable: <T>(m: T | null | undefined) => Maybe<T>;
71
88
  fromThrowable: <T>(fn: () => T) => Maybe<T>;
89
+ fromPredicate: <T>(value: T, predicate: (value: T) => boolean) => Maybe<T>;
72
90
  fromPromise: <T>(promise: Promise<T>) => Promise<Maybe<T>>;
73
- toNullable: <T>(m: Maybe<T>) => T | null;
74
- toUndefined: <T>(m: Maybe<T>) => T | undefined;
75
91
  map: <T, U>(m: Maybe<T>, fn: (v: T) => U) => Maybe<U>;
76
92
  flatMap: <T, U>(m: Maybe<T>, fn: (v: T) => Maybe<U>) => Maybe<U>;
77
93
  filter: <T>(m: Maybe<T>, predicate: (value: T) => boolean) => Maybe<T>;
94
+ fold: <T, U>(m: Maybe<T>, onNothing: () => U, onJust: (v: T) => U) => U;
78
95
  match: <T, U>(m: Maybe<T>, matcher: {
79
96
  some: (v: T) => U;
80
97
  nothing: () => U;
81
98
  }) => U;
82
- fold: <T, U>(m: Maybe<T>, onNothing: () => U, onJust: (v: T) => U) => U;
83
- getOrElse: <T>(m: Maybe<T>, d: T) => T;
99
+ getOrElse: <T>(m: Maybe<T>, defaultValue: T) => T;
84
100
  getOrUndefined: <T>(m: Maybe<T>) => T | undefined;
101
+ getOrNull: <T>(m: Maybe<T>) => T | null;
85
102
  getOrThrow: <T>(m: Maybe<T>) => T;
86
103
  zip: <T, U>(a: Maybe<T>, b: Maybe<U>) => Maybe<[T, U]>;
87
104
  apply: <T, U>(fn: Maybe<(value: T) => U>, opt: Maybe<T>) => Maybe<U>;
88
105
  orElse: <T>(opt: Maybe<T>, other: Maybe<T>) => Maybe<T>;
106
+ tap: <T>(m: Maybe<T>, fn: (v: T) => void) => Maybe<T>;
89
107
  };
90
108
 
91
- type None = {
109
+ interface None {
92
110
  readonly $: "None";
93
- };
94
- type Some<T> = {
111
+ }
112
+ interface Some<T> {
95
113
  readonly $: "Some";
96
114
  readonly value: T;
97
- };
115
+ }
98
116
  declare const none: <T>() => Option<T>;
99
117
  declare const some: <T>(value: T) => Option<T>;
100
118
  declare const isSome: <T>(option: Option<T>) => option is Some<T>;
@@ -102,9 +120,9 @@ declare const isNone: <T>(option: Option<T>) => option is None;
102
120
  declare const unwrap: <T>(option: Some<T>) => T;
103
121
  type Option<T> = None | Some<T>;
104
122
  declare const Option: {
123
+ new: <T>(value: T | null | undefined) => Option<T>;
105
124
  none: <T>() => Option<T>;
106
125
  some: <T>(value: T) => Option<T>;
107
- new: <T>(value: T | null | undefined) => Option<T>;
108
126
  isSome: <T>(option: Option<T>) => option is Some<T>;
109
127
  isNone: <T>(option: Option<T>) => option is None;
110
128
  fromNullable: <T>(value: T | null | undefined) => Option<T>;
@@ -114,6 +132,7 @@ declare const Option: {
114
132
  map: <T, U>(option: Option<T>, fn: (value: T) => U) => Option<U>;
115
133
  flatMap: <T, U>(option: Option<T>, fn: (value: T) => Option<U>) => Option<U>;
116
134
  filter: <T>(option: Option<T>, predicate: (value: T) => boolean) => Option<T>;
135
+ fold: <T, U>(option: Option<T>, onNone: () => U, onSome: (value: T) => U) => U;
117
136
  match: <T, U>(option: Option<T>, matcher: {
118
137
  some: (value: T) => U;
119
138
  none: () => U;
@@ -121,10 +140,12 @@ declare const Option: {
121
140
  unwrap: <T>(option: Some<T>) => T;
122
141
  getOrElse: <T>(option: Option<T>, defaultValue: T) => T;
123
142
  getOrUndefined: <T>(option: Option<T>) => T | undefined;
143
+ getOrNull: <T>(option: Option<T>) => T | null;
124
144
  getOrThrow: <T>(option: Option<T>) => T;
125
145
  zip: <T, U>(a: Option<T>, b: Option<U>) => Option<[T, U]>;
126
146
  apply: <T, U>(fn: Option<(value: T) => U>, opt: Option<T>) => Option<U>;
127
147
  orElse: <T>(opt: Option<T>, other: Option<T>) => Option<T>;
148
+ tap: <T>(option: Option<T>, f: (value: T) => void) => Option<T>;
128
149
  };
129
150
  declare global {
130
151
  interface Array<T> {
@@ -135,62 +156,79 @@ declare global {
135
156
  }
136
157
  }
137
158
 
159
+ declare const pair: <A, B>(a: A, b: B) => Pair<A, B>;
160
+ declare const curry: <A>(a: A) => <B>(b: B) => Pair<A, B>;
161
+ declare const fromArray: <A, B>([a, b]: [A, B]) => Pair<A, B>;
162
+ declare const fromObject: <A, B>(obj: {
163
+ fst: A;
164
+ snd: B;
165
+ }) => Pair<A, B>;
166
+ declare const toArray: <A, B>(p: Pair<A, B>) => [A, B];
167
+ declare const toObject: <A, B>(p: Pair<A, B>) => {
168
+ fst: A;
169
+ snd: B;
170
+ };
171
+ declare const mapFirst: <A, B, C>(p: Pair<A, B>, fn: (a: A) => C) => Pair<C, B>;
172
+ declare const mapSecond: <A, B, C>(p: Pair<A, B>, fn: (b: B) => C) => Pair<A, C>;
173
+ declare const eq: <A, B>(p1: Pair<A, B>, p2: Pair<A, B>) => boolean;
174
+ declare const eql: <A, B>(p1: Pair<A, B>, p2: Pair<A, B>, eqA: (a1: A, a2: A) => boolean, eqB: (b1: B, b2: B) => boolean) => boolean;
138
175
  declare const fst: <A, B>(p: Pair<A, B>) => A;
139
176
  declare const snd: <A, B>(p: Pair<A, B>) => B;
140
- declare const pair: <A, B>(a: A, b: B) => Pair<A, B>;
177
+ declare const app: <A, B, C>(p: Pair<(a: A) => B, C>, value: A) => Pair<B, C>;
141
178
  type Pair<A, B> = readonly [A, B];
142
179
  declare const Pair: {
143
- fst: <A, B>(p: Pair<A, B>) => A;
144
- snd: <A, B>(p: Pair<A, B>) => B;
145
180
  new: <A, B>(a: A, b: B) => Pair<A, B>;
181
+ curry: <A>(a: A) => <B>(b: B) => Pair<A, B>;
146
182
  fromArray: <A, B>([a, b]: [A, B]) => Pair<A, B>;
147
183
  fromObject: <A, B>(obj: {
148
184
  fst: A;
149
185
  snd: B;
150
186
  }) => Pair<A, B>;
151
- curry: <A>(a: A) => <B>(b: B) => Pair<A, B>;
152
- mapFirst: <A, B, C>(p: Pair<A, B>, fn: (a: A) => C) => Pair<C, B>;
153
- mapSecond: <A, B, C>(p: Pair<A, B>, fn: (b: B) => C) => Pair<A, C>;
154
- map: <A, B, C, D>(p: Pair<A, B>, fnA: (a: A) => C, fnB: (b: B) => D) => Pair<C, D>;
155
- swap: <A, B>(p: Pair<A, B>) => Pair<B, A>;
156
- apply: <A, B, C>(p: Pair<(a: A) => B, C>, value: A) => Pair<B, C>;
157
- apply2: <A, B, C>(fnPair: Pair<(a: A) => B, (b: B) => C>, vPair: Pair<A, B>) => Pair<A, C>;
158
- reduce: <A, B, C>(p: Pair<A, B>, fn: (a: A, b: B) => C) => C;
159
187
  toArray: <A, B>(p: Pair<A, B>) => [A, B];
160
188
  toObject: <A, B>(p: Pair<A, B>) => {
161
189
  fst: A;
162
190
  snd: B;
163
191
  };
192
+ mapFirst: <A, B, C>(p: Pair<A, B>, fn: (a: A) => C) => Pair<C, B>;
193
+ mapSecond: <A, B, C>(p: Pair<A, B>, fn: (b: B) => C) => Pair<A, C>;
194
+ map: <A, B, C, D>(p: Pair<A, B>, fnA: (a: A) => C, fnB: (b: B) => D) => Pair<C, D>;
195
+ swap: <A, B>(p: Pair<A, B>) => Pair<B, A>;
196
+ fold: <A, B, C>(p: Pair<A, B>, fn: (first: A, second: B) => C) => C;
197
+ match: <A, B, C>(p: Pair<A, B>, matcher: {
198
+ new: (first: A, second: B) => C;
199
+ }) => C;
164
200
  eq: <A, B>(p1: Pair<A, B>, p2: Pair<A, B>) => boolean;
165
- equals: <A, B>(p1: Pair<A, B>, p2: Pair<A, B>, eqA: (a1: A, a2: A) => boolean, eqB: (b1: B, b2: B) => boolean) => boolean;
201
+ eql: <A, B>(p1: Pair<A, B>, p2: Pair<A, B>, eqA: (a1: A, a2: A) => boolean, eqB: (b1: B, b2: B) => boolean) => boolean;
202
+ fst: <A, B>(p: Pair<A, B>) => A;
203
+ snd: <A, B>(p: Pair<A, B>) => B;
204
+ app: <A, B, C>(p: Pair<(a: A) => B, C>, value: A) => Pair<B, C>;
205
+ apply: <A, B, C>(fnPair: Pair<(a: A) => B, (b: B) => C>, vPair: Pair<A, B>) => Pair<A, C>;
166
206
  zip: <A, B, C, D>(p1: Pair<A, B>, p2: Pair<C, D>) => Pair<Pair<A, C>, Pair<B, D>>;
167
207
  };
168
208
 
169
- type Done<T> = {
209
+ interface Done<T> {
170
210
  readonly v: T;
171
- };
172
- type Fail<E> = {
211
+ }
212
+ interface Fail<E> {
173
213
  readonly e: E;
174
- };
175
- declare const done: <T>(v: T) => Done<T>;
176
- declare const fail: <E>(e: E) => Fail<E>;
177
- declare const Ok: <T>(v: T) => Done<T>;
178
- declare const Err: <E>(e: E) => Fail<E>;
214
+ }
215
+ declare const done: <T>(value: T) => Done<T>;
216
+ declare const fail: <E>(value: E) => Fail<E>;
217
+ declare const Ok: <T>(value: T) => Done<T>;
218
+ declare const Err: <E>(value: E) => Fail<E>;
179
219
  declare const isDone: <T, E>(r: Result<T, E>) => r is Done<T>;
180
220
  declare const isFail: <T, E>(r: Result<T, E>) => r is Fail<E>;
181
221
  declare const isOk: <T, E>(r: Result<T, E>) => r is Done<T>;
182
222
  declare const isErr: <T, E>(r: Result<T, E>) => r is Fail<E>;
223
+ declare const mapFail: <T, E, F>(r: Result<T, E>, fn: (b: E) => F) => Result<T, F>;
183
224
  declare const val: <T>(r: Done<T>) => T;
184
225
  declare const err: <E>(r: Fail<E>) => E;
226
+ declare const tapFail: <T, E>(r: Result<T, E>, f: (b: E) => void) => Result<T, E>;
185
227
  type Result<T, E> = Done<T> | Fail<E>;
186
228
  declare const Result: {
187
- done: <T>(v: T) => Done<T>;
188
- fail: <E>(e: E) => Fail<E>;
189
- Ok: <T>(v: T) => Done<T>;
190
- Err: <E>(e: E) => Fail<E>;
191
- val: <T>(r: Done<T>) => T;
192
- err: <E>(r: Fail<E>) => E;
193
229
  new: <T, E>(value: T | null | undefined, error: E) => Result<T, E>;
230
+ done: <T>(value: T) => Done<T>;
231
+ fail: <E>(value: E) => Fail<E>;
194
232
  isDone: <T, E>(r: Result<T, E>) => r is Done<T>;
195
233
  isFail: <T, E>(r: Result<T, E>) => r is Fail<E>;
196
234
  isOk: <T, E>(r: Result<T, E>) => r is Done<T>;
@@ -198,24 +236,40 @@ declare const Result: {
198
236
  fromNullable: <T, E>(value: T | null | undefined, error: E) => Result<T, E>;
199
237
  fromThrowable: <T, E>(fn: () => T, onError: (e: unknown) => E) => Result<T, E>;
200
238
  fromPromise: <T, E>(promise: Promise<T>, onError: (e: unknown) => E) => Promise<Result<T, E>>;
239
+ fromPromiseCallback: <T, E>(promise: Promise<T>, onError: (e: unknown) => E, callback: (result: Result<T, E>) => void) => void;
240
+ toPromise: <T, E>(r: Result<T, E>) => Promise<T>;
201
241
  map: <T, E, U>(r: Result<T, E>, fn: (a: T) => U) => Result<U, E>;
242
+ mapFail: <T, E, F>(r: Result<T, E>, fn: (b: E) => F) => Result<T, F>;
202
243
  mapErr: <T, E, F>(r: Result<T, E>, fn: (b: E) => F) => Result<T, F>;
203
244
  bimap: <T, E, U, F>(r: Result<T, E>, onDone: (a: T) => U, onFail: (b: E) => F) => Result<U, F>;
204
245
  flatMap: <T, E, C>(r: Result<T, E>, fn: (a: T) => Result<C, E>) => Result<C, E>;
246
+ filter: <T, E>(r: Result<T, E>, predicate: (a: T) => boolean, onFalse: E) => Result<T, E>;
205
247
  match: <T, E, U>(r: Result<T, E>, matcher: {
206
248
  done: (a: T) => U;
207
249
  fail: (b: E) => U;
208
250
  }) => U;
209
- fold: <T, E, U>(r: Result<T, E>, onFail: (error: E) => U, onDone: (value: T) => U) => U;
210
- recover: <T, E>(r: Result<T, E>, fn: (error: E) => T) => Result<T, E>;
211
- getOrElse: <T, E>(r: Result<T, E>, d: T) => T;
251
+ fold: <T, E, U>(r: Result<T, E>, onFail: (e: E) => U, onDone: (v: T) => U) => U;
252
+ chain: <T, E, C>(r: Result<T, E>, fn: (a: T) => Result<C, E>) => Result<C, E>;
253
+ recover: <T, E>(r: Result<T, E>, fn: (value: E) => T) => Result<T, E>;
254
+ flatMapCallback: <T, E, C>(r: Result<T, E>, fn: (a: T) => Promise<Result<C, E>>, callback: (result: Result<C, E>) => void) => void;
255
+ val: <T>(r: Done<T>) => T;
256
+ err: <E>(r: Fail<E>) => E;
257
+ getOrElse: <T, E>(r: Result<T, E>, defaultValue: T) => T;
212
258
  getOrUndefined: <T, E>(r: Result<T, E>) => T | undefined;
259
+ getOrNull: <T, E>(r: Result<T, E>) => T | null;
213
260
  getOrThrow: <T, E>(r: Result<T, E>) => T;
261
+ all: <T, E>(results: Result<T, E>[]) => Result<T[], E>;
262
+ collect: <T, E>(results: Result<T, E>[]) => Result<T[], E>;
263
+ flatten: <T, E>(r: Result<Result<T, E>, E>) => Result<T, E>;
264
+ partition: <T, E>(results: Result<T, E>[]) => {
265
+ done: T[];
266
+ fail: E[];
267
+ };
214
268
  zip: <T, U, E>(a: Result<T, E>, b: Result<U, E>) => Result<[T, U], E>;
215
269
  apply: <T, U, E>(fn: Result<(a: T) => U, E>, arg: Result<T, E>) => Result<U, E>;
216
270
  orElse: <T, E>(a: Result<T, E>, b: Result<T, E>) => Result<T, E>;
217
- filter: <T, E>(r: Result<T, E>, predicate: (a: T) => boolean, onFalse: E) => Result<T, E>;
218
271
  tap: <T, E>(r: Result<T, E>, f: (a: T) => void) => Result<T, E>;
272
+ tapFail: <T, E>(r: Result<T, E>, f: (b: E) => void) => Result<T, E>;
219
273
  tapErr: <T, E>(r: Result<T, E>, f: (b: E) => void) => Result<T, E>;
220
274
  };
221
275
  declare global {
@@ -224,4 +278,4 @@ declare global {
224
278
  }
225
279
  }
226
280
 
227
- export { type Done, Either, Err, type Fail, type Left, Maybe, type None, type Nothing, type NothingNull, type NothingUndefined, Ok, Option, Pair, Result, type Right, type Some, done, err, fail, fst, getLeft, getRight, isDone, isErr, isFail, isJust, isLeft, isNone, isNothing, isOk, isRight, isSome, just, left, none, nothing, pair, right, snd, some, unwrap, val };
281
+ export { type Done, Either, Err, type Fail, type Left, Maybe, type None, type Nothing, Ok, Option, Pair, Result, type Right, type Some, app, curry, done, eq, eql, err, fail, fromArray, fromObject, fst, isDone, isErr, isFail, isJust, isLeft, isNone, isNothing, isNull, isOk, isRight, isSome, isUndefined, just, left, lft, mapFail, mapFirst, mapLeft, mapSecond, none, nothing, nothingNull, nothingUndefined, pair, rgt, right, snd, some, tapFail, tapLeft, toArray, toObject, unwrap, val };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});var R=(t,e,o)=>new Promise((P,C)=>{var G=U=>{try{O(o.next(U))}catch(g){C(g)}},H=U=>{try{O(o.throw(U))}catch(g){C(g)}},O=U=>U.done?P(U.value):Promise.resolve(U.value).then(G,H);O((o=o.apply(t,e)).next())});var B=t=>({$:"Left",value:t}),l= exports.right =t=>({$:"Right",value:t}),E= exports.isLeft =t=>t.$==="Left",u= exports.isRight =t=>t.$==="Right",M=(t,e)=>t==null?B(e):l(t),I=(t,e)=>{try{return l(t())}catch(o){return B(e(o))}},K=(t,e)=>R(null,null,function*(){try{let o=yield t;return l(o)}catch(o){return B(e(o))}}),Q=(t,e)=>u(t)?l(e(r(t))):t,V=(t,e)=>E(t)?B(e(h(t))):t,W=(t,e,o)=>E(t)?B(e(h(t))):l(o(r(t))),X=(t,e)=>u(t)?e(r(t)):t,Y=(t,e)=>u(t)?e(r(t)):t,Z=(t,e,o)=>E(t)?e(h(t)):o(r(t)),tt=(t,e)=>u(t)?e.right(r(t)):e.left(h(t)),et=t=>u(t)?B(r(t)):l(h(t)),h= exports.getLeft =t=>t.value,r= exports.getRight =t=>t.value,ot=(t,e)=>u(t)?r(t):e,rt=t=>u(t)?r(t):void 0,nt=t=>{if(u(t))return r(t);throw h(t)},it=(t,e)=>E(t)?t:E(e)?e:l([r(t),r(e)]),st=(t,e)=>E(t)?t:E(e)?e:l(r(t)(r(e))),Tt=(t,e)=>(u(t)&&e(r(t)),t),pt=(t,e)=>(E(t)&&e(h(t)),t),at= exports.Either ={left:B,right:l,new:M,isLeft:E,isRight:u,fromNullable:M,fromThrowable:I,fromPromise:K,map:Q,mapLeft:V,bimap:W,flatMap:X,chain:Y,fold:Z,match:tt,swap:et,getLeft:h,getRight:r,getOrElse:ot,getOrUndefined:rt,getOrThrow:nt,zip:it,apply:st,tap:Tt,tapLeft:pt};Promise.prototype.toEither=function(t){return R(this,null,function*(){try{let e=yield this;return l(e)}catch(e){return B(t(e))}})};var w=t=>t,f= exports.nothing =()=>{},lt=()=>null,v=()=>{},b= exports.isJust =t=>t!=null,p= exports.isNothing =t=>t==null,ut=t=>t===null,ct=t=>t===void 0,N=t=>t,Et=t=>p(t)?null:t,xt=t=>p(t)?void 0:t,At=t=>{try{return t()}catch(e){return f()}},Bt=t=>t.then(e=>e,()=>f());var ht=(t,e)=>p(t)?f():e(t),ft=(t,e)=>p(t)?f():e(t),dt=(t,e)=>b(t)&&e(t)?t:f(),yt=(t,e)=>p(t)?e.nothing():e.some(t),mt=(t,e,o)=>p(t)?e():o(t),Ut=(t,e)=>p(t)?e:t,bt=t=>p(t)?v():t,Pt=t=>{if(p(t))throw new Error("Maybe is nothing");return t},Ot=(t,e)=>b(t)&&b(e)?[t,e]:f(),gt=(t,e)=>b(t)&&b(e)?t(e):f(),Rt=(t,e)=>p(t)?e:t,Ct= exports.Maybe ={just:w,nothing:f,new:N,nothingNull:lt,nothingUndefined:v,isJust:b,isNothing:p,isNothingNull:ut,isNothingUndefined:ct,fromNullable:N,fromThrowable:At,fromPromise:Bt,toNullable:Et,toUndefined:xt,map:ht,flatMap:ft,filter:dt,match:yt,fold:mt,getOrElse:Ut,getOrUndefined:bt,getOrThrow:Pt,zip:Ot,apply:gt,orElse:Rt};var a=()=>({$:"None"}),x= exports.some =t=>({$:"Some",value:t}),i= exports.isSome =t=>t.$==="Some",D= exports.isNone =t=>t.$==="None",L=t=>t==null?a():x(t),Mt=(t,e)=>e(t)?x(t):a(),Nt=t=>{try{return x(t())}catch(e){return a()}},k=t=>t.then(x,()=>a()),wt=(t,e)=>i(t)?x(e(T(t))):a(),vt=(t,e)=>i(t)?e(T(t)):a(),Lt=(t,e)=>i(t)&&e(T(t))?t:a(),Dt=(t,e)=>i(t)?e.some(T(t)):e.none(),T= exports.unwrap =t=>t.value,kt=(t,e)=>i(t)?T(t):e,Ft=t=>i(t)?T(t):void 0,$t=t=>{if(i(t))return T(t);throw new Error("Option is none")},St=(t,e)=>i(t)&&i(e)?x([T(t),T(e)]):a(),zt=(t,e)=>i(t)&&i(e)?x(T(t)(T(e))):a(),jt=(t,e)=>i(t)?t:e,qt= exports.Option ={none:a,some:x,new:L,isSome:i,isNone:D,fromNullable:L,fromPredicate:Mt,fromThrowable:Nt,fromPromise:k,map:wt,flatMap:vt,filter:Lt,match:Dt,unwrap:T,getOrElse:kt,getOrUndefined:Ft,getOrThrow:$t,zip:St,apply:zt,orElse:jt};Array.prototype.firstOption=function(){return this[0]?x(this[0]):a()};Promise.prototype.toOption=function(){return k(this)};var F=t=>t[0],$= exports.snd =t=>t[1],d=(t,e)=>[t,e],Jt= exports.pair =d,_t=([t,e])=>[t,e],Gt=t=>[t.fst,t.snd],Ht=t=>e=>[t,e],It=(t,e)=>d(e(t[0]),t[1]),Kt=(t,e)=>d(t[0],e(t[1])),Qt=(t,e,o)=>d(e(t[0]),o(t[1])),Vt=t=>d(t[1],t[0]),Wt=(t,e)=>d(t[0](e),t[1]),Xt=(t,e)=>d(e[0],t[1](e[1])),Yt=(t,e)=>e(t[0],t[1]),Zt=t=>[t[0],t[1]],te=t=>({fst:t[0],snd:t[1]}),ee=(t,e)=>t[0]===e[0]&&t[1]===e[1],oe=(t,e,o,P)=>o(t[0],e[0])&&P(t[1],e[1]),re=(t,e)=>d([t[0],e[0]],[t[1],e[1]]),ne= exports.Pair ={fst:F,snd:$,new:d,fromArray:_t,fromObject:Gt,curry:Ht,mapFirst:It,mapSecond:Kt,map:Qt,swap:Vt,apply:Wt,apply2:Xt,reduce:Yt,toArray:Zt,toObject:te,eq:ee,equals:oe,zip:re};var c=t=>({v:t}),y= exports.fail =t=>({e:t}),z= exports.Ok =c,j= exports.Err =y,s= exports.isDone =t=>"v"in t,A= exports.isFail =t=>"e"in t,q= exports.isOk =s,J= exports.isErr =A,S=(t,e)=>t==null?y(e):c(t),ie=(t,e)=>{try{return c(t())}catch(o){return y(e(o))}},_=(t,e)=>t.then(c,o=>y(e(o))),se=(t,e)=>s(t)?c(e(n(t))):t,Te=(t,e)=>A(t)?y(e(m(t))):t,pe=(t,e,o)=>s(t)?c(e(n(t))):y(o(m(t))),ae=(t,e)=>s(t)?e(n(t)):t,le=(t,e)=>s(t)?e.done(n(t)):e.fail(m(t)),ue=(t,e,o)=>s(t)?o(n(t)):e(m(t)),ce=(t,e)=>A(t)?c(e(m(t))):t,n= exports.val =t=>t.v,m= exports.err =t=>t.e,Ee=(t,e)=>s(t)?n(t):e,xe=t=>s(t)?n(t):void 0,Ae=t=>{if(s(t))return n(t);throw m(t)},Be=(t,e)=>A(t)?t:A(e)?e:c([n(t),n(e)]),he=(t,e)=>A(t)?t:A(e)?e:c(n(t)(n(e))),fe=(t,e)=>s(t)?t:e,de=(t,e,o)=>s(t)?e(n(t))?t:y(o):t,ye=(t,e)=>(s(t)&&e(n(t)),t),me=(t,e)=>(A(t)&&e(m(t)),t),Ue= exports.Result ={done:c,fail:y,Ok:z,Err:j,val:n,err:m,new:S,isDone:s,isFail:A,isOk:q,isErr:J,fromNullable:S,fromThrowable:ie,fromPromise:_,map:se,mapErr:Te,bimap:pe,flatMap:ae,match:le,fold:ue,recover:ce,getOrElse:Ee,getOrUndefined:xe,getOrThrow:Ae,zip:Be,apply:he,orElse:fe,filter:de,tap:ye,tapErr:me};Promise.prototype.toResult=function(t){return _(this,t)};exports.Either = at; exports.Err = j; exports.Maybe = Ct; exports.Ok = z; exports.Option = qt; exports.Pair = ne; exports.Result = Ue; exports.done = c; exports.err = m; exports.fail = y; exports.fst = F; exports.getLeft = h; exports.getRight = r; exports.isDone = s; exports.isErr = J; exports.isFail = A; exports.isJust = b; exports.isLeft = E; exports.isNone = D; exports.isNothing = p; exports.isOk = q; exports.isRight = u; exports.isSome = i; exports.just = w; exports.left = B; exports.none = a; exports.nothing = f; exports.pair = Jt; exports.right = l; exports.snd = $; exports.some = x; exports.unwrap = T; exports.val = n;
1
+ 'use strict';var b=t=>({$:"Left",value:t}),A=t=>({$:"Right",value:t}),P=t=>t.$==="Left",s=t=>t.$==="Right",C=(t,e)=>t==null?b(e):A(t),it=(t,e)=>{try{return A(t())}catch(o){return b(e(o))}},M=(t,e)=>t.then(A,o=>b(e(o))),st=(t,e,o)=>{t.then(T=>o(A(T)),T=>o(b(e(T))));},lt=(t,e,o)=>{P(t)?o(t):e(r(t)).then(o);},pt=t=>s(t)?Promise.resolve(r(t)):Promise.reject(h(t)),Tt=(t,e)=>s(t)?A(e(r(t))):t,N=(t,e)=>P(t)?b(e(h(t))):t,at=(t,e,o)=>s(t)?A(o(r(t))):b(e(h(t))),w=(t,e)=>s(t)?e(r(t)):t,ut=(t,e,o)=>s(t)?e(r(t))?t:b(o):t,ct=(t,e,o)=>s(t)?o(r(t)):e(h(t)),Et=(t,e)=>s(t)?e.right(r(t)):e.left(h(t));var At=t=>s(t)?b(r(t)):A(h(t)),h=t=>t.value,r=t=>t.value,ft=(t,e)=>s(t)?r(t):e,xt=t=>s(t)?r(t):null,Bt=t=>s(t)?r(t):void 0,ht=t=>{if(s(t))return r(t);let e=h(t);throw e instanceof Error?e:new Error(String(e))},dt=(t,e)=>P(t)?t:P(e)?e:A([r(t),r(e)]),mt=(t,e)=>P(t)?t:P(e)?e:A(r(t)(r(e))),yt=(t,e)=>s(t)?t:e,Pt=(t,e)=>(s(t)&&e(r(t)),t),F=(t,e)=>(P(t)&&e(h(t)),t),k=t=>{let e=[];for(let o of t){if(P(o))return o;e.push(r(o));}return A(e)},bt=k,Ot=t=>s(t)?r(t):t,Rt=t=>{let e=[],o=[];for(let T of t)s(T)?e.push(r(T)):o.push(h(T));return {right:e,left:o}},Ut={new:C,left:b,right:A,isLeft:P,isRight:s,fromNullable:C,fromThrowable:it,fromPromise:M,fromPromiseCallback:st,toPromise:pt,map:Tt,mapLeft:N,bimap:at,flatMap:w,filter:ut,chain:w,fold:ct,match:Et,swap:At,flatMapCallback:lt,lft:h,rgt:r,getOrElse:ft,getOrNull:xt,getOrUndefined:Bt,getOrThrow:ht,all:k,collect:bt,flatten:Ot,partition:Rt,zip:dt,apply:mt,orElse:yt,tap:Pt,tapLeft:F};"toEither"in Promise.prototype||Object.defineProperty(Promise.prototype,"toEither",{value:function(t){return M(this,t)},writable:true,configurable:true});var c=t=>t,d=()=>{},D=()=>null,S=()=>{},U=t=>t!=null,f=t=>t==null,$=t=>t===null,j=t=>t===void 0,L=t=>f(t)?d():c(t),gt=(t,e)=>e(t)?t:d(),vt=t=>{try{return t()}catch(e){return d()}},Ct=t=>t.then(c,()=>d()),wt=(t,e)=>f(t)?d():c(e(t)),Mt=(t,e)=>f(t)?d():c(e(t)),Nt=(t,e)=>U(t)&&e(t)?c(t):d(),Ft=(t,e,o)=>f(t)?e():o(t),kt=(t,e)=>f(t)?e.nothing():e.some(t),Lt=(t,e)=>f(t)?e:c(t),Dt=t=>f(t)?void 0:c(t),St=t=>f(t)?null:c(t),$t=t=>{if(f(t))throw new Error("Maybe is nothing");return c(t)},jt=(t,e)=>U(t)&&U(e)?c([t,e]):d(),zt=(t,e)=>U(t)&&U(e)?c(t(e)):d(),qt=(t,e)=>f(t)?e:t,Jt=(t,e)=>(U(t)&&c(e(t)),c(t)),Gt={new:L,just:c,nothing:d,nothingNull:D,nothingUndefined:S,isJust:U,isNothing:f,isNull:$,isUndefined:j,fromNullable:L,fromThrowable:vt,fromPredicate:gt,fromPromise:Ct,map:wt,flatMap:Mt,filter:Nt,fold:Ft,match:kt,getOrElse:Lt,getOrUndefined:Dt,getOrNull:St,getOrThrow:$t,zip:jt,apply:zt,orElse:qt,tap:Jt};var x=()=>({$:"None"}),O=t=>({$:"Some",value:t}),a=t=>t.$==="Some",q=t=>t.$==="None",z=t=>t==null?x():O(t),Ht=(t,e)=>e(t)?O(t):x(),It=t=>{try{return O(t())}catch(e){return x()}},J=t=>t.then(O,()=>x()),Kt=(t,e)=>a(t)?O(e(u(t))):x(),Qt=(t,e)=>a(t)?e(u(t)):x(),Vt=(t,e)=>a(t)&&e(u(t))?t:x(),Wt=(t,e,o)=>a(t)?o(u(t)):e(),Xt=(t,e)=>a(t)?e.some(u(t)):e.none(),u=t=>t.value,Yt=(t,e)=>a(t)?u(t):e,Zt=t=>a(t)?u(t):void 0,_t=t=>a(t)?u(t):null,te=t=>{if(a(t))return u(t);throw new Error("Option is none")},ee=(t,e)=>a(t)&&a(e)?O([u(t),u(e)]):x(),oe=(t,e)=>a(t)&&a(e)?O(u(t)(u(e))):x(),re=(t,e)=>a(t)?t:e,ne=(t,e)=>(a(t)&&e(u(t)),t),ie={new:z,none:x,some:O,isSome:a,isNone:q,fromNullable:z,fromPredicate:Ht,fromThrowable:It,fromPromise:J,map:Kt,flatMap:Qt,filter:Vt,fold:Wt,match:Xt,unwrap:u,getOrElse:Yt,getOrUndefined:Zt,getOrNull:_t,getOrThrow:te,zip:ee,apply:oe,orElse:re,tap:ne};"firstOption"in Array.prototype||Object.defineProperty(Array.prototype,"firstOption",{value:function(){return this[0]?O(this[0]):x()},writable:true,configurable:true});"toOption"in Promise.prototype||Object.defineProperty(Promise.prototype,"toOption",{value:function(){return J(this)},writable:true,configurable:true});var m=(t,e)=>[t,e],se=m,G=t=>e=>[t,e],H=([t,e])=>m(t,e),I=t=>m(t.fst,t.snd),K=t=>[l(t),i(t)],Q=t=>({fst:l(t),snd:i(t)}),V=(t,e)=>m(e(l(t)),i(t)),W=(t,e)=>m(l(t),e(i(t))),le=(t,e,o)=>m(e(l(t)),o(i(t))),pe=t=>m(i(t),l(t)),Te=(t,e)=>e(l(t),i(t)),ae=(t,e)=>e.new(l(t),i(t)),X=(t,e)=>l(t)===l(e)&&i(t)===i(e),Y=(t,e,o,T)=>o(l(t),l(e))&&T(i(t),i(e)),l=t=>t[0],i=t=>t[1],Z=(t,e)=>m(l(t)(e),i(t)),ue=(t,e)=>m(l(e),i(t)(i(e))),ce=(t,e)=>m([l(t),l(e)],[i(t),i(e)]),Ee={new:m,curry:G,fromArray:H,fromObject:I,toArray:K,toObject:Q,mapFirst:V,mapSecond:W,map:le,swap:pe,fold:Te,match:ae,eq:X,eql:Y,fst:l,snd:i,app:Z,apply:ue,zip:ce};var E=t=>({v:t}),R=t=>({e:t}),Ae=E,fe=R,p=t=>"v"in t,B=t=>"e"in t,et=p,ot=B,_=(t,e)=>t==null?R(e):E(t),xe=(t,e)=>{try{return E(t())}catch(o){return R(e(o))}},rt=(t,e)=>t.then(E,o=>R(e(o))),Be=(t,e,o)=>{t.then(T=>o(E(T)),T=>o(R(e(T))));},he=(t,e,o)=>{B(t)?o(t):e(n(t)).then(o);},de=t=>p(t)?Promise.resolve(n(t)):Promise.reject(y(t)),me=(t,e)=>p(t)?E(e(n(t))):t,g=(t,e)=>B(t)?R(e(y(t))):t,ye=(t,e,o)=>p(t)?E(e(n(t))):R(o(y(t))),tt=(t,e)=>p(t)?e(n(t)):t,Pe=(t,e,o)=>p(t)?e(n(t))?t:R(o):t,be=(t,e,o)=>p(t)?o(n(t)):e(y(t)),Oe=(t,e)=>p(t)?e.done(n(t)):e.fail(y(t)),Re=(t,e)=>B(t)?E(e(y(t))):t;var n=t=>t.v,y=t=>t.e,Ue=(t,e)=>p(t)?n(t):e,ge=t=>p(t)?n(t):void 0,ve=t=>p(t)?n(t):null,Ce=t=>{if(p(t))return n(t);let e=y(t);throw e instanceof Error?e:new Error(String(e))},we=(t,e)=>B(t)?t:B(e)?e:E([n(t),n(e)]),Me=(t,e)=>B(t)?t:B(e)?e:E(n(t)(n(e))),Ne=(t,e)=>p(t)?t:e,Fe=(t,e)=>(p(t)&&e(n(t)),t),v=(t,e)=>(B(t)&&e(y(t)),t),nt=t=>{let e=[];for(let o of t){if(B(o))return o;e.push(n(o));}return E(e)},ke=nt,Le=t=>p(t)?n(t):t,De=t=>{let e=[],o=[];for(let T of t)p(T)?e.push(n(T)):o.push(y(T));return {done:e,fail:o}},Se={new:_,done:E,fail:R,isDone:p,isFail:B,isOk:et,isErr:ot,fromNullable:_,fromThrowable:xe,fromPromise:rt,fromPromiseCallback:Be,toPromise:de,map:me,mapFail:g,mapErr:g,bimap:ye,flatMap:tt,filter:Pe,match:Oe,fold:be,chain:tt,recover:Re,flatMapCallback:he,val:n,err:y,getOrElse:Ue,getOrUndefined:ge,getOrNull:ve,getOrThrow:Ce,all:nt,collect:ke,flatten:Le,partition:De,zip:we,apply:Me,orElse:Ne,tap:Fe,tapFail:v,tapErr:v};"toResult"in Promise.prototype||Object.defineProperty(Promise.prototype,"toResult",{value:function(t){return rt(this,t)},writable:true,configurable:true});exports.Either=Ut;exports.Err=fe;exports.Maybe=Gt;exports.Ok=Ae;exports.Option=ie;exports.Pair=Ee;exports.Result=Se;exports.app=Z;exports.curry=G;exports.done=E;exports.eq=X;exports.eql=Y;exports.err=y;exports.fail=R;exports.fromArray=H;exports.fromObject=I;exports.fst=l;exports.isDone=p;exports.isErr=ot;exports.isFail=B;exports.isJust=U;exports.isLeft=P;exports.isNone=q;exports.isNothing=f;exports.isNull=$;exports.isOk=et;exports.isRight=s;exports.isSome=a;exports.isUndefined=j;exports.just=c;exports.left=b;exports.lft=h;exports.mapFail=g;exports.mapFirst=V;exports.mapLeft=N;exports.mapSecond=W;exports.none=x;exports.nothing=d;exports.nothingNull=D;exports.nothingUndefined=S;exports.pair=se;exports.rgt=r;exports.right=A;exports.snd=i;exports.some=O;exports.tapFail=v;exports.tapLeft=F;exports.toArray=K;exports.toObject=Q;exports.unwrap=u;exports.val=n;
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- var R=(t,e,o)=>new Promise((P,C)=>{var G=U=>{try{O(o.next(U))}catch(g){C(g)}},H=U=>{try{O(o.throw(U))}catch(g){C(g)}},O=U=>U.done?P(U.value):Promise.resolve(U.value).then(G,H);O((o=o.apply(t,e)).next())});var B=t=>({$:"Left",value:t}),l=t=>({$:"Right",value:t}),E=t=>t.$==="Left",u=t=>t.$==="Right",M=(t,e)=>t==null?B(e):l(t),I=(t,e)=>{try{return l(t())}catch(o){return B(e(o))}},K=(t,e)=>R(null,null,function*(){try{let o=yield t;return l(o)}catch(o){return B(e(o))}}),Q=(t,e)=>u(t)?l(e(r(t))):t,V=(t,e)=>E(t)?B(e(h(t))):t,W=(t,e,o)=>E(t)?B(e(h(t))):l(o(r(t))),X=(t,e)=>u(t)?e(r(t)):t,Y=(t,e)=>u(t)?e(r(t)):t,Z=(t,e,o)=>E(t)?e(h(t)):o(r(t)),tt=(t,e)=>u(t)?e.right(r(t)):e.left(h(t)),et=t=>u(t)?B(r(t)):l(h(t)),h=t=>t.value,r=t=>t.value,ot=(t,e)=>u(t)?r(t):e,rt=t=>u(t)?r(t):void 0,nt=t=>{if(u(t))return r(t);throw h(t)},it=(t,e)=>E(t)?t:E(e)?e:l([r(t),r(e)]),st=(t,e)=>E(t)?t:E(e)?e:l(r(t)(r(e))),Tt=(t,e)=>(u(t)&&e(r(t)),t),pt=(t,e)=>(E(t)&&e(h(t)),t),at={left:B,right:l,new:M,isLeft:E,isRight:u,fromNullable:M,fromThrowable:I,fromPromise:K,map:Q,mapLeft:V,bimap:W,flatMap:X,chain:Y,fold:Z,match:tt,swap:et,getLeft:h,getRight:r,getOrElse:ot,getOrUndefined:rt,getOrThrow:nt,zip:it,apply:st,tap:Tt,tapLeft:pt};Promise.prototype.toEither=function(t){return R(this,null,function*(){try{let e=yield this;return l(e)}catch(e){return B(t(e))}})};var w=t=>t,f=()=>{},lt=()=>null,v=()=>{},b=t=>t!=null,p=t=>t==null,ut=t=>t===null,ct=t=>t===void 0,N=t=>t,Et=t=>p(t)?null:t,xt=t=>p(t)?void 0:t,At=t=>{try{return t()}catch(e){return f()}},Bt=t=>t.then(e=>e,()=>f());var ht=(t,e)=>p(t)?f():e(t),ft=(t,e)=>p(t)?f():e(t),dt=(t,e)=>b(t)&&e(t)?t:f(),yt=(t,e)=>p(t)?e.nothing():e.some(t),mt=(t,e,o)=>p(t)?e():o(t),Ut=(t,e)=>p(t)?e:t,bt=t=>p(t)?v():t,Pt=t=>{if(p(t))throw new Error("Maybe is nothing");return t},Ot=(t,e)=>b(t)&&b(e)?[t,e]:f(),gt=(t,e)=>b(t)&&b(e)?t(e):f(),Rt=(t,e)=>p(t)?e:t,Ct={just:w,nothing:f,new:N,nothingNull:lt,nothingUndefined:v,isJust:b,isNothing:p,isNothingNull:ut,isNothingUndefined:ct,fromNullable:N,fromThrowable:At,fromPromise:Bt,toNullable:Et,toUndefined:xt,map:ht,flatMap:ft,filter:dt,match:yt,fold:mt,getOrElse:Ut,getOrUndefined:bt,getOrThrow:Pt,zip:Ot,apply:gt,orElse:Rt};var a=()=>({$:"None"}),x=t=>({$:"Some",value:t}),i=t=>t.$==="Some",D=t=>t.$==="None",L=t=>t==null?a():x(t),Mt=(t,e)=>e(t)?x(t):a(),Nt=t=>{try{return x(t())}catch(e){return a()}},k=t=>t.then(x,()=>a()),wt=(t,e)=>i(t)?x(e(T(t))):a(),vt=(t,e)=>i(t)?e(T(t)):a(),Lt=(t,e)=>i(t)&&e(T(t))?t:a(),Dt=(t,e)=>i(t)?e.some(T(t)):e.none(),T=t=>t.value,kt=(t,e)=>i(t)?T(t):e,Ft=t=>i(t)?T(t):void 0,$t=t=>{if(i(t))return T(t);throw new Error("Option is none")},St=(t,e)=>i(t)&&i(e)?x([T(t),T(e)]):a(),zt=(t,e)=>i(t)&&i(e)?x(T(t)(T(e))):a(),jt=(t,e)=>i(t)?t:e,qt={none:a,some:x,new:L,isSome:i,isNone:D,fromNullable:L,fromPredicate:Mt,fromThrowable:Nt,fromPromise:k,map:wt,flatMap:vt,filter:Lt,match:Dt,unwrap:T,getOrElse:kt,getOrUndefined:Ft,getOrThrow:$t,zip:St,apply:zt,orElse:jt};Array.prototype.firstOption=function(){return this[0]?x(this[0]):a()};Promise.prototype.toOption=function(){return k(this)};var F=t=>t[0],$=t=>t[1],d=(t,e)=>[t,e],Jt=d,_t=([t,e])=>[t,e],Gt=t=>[t.fst,t.snd],Ht=t=>e=>[t,e],It=(t,e)=>d(e(t[0]),t[1]),Kt=(t,e)=>d(t[0],e(t[1])),Qt=(t,e,o)=>d(e(t[0]),o(t[1])),Vt=t=>d(t[1],t[0]),Wt=(t,e)=>d(t[0](e),t[1]),Xt=(t,e)=>d(e[0],t[1](e[1])),Yt=(t,e)=>e(t[0],t[1]),Zt=t=>[t[0],t[1]],te=t=>({fst:t[0],snd:t[1]}),ee=(t,e)=>t[0]===e[0]&&t[1]===e[1],oe=(t,e,o,P)=>o(t[0],e[0])&&P(t[1],e[1]),re=(t,e)=>d([t[0],e[0]],[t[1],e[1]]),ne={fst:F,snd:$,new:d,fromArray:_t,fromObject:Gt,curry:Ht,mapFirst:It,mapSecond:Kt,map:Qt,swap:Vt,apply:Wt,apply2:Xt,reduce:Yt,toArray:Zt,toObject:te,eq:ee,equals:oe,zip:re};var c=t=>({v:t}),y=t=>({e:t}),z=c,j=y,s=t=>"v"in t,A=t=>"e"in t,q=s,J=A,S=(t,e)=>t==null?y(e):c(t),ie=(t,e)=>{try{return c(t())}catch(o){return y(e(o))}},_=(t,e)=>t.then(c,o=>y(e(o))),se=(t,e)=>s(t)?c(e(n(t))):t,Te=(t,e)=>A(t)?y(e(m(t))):t,pe=(t,e,o)=>s(t)?c(e(n(t))):y(o(m(t))),ae=(t,e)=>s(t)?e(n(t)):t,le=(t,e)=>s(t)?e.done(n(t)):e.fail(m(t)),ue=(t,e,o)=>s(t)?o(n(t)):e(m(t)),ce=(t,e)=>A(t)?c(e(m(t))):t,n=t=>t.v,m=t=>t.e,Ee=(t,e)=>s(t)?n(t):e,xe=t=>s(t)?n(t):void 0,Ae=t=>{if(s(t))return n(t);throw m(t)},Be=(t,e)=>A(t)?t:A(e)?e:c([n(t),n(e)]),he=(t,e)=>A(t)?t:A(e)?e:c(n(t)(n(e))),fe=(t,e)=>s(t)?t:e,de=(t,e,o)=>s(t)?e(n(t))?t:y(o):t,ye=(t,e)=>(s(t)&&e(n(t)),t),me=(t,e)=>(A(t)&&e(m(t)),t),Ue={done:c,fail:y,Ok:z,Err:j,val:n,err:m,new:S,isDone:s,isFail:A,isOk:q,isErr:J,fromNullable:S,fromThrowable:ie,fromPromise:_,map:se,mapErr:Te,bimap:pe,flatMap:ae,match:le,fold:ue,recover:ce,getOrElse:Ee,getOrUndefined:xe,getOrThrow:Ae,zip:Be,apply:he,orElse:fe,filter:de,tap:ye,tapErr:me};Promise.prototype.toResult=function(t){return _(this,t)};export{at as Either,j as Err,Ct as Maybe,z as Ok,qt as Option,ne as Pair,Ue as Result,c as done,m as err,y as fail,F as fst,h as getLeft,r as getRight,s as isDone,J as isErr,A as isFail,b as isJust,E as isLeft,D as isNone,p as isNothing,q as isOk,u as isRight,i as isSome,w as just,B as left,a as none,f as nothing,Jt as pair,l as right,$ as snd,x as some,T as unwrap,n as val};
1
+ var b=t=>({$:"Left",value:t}),A=t=>({$:"Right",value:t}),P=t=>t.$==="Left",s=t=>t.$==="Right",C=(t,e)=>t==null?b(e):A(t),it=(t,e)=>{try{return A(t())}catch(o){return b(e(o))}},M=(t,e)=>t.then(A,o=>b(e(o))),st=(t,e,o)=>{t.then(T=>o(A(T)),T=>o(b(e(T))));},lt=(t,e,o)=>{P(t)?o(t):e(r(t)).then(o);},pt=t=>s(t)?Promise.resolve(r(t)):Promise.reject(h(t)),Tt=(t,e)=>s(t)?A(e(r(t))):t,N=(t,e)=>P(t)?b(e(h(t))):t,at=(t,e,o)=>s(t)?A(o(r(t))):b(e(h(t))),w=(t,e)=>s(t)?e(r(t)):t,ut=(t,e,o)=>s(t)?e(r(t))?t:b(o):t,ct=(t,e,o)=>s(t)?o(r(t)):e(h(t)),Et=(t,e)=>s(t)?e.right(r(t)):e.left(h(t));var At=t=>s(t)?b(r(t)):A(h(t)),h=t=>t.value,r=t=>t.value,ft=(t,e)=>s(t)?r(t):e,xt=t=>s(t)?r(t):null,Bt=t=>s(t)?r(t):void 0,ht=t=>{if(s(t))return r(t);let e=h(t);throw e instanceof Error?e:new Error(String(e))},dt=(t,e)=>P(t)?t:P(e)?e:A([r(t),r(e)]),mt=(t,e)=>P(t)?t:P(e)?e:A(r(t)(r(e))),yt=(t,e)=>s(t)?t:e,Pt=(t,e)=>(s(t)&&e(r(t)),t),F=(t,e)=>(P(t)&&e(h(t)),t),k=t=>{let e=[];for(let o of t){if(P(o))return o;e.push(r(o));}return A(e)},bt=k,Ot=t=>s(t)?r(t):t,Rt=t=>{let e=[],o=[];for(let T of t)s(T)?e.push(r(T)):o.push(h(T));return {right:e,left:o}},Ut={new:C,left:b,right:A,isLeft:P,isRight:s,fromNullable:C,fromThrowable:it,fromPromise:M,fromPromiseCallback:st,toPromise:pt,map:Tt,mapLeft:N,bimap:at,flatMap:w,filter:ut,chain:w,fold:ct,match:Et,swap:At,flatMapCallback:lt,lft:h,rgt:r,getOrElse:ft,getOrNull:xt,getOrUndefined:Bt,getOrThrow:ht,all:k,collect:bt,flatten:Ot,partition:Rt,zip:dt,apply:mt,orElse:yt,tap:Pt,tapLeft:F};"toEither"in Promise.prototype||Object.defineProperty(Promise.prototype,"toEither",{value:function(t){return M(this,t)},writable:true,configurable:true});var c=t=>t,d=()=>{},D=()=>null,S=()=>{},U=t=>t!=null,f=t=>t==null,$=t=>t===null,j=t=>t===void 0,L=t=>f(t)?d():c(t),gt=(t,e)=>e(t)?t:d(),vt=t=>{try{return t()}catch(e){return d()}},Ct=t=>t.then(c,()=>d()),wt=(t,e)=>f(t)?d():c(e(t)),Mt=(t,e)=>f(t)?d():c(e(t)),Nt=(t,e)=>U(t)&&e(t)?c(t):d(),Ft=(t,e,o)=>f(t)?e():o(t),kt=(t,e)=>f(t)?e.nothing():e.some(t),Lt=(t,e)=>f(t)?e:c(t),Dt=t=>f(t)?void 0:c(t),St=t=>f(t)?null:c(t),$t=t=>{if(f(t))throw new Error("Maybe is nothing");return c(t)},jt=(t,e)=>U(t)&&U(e)?c([t,e]):d(),zt=(t,e)=>U(t)&&U(e)?c(t(e)):d(),qt=(t,e)=>f(t)?e:t,Jt=(t,e)=>(U(t)&&c(e(t)),c(t)),Gt={new:L,just:c,nothing:d,nothingNull:D,nothingUndefined:S,isJust:U,isNothing:f,isNull:$,isUndefined:j,fromNullable:L,fromThrowable:vt,fromPredicate:gt,fromPromise:Ct,map:wt,flatMap:Mt,filter:Nt,fold:Ft,match:kt,getOrElse:Lt,getOrUndefined:Dt,getOrNull:St,getOrThrow:$t,zip:jt,apply:zt,orElse:qt,tap:Jt};var x=()=>({$:"None"}),O=t=>({$:"Some",value:t}),a=t=>t.$==="Some",q=t=>t.$==="None",z=t=>t==null?x():O(t),Ht=(t,e)=>e(t)?O(t):x(),It=t=>{try{return O(t())}catch(e){return x()}},J=t=>t.then(O,()=>x()),Kt=(t,e)=>a(t)?O(e(u(t))):x(),Qt=(t,e)=>a(t)?e(u(t)):x(),Vt=(t,e)=>a(t)&&e(u(t))?t:x(),Wt=(t,e,o)=>a(t)?o(u(t)):e(),Xt=(t,e)=>a(t)?e.some(u(t)):e.none(),u=t=>t.value,Yt=(t,e)=>a(t)?u(t):e,Zt=t=>a(t)?u(t):void 0,_t=t=>a(t)?u(t):null,te=t=>{if(a(t))return u(t);throw new Error("Option is none")},ee=(t,e)=>a(t)&&a(e)?O([u(t),u(e)]):x(),oe=(t,e)=>a(t)&&a(e)?O(u(t)(u(e))):x(),re=(t,e)=>a(t)?t:e,ne=(t,e)=>(a(t)&&e(u(t)),t),ie={new:z,none:x,some:O,isSome:a,isNone:q,fromNullable:z,fromPredicate:Ht,fromThrowable:It,fromPromise:J,map:Kt,flatMap:Qt,filter:Vt,fold:Wt,match:Xt,unwrap:u,getOrElse:Yt,getOrUndefined:Zt,getOrNull:_t,getOrThrow:te,zip:ee,apply:oe,orElse:re,tap:ne};"firstOption"in Array.prototype||Object.defineProperty(Array.prototype,"firstOption",{value:function(){return this[0]?O(this[0]):x()},writable:true,configurable:true});"toOption"in Promise.prototype||Object.defineProperty(Promise.prototype,"toOption",{value:function(){return J(this)},writable:true,configurable:true});var m=(t,e)=>[t,e],se=m,G=t=>e=>[t,e],H=([t,e])=>m(t,e),I=t=>m(t.fst,t.snd),K=t=>[l(t),i(t)],Q=t=>({fst:l(t),snd:i(t)}),V=(t,e)=>m(e(l(t)),i(t)),W=(t,e)=>m(l(t),e(i(t))),le=(t,e,o)=>m(e(l(t)),o(i(t))),pe=t=>m(i(t),l(t)),Te=(t,e)=>e(l(t),i(t)),ae=(t,e)=>e.new(l(t),i(t)),X=(t,e)=>l(t)===l(e)&&i(t)===i(e),Y=(t,e,o,T)=>o(l(t),l(e))&&T(i(t),i(e)),l=t=>t[0],i=t=>t[1],Z=(t,e)=>m(l(t)(e),i(t)),ue=(t,e)=>m(l(e),i(t)(i(e))),ce=(t,e)=>m([l(t),l(e)],[i(t),i(e)]),Ee={new:m,curry:G,fromArray:H,fromObject:I,toArray:K,toObject:Q,mapFirst:V,mapSecond:W,map:le,swap:pe,fold:Te,match:ae,eq:X,eql:Y,fst:l,snd:i,app:Z,apply:ue,zip:ce};var E=t=>({v:t}),R=t=>({e:t}),Ae=E,fe=R,p=t=>"v"in t,B=t=>"e"in t,et=p,ot=B,_=(t,e)=>t==null?R(e):E(t),xe=(t,e)=>{try{return E(t())}catch(o){return R(e(o))}},rt=(t,e)=>t.then(E,o=>R(e(o))),Be=(t,e,o)=>{t.then(T=>o(E(T)),T=>o(R(e(T))));},he=(t,e,o)=>{B(t)?o(t):e(n(t)).then(o);},de=t=>p(t)?Promise.resolve(n(t)):Promise.reject(y(t)),me=(t,e)=>p(t)?E(e(n(t))):t,g=(t,e)=>B(t)?R(e(y(t))):t,ye=(t,e,o)=>p(t)?E(e(n(t))):R(o(y(t))),tt=(t,e)=>p(t)?e(n(t)):t,Pe=(t,e,o)=>p(t)?e(n(t))?t:R(o):t,be=(t,e,o)=>p(t)?o(n(t)):e(y(t)),Oe=(t,e)=>p(t)?e.done(n(t)):e.fail(y(t)),Re=(t,e)=>B(t)?E(e(y(t))):t;var n=t=>t.v,y=t=>t.e,Ue=(t,e)=>p(t)?n(t):e,ge=t=>p(t)?n(t):void 0,ve=t=>p(t)?n(t):null,Ce=t=>{if(p(t))return n(t);let e=y(t);throw e instanceof Error?e:new Error(String(e))},we=(t,e)=>B(t)?t:B(e)?e:E([n(t),n(e)]),Me=(t,e)=>B(t)?t:B(e)?e:E(n(t)(n(e))),Ne=(t,e)=>p(t)?t:e,Fe=(t,e)=>(p(t)&&e(n(t)),t),v=(t,e)=>(B(t)&&e(y(t)),t),nt=t=>{let e=[];for(let o of t){if(B(o))return o;e.push(n(o));}return E(e)},ke=nt,Le=t=>p(t)?n(t):t,De=t=>{let e=[],o=[];for(let T of t)p(T)?e.push(n(T)):o.push(y(T));return {done:e,fail:o}},Se={new:_,done:E,fail:R,isDone:p,isFail:B,isOk:et,isErr:ot,fromNullable:_,fromThrowable:xe,fromPromise:rt,fromPromiseCallback:Be,toPromise:de,map:me,mapFail:g,mapErr:g,bimap:ye,flatMap:tt,filter:Pe,match:Oe,fold:be,chain:tt,recover:Re,flatMapCallback:he,val:n,err:y,getOrElse:Ue,getOrUndefined:ge,getOrNull:ve,getOrThrow:Ce,all:nt,collect:ke,flatten:Le,partition:De,zip:we,apply:Me,orElse:Ne,tap:Fe,tapFail:v,tapErr:v};"toResult"in Promise.prototype||Object.defineProperty(Promise.prototype,"toResult",{value:function(t){return rt(this,t)},writable:true,configurable:true});export{Ut as Either,fe as Err,Gt as Maybe,Ae as Ok,ie as Option,Ee as Pair,Se as Result,Z as app,G as curry,E as done,X as eq,Y as eql,y as err,R as fail,H as fromArray,I as fromObject,l as fst,p as isDone,ot as isErr,B as isFail,U as isJust,P as isLeft,q as isNone,f as isNothing,$ as isNull,et as isOk,s as isRight,a as isSome,j as isUndefined,c as just,b as left,h as lft,g as mapFail,V as mapFirst,N as mapLeft,W as mapSecond,x as none,d as nothing,D as nothingNull,S as nothingUndefined,se as pair,r as rgt,A as right,i as snd,O as some,v as tapFail,F as tapLeft,K as toArray,Q as toObject,u as unwrap,n as val};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lite-fp",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "description": "Functional programming utilities.",
5
5
  "license": "MIT",
6
6
  "main": "./dist/index.js",