lite-fp 0.1.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,74 +1,82 @@
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;
1
+ type Left<A> = {
2
+ readonly $: "Left";
3
+ readonly value: A;
35
4
  };
36
- type Fail<E> = {
37
- readonly $: "Fail";
38
- readonly error: E;
5
+ type Right<B> = {
6
+ readonly $: "Right";
7
+ readonly value: B;
39
8
  };
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>;
9
+ declare const left: <A>(value: A) => Left<A>;
10
+ declare const right: <B>(value: B) => Right<B>;
11
+ type Either<A, B> = Left<A> | Right<B>;
12
+ declare const Either: {
13
+ left: <A>(value: A) => Left<A>;
14
+ right: <B>(value: B) => Right<B>;
15
+ new: <A, B>(value: B | null | undefined, error: A) => Either<A, B>;
16
+ isLeft: <A, B>(e: Either<A, B>) => e is Left<A>;
17
+ isRight: <A, B>(e: Either<A, B>) => e is Right<B>;
18
+ fromNullable: <A, B>(value: B | null | undefined, error: A) => Either<A, B>;
19
+ fromThrowable: <A, B>(fn: () => B, onError: (e: unknown) => A) => Either<A, B>;
20
+ fromPromise: <A, B>(promise: Promise<B>, onError: (e: unknown) => A) => Promise<Either<A, B>>;
21
+ map: <A, B, C>(e: Either<A, B>, fn: (r: B) => C) => Either<A, C>;
22
+ mapLeft: <A, B, C>(e: Either<A, B>, fn: (l: A) => C) => Either<C, B>;
23
+ bimap: <A, B, C, D>(e: Either<A, B>, fl: (l: A) => C, fr: (r: B) => D) => Either<C, D>;
24
+ flatMap: <A, B, C>(e: Either<A, B>, fn: (value: B) => Either<A, C>) => Either<A, C>;
25
+ chain: <A, B, C>(e: Either<A, B>, fn: (r: B) => Either<A, C>) => Either<A, C>;
26
+ fold: <A, B, C>(e: Either<A, B>, onLeft: (l: A) => C, onRight: (r: B) => C) => C;
27
+ match: <A, B, C>(e: Either<A, B>, matcher: {
28
+ right: (value: B) => C;
29
+ left: (value: A) => C;
30
+ }) => C;
31
+ swap: <A, B>(e: Either<A, B>) => Either<B, A>;
32
+ getOrElse: <A, B>(e: Either<A, B>, defaultValue: B) => B;
33
+ zip: <E, A, B>(a: Either<E, A>, b: Either<E, B>) => Either<E, [A, B]>;
34
+ apply: <E, A, B>(fn: Either<E, (value: A) => B>, arg: Either<E, A>) => Either<E, B>;
35
+ tap: <A, B>(e: Either<A, B>, f: (r: B) => void) => Either<A, B>;
36
+ tapLeft: <A, B>(e: Either<A, B>, f: (l: A) => void) => Either<A, B>;
63
37
  };
64
- declare const done: <T>(value: T) => Done<T>;
65
- declare const fail: <E>(error: E) => Fail<E>;
66
38
  declare global {
67
39
  interface Promise<T> {
68
- toResult<E = unknown>(onError?: (e: unknown) => E): Promise<Result<T, E>>;
40
+ toEither<A>(onError: (e: unknown) => A): Promise<Either<A, T>>;
69
41
  }
70
42
  }
71
43
 
44
+ type NothingNull = null;
45
+ type NothingUndefined = undefined;
46
+ type Nothing = NothingNull | NothingUndefined;
47
+ declare const just: <T>(value: T) => Maybe<T>;
48
+ declare const nothing: () => Nothing;
49
+ type Maybe<T> = T | Nothing;
50
+ declare const Maybe: {
51
+ just: <T>(value: T) => Maybe<T>;
52
+ nothing: () => Nothing;
53
+ new: <T>(v: T | null | undefined) => Maybe<T>;
54
+ nothingNull: () => NothingNull;
55
+ nothingUndefined: () => NothingUndefined;
56
+ isSome: <T>(m: Maybe<T>) => m is T;
57
+ isNothing: <T>(m: Maybe<T>) => m is Nothing;
58
+ isNothingNull: <T>(m: Maybe<T>) => m is NothingNull;
59
+ isNothingUndefined: <T>(m: Maybe<T>) => m is NothingUndefined;
60
+ fromNullable: <T>(v: T | null | undefined) => Maybe<T>;
61
+ toNullable: <T>(m: Maybe<T>) => T | null;
62
+ toUndefined: <T>(m: Maybe<T>) => T | undefined;
63
+ map: <T, U>(m: Maybe<T>, fn: (v: T) => U) => Maybe<U>;
64
+ flatMap: <T, U>(m: Maybe<T>, fn: (v: T) => Maybe<U>) => Maybe<U>;
65
+ filter: <T>(m: Maybe<T>, predicate: (value: T) => boolean) => Maybe<T>;
66
+ match: <T, U>(m: Maybe<T>, matcher: {
67
+ some: (v: T) => U;
68
+ nothing: () => U;
69
+ }) => U;
70
+ getOrElse: <T>(m: Maybe<T>, d: T) => T;
71
+ getOrUndefined: <T>(m: Maybe<T>) => T | undefined;
72
+ getOrThrow: <T>(m: Maybe<T>, error: Error) => T;
73
+ fromThrowable: <T>(fn: () => T) => Maybe<T>;
74
+ fromPromise: <T>(promise: Promise<T>) => Promise<Maybe<T>>;
75
+ zip: <T, U>(a: Maybe<T>, b: Maybe<U>) => Maybe<[T, U]>;
76
+ apply: <T, U>(fn: Maybe<(value: T) => U>, opt: Maybe<T>) => Maybe<U>;
77
+ orElse: <T>(opt: Maybe<T>, other: Maybe<T>) => Maybe<T>;
78
+ };
79
+
72
80
  type None = {
73
81
  readonly $: "None";
74
82
  };
@@ -76,11 +84,13 @@ type Some<T> = {
76
84
  readonly $: "Some";
77
85
  readonly value: T;
78
86
  };
87
+ declare const none: <T>() => Option<T>;
88
+ declare const some: <T>(value: T) => Option<T>;
79
89
  type Option<T> = None | Some<T>;
80
90
  declare const Option: {
81
91
  none: <T>() => Option<T>;
82
92
  some: <T>(value: T) => Option<T>;
83
- new: <T>(value: T) => Option<T>;
93
+ new: <T>(value: T | null | undefined) => Option<T>;
84
94
  isSome: <T>(option: Option<T>) => option is Some<T>;
85
95
  isNone: <T>(option: Option<T>) => option is None;
86
96
  fromNullable: <T>(value: T | null | undefined) => Option<T>;
@@ -100,78 +110,90 @@ declare const Option: {
100
110
  zip: <T, U>(a: Option<T>, b: Option<U>) => Option<[T, U]>;
101
111
  apply: <T, U>(fn: Option<(value: T) => U>, opt: Option<T>) => Option<U>;
102
112
  orElse: <T>(opt: Option<T>, other: Option<T>) => Option<T>;
103
- toResult: <T, E>(option: Option<T>, error: E) => Result<T, E>;
104
113
  };
105
- declare const none: <T>() => Option<T>;
106
- declare const some: <T>(value: T) => Option<T>;
107
114
  declare global {
108
115
  interface Array<T> {
109
116
  firstOption(): Option<T>;
110
117
  }
118
+ interface Promise<T> {
119
+ toOption(): Promise<Option<T>>;
120
+ }
111
121
  }
112
122
 
113
- type Left<A> = {
114
- readonly $: "Left";
115
- readonly value: A;
123
+ type Pair<A, B> = readonly [A, B];
124
+ declare const Pair: {
125
+ fst: <A, B>(p: Pair<A, B>) => A;
126
+ snd: <A, B>(p: Pair<A, B>) => B;
127
+ new: <A, B>(a: A, b: B) => Pair<A, B>;
128
+ fromArray: <A, B>([a, b]: [A, B]) => Pair<A, B>;
129
+ fromObject: <A, B>(obj: {
130
+ fst: A;
131
+ snd: B;
132
+ }) => Pair<A, B>;
133
+ curry: <A>(a: A) => <B>(b: B) => Pair<A, B>;
134
+ mapFirst: <A, B, C>(p: Pair<A, B>, fn: (a: A) => C) => Pair<C, B>;
135
+ mapSecond: <A, B, C>(p: Pair<A, B>, fn: (b: B) => C) => Pair<A, C>;
136
+ map: <A, B, C, D>(p: Pair<A, B>, fnA: (a: A) => C, fnB: (b: B) => D) => Pair<C, D>;
137
+ swap: <A, B>(p: Pair<A, B>) => Pair<B, A>;
138
+ apply: <A, B, C>(p: Pair<(a: A) => B, C>, value: A) => Pair<B, C>;
139
+ apply2: <A, B, C>(fnPair: Pair<(a: A) => B, (b: B) => C>, vPair: Pair<A, B>) => Pair<A, C>;
140
+ reduce: <A, B, C>(p: Pair<A, B>, fn: (a: A, b: B) => C) => C;
141
+ toArray: <A, B>(p: Pair<A, B>) => [A, B];
142
+ toObject: <A, B>(p: Pair<A, B>) => {
143
+ fst: A;
144
+ snd: B;
145
+ };
146
+ eq: <A, B>(p1: Pair<A, B>, p2: Pair<A, B>) => boolean;
147
+ equals: <A, B>(p1: Pair<A, B>, p2: Pair<A, B>, eqA: (a1: A, a2: A) => boolean, eqB: (b1: B, b2: B) => boolean) => boolean;
148
+ zip: <A, B, C, D>(p1: Pair<A, B>, p2: Pair<C, D>) => Pair<Pair<A, C>, Pair<B, D>>;
116
149
  };
117
- type Right<B> = {
118
- readonly $: "Right";
119
- readonly value: B;
150
+ declare const pair: <A, B>(a: A, b: B) => Pair<A, B>;
151
+
152
+ type Done<T> = {
153
+ readonly v: T;
120
154
  };
121
- type 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>;
155
+ type Fail<E> = {
156
+ readonly e: E;
157
+ };
158
+ declare const done: <T>(value: T) => Done<T>;
159
+ declare const fail: <E>(error: E) => Fail<E>;
160
+ type Result<T, E> = Done<T> | Fail<E>;
161
+ declare const Result: {
162
+ done: <T>(value: T) => Done<T>;
163
+ fail: <E>(error: E) => Fail<E>;
164
+ OK: <T>(value: T) => Done<T>;
165
+ Err: <E>(error: E) => Fail<E>;
166
+ new: <T, E>(value: T | null | undefined, error: E) => Result<T, E>;
167
+ isDone: <T, E>(r: Result<T, E>) => r is Done<T>;
168
+ isFail: <T, E>(r: Result<T, E>) => r is Fail<E>;
169
+ isOk: <T, E>(r: Result<T, E>) => r is Done<T>;
170
+ isErr: <T, E>(r: Result<T, E>) => r is Fail<E>;
171
+ fromNullable: <T, E>(value: T | null | undefined, error: E) => Result<T, E>;
172
+ fromThrowable: <T, E>(fn: () => T, onError: (e: unknown) => E) => Result<T, E>;
173
+ fromPromise: <T, E>(promise: Promise<T>, onError: (e: unknown) => E) => Promise<Result<T, E>>;
174
+ map: <T, E, U>(r: Result<T, E>, fn: (a: T) => U) => Result<U, E>;
175
+ mapErr: <T, E, F>(r: Result<T, E>, fn: (b: E) => F) => Result<T, F>;
176
+ bimap: <T, E, U, F>(r: Result<T, E>, onDone: (a: T) => U, onFail: (b: E) => F) => Result<U, F>;
177
+ flatMap: <T, E, C>(r: Result<T, E>, fn: (a: T) => Result<C, E>) => Result<C, E>;
178
+ match: <T, E, U>(r: Result<T, E>, matcher: {
179
+ done: (a: T) => U;
180
+ fail: (b: E) => U;
181
+ }) => U;
182
+ fold: <T, E, U>(r: Result<T, E>, onFail: (error: E) => U, onDone: (value: T) => U) => U;
183
+ recover: <T, E>(r: Result<T, E>, fn: (error: E) => T) => Result<T, E>;
184
+ getOrElse: <T, E>(r: Result<T, E>, d: T) => T;
185
+ getOrThrow: <T, E>(r: Result<T, E>, onError: (b: E) => never) => T;
186
+ zip: <T, U, E>(a: Result<T, E>, b: Result<U, E>) => Result<[T, U], E>;
187
+ apply: <T, U, E>(fn: Result<(a: T) => U, E>, arg: Result<T, E>) => Result<U, E>;
188
+ orElse: <T, E>(a: Result<T, E>, b: Result<T, E>) => Result<T, E>;
189
+ filter: <T, E>(r: Result<T, E>, predicate: (a: T) => boolean, onFalse: E) => Result<T, E>;
190
+ tap: <T, E>(r: Result<T, E>, f: (a: T) => void) => Result<T, E>;
191
+ tapErr: <T, E>(r: Result<T, E>, f: (b: E) => void) => Result<T, E>;
147
192
  };
148
193
  declare global {
149
194
  interface Promise<T> {
150
- toEither<A>(onError: (e: unknown) => A): Promise<Either<A, T>>;
195
+ toResult<E = unknown>(onError: (e: unknown) => E): Promise<Result<T, E>>;
151
196
  }
152
197
  }
153
- declare const left: <A>(value: A) => Left<A>;
154
- declare const right: <B>(value: B) => Right<B>;
155
-
156
- type Triple<A, B, C> = readonly [A, B, C];
157
- declare const Triple: {
158
- fst: <A, B, C>(t: Triple<A, B, C>) => A;
159
- snd: <A, B, C>(t: Triple<A, B, C>) => B;
160
- thd: <A, B, C>(t: Triple<A, B, C>) => C;
161
- new: <A, B, C>(fst: A, snd: B, thd: C) => Triple<A, B, C>;
162
- fromArray: <A, B, C>([fst, snd, thd]: [A, B, C]) => Triple<A, B, C>;
163
- toArray: <A, B, C>(t: Triple<A, B, C>) => [A, B, C];
164
- fromObject: <A, B, C>(obj: {
165
- fst: A;
166
- snd: B;
167
- thd: C;
168
- }) => Triple<A, B, C>;
169
- curry: <A>(fst: A) => <B>(snd: B) => <C>(thd: C) => Triple<A, B, C>;
170
- equals: <A, B, C>(t1: Triple<A, B, C>, t2: Triple<A, B, C>, eqA: (a1: A, a2: A) => boolean, eqB: (b1: B, b2: B) => boolean, eqC: (c1: C, c2: C) => boolean) => boolean;
171
- map: <A, B, C, D, E, F>(t: Triple<A, B, C>, fnA: (a: A) => D, fnB: (b: B) => E, fnC: (c: C) => F) => Triple<D, E, F>;
172
- zip: <A, B, C, D, E, F>(t1: Triple<A, B, C>, t2: Triple<D, E, F>) => Triple<A & D, B & E, C & F>;
173
- apply: <A, B, C, D>(t: Triple<(a: A) => B, (b: B) => C, (c: C) => D>, value: A) => Triple<B, C, D>;
174
- };
175
- declare const triple: <A, B, C>(fst: A, snd: B, thd: C) => Triple<A, B, C>;
176
198
 
177
- export { type Done, Either, type Fail, type Left, type None, Option, Pair, Result, type Right, type Some, Triple, done, fail, left, none, pair, right, some, triple };
199
+ export { type Done, Either, type Fail, type Left, Maybe, type None, type Nothing, type NothingNull, type NothingUndefined, Option, Pair, Result, type Right, type Some, done, fail, just, left, none, nothing, pair, right, some };
package/dist/index.js CHANGED
@@ -1,287 +1 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- Either: () => Either,
24
- Option: () => Option,
25
- Pair: () => Pair,
26
- Result: () => Result,
27
- Triple: () => Triple,
28
- done: () => done,
29
- fail: () => fail,
30
- left: () => left,
31
- none: () => none,
32
- pair: () => pair,
33
- right: () => right,
34
- some: () => some,
35
- triple: () => triple
36
- });
37
- module.exports = __toCommonJS(index_exports);
38
-
39
- // src/Result.ts
40
- var Result = {
41
- // Constructors
42
- done: (value) => ({ $: "Done", value }),
43
- fail: (error) => ({ $: "Fail", error }),
44
- new: (value, error) => value === void 0 || value === null ? { $: "Fail", error } : { $: "Done", value },
45
- // Guards
46
- isDone: (result) => result.$ === "Done",
47
- isFail: (result) => result.$ === "Fail",
48
- // Conversions
49
- fromNullable: (value, error) => value == null ? Result.fail(error) : Result.done(value),
50
- fromThrowable: (fn, onError) => {
51
- try {
52
- return Result.done(fn());
53
- } catch (e) {
54
- return Result.fail(onError(e));
55
- }
56
- },
57
- fromPromise: async (promise, onError) => {
58
- try {
59
- const value = await promise;
60
- return Result.done(value);
61
- } catch (e) {
62
- return Result.fail(onError(e));
63
- }
64
- },
65
- // Ops
66
- map: (result, fn) => result.$ === "Done" ? Result.done(fn(result.value)) : result,
67
- mapError: (result, fn) => result.$ === "Fail" ? Result.fail(fn(result.error)) : result,
68
- flatMap: (result, fn) => result.$ === "Done" ? fn(result.value) : result,
69
- match: (result, matcher) => result.$ === "Done" ? matcher.done(result.value) : matcher.fail(result.error),
70
- recover: (result, fn) => result.$ === "Fail" ? Result.done(fn(result.error)) : result,
71
- // Extract
72
- getOrElse: (result, defaultValue) => result.$ === "Done" ? result.value : defaultValue,
73
- getOrThrow: (result, onError) => result.$ === "Done" ? result.value : onError(result.error),
74
- // Combine
75
- zip: (a, b) => {
76
- if (a.$ === "Fail") return a;
77
- if (b.$ === "Fail") return b;
78
- return Result.done([a.value, b.value]);
79
- },
80
- apply: (fn, arg) => {
81
- if (fn.$ === "Fail") return fn;
82
- if (arg.$ === "Fail") return arg;
83
- return Result.done(fn.value(arg.value));
84
- },
85
- // Convert
86
- toOption: (result) => result.$ === "Done" ? some(result.value) : none()
87
- };
88
- var done = Result.done;
89
- var fail = Result.fail;
90
- Promise.prototype.toResult = function(onError) {
91
- const mapErr = (e) => onError ? onError(e) : e;
92
- return Result.fromPromise(this, mapErr);
93
- };
94
-
95
- // src/Option.ts
96
- var Option = {
97
- // Constructors
98
- none: () => ({ $: "None" }),
99
- some: (value) => ({ $: "Some", value }),
100
- new: (value) => value === void 0 || value === null ? { $: "None" } : { $: "Some", value },
101
- // Guards
102
- isSome: (option) => option.$ === "Some",
103
- isNone: (option) => option.$ === "None",
104
- // Conversions
105
- fromNullable: (value) => value == null ? Option.none() : Option.some(value),
106
- fromPredicate: (value, predicate) => predicate(value) ? Option.some(value) : Option.none(),
107
- fromThrowable: (fn) => {
108
- try {
109
- return Option.some(fn());
110
- } catch {
111
- return Option.none();
112
- }
113
- },
114
- fromPromise: async (promise) => {
115
- try {
116
- const value = await promise;
117
- return Option.some(value);
118
- } catch {
119
- return Option.none();
120
- }
121
- },
122
- // Ops
123
- map: (option, fn) => option.$ === "Some" ? Option.some(fn(option.value)) : Option.none(),
124
- flatMap: (option, fn) => option.$ === "Some" ? fn(option.value) : Option.none(),
125
- filter: (option, predicate) => option.$ === "Some" && predicate(option.value) ? option : Option.none(),
126
- match: (option, matcher) => option.$ === "Some" ? matcher.some(option.value) : matcher.none(),
127
- // Extract
128
- getOrElse: (option, defaultValue) => option.$ === "Some" ? option.value : defaultValue,
129
- getOrUndefined: (option) => option.$ === "Some" ? option.value : void 0,
130
- getOrThrow: (option, error) => {
131
- if (option.$ === "Some") return option.value;
132
- throw error;
133
- },
134
- // Combine
135
- zip: (a, b) => a.$ === "Some" && b.$ === "Some" ? Option.some([a.value, b.value]) : Option.none(),
136
- apply: (fn, opt) => fn.$ === "Some" && opt.$ === "Some" ? Option.some(fn.value(opt.value)) : Option.none(),
137
- orElse: (opt, other) => opt.$ === "Some" ? opt : other,
138
- // Convert
139
- toResult: (option, error) => option.$ === "Some" ? done(option.value) : fail(error)
140
- };
141
- var none = Option.none;
142
- var some = Option.some;
143
- Array.prototype.firstOption = function() {
144
- return this[0] ? some(this[0]) : none();
145
- };
146
-
147
- // src/Pair.ts
148
- var Pair = {
149
- fst: (p) => p[0],
150
- snd: (p) => p[1],
151
- new: (fst, snd) => [fst, snd],
152
- fromArray: ([fst, snd]) => [fst, snd],
153
- fromObject: (obj) => [obj.fst, obj.snd],
154
- curry: (fst) => (snd) => [fst, snd],
155
- mapFirst: (p, fn) => pair(fn(p[0]), p[1]),
156
- mapSecond: (p, fn) => pair(p[0], fn(p[1])),
157
- map: (p, fnA, fnB) => pair(fnA(p[0]), fnB(p[1])),
158
- swap: (p) => pair(p[1], p[0]),
159
- apply: (p, value) => pair(p[0](value), p[1]),
160
- apply2: (fnPair, vPair) => pair(vPair[0], fnPair[1](vPair[1])),
161
- reduce: (p, fn) => fn(p[0], p[1]),
162
- toArray: (p) => [p[0], p[1]],
163
- toObject: (p) => ({
164
- fst: p[0],
165
- snd: p[1]
166
- }),
167
- eq: (p1, p2) => p1[0] === p2[0] && p1[1] === p2[1],
168
- equals: (p1, p2, eqA, eqB) => eqA(p1[0], p2[0]) && eqB(p1[1], p2[1]),
169
- traverseOption: (p, fn) => p[1].$ === "Some" ? some(Pair.new(p[0], fn(p[0], p[1].value))) : none(),
170
- traverseResult: (p, fn) => p[1].$ === "Done" ? done(Pair.new(p[0], fn(p[0], p[1].value))) : fail(p[1].error),
171
- zip: (p1, p2) => pair({ ...p1[0], ...p2[0] }, pair(p1[1], p2[1]))
172
- };
173
- var pair = Pair.new;
174
-
175
- // src/Either.ts
176
- var Either = {
177
- // Constructors
178
- left: (value) => ({ $: "Left", value }),
179
- right: (value) => ({ $: "Right", value }),
180
- new: (rgt, lft) => rgt === void 0 || rgt === null ? Either.left(lft) : Either.right(rgt),
181
- // Guards
182
- isLeft: (e) => e.$ === "Left",
183
- isRight: (e) => e.$ === "Right",
184
- // Conversions
185
- fromNullable: (value, error) => value == null ? Either.left(error) : Either.right(value),
186
- fromThrowable: (fn, onError) => {
187
- try {
188
- return Either.right(fn());
189
- } catch (e) {
190
- return Either.left(onError(e));
191
- }
192
- },
193
- fromPromise: async (promise, onError) => {
194
- try {
195
- const value = await promise;
196
- return Either.right(value);
197
- } catch (e) {
198
- return Either.left(onError(e));
199
- }
200
- },
201
- // Ops
202
- map: (e, fn) => e.$ === "Right" ? Either.right(fn(e.value)) : e,
203
- mapLeft: (e, fn) => e.$ === "Left" ? Either.left(fn(e.value)) : e,
204
- bimap: (e, fl, fr) => e.$ === "Left" ? Either.left(fl(e.value)) : Either.right(fr(e.value)),
205
- flatMap: (e, fn) => e.$ === "Right" ? fn(e.value) : e,
206
- chain: (e, fn) => e.$ === "Right" ? fn(e.value) : e,
207
- fold: (e, onLeft, onRight) => e.$ === "Left" ? onLeft(e.value) : onRight(e.value),
208
- match: (e, matcher) => e.$ === "Right" ? matcher.right(e.value) : matcher.left(e.value),
209
- swap: (e) => e.$ === "Right" ? Either.left(e.value) : Either.right(e.value),
210
- // Extract
211
- getOrElse: (e, defaultValue) => e.$ === "Right" ? e.value : defaultValue,
212
- // Combine
213
- zip: (a, b) => {
214
- if (a.$ === "Left") return a;
215
- if (b.$ === "Left") return b;
216
- return Either.right(pair(a.value, b.value));
217
- },
218
- apply: (fn, arg) => {
219
- if (fn.$ === "Left") return fn;
220
- if (arg.$ === "Left") return arg;
221
- return Either.right(fn.value(arg.value));
222
- },
223
- tap: (e, f) => {
224
- if (e.$ === "Right") f(e.value);
225
- return e;
226
- },
227
- tapLeft: (e, f) => {
228
- if (e.$ === "Left") f(e.value);
229
- return e;
230
- }
231
- };
232
- Promise.prototype.toEither = async function(onError) {
233
- try {
234
- const v = await this;
235
- return right(v);
236
- } catch (e_1) {
237
- return left(onError(e_1));
238
- }
239
- };
240
- var left = Either.left;
241
- var right = Either.right;
242
-
243
- // src/Triple.ts
244
- var Triple = {
245
- fst: (t) => t[0],
246
- snd: (t) => t[1],
247
- thd: (t) => t[2],
248
- new: (fst, snd, thd) => [fst, snd, thd],
249
- fromArray: ([fst, snd, thd]) => [
250
- fst,
251
- snd,
252
- thd
253
- ],
254
- toArray: (t) => [t[0], t[1], t[2]],
255
- fromObject: (obj) => [
256
- obj.fst,
257
- obj.snd,
258
- obj.thd
259
- ],
260
- curry: (fst) => (snd) => (thd) => [fst, snd, thd],
261
- equals: (t1, t2, eqA, eqB, eqC) => eqA(t1[0], t2[0]) && eqB(t1[1], t2[1]) && eqC(t1[2], t2[2]),
262
- map: (t, fnA, fnB, fnC) => triple(fnA(t[0]), fnB(t[1]), fnC(t[2])),
263
- zip: (t1, t2) => triple(
264
- { ...t1[0], ...t2[0] },
265
- { ...t1[1], ...t2[1] },
266
- { ...t1[2], ...t2[2] }
267
- ),
268
- apply: (t, value) => triple(t[0](value), t[1](t[0](value)), t[2](t[1](t[0](value))))
269
- };
270
- var triple = Triple.new;
271
- // Annotate the CommonJS export names for ESM import in node:
272
- 0 && (module.exports = {
273
- Either,
274
- Option,
275
- Pair,
276
- Result,
277
- Triple,
278
- done,
279
- fail,
280
- left,
281
- none,
282
- pair,
283
- right,
284
- some,
285
- triple
286
- });
287
- //# sourceMappingURL=index.js.map
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var h=(e,t,o)=>new Promise((x,f)=>{var O=E=>{try{A(o.next(E))}catch(B){f(B)}},g=E=>{try{A(o.throw(E))}catch(B){f(B)}},A=E=>E.done?x(E.value):Promise.resolve(E.value).then(O,g);A((o=o.apply(e,t)).next())});var T=e=>({$:"Left",value:e}),i= exports.right =e=>({$:"Right",value:e}),C=e=>e.$==="Left",M=e=>e.$==="Right",y=(e,t)=>e==null?T(t):i(e),$=(e,t)=>{try{return i(e())}catch(o){return T(t(o))}},N=(e,t)=>h(null,null,function*(){try{let o=yield e;return i(o)}catch(o){return T(t(o))}}),w=(e,t)=>e.$==="Right"?i(t(e.value)):e,L=(e,t)=>e.$==="Left"?T(t(e.value)):e,S=(e,t,o)=>e.$==="Left"?T(t(e.value)):i(o(e.value)),D=(e,t)=>e.$==="Right"?t(e.value):e,k=(e,t)=>e.$==="Right"?t(e.value):e,F=(e,t,o)=>e.$==="Left"?t(e.value):o(e.value),z=(e,t)=>e.$==="Right"?t.right(e.value):t.left(e.value),j=e=>e.$==="Right"?T(e.value):i(e.value),q=(e,t)=>e.$==="Right"?e.value:t,K=(e,t)=>e.$==="Left"?e:t.$==="Left"?t:i([e.value,t.value]),_=(e,t)=>e.$==="Left"?e:t.$==="Left"?t:i(e.value(t.value)),G=(e,t)=>(e.$==="Right"&&t(e.value),e),H=(e,t)=>(e.$==="Left"&&t(e.value),e),I= exports.Either ={left:T,right:i,new:y,isLeft:C,isRight:M,fromNullable:y,fromThrowable:$,fromPromise:N,map:w,mapLeft:L,bimap:S,flatMap:D,chain:k,fold:F,match:z,swap:j,getOrElse:q,zip:K,apply:_,tap:G,tapLeft:H};Promise.prototype.toEither=function(e){return h(this,null,function*(){try{let t=yield this;return i(t)}catch(t){return T(e(t))}})};var d=e=>e,a= exports.nothing =()=>{},J=()=>null,v=()=>{},Q=e=>e!=null,V=e=>e==null,W=e=>e===null,X=e=>e===void 0,m=e=>e,Y=e=>e==null?null:e,Z=e=>e==null?void 0:e,ee=e=>{try{return e()}catch(t){return a()}},te=e=>e.then(t=>t,()=>a());var oe=(e,t)=>e==null?a():t(e),re=(e,t)=>e==null?a():t(e),ne=(e,t)=>e!=null&&t(e)?e:a(),ie=(e,t)=>e==null?t.nothing():t.some(e),se=(e,t)=>e==null?t:e,le=e=>e==null?v():e,Te=(e,t)=>{if(e!=null)return e;throw t},ae=(e,t)=>e!=null&&t!=null?[e,t]:a(),pe=(e,t)=>e!=null&&t!=null?e(t):a(),ue=(e,t)=>e!=null?e:t,ce= exports.Maybe ={just:d,nothing:a,new:m,nothingNull:J,nothingUndefined:v,isSome:Q,isNothing:V,isNothingNull:W,isNothingUndefined:X,fromNullable:m,toNullable:Y,toUndefined:Z,map:oe,flatMap:re,filter:ne,match:ie,getOrElse:se,getOrUndefined:le,getOrThrow:Te,fromThrowable:ee,fromPromise:te,zip:ae,apply:pe,orElse:ue};var r=()=>({$:"None"}),l= exports.some =e=>({$:"Some",value:e}),Ee=e=>e.$==="Some",xe=e=>e.$==="None",b=e=>e==null?r():l(e),Ae=(e,t)=>t(e)?l(e):r(),Be=e=>{try{return l(e())}catch(t){return r()}},U=e=>e.then(l,()=>r()),he=(e,t)=>e.$==="Some"?l(t(e.value)):r(),fe=(e,t)=>e.$==="Some"?t(e.value):r(),ye=(e,t)=>e.$==="Some"&&t(e.value)?e:r(),me=(e,t)=>e.$==="Some"?t.some(e.value):t.none(),de=(e,t)=>e.$==="Some"?e.value:t,ve=e=>e.$==="Some"?e.value:void 0,be=(e,t)=>{if(e.$==="Some")return e.value;throw t},Ue=(e,t)=>e.$==="Some"&&t.$==="Some"?l([e.value,t.value]):r(),Pe=(e,t)=>e.$==="Some"&&t.$==="Some"?l(e.value(t.value)):r(),Re=(e,t)=>e.$==="Some"?e:t,Oe= exports.Option ={none:r,some:l,new:b,isSome:Ee,isNone:xe,fromNullable:b,fromPredicate:Ae,fromThrowable:Be,fromPromise:U,map:he,flatMap:fe,filter:ye,match:me,getOrElse:de,getOrUndefined:ve,getOrThrow:be,zip:Ue,apply:Pe,orElse:Re};Array.prototype.firstOption=function(){return this[0]?l(this[0]):r()};Promise.prototype.toOption=function(){return U(this)};var ge=e=>e[0],Ce=e=>e[1],p=(e,t)=>[e,t],Me=([e,t])=>[e,t],$e=e=>[e.fst,e.snd],Ne=e=>t=>[e,t],we=(e,t)=>p(t(e[0]),e[1]),Le=(e,t)=>p(e[0],t(e[1])),Se=(e,t,o)=>p(t(e[0]),o(e[1])),De=e=>p(e[1],e[0]),ke=(e,t)=>p(e[0](t),e[1]),Fe=(e,t)=>p(t[0],e[1](t[1])),ze=(e,t)=>t(e[0],e[1]),je=e=>[e[0],e[1]],qe=e=>({fst:e[0],snd:e[1]}),Ke=(e,t)=>e[0]===t[0]&&e[1]===t[1],_e=(e,t,o,x)=>o(e[0],t[0])&&x(e[1],t[1]),Ge=(e,t)=>p([e[0],t[0]],[e[1],t[1]]),He= exports.Pair ={fst:ge,snd:Ce,new:p,fromArray:Me,fromObject:$e,curry:Ne,mapFirst:we,mapSecond:Le,map:Se,swap:De,apply:ke,apply2:Fe,reduce:ze,toArray:je,toObject:qe,eq:Ke,equals:_e,zip:Ge},Ie= exports.pair =p;var s=e=>({v:e}),u= exports.fail =e=>({e}),n=e=>"value"in e,c=e=>"error"in e,P=(e,t)=>e==null?u(t):s(e),Je=(e,t)=>{try{return s(e())}catch(o){return u(t(o))}},R=(e,t)=>e.then(s,o=>u(t(o))),Qe=(e,t)=>n(e)?s(t(e.v)):e,Ve=(e,t)=>c(e)?u(t(e.e)):e,We=(e,t,o)=>n(e)?s(t(e.v)):u(o(e.e)),Xe=(e,t)=>n(e)?t(e.v):e,Ye=(e,t)=>n(e)?t.done(e.v):t.fail(e.e),Ze=(e,t,o)=>n(e)?o(e.v):t(e.e),et=(e,t)=>c(e)?s(t(e.e)):e,tt=(e,t)=>n(e)?e.v:t;var ot=(e,t)=>n(e)?e.v:t(e.e),rt=(e,t)=>c(e)?e:c(t)?t:s([e.v,t.v]),nt=(e,t)=>c(e)?e:c(t)?t:s(e.v(t.v)),it=(e,t)=>n(e)?e:t,st=(e,t,o)=>n(e)?t(e.v)?e:u(o):e,lt=(e,t)=>(n(e)&&t(e.v),e),Tt=(e,t)=>(c(e)&&t(e.e),e),at= exports.Result ={done:s,fail:u,OK:s,Err:u,new:P,isDone:n,isFail:c,isOk:n,isErr:c,fromNullable:P,fromThrowable:Je,fromPromise:R,map:Qe,mapErr:Ve,bimap:We,flatMap:Xe,match:Ye,fold:Ze,recover:et,getOrElse:tt,getOrThrow:ot,zip:rt,apply:nt,orElse:it,filter:st,tap:lt,tapErr:Tt};Promise.prototype.toResult=function(e){return R(this,e)};exports.Either = I; exports.Maybe = ce; exports.Option = Oe; exports.Pair = He; exports.Result = at; exports.done = s; exports.fail = u; exports.just = d; exports.left = T; exports.none = r; exports.nothing = a; exports.pair = Ie; exports.right = i; exports.some = l;