resultar 1.1.2 → 1.1.4

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.cjs CHANGED
@@ -35,172 +35,19 @@ __export(src_exports, {
35
35
  });
36
36
  module.exports = __toCommonJS(src_exports);
37
37
 
38
- // src/result-async.ts
39
- var DisposableResultAsync = class {
40
- innerPromise;
41
- constructor(res) {
42
- this.innerPromise = res;
43
- }
44
- then(successCallback, failureCallback) {
45
- return this.innerPromise.then(successCallback, failureCallback);
46
- }
38
+ // src/error.ts
39
+ var defaultErrorConfig = {
40
+ withStackTrace: false
47
41
  };
48
- var ResultAsync = class _ResultAsync {
49
- static okAsync(value) {
50
- return new _ResultAsync(Promise.resolve(Result.ok(value)));
51
- }
52
- static errAsync(error) {
53
- return new _ResultAsync(Promise.resolve(Result.err(error)));
54
- }
55
- static fromSafePromise(promise) {
56
- const newPromise = promise.then((value) => Result.ok(value));
57
- return new _ResultAsync(newPromise);
58
- }
59
- static fromPromise(promise, errorFn) {
60
- const newPromise = promise.then((value) => Result.ok(value)).catch((error) => Result.err(errorFn(error)));
61
- return new _ResultAsync(newPromise);
62
- }
63
- static combine(asyncResultList) {
64
- return combineResultAsyncList(asyncResultList);
65
- }
66
- static combineWithAllErrors(asyncResultList) {
67
- return combineResultAsyncListWithAllErrors(
68
- asyncResultList
69
- );
70
- }
71
- /**
72
- * Wraps a async function with a try catch, creating a new function with the same
73
- * arguments but returning `Ok` if successful, `Err` if the function throws
74
- *
75
- * @param fn function to wrap with ok on success or err on failure
76
- * @param errorFn when an error is thrown, this will wrap the error result if provided
77
- * @returns a new function that returns a `ResultAsync`
78
- */
79
- static fromThrowable(fn, errorFn) {
80
- return (...args) => new _ResultAsync(
81
- (async () => {
82
- try {
83
- const v = await fn(...args);
84
- return Result.ok(v);
85
- } catch (error) {
86
- const e = errorFn ? errorFn(error) : error;
87
- return Result.err(e);
88
- }
89
- })()
90
- );
91
- }
92
- innerPromise;
93
- constructor(res) {
94
- this.innerPromise = res;
95
- }
96
- then(successCallback, failureCallback) {
97
- return this.innerPromise.then(successCallback, failureCallback);
98
- }
99
- mapErr(f) {
100
- return new _ResultAsync(this.innerPromise.then(async (res) => {
101
- if (res.isOk()) {
102
- return okAsync(res.value);
103
- }
104
- return errAsync(await f(res.error));
105
- }));
106
- }
107
- map(f) {
108
- const x = new _ResultAsync(
109
- this.innerPromise.then(async (res) => {
110
- if (res.isErr()) {
111
- return errAsync(res.error);
112
- }
113
- return okAsync(await f(res.value));
114
- })
115
- );
116
- return x;
117
- }
118
- andThen(f) {
119
- return new _ResultAsync(
120
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
121
- this.innerPromise.then(async (res) => {
122
- if (res.isErr()) {
123
- return errAsync(res.error);
124
- }
125
- const newValue = f(res.value);
126
- return newValue instanceof _ResultAsync ? newValue.innerPromise : newValue;
127
- })
128
- );
129
- }
130
- if(fCondition) {
131
- return {
132
- true: (fTrue) => ({
133
- false: (fFalse) => new _ResultAsync(this.innerPromise.then(async (res) => {
134
- if (res.isOk()) {
135
- const condition = fCondition(res.value);
136
- return condition ? fTrue(res.value) : fFalse(res.value);
137
- }
138
- return errAsync(res.error);
139
- }))
140
- })
141
- };
142
- }
143
- orElse(f) {
144
- return new _ResultAsync(
145
- this.innerPromise.then(async (res) => {
146
- if (res.isErr()) {
147
- return f(res.error);
148
- }
149
- return okAsync(res.value);
150
- })
151
- );
152
- }
153
- async match(ok2, _err) {
154
- return this.innerPromise.then((res) => res.match(ok2, _err));
155
- }
156
- async unwrapOr(t) {
157
- return this.innerPromise.then((res) => res.unwrapOr(t));
158
- }
159
- tap(f) {
160
- return new _ResultAsync(
161
- this.innerPromise.then(async (res) => {
162
- if (res.isErr()) {
163
- return errAsync(res.error);
164
- }
165
- try {
166
- await f(res.value);
167
- } catch {
168
- }
169
- return okAsync(res.value);
170
- })
171
- );
172
- }
173
- finally(f) {
174
- return new DisposableResultAsync(
175
- this.innerPromise.then(async (res) => {
176
- try {
177
- res.finally(f);
178
- } catch {
179
- }
180
- return res;
181
- })
182
- );
183
- }
184
- /**
185
- * Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`.
186
- */
187
- async *safeUnwrap() {
188
- return yield* await this.innerPromise.then((res) => res.safeUnwrap());
189
- }
42
+ var createResultarError = (message, result, config = defaultErrorConfig) => {
43
+ const data = result.isOk() ? { type: "Ok", value: result.value } : { type: "Err", value: result.error };
44
+ const maybeStack = config.withStackTrace ? new Error().stack : void 0;
45
+ return {
46
+ data,
47
+ message,
48
+ stack: maybeStack
49
+ };
190
50
  };
191
- var { okAsync, errAsync, fromPromise, fromSafePromise } = ResultAsync;
192
- var fromThrowableAsync = ResultAsync.fromThrowable;
193
- function safeTryAsync(body) {
194
- return new ResultAsync(
195
- (async () => {
196
- const n = await body().next();
197
- if (n.value.isOk()) {
198
- return okAsync(n.value.value);
199
- }
200
- return errAsync(n.value.error);
201
- })()
202
- );
203
- }
204
51
 
205
52
  // src/utils.ts
206
53
  var appendValueToEndOfList = (value) => (list) => [...list, value];
@@ -224,30 +71,14 @@ var combineResultListWithAllErrors = (resultList) => resultList.reduce(
224
71
  ok([])
225
72
  );
226
73
 
227
- // src/error.ts
228
- var defaultErrorConfig = {
229
- withStackTrace: false
230
- };
231
- var createResultarError = (message, result, config = defaultErrorConfig) => {
232
- const data = result.isOk() ? { type: "Ok", value: result.value } : { type: "Err", value: result.error };
233
- const maybeStack = config.withStackTrace ? new Error().stack : void 0;
234
- return {
235
- data,
236
- message,
237
- stack: maybeStack
238
- };
239
- };
240
-
241
74
  // src/result.ts
242
75
  var Ok = class {
243
- // eslint-disable-line @typescript-eslint/class-literal-property-style
244
76
  constructor(value) {
245
77
  this.value = value;
246
78
  }
247
79
  ok = true;
248
80
  };
249
81
  var Err = class {
250
- // eslint-disable-line @typescript-eslint/class-literal-property-style
251
82
  constructor(error) {
252
83
  this.error = error;
253
84
  }
@@ -271,6 +102,7 @@ var Result = class _Result {
271
102
  * @param fn function to wrap with ok on success or err on failure
272
103
  * @param errorFn when an error is thrown, this will wrap the error result if provided
273
104
  */
105
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
274
106
  static fromThrowable(fn, errorFn) {
275
107
  return (...args) => {
276
108
  try {
@@ -410,7 +242,6 @@ var Result = class _Result {
410
242
  return defaultValue;
411
243
  }
412
244
  /**
413
- *
414
245
  * Given 2 functions (one for the `Ok` variant and one for the `Err` variant)
415
246
  * execute the function that matches the `Result` variant.
416
247
  *
@@ -429,21 +260,48 @@ var Result = class _Result {
429
260
  }
430
261
  return fnErr(this.error);
431
262
  }
263
+ /**
264
+ * Performs a side effect no matter what the `Result` variant is.
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
+ }
432
276
  /**
433
277
  * Performs a side effect for the `Ok` variant of `Result`.
434
278
  *
435
- * @param f The function to apply an `OK` value
279
+ * @param fn The function to apply an `OK` value
436
280
  * @returns the result of applying `f` or an `Err` untouched
437
281
  */
438
- tap(f) {
282
+ tap(fn) {
439
283
  if (this.state.ok) {
440
284
  try {
441
- f(this.value);
285
+ fn(this.value);
442
286
  } catch {
443
287
  }
444
- return ok(this.value);
445
288
  }
446
- return err(this.error);
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;
447
305
  }
448
306
  finally(f) {
449
307
  const resultDisposable = new DisposableResult(this, f);
@@ -532,6 +390,228 @@ function safeTry(body) {
532
390
  }
533
391
  return n.value;
534
392
  }
393
+
394
+ // src/result-async.ts
395
+ var DisposableResultAsync = class {
396
+ innerPromise;
397
+ constructor(res) {
398
+ this.innerPromise = res;
399
+ }
400
+ then(successCallback, failureCallback) {
401
+ return this.innerPromise.then(successCallback, failureCallback);
402
+ }
403
+ };
404
+ var ResultAsync = class _ResultAsync {
405
+ static okAsync(value) {
406
+ return new _ResultAsync(Promise.resolve(Result.ok(value)));
407
+ }
408
+ static errAsync(error) {
409
+ return new _ResultAsync(Promise.resolve(Result.err(error)));
410
+ }
411
+ static fromSafePromise(promise) {
412
+ const newPromise = promise.then((value) => Result.ok(value));
413
+ return new _ResultAsync(newPromise);
414
+ }
415
+ static fromPromise(promise, errorFn) {
416
+ const newPromise = promise.then((value) => Result.ok(value)).catch((error) => Result.err(errorFn(error)));
417
+ return new _ResultAsync(newPromise);
418
+ }
419
+ static combine(asyncResultList) {
420
+ return combineResultAsyncList(asyncResultList);
421
+ }
422
+ static combineWithAllErrors(asyncResultList) {
423
+ return combineResultAsyncListWithAllErrors(
424
+ asyncResultList
425
+ );
426
+ }
427
+ /**
428
+ * Wraps a async function with a try catch, creating a new function with the same
429
+ * arguments but returning `Ok` if successful, `Err` if the function throws
430
+ *
431
+ * @param fn function to wrap with ok on success or err on failure
432
+ * @param errorFn when an error is thrown, this will wrap the error result if provided
433
+ * @returns a new function that returns a `ResultAsync`
434
+ */
435
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
436
+ static fromThrowable(fn, errorFn) {
437
+ return (...args) => new _ResultAsync(
438
+ (async () => {
439
+ try {
440
+ const v = await fn(...args);
441
+ return Result.ok(v);
442
+ } catch (error) {
443
+ const e = errorFn ? errorFn(error) : error;
444
+ return Result.err(e);
445
+ }
446
+ })()
447
+ );
448
+ }
449
+ innerPromise;
450
+ constructor(res) {
451
+ this.innerPromise = res;
452
+ }
453
+ then(successCallback, failureCallback) {
454
+ return this.innerPromise.then(successCallback, failureCallback);
455
+ }
456
+ mapErr(f) {
457
+ return new _ResultAsync(this.innerPromise.then(async (res) => {
458
+ if (res.isOk()) {
459
+ return okAsync(res.value);
460
+ }
461
+ return errAsync(await f(res.error));
462
+ }));
463
+ }
464
+ map(f) {
465
+ const x = new _ResultAsync(
466
+ this.innerPromise.then(async (res) => {
467
+ if (res.isErr()) {
468
+ return errAsync(res.error);
469
+ }
470
+ return okAsync(await f(res.value));
471
+ })
472
+ );
473
+ return x;
474
+ }
475
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
476
+ andThen(f) {
477
+ return new _ResultAsync(
478
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
479
+ this.innerPromise.then(async (res) => {
480
+ if (res.isErr()) {
481
+ return errAsync(res.error);
482
+ }
483
+ const newValue = f(res.value);
484
+ return newValue instanceof _ResultAsync ? newValue.innerPromise : newValue;
485
+ })
486
+ );
487
+ }
488
+ if(fCondition) {
489
+ return {
490
+ true: (fTrue) => ({
491
+ false: (fFalse) => new _ResultAsync(this.innerPromise.then(async (res) => {
492
+ if (res.isOk()) {
493
+ const condition = fCondition(res.value);
494
+ return condition ? fTrue(res.value) : fFalse(res.value);
495
+ }
496
+ return errAsync(res.error);
497
+ }))
498
+ })
499
+ };
500
+ }
501
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
502
+ orElse(f) {
503
+ return new _ResultAsync(
504
+ this.innerPromise.then(async (res) => {
505
+ if (res.isErr()) {
506
+ return f(res.error);
507
+ }
508
+ return okAsync(res.value);
509
+ })
510
+ );
511
+ }
512
+ async match(ok2, err2) {
513
+ return this.innerPromise.then((res) => res.match(ok2, err2));
514
+ }
515
+ async unwrapOr(t) {
516
+ return this.innerPromise.then((res) => res.unwrapOr(t));
517
+ }
518
+ /**
519
+ * Performs a side effect for the `Ok` variant of `ResultAsync`.
520
+ * This function can be used for control flow based on result values.
521
+ * @param f The function to call if the result is `Ok`
522
+ * @returns `self` if the result is `Err`, otherwise the result of `f`
523
+ */
524
+ log(f) {
525
+ return new _ResultAsync(
526
+ this.innerPromise.then(async (res) => {
527
+ if (res.isOk()) {
528
+ try {
529
+ await f(res.value);
530
+ } catch {
531
+ }
532
+ return okAsync(res.value);
533
+ }
534
+ try {
535
+ await f(void 0, res.error);
536
+ } catch {
537
+ }
538
+ return errAsync(res.error);
539
+ })
540
+ );
541
+ }
542
+ /**
543
+ * Performs a side effect for the `Ok` variant of `ResultAsync`.
544
+ * This function can be used for control flow based on result values.
545
+ * @param f The function to call if the result is `Ok`
546
+ * @returns `self` if the result is `Err`, otherwise the result of `f`
547
+ */
548
+ tap(f) {
549
+ return new _ResultAsync(
550
+ this.innerPromise.then(async (res) => {
551
+ if (res.isErr()) {
552
+ return errAsync(res.error);
553
+ }
554
+ try {
555
+ await f(res.value);
556
+ } catch {
557
+ }
558
+ return okAsync(res.value);
559
+ })
560
+ );
561
+ }
562
+ /**
563
+ * Performs a side effect for the `Err` variant of `ResultAsync`.
564
+ * This function can be used for control flow based on result values.
565
+ * @param f The function to call if the result is `Err`
566
+ * @returns `self` if the result is `Ok`, otherwise the result of `f`
567
+ * @example
568
+ */
569
+ tapError(f) {
570
+ return new _ResultAsync(
571
+ this.innerPromise.then(async (res) => {
572
+ if (res.isErr()) {
573
+ try {
574
+ await f(res.error);
575
+ } catch {
576
+ }
577
+ return errAsync(res.error);
578
+ }
579
+ return okAsync(res.value);
580
+ })
581
+ );
582
+ }
583
+ finally(f) {
584
+ return new DisposableResultAsync(
585
+ // eslint-disable-next-line @typescript-eslint/require-await
586
+ this.innerPromise.then(async (res) => {
587
+ try {
588
+ res.finally(f);
589
+ } catch {
590
+ }
591
+ return res;
592
+ })
593
+ );
594
+ }
595
+ /**
596
+ * Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`.
597
+ */
598
+ async *safeUnwrap() {
599
+ return yield* await this.innerPromise.then((res) => res.safeUnwrap());
600
+ }
601
+ };
602
+ var { okAsync, errAsync, fromPromise, fromSafePromise } = ResultAsync;
603
+ var fromThrowableAsync = ResultAsync.fromThrowable;
604
+ function safeTryAsync(body) {
605
+ return new ResultAsync(
606
+ (async () => {
607
+ const n = await body().next();
608
+ if (n.value.isOk()) {
609
+ return okAsync(n.value.value);
610
+ }
611
+ return errAsync(n.value.error);
612
+ })()
613
+ );
614
+ }
535
615
  // Annotate the CommonJS export names for ESM import in node:
536
616
  0 && (module.exports = {
537
617
  Result,