@sapphire/result 2.7.4-next.ee9cdb94 → 2.8.0-next.6abc5522
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/cjs/index.cjs +48 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +119 -5
- package/dist/esm/index.d.mts +119 -5
- package/dist/esm/index.mjs +48 -0
- package/dist/esm/index.mjs.map +1 -1
- package/dist/iife/index.global.js +48 -0
- package/dist/iife/index.global.js.map +1 -1
- package/package.json +1 -1
package/dist/cjs/index.cjs
CHANGED
|
@@ -41,6 +41,7 @@ var ResultError = _ResultError;
|
|
|
41
41
|
// src/lib/Result.ts
|
|
42
42
|
var ValueProperty = Symbol.for("@sapphire/result:Result.value");
|
|
43
43
|
var SuccessProperty = Symbol.for("@sapphire/result:Result.success");
|
|
44
|
+
var UnwrapSafeProperty = Symbol.for("@sapphire/result:Result.safeUnwrap");
|
|
44
45
|
var _a, _b;
|
|
45
46
|
var _Result = class _Result {
|
|
46
47
|
constructor(value, success) {
|
|
@@ -467,6 +468,7 @@ var _Result = class _Result {
|
|
|
467
468
|
* @seealso {@link unwrapOrElse}
|
|
468
469
|
* @seealso {@link unwrapErr}
|
|
469
470
|
* @seealso {@link unwrapRaw}
|
|
471
|
+
* @seealso {@link unwrapSafe}
|
|
470
472
|
*
|
|
471
473
|
* @example
|
|
472
474
|
* ```typescript
|
|
@@ -497,6 +499,7 @@ var _Result = class _Result {
|
|
|
497
499
|
* @seealso {@link unwrapOr}
|
|
498
500
|
* @seealso {@link unwrapOrElse}
|
|
499
501
|
* @seealso {@link unwrapRaw}
|
|
502
|
+
* @seealso {@link unwrapSafe}
|
|
500
503
|
*
|
|
501
504
|
* @example
|
|
502
505
|
* ```typescript
|
|
@@ -528,6 +531,7 @@ var _Result = class _Result {
|
|
|
528
531
|
* @seealso {@link unwrapOrElse}
|
|
529
532
|
* @seealso {@link unwrapErr}
|
|
530
533
|
* @seealso {@link unwrapRaw}
|
|
534
|
+
* @seealso {@link unwrapSafe}
|
|
531
535
|
*
|
|
532
536
|
* @param defaultValue The default value.
|
|
533
537
|
*
|
|
@@ -553,6 +557,7 @@ var _Result = class _Result {
|
|
|
553
557
|
* @seealso {@link unwrapOr}
|
|
554
558
|
* @seealso {@link unwrapErr}
|
|
555
559
|
* @seealso {@link unwrapRaw}
|
|
560
|
+
* @seealso {@link unwrapSafe}
|
|
556
561
|
*
|
|
557
562
|
* @param op The predicate.
|
|
558
563
|
*
|
|
@@ -577,6 +582,7 @@ var _Result = class _Result {
|
|
|
577
582
|
* @seealso {@link unwrapOr}
|
|
578
583
|
* @seealso {@link unwrapOrElse}
|
|
579
584
|
* @seealso {@link unwrapErr}
|
|
585
|
+
* @seealso {@link unwrapSafe}
|
|
580
586
|
*
|
|
581
587
|
* @example
|
|
582
588
|
* ```typescript
|
|
@@ -597,6 +603,26 @@ var _Result = class _Result {
|
|
|
597
603
|
if (this.isErr()) throw this[ValueProperty];
|
|
598
604
|
return this[ValueProperty];
|
|
599
605
|
}
|
|
606
|
+
/**
|
|
607
|
+
* Returns the contained `Ok` value or yelds the contained `Err` value.
|
|
608
|
+
* Emulates Rust's `?` operator in `safeTry`'s body. See also {@link Result.safeTry}.
|
|
609
|
+
*
|
|
610
|
+
* If used outside of a `safeTry`'s' body, throws the contained error.
|
|
611
|
+
* @seealso {@link unwrap}
|
|
612
|
+
* @seealso {@link unwrapOr}
|
|
613
|
+
* @seealso {@link unwrapErr}
|
|
614
|
+
* @seealso {@link unwrapRaw}
|
|
615
|
+
* @seealso {@link unwrapSafe}
|
|
616
|
+
*
|
|
617
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_safe}
|
|
618
|
+
*/
|
|
619
|
+
*unwrapSafe() {
|
|
620
|
+
if (this.isOk()) {
|
|
621
|
+
return this[ValueProperty];
|
|
622
|
+
}
|
|
623
|
+
yield this;
|
|
624
|
+
throw new ResultError("Should not be used outside of a safe try generator", this[ValueProperty]);
|
|
625
|
+
}
|
|
600
626
|
/**
|
|
601
627
|
* Returns `result` if the result is `Ok`, otherwise returns the `Err` value of itself.
|
|
602
628
|
* @param result The result to check.
|
|
@@ -871,6 +897,16 @@ var _Result = class _Result {
|
|
|
871
897
|
get [Symbol.toStringTag]() {
|
|
872
898
|
return this.match({ ok: /* @__PURE__ */ __name(() => "Ok", "ok"), err: /* @__PURE__ */ __name(() => "Err", "err") });
|
|
873
899
|
}
|
|
900
|
+
/**
|
|
901
|
+
* This function, in combination with `[$]`, is intended to emulate
|
|
902
|
+
* Rust's ? operator.
|
|
903
|
+
*
|
|
904
|
+
* @see {@link Result.safeTry}
|
|
905
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.safeTry}
|
|
906
|
+
*/
|
|
907
|
+
get [UnwrapSafeProperty]() {
|
|
908
|
+
return this.unwrapSafe();
|
|
909
|
+
}
|
|
874
910
|
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
875
911
|
static ok(value) {
|
|
876
912
|
return new _Result(value, true);
|
|
@@ -990,6 +1026,13 @@ var _Result = class _Result {
|
|
|
990
1026
|
}
|
|
991
1027
|
return err(errors);
|
|
992
1028
|
}
|
|
1029
|
+
static safeTry(body) {
|
|
1030
|
+
const n = body({ $: UnwrapSafeProperty, $async: unwrapSafeAsync }).next();
|
|
1031
|
+
if (n instanceof Promise) {
|
|
1032
|
+
return n.then((r) => r.value);
|
|
1033
|
+
}
|
|
1034
|
+
return n.value;
|
|
1035
|
+
}
|
|
993
1036
|
};
|
|
994
1037
|
__name(_Result, "Result");
|
|
995
1038
|
var Result = _Result;
|
|
@@ -998,6 +1041,11 @@ function resolve(value) {
|
|
|
998
1041
|
return Result.isResult(value) ? value : ok(value);
|
|
999
1042
|
}
|
|
1000
1043
|
__name(resolve, "resolve");
|
|
1044
|
+
async function* unwrapSafeAsync(result) {
|
|
1045
|
+
const _result = await result;
|
|
1046
|
+
return yield* _result.unwrapSafe();
|
|
1047
|
+
}
|
|
1048
|
+
__name(unwrapSafeAsync, "unwrapSafeAsync");
|
|
1001
1049
|
|
|
1002
1050
|
// src/lib/Option.ts
|
|
1003
1051
|
var ValueProperty2 = Symbol.for("@sapphire/result:Option.value");
|