retuple 1.0.0-next.15 → 1.0.0-next.17
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 +65 -41
- package/dist/index.d.cts +41 -39
- package/dist/index.d.ts +41 -39
- package/dist/index.js +62 -38
- package/package.json +9 -6
package/dist/index.cjs
CHANGED
|
@@ -12,45 +12,11 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
12
12
|
};
|
|
13
13
|
var _ResultAsync_inner, _a, _ResultRetry_f, _ResultRetry_promise, _ResultRetry_times, _ResultRetry_attempt, _ResultRetry_aborted, _ResultRetry_abort, _ResultRetry_getDelay, _ResultRetry_handler;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.RetupleArrayMethodUnavailableError = exports.RetupleInvalidUnionError = exports.RetupleCaughtValueError = exports.RetupleExpectFailed = exports.RetupleUnwrapErrFailed = exports.RetupleUnwrapFailed =
|
|
15
|
+
exports.RetupleArrayMethodUnavailableError = exports.RetupleInvalidUnionError = exports.RetupleCaughtValueError = exports.RetupleExpectFailed = exports.RetupleUnwrapErrFailed = exports.RetupleUnwrapFailed = void 0;
|
|
16
16
|
exports.Result = Result;
|
|
17
17
|
exports.Ok = Ok;
|
|
18
18
|
exports.Err = Err;
|
|
19
|
-
|
|
20
|
-
* ## Result Like Symbol
|
|
21
|
-
*
|
|
22
|
-
* Implement a custom result-like by implementing the `ResultLike` interface
|
|
23
|
-
* on a class or object. An object with this implementation can be converted
|
|
24
|
-
* to a `Result` and can be used in most places where a `Result` is required.
|
|
25
|
-
*
|
|
26
|
-
* ```ts
|
|
27
|
-
* import { ResultLikeSymbol } from "retuple/symbol";
|
|
28
|
-
* import { Result, Ok, Err, type ResultLike } from "retuple";
|
|
29
|
-
*
|
|
30
|
-
* class CustomResult<T> implements ResultLike<T, CustomError> {
|
|
31
|
-
* value: T;
|
|
32
|
-
*
|
|
33
|
-
* constructor(value: T) {
|
|
34
|
-
* this.value = value;
|
|
35
|
-
* }
|
|
36
|
-
*
|
|
37
|
-
* [ResultLikeSymbol](): Result<T, CustomError> {
|
|
38
|
-
* return this.value === "test"
|
|
39
|
-
* ? Ok(this.value)
|
|
40
|
-
* : Err(new CustomError("Value was not test"));
|
|
41
|
-
* }
|
|
42
|
-
* }
|
|
43
|
-
*
|
|
44
|
-
* const custom = new CustomResult("test");
|
|
45
|
-
* const result: Result<string, Error> = Result(custom);
|
|
46
|
-
*
|
|
47
|
-
* const chain = Ok()
|
|
48
|
-
* .$map(() => "value")
|
|
49
|
-
* .$andThen((value) => new CustomResult(value))
|
|
50
|
-
* .$or(myresult);
|
|
51
|
-
* ```
|
|
52
|
-
*/
|
|
53
|
-
exports.ResultLikeSymbol = Symbol("retuple/result");
|
|
19
|
+
const retuple_symbols_1 = require("retuple-symbols");
|
|
54
20
|
/**
|
|
55
21
|
* ## Retuple Unwrap Failed
|
|
56
22
|
*
|
|
@@ -720,8 +686,8 @@ class ResultOk extends RetupleArray {
|
|
|
720
686
|
this[0] = undefined;
|
|
721
687
|
this[1] = value;
|
|
722
688
|
}
|
|
723
|
-
[
|
|
724
|
-
return this;
|
|
689
|
+
[retuple_symbols_1.ResultLikeSymbol]() {
|
|
690
|
+
return { ok: true, value: this[1] };
|
|
725
691
|
}
|
|
726
692
|
toJSON() {
|
|
727
693
|
return this[1];
|
|
@@ -774,22 +740,43 @@ class ResultOk extends RetupleArray {
|
|
|
774
740
|
$or() {
|
|
775
741
|
return this;
|
|
776
742
|
}
|
|
743
|
+
$orAsync() {
|
|
744
|
+
return this.$async();
|
|
745
|
+
}
|
|
777
746
|
$orElse() {
|
|
778
747
|
return this;
|
|
779
748
|
}
|
|
749
|
+
$orElseAsync() {
|
|
750
|
+
return this.$async();
|
|
751
|
+
}
|
|
780
752
|
$orSafe() {
|
|
781
753
|
return this;
|
|
782
754
|
}
|
|
755
|
+
$orSafeAsync() {
|
|
756
|
+
return this.$async();
|
|
757
|
+
}
|
|
758
|
+
$orSafePromise() {
|
|
759
|
+
return this.$async();
|
|
760
|
+
}
|
|
783
761
|
$and(and) {
|
|
784
762
|
return asResult(and);
|
|
785
763
|
}
|
|
764
|
+
$andAsync(and) {
|
|
765
|
+
return this.$async().$and(and);
|
|
766
|
+
}
|
|
786
767
|
$andThen(f) {
|
|
787
768
|
return asResult(f(this[1]));
|
|
788
769
|
}
|
|
770
|
+
$andThenAsync(f) {
|
|
771
|
+
return this.$async().$andThen(f);
|
|
772
|
+
}
|
|
789
773
|
$andThrough(f) {
|
|
790
774
|
const res = asResult(f(this[1]));
|
|
791
775
|
return res instanceof ResultErr ? res : this;
|
|
792
776
|
}
|
|
777
|
+
$andThroughAsync(f) {
|
|
778
|
+
return this.$async().$andThrough(f);
|
|
779
|
+
}
|
|
793
780
|
$andSafe(f, mapError = ensureError) {
|
|
794
781
|
try {
|
|
795
782
|
return Ok(f(this[1]));
|
|
@@ -798,6 +785,12 @@ class ResultOk extends RetupleArray {
|
|
|
798
785
|
return Err(mapError(err));
|
|
799
786
|
}
|
|
800
787
|
}
|
|
788
|
+
$andSafeAsync(f, mapError = ensureError) {
|
|
789
|
+
return this.$async().$andSafe(f, mapError);
|
|
790
|
+
}
|
|
791
|
+
$andSafePromise(promise, mapError = ensureError) {
|
|
792
|
+
return this.$async().$andSafePromise(promise, mapError);
|
|
793
|
+
}
|
|
801
794
|
$peek(f) {
|
|
802
795
|
f(this);
|
|
803
796
|
return this;
|
|
@@ -833,8 +826,8 @@ class ResultErr extends RetupleArray {
|
|
|
833
826
|
this[0] = err;
|
|
834
827
|
this[1] = undefined;
|
|
835
828
|
}
|
|
836
|
-
[
|
|
837
|
-
return this;
|
|
829
|
+
[retuple_symbols_1.ResultLikeSymbol]() {
|
|
830
|
+
return { ok: false, value: this[0] };
|
|
838
831
|
}
|
|
839
832
|
toJSON() {
|
|
840
833
|
return null;
|
|
@@ -890,9 +883,15 @@ class ResultErr extends RetupleArray {
|
|
|
890
883
|
$or(or) {
|
|
891
884
|
return asResult(or);
|
|
892
885
|
}
|
|
886
|
+
$orAsync(or) {
|
|
887
|
+
return this.$async().$or(or);
|
|
888
|
+
}
|
|
893
889
|
$orElse(f) {
|
|
894
890
|
return asResult(f(this[0]));
|
|
895
891
|
}
|
|
892
|
+
$orElseAsync(f) {
|
|
893
|
+
return this.$async().$orElse(f);
|
|
894
|
+
}
|
|
896
895
|
$orSafe(f, mapError = ensureError) {
|
|
897
896
|
try {
|
|
898
897
|
return Ok(f(this[0]));
|
|
@@ -901,18 +900,39 @@ class ResultErr extends RetupleArray {
|
|
|
901
900
|
return Err(mapError(err));
|
|
902
901
|
}
|
|
903
902
|
}
|
|
903
|
+
$orSafeAsync(f, mapError = ensureError) {
|
|
904
|
+
return this.$async().$orSafe(f, mapError);
|
|
905
|
+
}
|
|
906
|
+
$orSafePromise(promise, mapError = ensureError) {
|
|
907
|
+
return this.$async().$orSafePromise(promise, mapError);
|
|
908
|
+
}
|
|
904
909
|
$and() {
|
|
905
910
|
return this;
|
|
906
911
|
}
|
|
912
|
+
$andAsync() {
|
|
913
|
+
return this.$async();
|
|
914
|
+
}
|
|
907
915
|
$andThen() {
|
|
908
916
|
return this;
|
|
909
917
|
}
|
|
918
|
+
$andThenAsync() {
|
|
919
|
+
return this.$async();
|
|
920
|
+
}
|
|
910
921
|
$andThrough() {
|
|
911
922
|
return this;
|
|
912
923
|
}
|
|
924
|
+
$andThroughAsync() {
|
|
925
|
+
return this.$async();
|
|
926
|
+
}
|
|
913
927
|
$andSafe() {
|
|
914
928
|
return this;
|
|
915
929
|
}
|
|
930
|
+
$andSafeAsync() {
|
|
931
|
+
return this.$async();
|
|
932
|
+
}
|
|
933
|
+
$andSafePromise() {
|
|
934
|
+
return this.$async();
|
|
935
|
+
}
|
|
916
936
|
$peek(f) {
|
|
917
937
|
f(this);
|
|
918
938
|
return this;
|
|
@@ -1352,7 +1372,11 @@ _a = ResultRetry, _ResultRetry_f = new WeakMap(), _ResultRetry_promise = new Wea
|
|
|
1352
1372
|
ResultRetry.MAX_TIMEOUT = 3600000;
|
|
1353
1373
|
ResultRetry.MAX_RETRY = 100;
|
|
1354
1374
|
function asResult(resultLike) {
|
|
1355
|
-
|
|
1375
|
+
if (resultLike instanceof ResultOk || resultLike instanceof ResultErr) {
|
|
1376
|
+
return resultLike;
|
|
1377
|
+
}
|
|
1378
|
+
const result = resultLike[retuple_symbols_1.ResultLikeSymbol]();
|
|
1379
|
+
return result.ok ? new ResultOk(result.value) : new ResultErr(result.value);
|
|
1356
1380
|
}
|
|
1357
1381
|
function ensureError(err) {
|
|
1358
1382
|
if (err instanceof Error) {
|
package/dist/index.d.cts
CHANGED
|
@@ -1,42 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* ## Result Like Symbol
|
|
3
|
-
*
|
|
4
|
-
* Implement a custom result-like by implementing the `ResultLike` interface
|
|
5
|
-
* on a class or object. An object with this implementation can be converted
|
|
6
|
-
* to a `Result` and can be used in most places where a `Result` is required.
|
|
7
|
-
*
|
|
8
|
-
* ```ts
|
|
9
|
-
* import { ResultLikeSymbol } from "retuple/symbol";
|
|
10
|
-
* import { Result, Ok, Err, type ResultLike } from "retuple";
|
|
11
|
-
*
|
|
12
|
-
* class CustomResult<T> implements ResultLike<T, CustomError> {
|
|
13
|
-
* value: T;
|
|
14
|
-
*
|
|
15
|
-
* constructor(value: T) {
|
|
16
|
-
* this.value = value;
|
|
17
|
-
* }
|
|
18
|
-
*
|
|
19
|
-
* [ResultLikeSymbol](): Result<T, CustomError> {
|
|
20
|
-
* return this.value === "test"
|
|
21
|
-
* ? Ok(this.value)
|
|
22
|
-
* : Err(new CustomError("Value was not test"));
|
|
23
|
-
* }
|
|
24
|
-
* }
|
|
25
|
-
*
|
|
26
|
-
* const custom = new CustomResult("test");
|
|
27
|
-
* const result: Result<string, Error> = Result(custom);
|
|
28
|
-
*
|
|
29
|
-
* const chain = Ok()
|
|
30
|
-
* .$map(() => "value")
|
|
31
|
-
* .$andThen((value) => new CustomResult(value))
|
|
32
|
-
* .$or(myresult);
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
export declare const ResultLikeSymbol: unique symbol;
|
|
36
|
-
export type ResultLikeSymbol = typeof ResultLikeSymbol;
|
|
37
|
-
export type ResultLike<T, E> = {
|
|
38
|
-
[ResultLikeSymbol](): Result<T, E>;
|
|
39
|
-
};
|
|
1
|
+
import { type ResultLike } from "retuple-symbols";
|
|
40
2
|
export type Ok = typeof Ok;
|
|
41
3
|
export type Err = typeof Err;
|
|
42
4
|
export type Result<T, E> = (OkTuple<T> | ErrTuple<E>) & Retuple<T, E>;
|
|
@@ -1413,6 +1375,10 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined>, ResultLike<T, E
|
|
|
1413
1375
|
* ```
|
|
1414
1376
|
*/
|
|
1415
1377
|
$or<U = T, F = E>(this: Result<T, E>, or: ResultLike<U, F>): Result<T | U, F>;
|
|
1378
|
+
/**
|
|
1379
|
+
* Shorthand for `result.$async().$or(...)`
|
|
1380
|
+
*/
|
|
1381
|
+
$orAsync<U = T, F = E>(this: Result<T, E>, or: ResultLikeAwaitable<U, F>): ResultAsync<T | U, F>;
|
|
1416
1382
|
/**
|
|
1417
1383
|
* Returns the result returned by the or function, when this result is `Err`.
|
|
1418
1384
|
*
|
|
@@ -1449,6 +1415,10 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined>, ResultLike<T, E
|
|
|
1449
1415
|
* ```
|
|
1450
1416
|
*/
|
|
1451
1417
|
$orElse<U = T, F = E>(this: Result<T, E>, f: (err: E) => ResultLike<U, F>): Result<T | U, F>;
|
|
1418
|
+
/**
|
|
1419
|
+
* Shorthand for `result.$async().$orElse(...)`
|
|
1420
|
+
*/
|
|
1421
|
+
$orElseAsync<U = T, F = E>(this: Result<T, E>, f: (err: E) => ResultLikeAwaitable<U, F>): ResultAsync<T | U, F>;
|
|
1452
1422
|
/**
|
|
1453
1423
|
* Returns a {@link Result} based on the outcome of the safe function when
|
|
1454
1424
|
* this result is `Err`.
|
|
@@ -1460,6 +1430,16 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined>, ResultLike<T, E
|
|
|
1460
1430
|
*/
|
|
1461
1431
|
$orSafe<U = T>(this: Result<T, E>, f: (err: E) => U): Result<T | U, Error>;
|
|
1462
1432
|
$orSafe<U = T, F = E>(this: Result<T, E>, f: (err: E) => U, mapError: (err: unknown) => F): Result<T | U, F>;
|
|
1433
|
+
/**
|
|
1434
|
+
* Shorthand for `result.$async().$orSafe(...)`
|
|
1435
|
+
*/
|
|
1436
|
+
$orSafeAsync<U = T>(this: Result<T, E>, f: (err: E) => U | PromiseLike<U>): ResultAsync<T | U, Error>;
|
|
1437
|
+
$orSafeAsync<U = T, F = E>(this: Result<T, E>, f: (err: E) => U | PromiseLike<U>, mapError: (err: unknown) => F): ResultAsync<T | U, F>;
|
|
1438
|
+
/**
|
|
1439
|
+
* Shorthand for `result.$async().$orSafePromise(...)`
|
|
1440
|
+
*/
|
|
1441
|
+
$orSafePromise<U = T>(this: Result<T, E>, promise: PromiseLike<U>): ResultAsync<T | U, Error>;
|
|
1442
|
+
$orSafePromise<U = T, F = E>(this: Result<T, E>, promise: PromiseLike<U>, mapError: (err: unknown) => F): ResultAsync<T | U, F>;
|
|
1463
1443
|
/**
|
|
1464
1444
|
* Returns the and result, when this result is `Ok`.
|
|
1465
1445
|
*
|
|
@@ -1496,6 +1476,10 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined>, ResultLike<T, E
|
|
|
1496
1476
|
* ```
|
|
1497
1477
|
*/
|
|
1498
1478
|
$and<U = T, F = E>(this: Result<T, E>, and: ResultLike<U, F>): Result<U, E | F>;
|
|
1479
|
+
/**
|
|
1480
|
+
* Shorthand for `result.$async().$and(...)`
|
|
1481
|
+
*/
|
|
1482
|
+
$andAsync<U = T, F = E>(this: Result<T, E>, and: ResultLikeAwaitable<U, F>): ResultAsync<U, E | F>;
|
|
1499
1483
|
/**
|
|
1500
1484
|
* Returns the and result, when this result is `Ok`.
|
|
1501
1485
|
*
|
|
@@ -1532,6 +1516,10 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined>, ResultLike<T, E
|
|
|
1532
1516
|
* ```
|
|
1533
1517
|
*/
|
|
1534
1518
|
$andThen<U = T, F = E>(this: Result<T, E>, f: (val: T) => ResultLike<U, F>): Result<U, E | F>;
|
|
1519
|
+
/**
|
|
1520
|
+
* Shorthand for `result.$async().$andThen(...)`
|
|
1521
|
+
*/
|
|
1522
|
+
$andThenAsync<U = T, F = E>(this: Result<T, E>, f: (val: T) => ResultLikeAwaitable<U, F>): ResultAsync<U, E | F>;
|
|
1535
1523
|
/**
|
|
1536
1524
|
* Calls the through function when this result is `Ok` and returns:
|
|
1537
1525
|
*
|
|
@@ -1572,6 +1560,10 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined>, ResultLike<T, E
|
|
|
1572
1560
|
* ```
|
|
1573
1561
|
*/
|
|
1574
1562
|
$andThrough<F = E>(this: Result<T, E>, f: (val: T) => ResultLike<any, F>): Result<T, E | F>;
|
|
1563
|
+
/**
|
|
1564
|
+
* Shorthand for `result.$async().$andThrough(...)`
|
|
1565
|
+
*/
|
|
1566
|
+
$andThroughAsync<F = E>(this: Result<T, E>, f: (val: T) => ResultLikeAwaitable<any, F>): ResultAsync<T, E | F>;
|
|
1575
1567
|
/**
|
|
1576
1568
|
* Returns a result based on the outcome of the safe function when this
|
|
1577
1569
|
* result is `Ok`.
|
|
@@ -1586,6 +1578,16 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined>, ResultLike<T, E
|
|
|
1586
1578
|
*/
|
|
1587
1579
|
$andSafe<U = T>(this: Result<T, E>, f: (val: T) => U): Result<U, E | Error>;
|
|
1588
1580
|
$andSafe<U = T, F = E>(this: Result<T, E>, f: (val: T) => U, mapError: (err: unknown) => F): Result<U, E | F>;
|
|
1581
|
+
/**
|
|
1582
|
+
* Shorthand for `result.$async().$andSafe(...)`
|
|
1583
|
+
*/
|
|
1584
|
+
$andSafeAsync<U = T>(this: Result<T, E>, f: (val: T) => U | PromiseLike<U>): ResultAsync<U, E | Error>;
|
|
1585
|
+
$andSafeAsync<U = T, F = E>(this: Result<T, E>, f: (val: T) => U | PromiseLike<U>, mapError: (err: unknown) => F): ResultAsync<U, E | F>;
|
|
1586
|
+
/**
|
|
1587
|
+
* Shorthand for `result.$async().$andSafePromise(...)`
|
|
1588
|
+
*/
|
|
1589
|
+
$andSafePromise<U = T>(this: Result<T, E>, promise: PromiseLike<U>): ResultAsync<U, E | Error>;
|
|
1590
|
+
$andSafePromise<U = T, F = E>(this: Result<T, E>, promise: PromiseLike<U>, mapError: (err: unknown) => F): ResultAsync<U, E | F>;
|
|
1589
1591
|
/**
|
|
1590
1592
|
* Calls the peek function and returns {@link Result} equivalent to this
|
|
1591
1593
|
* result.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,42 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* ## Result Like Symbol
|
|
3
|
-
*
|
|
4
|
-
* Implement a custom result-like by implementing the `ResultLike` interface
|
|
5
|
-
* on a class or object. An object with this implementation can be converted
|
|
6
|
-
* to a `Result` and can be used in most places where a `Result` is required.
|
|
7
|
-
*
|
|
8
|
-
* ```ts
|
|
9
|
-
* import { ResultLikeSymbol } from "retuple/symbol";
|
|
10
|
-
* import { Result, Ok, Err, type ResultLike } from "retuple";
|
|
11
|
-
*
|
|
12
|
-
* class CustomResult<T> implements ResultLike<T, CustomError> {
|
|
13
|
-
* value: T;
|
|
14
|
-
*
|
|
15
|
-
* constructor(value: T) {
|
|
16
|
-
* this.value = value;
|
|
17
|
-
* }
|
|
18
|
-
*
|
|
19
|
-
* [ResultLikeSymbol](): Result<T, CustomError> {
|
|
20
|
-
* return this.value === "test"
|
|
21
|
-
* ? Ok(this.value)
|
|
22
|
-
* : Err(new CustomError("Value was not test"));
|
|
23
|
-
* }
|
|
24
|
-
* }
|
|
25
|
-
*
|
|
26
|
-
* const custom = new CustomResult("test");
|
|
27
|
-
* const result: Result<string, Error> = Result(custom);
|
|
28
|
-
*
|
|
29
|
-
* const chain = Ok()
|
|
30
|
-
* .$map(() => "value")
|
|
31
|
-
* .$andThen((value) => new CustomResult(value))
|
|
32
|
-
* .$or(myresult);
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
export declare const ResultLikeSymbol: unique symbol;
|
|
36
|
-
export type ResultLikeSymbol = typeof ResultLikeSymbol;
|
|
37
|
-
export type ResultLike<T, E> = {
|
|
38
|
-
[ResultLikeSymbol](): Result<T, E>;
|
|
39
|
-
};
|
|
1
|
+
import { type ResultLike } from "retuple-symbols";
|
|
40
2
|
export type Ok = typeof Ok;
|
|
41
3
|
export type Err = typeof Err;
|
|
42
4
|
export type Result<T, E> = (OkTuple<T> | ErrTuple<E>) & Retuple<T, E>;
|
|
@@ -1413,6 +1375,10 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined>, ResultLike<T, E
|
|
|
1413
1375
|
* ```
|
|
1414
1376
|
*/
|
|
1415
1377
|
$or<U = T, F = E>(this: Result<T, E>, or: ResultLike<U, F>): Result<T | U, F>;
|
|
1378
|
+
/**
|
|
1379
|
+
* Shorthand for `result.$async().$or(...)`
|
|
1380
|
+
*/
|
|
1381
|
+
$orAsync<U = T, F = E>(this: Result<T, E>, or: ResultLikeAwaitable<U, F>): ResultAsync<T | U, F>;
|
|
1416
1382
|
/**
|
|
1417
1383
|
* Returns the result returned by the or function, when this result is `Err`.
|
|
1418
1384
|
*
|
|
@@ -1449,6 +1415,10 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined>, ResultLike<T, E
|
|
|
1449
1415
|
* ```
|
|
1450
1416
|
*/
|
|
1451
1417
|
$orElse<U = T, F = E>(this: Result<T, E>, f: (err: E) => ResultLike<U, F>): Result<T | U, F>;
|
|
1418
|
+
/**
|
|
1419
|
+
* Shorthand for `result.$async().$orElse(...)`
|
|
1420
|
+
*/
|
|
1421
|
+
$orElseAsync<U = T, F = E>(this: Result<T, E>, f: (err: E) => ResultLikeAwaitable<U, F>): ResultAsync<T | U, F>;
|
|
1452
1422
|
/**
|
|
1453
1423
|
* Returns a {@link Result} based on the outcome of the safe function when
|
|
1454
1424
|
* this result is `Err`.
|
|
@@ -1460,6 +1430,16 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined>, ResultLike<T, E
|
|
|
1460
1430
|
*/
|
|
1461
1431
|
$orSafe<U = T>(this: Result<T, E>, f: (err: E) => U): Result<T | U, Error>;
|
|
1462
1432
|
$orSafe<U = T, F = E>(this: Result<T, E>, f: (err: E) => U, mapError: (err: unknown) => F): Result<T | U, F>;
|
|
1433
|
+
/**
|
|
1434
|
+
* Shorthand for `result.$async().$orSafe(...)`
|
|
1435
|
+
*/
|
|
1436
|
+
$orSafeAsync<U = T>(this: Result<T, E>, f: (err: E) => U | PromiseLike<U>): ResultAsync<T | U, Error>;
|
|
1437
|
+
$orSafeAsync<U = T, F = E>(this: Result<T, E>, f: (err: E) => U | PromiseLike<U>, mapError: (err: unknown) => F): ResultAsync<T | U, F>;
|
|
1438
|
+
/**
|
|
1439
|
+
* Shorthand for `result.$async().$orSafePromise(...)`
|
|
1440
|
+
*/
|
|
1441
|
+
$orSafePromise<U = T>(this: Result<T, E>, promise: PromiseLike<U>): ResultAsync<T | U, Error>;
|
|
1442
|
+
$orSafePromise<U = T, F = E>(this: Result<T, E>, promise: PromiseLike<U>, mapError: (err: unknown) => F): ResultAsync<T | U, F>;
|
|
1463
1443
|
/**
|
|
1464
1444
|
* Returns the and result, when this result is `Ok`.
|
|
1465
1445
|
*
|
|
@@ -1496,6 +1476,10 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined>, ResultLike<T, E
|
|
|
1496
1476
|
* ```
|
|
1497
1477
|
*/
|
|
1498
1478
|
$and<U = T, F = E>(this: Result<T, E>, and: ResultLike<U, F>): Result<U, E | F>;
|
|
1479
|
+
/**
|
|
1480
|
+
* Shorthand for `result.$async().$and(...)`
|
|
1481
|
+
*/
|
|
1482
|
+
$andAsync<U = T, F = E>(this: Result<T, E>, and: ResultLikeAwaitable<U, F>): ResultAsync<U, E | F>;
|
|
1499
1483
|
/**
|
|
1500
1484
|
* Returns the and result, when this result is `Ok`.
|
|
1501
1485
|
*
|
|
@@ -1532,6 +1516,10 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined>, ResultLike<T, E
|
|
|
1532
1516
|
* ```
|
|
1533
1517
|
*/
|
|
1534
1518
|
$andThen<U = T, F = E>(this: Result<T, E>, f: (val: T) => ResultLike<U, F>): Result<U, E | F>;
|
|
1519
|
+
/**
|
|
1520
|
+
* Shorthand for `result.$async().$andThen(...)`
|
|
1521
|
+
*/
|
|
1522
|
+
$andThenAsync<U = T, F = E>(this: Result<T, E>, f: (val: T) => ResultLikeAwaitable<U, F>): ResultAsync<U, E | F>;
|
|
1535
1523
|
/**
|
|
1536
1524
|
* Calls the through function when this result is `Ok` and returns:
|
|
1537
1525
|
*
|
|
@@ -1572,6 +1560,10 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined>, ResultLike<T, E
|
|
|
1572
1560
|
* ```
|
|
1573
1561
|
*/
|
|
1574
1562
|
$andThrough<F = E>(this: Result<T, E>, f: (val: T) => ResultLike<any, F>): Result<T, E | F>;
|
|
1563
|
+
/**
|
|
1564
|
+
* Shorthand for `result.$async().$andThrough(...)`
|
|
1565
|
+
*/
|
|
1566
|
+
$andThroughAsync<F = E>(this: Result<T, E>, f: (val: T) => ResultLikeAwaitable<any, F>): ResultAsync<T, E | F>;
|
|
1575
1567
|
/**
|
|
1576
1568
|
* Returns a result based on the outcome of the safe function when this
|
|
1577
1569
|
* result is `Ok`.
|
|
@@ -1586,6 +1578,16 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined>, ResultLike<T, E
|
|
|
1586
1578
|
*/
|
|
1587
1579
|
$andSafe<U = T>(this: Result<T, E>, f: (val: T) => U): Result<U, E | Error>;
|
|
1588
1580
|
$andSafe<U = T, F = E>(this: Result<T, E>, f: (val: T) => U, mapError: (err: unknown) => F): Result<U, E | F>;
|
|
1581
|
+
/**
|
|
1582
|
+
* Shorthand for `result.$async().$andSafe(...)`
|
|
1583
|
+
*/
|
|
1584
|
+
$andSafeAsync<U = T>(this: Result<T, E>, f: (val: T) => U | PromiseLike<U>): ResultAsync<U, E | Error>;
|
|
1585
|
+
$andSafeAsync<U = T, F = E>(this: Result<T, E>, f: (val: T) => U | PromiseLike<U>, mapError: (err: unknown) => F): ResultAsync<U, E | F>;
|
|
1586
|
+
/**
|
|
1587
|
+
* Shorthand for `result.$async().$andSafePromise(...)`
|
|
1588
|
+
*/
|
|
1589
|
+
$andSafePromise<U = T>(this: Result<T, E>, promise: PromiseLike<U>): ResultAsync<U, E | Error>;
|
|
1590
|
+
$andSafePromise<U = T, F = E>(this: Result<T, E>, promise: PromiseLike<U>, mapError: (err: unknown) => F): ResultAsync<U, E | F>;
|
|
1589
1591
|
/**
|
|
1590
1592
|
* Calls the peek function and returns {@link Result} equivalent to this
|
|
1591
1593
|
* result.
|
package/dist/index.js
CHANGED
|
@@ -10,41 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
12
|
var _ResultAsync_inner, _a, _ResultRetry_f, _ResultRetry_promise, _ResultRetry_times, _ResultRetry_attempt, _ResultRetry_aborted, _ResultRetry_abort, _ResultRetry_getDelay, _ResultRetry_handler;
|
|
13
|
-
|
|
14
|
-
* ## Result Like Symbol
|
|
15
|
-
*
|
|
16
|
-
* Implement a custom result-like by implementing the `ResultLike` interface
|
|
17
|
-
* on a class or object. An object with this implementation can be converted
|
|
18
|
-
* to a `Result` and can be used in most places where a `Result` is required.
|
|
19
|
-
*
|
|
20
|
-
* ```ts
|
|
21
|
-
* import { ResultLikeSymbol } from "retuple/symbol";
|
|
22
|
-
* import { Result, Ok, Err, type ResultLike } from "retuple";
|
|
23
|
-
*
|
|
24
|
-
* class CustomResult<T> implements ResultLike<T, CustomError> {
|
|
25
|
-
* value: T;
|
|
26
|
-
*
|
|
27
|
-
* constructor(value: T) {
|
|
28
|
-
* this.value = value;
|
|
29
|
-
* }
|
|
30
|
-
*
|
|
31
|
-
* [ResultLikeSymbol](): Result<T, CustomError> {
|
|
32
|
-
* return this.value === "test"
|
|
33
|
-
* ? Ok(this.value)
|
|
34
|
-
* : Err(new CustomError("Value was not test"));
|
|
35
|
-
* }
|
|
36
|
-
* }
|
|
37
|
-
*
|
|
38
|
-
* const custom = new CustomResult("test");
|
|
39
|
-
* const result: Result<string, Error> = Result(custom);
|
|
40
|
-
*
|
|
41
|
-
* const chain = Ok()
|
|
42
|
-
* .$map(() => "value")
|
|
43
|
-
* .$andThen((value) => new CustomResult(value))
|
|
44
|
-
* .$or(myresult);
|
|
45
|
-
* ```
|
|
46
|
-
*/
|
|
47
|
-
export const ResultLikeSymbol = Symbol("retuple/result");
|
|
13
|
+
import { ResultLikeSymbol, } from "retuple-symbols";
|
|
48
14
|
/**
|
|
49
15
|
* ## Retuple Unwrap Failed
|
|
50
16
|
*
|
|
@@ -709,7 +675,7 @@ class ResultOk extends RetupleArray {
|
|
|
709
675
|
this[1] = value;
|
|
710
676
|
}
|
|
711
677
|
[ResultLikeSymbol]() {
|
|
712
|
-
return this;
|
|
678
|
+
return { ok: true, value: this[1] };
|
|
713
679
|
}
|
|
714
680
|
toJSON() {
|
|
715
681
|
return this[1];
|
|
@@ -762,22 +728,43 @@ class ResultOk extends RetupleArray {
|
|
|
762
728
|
$or() {
|
|
763
729
|
return this;
|
|
764
730
|
}
|
|
731
|
+
$orAsync() {
|
|
732
|
+
return this.$async();
|
|
733
|
+
}
|
|
765
734
|
$orElse() {
|
|
766
735
|
return this;
|
|
767
736
|
}
|
|
737
|
+
$orElseAsync() {
|
|
738
|
+
return this.$async();
|
|
739
|
+
}
|
|
768
740
|
$orSafe() {
|
|
769
741
|
return this;
|
|
770
742
|
}
|
|
743
|
+
$orSafeAsync() {
|
|
744
|
+
return this.$async();
|
|
745
|
+
}
|
|
746
|
+
$orSafePromise() {
|
|
747
|
+
return this.$async();
|
|
748
|
+
}
|
|
771
749
|
$and(and) {
|
|
772
750
|
return asResult(and);
|
|
773
751
|
}
|
|
752
|
+
$andAsync(and) {
|
|
753
|
+
return this.$async().$and(and);
|
|
754
|
+
}
|
|
774
755
|
$andThen(f) {
|
|
775
756
|
return asResult(f(this[1]));
|
|
776
757
|
}
|
|
758
|
+
$andThenAsync(f) {
|
|
759
|
+
return this.$async().$andThen(f);
|
|
760
|
+
}
|
|
777
761
|
$andThrough(f) {
|
|
778
762
|
const res = asResult(f(this[1]));
|
|
779
763
|
return res instanceof ResultErr ? res : this;
|
|
780
764
|
}
|
|
765
|
+
$andThroughAsync(f) {
|
|
766
|
+
return this.$async().$andThrough(f);
|
|
767
|
+
}
|
|
781
768
|
$andSafe(f, mapError = ensureError) {
|
|
782
769
|
try {
|
|
783
770
|
return Ok(f(this[1]));
|
|
@@ -786,6 +773,12 @@ class ResultOk extends RetupleArray {
|
|
|
786
773
|
return Err(mapError(err));
|
|
787
774
|
}
|
|
788
775
|
}
|
|
776
|
+
$andSafeAsync(f, mapError = ensureError) {
|
|
777
|
+
return this.$async().$andSafe(f, mapError);
|
|
778
|
+
}
|
|
779
|
+
$andSafePromise(promise, mapError = ensureError) {
|
|
780
|
+
return this.$async().$andSafePromise(promise, mapError);
|
|
781
|
+
}
|
|
789
782
|
$peek(f) {
|
|
790
783
|
f(this);
|
|
791
784
|
return this;
|
|
@@ -822,7 +815,7 @@ class ResultErr extends RetupleArray {
|
|
|
822
815
|
this[1] = undefined;
|
|
823
816
|
}
|
|
824
817
|
[ResultLikeSymbol]() {
|
|
825
|
-
return this;
|
|
818
|
+
return { ok: false, value: this[0] };
|
|
826
819
|
}
|
|
827
820
|
toJSON() {
|
|
828
821
|
return null;
|
|
@@ -878,9 +871,15 @@ class ResultErr extends RetupleArray {
|
|
|
878
871
|
$or(or) {
|
|
879
872
|
return asResult(or);
|
|
880
873
|
}
|
|
874
|
+
$orAsync(or) {
|
|
875
|
+
return this.$async().$or(or);
|
|
876
|
+
}
|
|
881
877
|
$orElse(f) {
|
|
882
878
|
return asResult(f(this[0]));
|
|
883
879
|
}
|
|
880
|
+
$orElseAsync(f) {
|
|
881
|
+
return this.$async().$orElse(f);
|
|
882
|
+
}
|
|
884
883
|
$orSafe(f, mapError = ensureError) {
|
|
885
884
|
try {
|
|
886
885
|
return Ok(f(this[0]));
|
|
@@ -889,18 +888,39 @@ class ResultErr extends RetupleArray {
|
|
|
889
888
|
return Err(mapError(err));
|
|
890
889
|
}
|
|
891
890
|
}
|
|
891
|
+
$orSafeAsync(f, mapError = ensureError) {
|
|
892
|
+
return this.$async().$orSafe(f, mapError);
|
|
893
|
+
}
|
|
894
|
+
$orSafePromise(promise, mapError = ensureError) {
|
|
895
|
+
return this.$async().$orSafePromise(promise, mapError);
|
|
896
|
+
}
|
|
892
897
|
$and() {
|
|
893
898
|
return this;
|
|
894
899
|
}
|
|
900
|
+
$andAsync() {
|
|
901
|
+
return this.$async();
|
|
902
|
+
}
|
|
895
903
|
$andThen() {
|
|
896
904
|
return this;
|
|
897
905
|
}
|
|
906
|
+
$andThenAsync() {
|
|
907
|
+
return this.$async();
|
|
908
|
+
}
|
|
898
909
|
$andThrough() {
|
|
899
910
|
return this;
|
|
900
911
|
}
|
|
912
|
+
$andThroughAsync() {
|
|
913
|
+
return this.$async();
|
|
914
|
+
}
|
|
901
915
|
$andSafe() {
|
|
902
916
|
return this;
|
|
903
917
|
}
|
|
918
|
+
$andSafeAsync() {
|
|
919
|
+
return this.$async();
|
|
920
|
+
}
|
|
921
|
+
$andSafePromise() {
|
|
922
|
+
return this.$async();
|
|
923
|
+
}
|
|
904
924
|
$peek(f) {
|
|
905
925
|
f(this);
|
|
906
926
|
return this;
|
|
@@ -1340,7 +1360,11 @@ _a = ResultRetry, _ResultRetry_f = new WeakMap(), _ResultRetry_promise = new Wea
|
|
|
1340
1360
|
ResultRetry.MAX_TIMEOUT = 3600000;
|
|
1341
1361
|
ResultRetry.MAX_RETRY = 100;
|
|
1342
1362
|
function asResult(resultLike) {
|
|
1343
|
-
|
|
1363
|
+
if (resultLike instanceof ResultOk || resultLike instanceof ResultErr) {
|
|
1364
|
+
return resultLike;
|
|
1365
|
+
}
|
|
1366
|
+
const result = resultLike[ResultLikeSymbol]();
|
|
1367
|
+
return result.ok ? new ResultOk(result.value) : new ResultErr(result.value);
|
|
1344
1368
|
}
|
|
1345
1369
|
function ensureError(err) {
|
|
1346
1370
|
if (err instanceof Error) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "retuple",
|
|
3
|
-
"version": "1.0.0-next.
|
|
3
|
+
"version": "1.0.0-next.17",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"test": "vitest",
|
|
6
6
|
"lint": "eslint . --ext .ts -c eslint.config.mjs --fix",
|
|
@@ -27,16 +27,19 @@
|
|
|
27
27
|
"type": "git",
|
|
28
28
|
"url": "git+https://github.com/traverse1984/retuple.git"
|
|
29
29
|
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"retuple-symbols": "^1.0.0-next.2"
|
|
32
|
+
},
|
|
30
33
|
"devDependencies": {
|
|
31
|
-
"typescript": "^
|
|
32
|
-
"
|
|
33
|
-
"vitest": "^3.2.4",
|
|
34
|
+
"@typescript-eslint/eslint-plugin": "^8.43.0",
|
|
35
|
+
"@typescript-eslint/parser": "^8.43.0",
|
|
34
36
|
"@vitest/coverage-v8": "^3.2.4",
|
|
35
37
|
"eslint": "^9.35.0",
|
|
36
38
|
"eslint-config-prettier": "^10.1.8",
|
|
37
39
|
"eslint-plugin-prettier": "^5.5.4",
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
+
"typescript": "^5.9.2",
|
|
41
|
+
"vitest": "^3.2.4",
|
|
42
|
+
"zshy": "^0.4.1"
|
|
40
43
|
},
|
|
41
44
|
"exports": {
|
|
42
45
|
".": {
|