resultar 1.4.4 → 2.0.0-beta.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/README.md +588 -1417
- package/dist/index.d.ts +532 -1
- package/dist/index.js +841 -36
- package/dist/index.js.map +1 -1
- package/package.json +61 -80
- package/dist/chunk-G4XXW6QA.js +0 -717
- package/dist/chunk-G4XXW6QA.js.map +0 -1
- package/dist/chunk-VBI3T5VS.js +0 -19
- package/dist/chunk-VBI3T5VS.js.map +0 -1
- package/dist/error.cjs +0 -43
- package/dist/error.cjs.map +0 -1
- package/dist/error.d.cts +0 -1
- package/dist/error.d.ts +0 -1
- package/dist/error.js +0 -7
- package/dist/error.js.map +0 -1
- package/dist/index-lTU9Lum3.d.cts +0 -612
- package/dist/index-lTU9Lum3.d.ts +0 -612
- package/dist/index.cjs +0 -763
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -1
- package/dist/result-async.cjs +0 -744
- package/dist/result-async.cjs.map +0 -1
- package/dist/result-async.d.cts +0 -1
- package/dist/result-async.d.ts +0 -1
- package/dist/result-async.js +0 -26
- package/dist/result-async.js.map +0 -1
- package/dist/result.cjs +0 -736
- package/dist/result.cjs.map +0 -1
- package/dist/result.d.cts +0 -1
- package/dist/result.d.ts +0 -1
- package/dist/result.js +0 -22
- package/dist/result.js.map +0 -1
- package/dist/utils.cjs +0 -721
- package/dist/utils.cjs.map +0 -1
- package/dist/utils.d.cts +0 -1
- package/dist/utils.d.ts +0 -1
- package/dist/utils.js +0 -14
- package/dist/utils.js.map +0 -1
package/dist/utils.cjs
DELETED
|
@@ -1,721 +0,0 @@
|
|
|
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/utils.ts
|
|
21
|
-
var utils_exports = {};
|
|
22
|
-
__export(utils_exports, {
|
|
23
|
-
combineResultAsyncList: () => combineResultAsyncList,
|
|
24
|
-
combineResultAsyncListWithAllErrors: () => combineResultAsyncListWithAllErrors,
|
|
25
|
-
combineResultList: () => combineResultList,
|
|
26
|
-
combineResultListWithAllErrors: () => combineResultListWithAllErrors
|
|
27
|
-
});
|
|
28
|
-
module.exports = __toCommonJS(utils_exports);
|
|
29
|
-
|
|
30
|
-
// src/error.ts
|
|
31
|
-
var defaultErrorConfig = {
|
|
32
|
-
withStackTrace: false
|
|
33
|
-
};
|
|
34
|
-
var createResultarError = (message, result, config = defaultErrorConfig) => {
|
|
35
|
-
const data = result.isOk() ? { type: "Ok", value: result.value } : { type: "Err", value: result.error };
|
|
36
|
-
const maybeStack = config.withStackTrace ? new Error().stack : void 0;
|
|
37
|
-
return {
|
|
38
|
-
name: "ResultarError",
|
|
39
|
-
data,
|
|
40
|
-
message,
|
|
41
|
-
stack: maybeStack
|
|
42
|
-
};
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
// src/result.ts
|
|
46
|
-
var Ok = class {
|
|
47
|
-
constructor(value) {
|
|
48
|
-
this.value = value;
|
|
49
|
-
}
|
|
50
|
-
ok = true;
|
|
51
|
-
};
|
|
52
|
-
var Err = class {
|
|
53
|
-
constructor(error) {
|
|
54
|
-
this.error = error;
|
|
55
|
-
}
|
|
56
|
-
ok = false;
|
|
57
|
-
};
|
|
58
|
-
var Result = class _Result {
|
|
59
|
-
constructor(state) {
|
|
60
|
-
this.state = state;
|
|
61
|
-
if (state.ok) {
|
|
62
|
-
this.value = state.value;
|
|
63
|
-
this.error = void 0;
|
|
64
|
-
} else {
|
|
65
|
-
this.error = state.error;
|
|
66
|
-
this.value = void 0;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Creates a `Result` by running a function that might throw.
|
|
71
|
-
* If the function throws, the error will be caught and returned as an `Err`.
|
|
72
|
-
* If the function succeeds, the value will be wrapped in an `Ok`.
|
|
73
|
-
*
|
|
74
|
-
* @param fn - The function to execute, which might throw an error
|
|
75
|
-
* @param errorFn - Optional function to transform the thrown error before wrapping in `Err`
|
|
76
|
-
* @returns A `Result` containing either the function's return value or the caught error
|
|
77
|
-
*/
|
|
78
|
-
static tryCatch(fn, errorFn) {
|
|
79
|
-
try {
|
|
80
|
-
return ok(fn());
|
|
81
|
-
} catch (error) {
|
|
82
|
-
if (errorFn) {
|
|
83
|
-
return err(errorFn(error));
|
|
84
|
-
}
|
|
85
|
-
return err(error);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Wraps a function with a try catch, creating a new function with the same
|
|
90
|
-
* arguments but returning `Ok` if successful, `Err` if the function throws
|
|
91
|
-
*
|
|
92
|
-
* @param fn function to wrap with ok on success or err on failure
|
|
93
|
-
* @param errorFn when an error is thrown, this will wrap the error result if provided
|
|
94
|
-
*/
|
|
95
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
96
|
-
static fromThrowable(fn, errorFn) {
|
|
97
|
-
return (...args) => {
|
|
98
|
-
try {
|
|
99
|
-
const result = fn(...args);
|
|
100
|
-
return ok(result);
|
|
101
|
-
} catch (error) {
|
|
102
|
-
if (errorFn) {
|
|
103
|
-
return err(errorFn(error));
|
|
104
|
-
}
|
|
105
|
-
return err(error);
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
static ok(value) {
|
|
110
|
-
return new _Result(new Ok(value));
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Creates a new `Result` instance representing a successful operation with an undefined value.
|
|
114
|
-
*
|
|
115
|
-
* @return {Result<undefined, E>} A new `Result` instance with an undefined value.
|
|
116
|
-
*/
|
|
117
|
-
static unit() {
|
|
118
|
-
return new _Result(new Ok(void 0));
|
|
119
|
-
}
|
|
120
|
-
static err(error) {
|
|
121
|
-
return new _Result(new Err(error));
|
|
122
|
-
}
|
|
123
|
-
static combine(resultList) {
|
|
124
|
-
return combineResultList(resultList);
|
|
125
|
-
}
|
|
126
|
-
static combineWithAllErrors(resultList) {
|
|
127
|
-
return combineResultListWithAllErrors(
|
|
128
|
-
resultList
|
|
129
|
-
);
|
|
130
|
-
}
|
|
131
|
-
value;
|
|
132
|
-
error;
|
|
133
|
-
/**
|
|
134
|
-
* Used to check if a `Result` is an `OK`
|
|
135
|
-
*
|
|
136
|
-
* @returns `true` if the result is an `OK` variant of Result
|
|
137
|
-
*/
|
|
138
|
-
isOk() {
|
|
139
|
-
return this.state.ok;
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Used to check if a `Result` is an `Err`
|
|
143
|
-
*
|
|
144
|
-
* @returns `true` if the result is an `Err` variant of Result
|
|
145
|
-
*/
|
|
146
|
-
isErr() {
|
|
147
|
-
return !this.state.ok;
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Maps a `Result<T, E>` to `Result<U, E>`
|
|
151
|
-
* by applying a function to a contained `Ok` value, leaving an `Err` value
|
|
152
|
-
* untouched.
|
|
153
|
-
*
|
|
154
|
-
* @param f The function to apply an `OK` value
|
|
155
|
-
* @returns the result of applying `f` or an `Err` untouched
|
|
156
|
-
*/
|
|
157
|
-
map(f) {
|
|
158
|
-
if (this.state.ok) {
|
|
159
|
-
const value = f(this.value);
|
|
160
|
-
return ok(value);
|
|
161
|
-
}
|
|
162
|
-
return err(this.error);
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a
|
|
166
|
-
* contained `Err` value, leaving an `Ok` value untouched.
|
|
167
|
-
*
|
|
168
|
-
* This function can be used to pass through a successful result while
|
|
169
|
-
* handling an error.
|
|
170
|
-
*
|
|
171
|
-
* @param f a function to apply to the error `Err` value
|
|
172
|
-
*/
|
|
173
|
-
mapErr(fn) {
|
|
174
|
-
if (this.state.ok) {
|
|
175
|
-
return ok(this.value);
|
|
176
|
-
}
|
|
177
|
-
return err(fn(this.error));
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* Similar to `map` Except you must return a new `Result`.
|
|
181
|
-
*
|
|
182
|
-
* This is useful for when you need to do a subsequent computation using the
|
|
183
|
-
* inner `T` value, but that computation might fail.
|
|
184
|
-
* Additionally, `andThen` is really useful as a tool to flatten a
|
|
185
|
-
* `Result<Result<A, E2>, E1>` into a `Result<A, E2>` (see example below).
|
|
186
|
-
*
|
|
187
|
-
* @param f The function to apply to the current value
|
|
188
|
-
*/
|
|
189
|
-
andThen(f) {
|
|
190
|
-
if (this.state.ok) {
|
|
191
|
-
return f(this.value);
|
|
192
|
-
}
|
|
193
|
-
return err(this.error);
|
|
194
|
-
}
|
|
195
|
-
if(fCondition) {
|
|
196
|
-
return {
|
|
197
|
-
true: (fTrue) => ({
|
|
198
|
-
false: (fFalse) => {
|
|
199
|
-
if (this.state.ok) {
|
|
200
|
-
const condition = fCondition(this.value);
|
|
201
|
-
return condition ? fTrue(this.value) : fFalse(this.value);
|
|
202
|
-
}
|
|
203
|
-
return err(this.error);
|
|
204
|
-
}
|
|
205
|
-
})
|
|
206
|
-
};
|
|
207
|
-
}
|
|
208
|
-
orElse(f) {
|
|
209
|
-
if (this.state.ok) {
|
|
210
|
-
return ok(this.value);
|
|
211
|
-
}
|
|
212
|
-
return f(this.error);
|
|
213
|
-
}
|
|
214
|
-
asyncAndThen(f) {
|
|
215
|
-
if (this.state.ok) {
|
|
216
|
-
return f(this.value);
|
|
217
|
-
}
|
|
218
|
-
return errAsync(this.error);
|
|
219
|
-
}
|
|
220
|
-
/**
|
|
221
|
-
* Maps a `Result<T, E>` to `ResultAsync<U, E>`
|
|
222
|
-
* by applying an async function to a contained `Ok` value, leaving an `Err`
|
|
223
|
-
* value untouched.
|
|
224
|
-
*
|
|
225
|
-
* @param f An async function to apply an `OK` value
|
|
226
|
-
*/
|
|
227
|
-
asyncMap(f) {
|
|
228
|
-
if (this.state.ok) {
|
|
229
|
-
return ResultAsync.fromSafePromise(f(this.value));
|
|
230
|
-
}
|
|
231
|
-
return errAsync(this.error);
|
|
232
|
-
}
|
|
233
|
-
/**
|
|
234
|
-
* Unwrap the `Ok` value, or return the default if there is an `Err`
|
|
235
|
-
*
|
|
236
|
-
* @param v the default value to return if there is an `Err`
|
|
237
|
-
*/
|
|
238
|
-
unwrapOr(defaultValue) {
|
|
239
|
-
if (this.state.ok) {
|
|
240
|
-
return this.value;
|
|
241
|
-
}
|
|
242
|
-
return defaultValue;
|
|
243
|
-
}
|
|
244
|
-
/**
|
|
245
|
-
* Given 2 functions (one for the `Ok` variant and one for the `Err` variant)
|
|
246
|
-
* execute the function that matches the `Result` variant.
|
|
247
|
-
*
|
|
248
|
-
* Match callbacks do not necessitate to return a `Result`, however you can
|
|
249
|
-
* return a `Result` if you want to.
|
|
250
|
-
*
|
|
251
|
-
* `match` is like chaining `map` and `mapErr`, with the distinction that
|
|
252
|
-
* with `match` both functions must have the same return type.
|
|
253
|
-
*
|
|
254
|
-
* @param ok
|
|
255
|
-
* @param err
|
|
256
|
-
*/
|
|
257
|
-
match(fnOk, fnErr) {
|
|
258
|
-
if (this.state.ok) {
|
|
259
|
-
return fnOk(this.value);
|
|
260
|
-
}
|
|
261
|
-
return fnErr(this.error);
|
|
262
|
-
}
|
|
263
|
-
/**
|
|
264
|
-
* Performs a side effect for the `Ok` or `Err` variant of `Result`.
|
|
265
|
-
*
|
|
266
|
-
* @param fn The function to apply an `OK` value
|
|
267
|
-
* @returns the result of applying `f` or an `Err` untouched
|
|
268
|
-
*/
|
|
269
|
-
log(fn) {
|
|
270
|
-
try {
|
|
271
|
-
fn(this.value, this.error);
|
|
272
|
-
} catch {
|
|
273
|
-
}
|
|
274
|
-
return this;
|
|
275
|
-
}
|
|
276
|
-
/**
|
|
277
|
-
* Performs a side effect for the `Ok` variant of `Result`.
|
|
278
|
-
*
|
|
279
|
-
* @param fn The function to apply an `OK` value
|
|
280
|
-
* @returns the result of applying `f` or an `Err` untouched
|
|
281
|
-
*/
|
|
282
|
-
tap(fn) {
|
|
283
|
-
if (this.state.ok) {
|
|
284
|
-
try {
|
|
285
|
-
fn(this.value);
|
|
286
|
-
} catch {
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
return this;
|
|
290
|
-
}
|
|
291
|
-
/**
|
|
292
|
-
* Performs a side effect for the `Err` variant of `Result`.
|
|
293
|
-
*
|
|
294
|
-
* @param fn The function to apply an `Err` value
|
|
295
|
-
* @returns the result of applying `f` or an `Ok` untouched
|
|
296
|
-
*/
|
|
297
|
-
tapError(fn) {
|
|
298
|
-
if (!this.state.ok) {
|
|
299
|
-
try {
|
|
300
|
-
fn(this.error);
|
|
301
|
-
} catch {
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
return this;
|
|
305
|
-
}
|
|
306
|
-
finally(f) {
|
|
307
|
-
const resultDisposable = new DisposableResult(this, f);
|
|
308
|
-
try {
|
|
309
|
-
return resultDisposable;
|
|
310
|
-
} finally {
|
|
311
|
-
resultDisposable[Symbol.dispose]();
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
unwrapOrThrow() {
|
|
315
|
-
if (this.state.ok) {
|
|
316
|
-
return this.value;
|
|
317
|
-
}
|
|
318
|
-
throw this.error;
|
|
319
|
-
}
|
|
320
|
-
/**
|
|
321
|
-
* **This method is unsafe, and should only be used in a test environments**
|
|
322
|
-
*
|
|
323
|
-
* Takes a `Result<T, E>` and returns a `T` when the result is an `Ok`, otherwise it throws a custom object.
|
|
324
|
-
*
|
|
325
|
-
* @param config
|
|
326
|
-
*/
|
|
327
|
-
_unsafeUnwrap(config) {
|
|
328
|
-
if (this.state.ok) {
|
|
329
|
-
return this.value;
|
|
330
|
-
}
|
|
331
|
-
throw createResultarError("Called `_unsafeUnwrap` on an Err", this, config);
|
|
332
|
-
}
|
|
333
|
-
/**
|
|
334
|
-
* **This method is unsafe, and should only be used in a test environments**
|
|
335
|
-
*
|
|
336
|
-
* takes a `Result<T, E>` and returns a `E` when the result is an `Err`,
|
|
337
|
-
* otherwise it throws a custom object.
|
|
338
|
-
*
|
|
339
|
-
* @param config
|
|
340
|
-
*/
|
|
341
|
-
_unsafeUnwrapErr(config) {
|
|
342
|
-
if (this.state.ok) {
|
|
343
|
-
throw createResultarError(
|
|
344
|
-
"Called `_unsafeUnwrapErr` on an Ok",
|
|
345
|
-
this,
|
|
346
|
-
config
|
|
347
|
-
);
|
|
348
|
-
}
|
|
349
|
-
return this.error;
|
|
350
|
-
}
|
|
351
|
-
*[Symbol.iterator]() {
|
|
352
|
-
if (this.state.ok) {
|
|
353
|
-
return this.value;
|
|
354
|
-
}
|
|
355
|
-
const self = this;
|
|
356
|
-
yield self;
|
|
357
|
-
return self;
|
|
358
|
-
}
|
|
359
|
-
safeUnwrap() {
|
|
360
|
-
if (this.state.ok) {
|
|
361
|
-
const { value } = this;
|
|
362
|
-
return function* () {
|
|
363
|
-
return value;
|
|
364
|
-
}();
|
|
365
|
-
}
|
|
366
|
-
const { error } = this;
|
|
367
|
-
return function* () {
|
|
368
|
-
yield err(error);
|
|
369
|
-
throw new Error("Do not use this generator out of `safeTry`");
|
|
370
|
-
}();
|
|
371
|
-
}
|
|
372
|
-
};
|
|
373
|
-
var DisposableResult = class {
|
|
374
|
-
constructor(result, finalizer) {
|
|
375
|
-
this.result = result;
|
|
376
|
-
this.finalizer = finalizer;
|
|
377
|
-
}
|
|
378
|
-
get value() {
|
|
379
|
-
return this.result.value;
|
|
380
|
-
}
|
|
381
|
-
get error() {
|
|
382
|
-
return this.result.error;
|
|
383
|
-
}
|
|
384
|
-
_unsafeUnwrap(config) {
|
|
385
|
-
return this.result._unsafeUnwrap(config);
|
|
386
|
-
}
|
|
387
|
-
_unsafeUnwrapErr(config) {
|
|
388
|
-
return this.result._unsafeUnwrapErr(config);
|
|
389
|
-
}
|
|
390
|
-
isOk() {
|
|
391
|
-
return this.result.isOk();
|
|
392
|
-
}
|
|
393
|
-
isErr() {
|
|
394
|
-
return this.result.isErr();
|
|
395
|
-
}
|
|
396
|
-
unwrapOr(defaultValue) {
|
|
397
|
-
return this.result.unwrapOr(defaultValue);
|
|
398
|
-
}
|
|
399
|
-
[Symbol.dispose]() {
|
|
400
|
-
this.finalizer(this.result.value, this.result.error);
|
|
401
|
-
}
|
|
402
|
-
};
|
|
403
|
-
var ok = Result.ok;
|
|
404
|
-
var err = Result.err;
|
|
405
|
-
var fromThrowable = Result.fromThrowable;
|
|
406
|
-
var unit = Result.unit;
|
|
407
|
-
var tryCatch = Result.tryCatch;
|
|
408
|
-
|
|
409
|
-
// src/result-async.ts
|
|
410
|
-
var DisposableResultAsync = class {
|
|
411
|
-
innerPromise;
|
|
412
|
-
constructor(res) {
|
|
413
|
-
this.innerPromise = res;
|
|
414
|
-
}
|
|
415
|
-
then(successCallback, failureCallback) {
|
|
416
|
-
return this.innerPromise.then(successCallback, failureCallback);
|
|
417
|
-
}
|
|
418
|
-
};
|
|
419
|
-
var ResultAsync = class _ResultAsync {
|
|
420
|
-
static okAsync(value) {
|
|
421
|
-
return new _ResultAsync(Promise.resolve(Result.ok(value)));
|
|
422
|
-
}
|
|
423
|
-
/**
|
|
424
|
-
* Returns a ResultAsync that is immediately resolved with a Result.ok(undefined) value.
|
|
425
|
-
*
|
|
426
|
-
* @return {ResultAsync<undefined, E>} A ResultAsync instance with undefined as the value type and E as the error type.
|
|
427
|
-
*/
|
|
428
|
-
static unitAsync() {
|
|
429
|
-
return new _ResultAsync(Promise.resolve(Result.unit()));
|
|
430
|
-
}
|
|
431
|
-
static errAsync(error) {
|
|
432
|
-
return new _ResultAsync(Promise.resolve(Result.err(error)));
|
|
433
|
-
}
|
|
434
|
-
/**
|
|
435
|
-
* Creates a ResultAsync from a Promise, catching any errors that occur during its execution.
|
|
436
|
-
*
|
|
437
|
-
* @param fn - The Promise to be wrapped in a ResultAsync.
|
|
438
|
-
* @param errorFn - Optional function to transform the caught error into a specific error type.
|
|
439
|
-
* If not provided, the original error will be used.
|
|
440
|
-
* @returns A ResultAsync that will resolve to Ok with the promise's value if successful,
|
|
441
|
-
* or Err with either the transformed error (if errorFn is provided) or the original error.
|
|
442
|
-
*
|
|
443
|
-
* @example
|
|
444
|
-
* ```typescript
|
|
445
|
-
* // Without error transformer
|
|
446
|
-
* const result = ResultAsync.tryCatch(Promise.resolve(42));
|
|
447
|
-
*
|
|
448
|
-
* // With error transformer
|
|
449
|
-
* const result = ResultAsync.tryCatch(
|
|
450
|
-
* fetch('https://api.example.com'),
|
|
451
|
-
* (error) => new CustomError('API request failed')
|
|
452
|
-
* );
|
|
453
|
-
* ```
|
|
454
|
-
*/
|
|
455
|
-
static tryCatch(fn, errorFn) {
|
|
456
|
-
const newPromise = fn.then((value) => Result.ok(value)).catch((error) => {
|
|
457
|
-
if (errorFn) {
|
|
458
|
-
return Result.err(errorFn(error));
|
|
459
|
-
}
|
|
460
|
-
return Result.err(error);
|
|
461
|
-
});
|
|
462
|
-
return new _ResultAsync(newPromise);
|
|
463
|
-
}
|
|
464
|
-
static fromSafePromise(promise) {
|
|
465
|
-
const newPromise = promise.then((value) => Result.ok(value));
|
|
466
|
-
return new _ResultAsync(newPromise);
|
|
467
|
-
}
|
|
468
|
-
static fromPromise(promise, errorFn) {
|
|
469
|
-
const newPromise = promise.then((value) => Result.ok(value)).catch((error) => Result.err(errorFn(error)));
|
|
470
|
-
return new _ResultAsync(newPromise);
|
|
471
|
-
}
|
|
472
|
-
static combine(asyncResultList) {
|
|
473
|
-
return combineResultAsyncList(asyncResultList);
|
|
474
|
-
}
|
|
475
|
-
static combineWithAllErrors(asyncResultList) {
|
|
476
|
-
return combineResultAsyncListWithAllErrors(
|
|
477
|
-
asyncResultList
|
|
478
|
-
);
|
|
479
|
-
}
|
|
480
|
-
/**
|
|
481
|
-
* Wraps a async function with a try catch, creating a new function with the same
|
|
482
|
-
* arguments but returning `Ok` if successful, `Err` if the function throws
|
|
483
|
-
*
|
|
484
|
-
* @param fn function to wrap with ok on success or err on failure
|
|
485
|
-
* @param errorFn when an error is thrown, this will wrap the error result if provided
|
|
486
|
-
* @returns a new function that returns a `ResultAsync`
|
|
487
|
-
*/
|
|
488
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
489
|
-
static fromThrowable(fn, errorFn) {
|
|
490
|
-
return (...args) => new _ResultAsync(
|
|
491
|
-
(async () => {
|
|
492
|
-
try {
|
|
493
|
-
const v = await fn(...args);
|
|
494
|
-
return Result.ok(v);
|
|
495
|
-
} catch (error) {
|
|
496
|
-
const e = errorFn ? errorFn(error) : error;
|
|
497
|
-
return Result.err(e);
|
|
498
|
-
}
|
|
499
|
-
})()
|
|
500
|
-
);
|
|
501
|
-
}
|
|
502
|
-
innerPromise;
|
|
503
|
-
constructor(res) {
|
|
504
|
-
this.innerPromise = res;
|
|
505
|
-
}
|
|
506
|
-
then(successCallback, failureCallback) {
|
|
507
|
-
return this.innerPromise.then(successCallback, failureCallback);
|
|
508
|
-
}
|
|
509
|
-
mapErr(f) {
|
|
510
|
-
return new _ResultAsync(this.innerPromise.then(async (res) => {
|
|
511
|
-
if (res.isOk()) {
|
|
512
|
-
return okAsync(res.value);
|
|
513
|
-
}
|
|
514
|
-
return errAsync(await f(res.error));
|
|
515
|
-
}));
|
|
516
|
-
}
|
|
517
|
-
map(f) {
|
|
518
|
-
const x = new _ResultAsync(
|
|
519
|
-
this.innerPromise.then(async (res) => {
|
|
520
|
-
if (res.isErr()) {
|
|
521
|
-
return errAsync(res.error);
|
|
522
|
-
}
|
|
523
|
-
return okAsync(await f(res.value));
|
|
524
|
-
})
|
|
525
|
-
);
|
|
526
|
-
return x;
|
|
527
|
-
}
|
|
528
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
529
|
-
andThen(f) {
|
|
530
|
-
return new _ResultAsync(
|
|
531
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
532
|
-
this.innerPromise.then(async (res) => {
|
|
533
|
-
if (res.isErr()) {
|
|
534
|
-
return errAsync(res.error);
|
|
535
|
-
}
|
|
536
|
-
const newValue = f(res.value);
|
|
537
|
-
return newValue instanceof _ResultAsync ? newValue.innerPromise : newValue;
|
|
538
|
-
})
|
|
539
|
-
);
|
|
540
|
-
}
|
|
541
|
-
if(fCondition) {
|
|
542
|
-
return {
|
|
543
|
-
true: (fTrue) => ({
|
|
544
|
-
false: (fFalse) => new _ResultAsync(this.innerPromise.then(async (res) => {
|
|
545
|
-
if (res.isOk()) {
|
|
546
|
-
const condition = fCondition(res.value);
|
|
547
|
-
return condition ? fTrue(res.value) : fFalse(res.value);
|
|
548
|
-
}
|
|
549
|
-
return errAsync(res.error);
|
|
550
|
-
}))
|
|
551
|
-
})
|
|
552
|
-
};
|
|
553
|
-
}
|
|
554
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
555
|
-
orElse(f) {
|
|
556
|
-
return new _ResultAsync(
|
|
557
|
-
this.innerPromise.then(async (res) => {
|
|
558
|
-
if (res.isErr()) {
|
|
559
|
-
return f(res.error);
|
|
560
|
-
}
|
|
561
|
-
return okAsync(res.value);
|
|
562
|
-
})
|
|
563
|
-
);
|
|
564
|
-
}
|
|
565
|
-
async match(ok2, fnErr) {
|
|
566
|
-
return this.innerPromise.then((res) => res.match(ok2, fnErr));
|
|
567
|
-
}
|
|
568
|
-
async unwrapOr(t) {
|
|
569
|
-
return this.innerPromise.then((res) => res.unwrapOr(t));
|
|
570
|
-
}
|
|
571
|
-
async unwrapOrThrow() {
|
|
572
|
-
return this.innerPromise.then((res) => res.unwrapOrThrow());
|
|
573
|
-
}
|
|
574
|
-
/**
|
|
575
|
-
* Performs a side effect for the `Ok` variant of `ResultAsync`.
|
|
576
|
-
* This function can be used for control flow based on result values.
|
|
577
|
-
* @param f The function to call if the result is `Ok`
|
|
578
|
-
* @returns `self` if the result is `Err`, otherwise the result of `f`
|
|
579
|
-
*/
|
|
580
|
-
log(f) {
|
|
581
|
-
return new _ResultAsync(
|
|
582
|
-
this.innerPromise.then(async (res) => {
|
|
583
|
-
if (res.isOk()) {
|
|
584
|
-
try {
|
|
585
|
-
await f(res.value);
|
|
586
|
-
} catch {
|
|
587
|
-
}
|
|
588
|
-
return okAsync(res.value);
|
|
589
|
-
}
|
|
590
|
-
try {
|
|
591
|
-
await f(void 0, res.error);
|
|
592
|
-
} catch {
|
|
593
|
-
}
|
|
594
|
-
return errAsync(res.error);
|
|
595
|
-
})
|
|
596
|
-
);
|
|
597
|
-
}
|
|
598
|
-
/**
|
|
599
|
-
* Performs a side effect for the `Ok` variant of `ResultAsync`.
|
|
600
|
-
* This function can be used for control flow based on result values.
|
|
601
|
-
* @param f The function to call if the result is `Ok`
|
|
602
|
-
* @returns `self` if the result is `Err`, otherwise the result of `f`
|
|
603
|
-
*/
|
|
604
|
-
tap(f) {
|
|
605
|
-
return new _ResultAsync(
|
|
606
|
-
this.innerPromise.then(async (res) => {
|
|
607
|
-
if (res.isErr()) {
|
|
608
|
-
return errAsync(res.error);
|
|
609
|
-
}
|
|
610
|
-
try {
|
|
611
|
-
await f(res.value);
|
|
612
|
-
} catch {
|
|
613
|
-
}
|
|
614
|
-
return okAsync(res.value);
|
|
615
|
-
})
|
|
616
|
-
);
|
|
617
|
-
}
|
|
618
|
-
/**
|
|
619
|
-
* Performs a side effect for the `Err` variant of `ResultAsync`.
|
|
620
|
-
* This function can be used for control flow based on result values.
|
|
621
|
-
* @param f The function to call if the result is `Err`
|
|
622
|
-
* @returns `self` if the result is `Ok`, otherwise the result of `f`
|
|
623
|
-
* @example
|
|
624
|
-
*/
|
|
625
|
-
tapError(f) {
|
|
626
|
-
return new _ResultAsync(
|
|
627
|
-
this.innerPromise.then(async (res) => {
|
|
628
|
-
if (res.isErr()) {
|
|
629
|
-
try {
|
|
630
|
-
await f(res.error);
|
|
631
|
-
} catch {
|
|
632
|
-
}
|
|
633
|
-
return errAsync(res.error);
|
|
634
|
-
}
|
|
635
|
-
return okAsync(res.value);
|
|
636
|
-
})
|
|
637
|
-
);
|
|
638
|
-
}
|
|
639
|
-
finally(f) {
|
|
640
|
-
return new DisposableResultAsync(
|
|
641
|
-
// eslint-disable-next-line @typescript-eslint/require-await
|
|
642
|
-
this.innerPromise.then(async (res) => {
|
|
643
|
-
try {
|
|
644
|
-
res.finally(f);
|
|
645
|
-
} catch {
|
|
646
|
-
}
|
|
647
|
-
return res;
|
|
648
|
-
})
|
|
649
|
-
);
|
|
650
|
-
}
|
|
651
|
-
async *[Symbol.asyncIterator]() {
|
|
652
|
-
const result = await this.innerPromise;
|
|
653
|
-
if (result.isErr()) {
|
|
654
|
-
yield errAsync(result.error);
|
|
655
|
-
}
|
|
656
|
-
return result.value;
|
|
657
|
-
}
|
|
658
|
-
/**
|
|
659
|
-
* @deprecated will be removed in 2.0.0.
|
|
660
|
-
*
|
|
661
|
-
* You can use `safeTry` without this method.
|
|
662
|
-
* @example
|
|
663
|
-
* ```typescript
|
|
664
|
-
* safeTry(async function* () {
|
|
665
|
-
* const okValue = yield* yourResult
|
|
666
|
-
* })
|
|
667
|
-
* ```
|
|
668
|
-
* Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`.
|
|
669
|
-
*/
|
|
670
|
-
async *safeUnwrap() {
|
|
671
|
-
return yield* await this.innerPromise.then((res) => res.safeUnwrap());
|
|
672
|
-
}
|
|
673
|
-
};
|
|
674
|
-
var okAsync = ResultAsync.okAsync;
|
|
675
|
-
var errAsync = ResultAsync.errAsync;
|
|
676
|
-
var fromPromise = ResultAsync.fromPromise;
|
|
677
|
-
var fromSafePromise = ResultAsync.fromSafePromise;
|
|
678
|
-
var unitAsync = ResultAsync.unitAsync;
|
|
679
|
-
var fromThrowableAsync = ResultAsync.fromThrowable;
|
|
680
|
-
var tryCatchAsync = ResultAsync.tryCatch;
|
|
681
|
-
|
|
682
|
-
// src/utils.ts
|
|
683
|
-
var combineResultList = (resultList) => {
|
|
684
|
-
let acc = ok([]);
|
|
685
|
-
for (const result of resultList) {
|
|
686
|
-
if (result.isErr()) {
|
|
687
|
-
acc = err(result.error);
|
|
688
|
-
break;
|
|
689
|
-
} else {
|
|
690
|
-
acc.map((list) => list.push(result.value));
|
|
691
|
-
}
|
|
692
|
-
}
|
|
693
|
-
return acc;
|
|
694
|
-
};
|
|
695
|
-
var combineResultAsyncList = (asyncResultList) => ResultAsync.fromSafePromise(Promise.all(asyncResultList)).andThen(
|
|
696
|
-
combineResultList
|
|
697
|
-
);
|
|
698
|
-
var combineResultListWithAllErrors = (resultList) => {
|
|
699
|
-
let acc = ok([]);
|
|
700
|
-
for (const result of resultList) {
|
|
701
|
-
if (result.isErr() && acc.isErr()) {
|
|
702
|
-
acc.error.push(result.error);
|
|
703
|
-
} else if (result.isErr() && acc.isOk()) {
|
|
704
|
-
acc = err([result.error]);
|
|
705
|
-
} else if (result.isOk() && acc.isOk()) {
|
|
706
|
-
acc.value.push(result.value);
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
|
-
return acc;
|
|
710
|
-
};
|
|
711
|
-
var combineResultAsyncListWithAllErrors = (asyncResultList) => ResultAsync.fromSafePromise(Promise.all(asyncResultList)).andThen(
|
|
712
|
-
combineResultListWithAllErrors
|
|
713
|
-
);
|
|
714
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
715
|
-
0 && (module.exports = {
|
|
716
|
-
combineResultAsyncList,
|
|
717
|
-
combineResultAsyncListWithAllErrors,
|
|
718
|
-
combineResultList,
|
|
719
|
-
combineResultListWithAllErrors
|
|
720
|
-
});
|
|
721
|
-
//# sourceMappingURL=utils.cjs.map
|