-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
206 lines (181 loc) · 6 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
export type Result<T, E> = Ok<T> | Err<E>;
export class Ok<const T> {
public constructor(public readonly value: T) {}
}
export class Err<const E> extends Error {
public constructor(public readonly reason: E) {
super();
}
public override toString(): string {
return this.reason == null || this.reason.toString === Object.prototype.toString
? "Err"
: String(this.reason);
}
}
export function ok(): Ok<null>;
export function ok<const T>(value: T): Ok<T>;
export function ok<const T = null>(value: T = null as T): Ok<T> {
return new Ok(value);
}
export function err(): Err<null>;
export function err<const E>(reason: E): Err<E>;
export function err<const E = null>(reason: E = null as E): Err<E> {
return new Err(reason);
}
export function resultFrom<const T>(fn: () => T): Result<T, null>;
export function resultFrom<const T, const E>(
fn: () => T,
catchFn: (error: unknown) => E
): Result<T, E>;
export function resultFrom<const T, const E>(
fn: () => T,
catchFn: (error: unknown) => E = () => null as E
): Result<T, E> {
try {
return ok(fn());
} catch (reason: unknown) {
return err(catchFn(reason));
}
}
export function asyncResultFrom<const T>(fn: () => T | Promise<T>): Promise<Result<T, null>>;
export function asyncResultFrom<const T, const E>(
fn: () => T | Promise<T>,
catchFn: (error: unknown) => E | Promise<E>
): Promise<Result<T, E>>;
export async function asyncResultFrom<const T, const E = null>(
fn: () => T | Promise<T>,
catchFn: (error: unknown) => E | Promise<E> = () => null as E
): Promise<Result<T, E>> {
try {
return ok(await fn());
} catch (reason: unknown) {
return err(await catchFn(reason));
}
}
export function isOk<const T>(result: Result<T, unknown>): result is Ok<T> {
return result instanceof Ok;
}
export function isErr<const E>(result: Result<unknown, E>): result is Err<E> {
return result instanceof Err;
}
export function unwrapOk<const T>(result: Result<T, unknown>): T {
if (result instanceof Ok) {
return result.value;
} else {
throw result;
}
}
export function unwrapErr<const E>(result: Result<unknown, E>): E {
if (result instanceof Ok) {
throw new TypeError("Err expected");
} else {
return result.reason;
}
}
export function bindResult<const TInValue, const TOutValue, const TInReason, const TOutReason>(
result: Result<TInValue, TInReason>,
fn: (value: TInValue) => Result<TOutValue, TOutReason>
): Result<TOutValue, TInReason | TOutReason> {
if (result instanceof Ok) {
return fn(result.value);
} else {
return result;
}
}
export function bindResultFn<const TInValue, const TOutValue, const TOutReason>(
fn: (value: TInValue) => Result<TOutValue, TOutReason>
): <const TInReason>(
result: Result<TInValue, TInReason>
) => Result<TOutValue, TInReason | TOutReason> {
return result => bindResult(result, fn);
}
export function mapOk<const TInValue, const TOutValue, const TReason>(
result: Result<TInValue, TReason>,
fn: (value: TInValue) => TOutValue
): Result<TOutValue, TReason> {
if (result instanceof Ok) {
return ok(fn(result.value));
} else {
return result;
}
}
export function mapOkFn<const TInValue, const TOutValue>(
fn: (value: TInValue) => TOutValue
): <const TReason>(result: Result<TInValue, TReason>) => Result<TOutValue, TReason> {
return reason => mapOk(reason, fn);
}
export function mapErr<const TValue, const TInReason, const TOutReason>(
result: Result<TValue, TInReason>,
fn: (reason: TInReason) => TOutReason
): Result<TValue, TOutReason> {
if (result instanceof Ok) {
return result;
} else {
return err(fn(result.reason));
}
}
export function mapErrFn<const TInReason, const TOutReason>(
fn: (reason: TInReason) => TOutReason
): <const TValue>(result: Result<TValue, TInReason>) => Result<TValue, TOutReason> {
return result => mapErr(result, fn);
}
export function unwrapOkOr<const TValue, const TDefault>(
result: Result<TValue, unknown>,
defaultValue: TDefault
): TValue | TDefault {
if (result instanceof Ok) {
return result.value;
} else {
return defaultValue;
}
}
export function unwrapOkOrFn<const TDefault>(
defaultValue: TDefault
): <const TValue>(result: Result<TValue, unknown>) => TValue | TDefault {
return result => unwrapOkOr(result, defaultValue);
}
export function unwrapOkOrElse<const TInValue, const TOutValue, const TReason>(
result: Result<TInValue, TReason>,
elseFn: (reason: TReason) => TOutValue
): TInValue | TOutValue {
if (result instanceof Ok) {
return result.value;
} else {
return elseFn(result.reason);
}
}
export function unwrapOkOrElseFn<const TOutValue, const TReason>(
elseFn: (reason: TReason) => TOutValue
): <const TInValue>(result: Result<TInValue, TReason>) => TInValue | TOutValue {
return result => unwrapOkOrElse(result, elseFn);
}
export function unwrapErrOr<const TReason, const TDefault>(
result: Result<unknown, TReason>,
defaultReason: TDefault
): TReason | TDefault {
if (result instanceof Ok) {
return defaultReason;
} else {
return result.reason;
}
}
export function unwrapErrOrFn<const TDefault>(
defaultReason: TDefault
): <const TReason>(result: Result<unknown, TReason>) => TReason | TDefault {
return result => unwrapErrOr(result, defaultReason);
}
export function unwrapErrOrElse<const TValue, const TInReason, const TOutReason>(
result: Result<TValue, TInReason>,
elseFn: (value: TValue) => TOutReason
): TInReason | TOutReason {
if (result instanceof Ok) {
return elseFn(result.value);
} else {
return result.reason;
}
}
export function unwrapErrOrElseFn<const TValue, const TOutReason>(
elseFn: (value: TValue) => TOutReason
): <const TInReason>(result: Result<TValue, TInReason>) => TInReason | TOutReason {
return result => unwrapErrOrElse(result, elseFn);
}