retuple 1.0.0-next.25 → 1.0.0-next.27

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
@@ -12,7 +12,7 @@ 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.RetupleCheckFailedError = exports.RetupleInvalidUnionError = exports.RetupleCaughtValueError = exports.RetupleExpectFailed = exports.RetupleUnwrapErrFailed = exports.RetupleUnwrapFailed = void 0;
15
+ exports.RetupleInvalidUnionError = exports.RetupleCaughtValueError = exports.RetupleIndexFailed = exports.RetupleFilterFailed = exports.RetupleAssertFailed = exports.RetupleExpectFailed = exports.RetupleUnwrapErrFailed = exports.RetupleUnwrapFailed = void 0;
16
16
  exports.Result = Result;
17
17
  exports.Ok = Ok;
18
18
  exports.Err = Err;
@@ -54,6 +54,46 @@ class RetupleExpectFailed extends Error {
54
54
  }
55
55
  }
56
56
  exports.RetupleExpectFailed = RetupleExpectFailed;
57
+ /**
58
+ * ## Retuple Assert Failed
59
+ *
60
+ * This error is used as the error type of a `Result` when using $assert,
61
+ * when no map error function is provided.
62
+ */
63
+ class RetupleAssertFailed extends Error {
64
+ constructor(value) {
65
+ super("Assert failed");
66
+ this.value = value;
67
+ }
68
+ }
69
+ exports.RetupleAssertFailed = RetupleAssertFailed;
70
+ /**
71
+ * ## Retuple Filter Failed
72
+ *
73
+ * This error is used as the error type of a `Result` when using $filter,
74
+ * when no map error function is provided.
75
+ */
76
+ class RetupleFilterFailed extends Error {
77
+ constructor(value) {
78
+ super("Filter failed");
79
+ this.value = value;
80
+ }
81
+ }
82
+ exports.RetupleFilterFailed = RetupleFilterFailed;
83
+ /**
84
+ * ## Retuple Index Failed
85
+ *
86
+ * This error is used as the error type of a `Result` when using $atIndex or
87
+ * $firstIndex, when no map error function is provided.
88
+ */
89
+ class RetupleIndexFailed extends Error {
90
+ constructor(index, value) {
91
+ super("Index failed");
92
+ this.index = index;
93
+ this.value = value;
94
+ }
95
+ }
96
+ exports.RetupleIndexFailed = RetupleIndexFailed;
57
97
  /**
58
98
  * ## Retuple Thrown Value Error
59
99
  *
@@ -84,19 +124,6 @@ class RetupleInvalidUnionError extends Error {
84
124
  }
85
125
  }
86
126
  exports.RetupleInvalidUnionError = RetupleInvalidUnionError;
87
- /**
88
- * ## Retuple Check Failed Error
89
- *
90
- * This error is used as the error type of a `Result` when using $andAssert
91
- * or $andCheck, when no custom error handler is provided.
92
- */
93
- class RetupleCheckFailedError extends Error {
94
- constructor(value) {
95
- super("Check failed");
96
- this.value = value;
97
- }
98
- }
99
- exports.RetupleCheckFailedError = RetupleCheckFailedError;
100
127
  /**
101
128
  * ## Result
102
129
  *
@@ -579,19 +606,33 @@ class ResultOk extends Array {
579
606
  $mapOrElse(_def, f) {
580
607
  return Ok(f(this[1]));
581
608
  }
582
- $assert(mapError = mapCheckError) {
583
- return this[1] ? this : Err(mapError(this[1]));
609
+ $assert(mapError) {
610
+ return this[1]
611
+ ? this
612
+ : Err(mapError
613
+ ? mapError(this[1])
614
+ : new RetupleAssertFailed(this[1]));
584
615
  }
585
- $check(check, mapError = mapCheckError) {
586
- return check(this[1]) ? this : Err(mapError(this[1]));
616
+ $filter(filter, mapError) {
617
+ return filter(this[1])
618
+ ? this
619
+ : Err(mapError ? mapError(this[1]) : new RetupleFilterFailed(this[1]));
587
620
  }
588
- $atIndex(index, mapError = mapCheckError) {
621
+ $atIndex(index, mapError) {
589
622
  const element = this[1][index];
590
- return element ? Ok(element) : Err(mapError(this[1]));
623
+ return element
624
+ ? Ok(element)
625
+ : Err(mapError
626
+ ? mapError(this[1])
627
+ : new RetupleIndexFailed(index, this[1]));
591
628
  }
592
- $firstIndex(mapError = mapCheckError) {
629
+ $firstIndex(mapError) {
593
630
  const first = this[1][0];
594
- return first ? Ok(first) : Err(mapError(this[1]));
631
+ return first
632
+ ? Ok(first)
633
+ : Err(mapError
634
+ ? mapError(this[1])
635
+ : new RetupleIndexFailed(0, this[1]));
595
636
  }
596
637
  $or() {
597
638
  return this;
@@ -739,7 +780,7 @@ class ResultErr extends Array {
739
780
  $assert() {
740
781
  return this;
741
782
  }
742
- $check() {
783
+ $filter() {
743
784
  return this;
744
785
  }
745
786
  $atIndex() {
@@ -922,40 +963,54 @@ class ResultAsync {
922
963
  : new ResultOk(def(res[0]));
923
964
  }));
924
965
  }
925
- $assert(mapError = mapCheckError) {
966
+ $assert(mapError) {
926
967
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
927
968
  if (res instanceof ResultErr) {
928
969
  return res;
929
970
  }
930
971
  return res[1]
931
972
  ? res
932
- : Err(mapError(res[1]));
973
+ : Err(mapError
974
+ ? mapError(res[1])
975
+ : new RetupleAssertFailed(res[1]));
933
976
  }));
934
977
  }
935
- $check(check, mapError = mapCheckError) {
978
+ $filter(check, mapError) {
936
979
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
937
980
  if (res instanceof ResultErr) {
938
981
  return res;
939
982
  }
940
- return check(res[1]) ? res : Err(mapError(res[1]));
983
+ return check(res[1])
984
+ ? res
985
+ : Err(mapError
986
+ ? mapError(res[1])
987
+ : new RetupleFilterFailed(res[1]));
941
988
  }));
942
989
  }
943
- $atIndex(index, mapError = mapCheckError) {
990
+ $atIndex(index, mapError) {
944
991
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then((res) => {
945
992
  if (res instanceof ResultErr) {
946
993
  return res;
947
994
  }
948
995
  const element = res[1][index];
949
- return element ? Ok(element) : Err(mapError(res[1]));
996
+ return element
997
+ ? Ok(element)
998
+ : Err(mapError
999
+ ? mapError(res[1])
1000
+ : new RetupleIndexFailed(index, res[1]));
950
1001
  }));
951
1002
  }
952
- $firstIndex(mapError = mapCheckError) {
1003
+ $firstIndex(mapError) {
953
1004
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then((res) => {
954
1005
  if (res instanceof ResultErr) {
955
1006
  return res;
956
1007
  }
957
1008
  const first = res[1][0];
958
- return first ? Ok(first) : Err(mapError(res[1]));
1009
+ return first
1010
+ ? Ok(first)
1011
+ : Err(mapError
1012
+ ? mapError(res[1])
1013
+ : new RetupleIndexFailed(0, res[1]));
959
1014
  }));
960
1015
  }
961
1016
  $or(or) {
@@ -1272,6 +1327,3 @@ function ensureError(err) {
1272
1327
  function mapTrue() {
1273
1328
  return true;
1274
1329
  }
1275
- function mapCheckError(value) {
1276
- return new RetupleCheckFailedError(value);
1277
- }
package/dist/index.d.cts CHANGED
@@ -36,6 +36,37 @@ export declare class RetupleExpectFailed<const E = unknown> extends Error {
36
36
  value: E;
37
37
  constructor(value: E);
38
38
  }
39
+ /**
40
+ * ## Retuple Assert Failed
41
+ *
42
+ * This error is used as the error type of a `Result` when using $assert,
43
+ * when no map error function is provided.
44
+ */
45
+ export declare class RetupleAssertFailed<const T = unknown> extends Error {
46
+ value: T;
47
+ constructor(value: T);
48
+ }
49
+ /**
50
+ * ## Retuple Filter Failed
51
+ *
52
+ * This error is used as the error type of a `Result` when using $filter,
53
+ * when no map error function is provided.
54
+ */
55
+ export declare class RetupleFilterFailed<const T = unknown> extends Error {
56
+ value: T;
57
+ constructor(value: T);
58
+ }
59
+ /**
60
+ * ## Retuple Index Failed
61
+ *
62
+ * This error is used as the error type of a `Result` when using $atIndex or
63
+ * $firstIndex, when no map error function is provided.
64
+ */
65
+ export declare class RetupleIndexFailed<const T = unknown> extends Error {
66
+ index: number;
67
+ value: T;
68
+ constructor(index: number, value: T);
69
+ }
39
70
  /**
40
71
  * ## Retuple Thrown Value Error
41
72
  *
@@ -59,16 +90,6 @@ export declare class RetupleInvalidUnionError extends Error {
59
90
  value: unknown;
60
91
  constructor(value: unknown);
61
92
  }
62
- /**
63
- * ## Retuple Check Failed Error
64
- *
65
- * This error is used as the error type of a `Result` when using $andAssert
66
- * or $andCheck, when no custom error handler is provided.
67
- */
68
- export declare class RetupleCheckFailedError<const T = unknown> extends Error {
69
- value: T;
70
- constructor(value: T);
71
- }
72
93
  /**
73
94
  * ## Result
74
95
  *
@@ -226,30 +247,30 @@ declare class ResultAsync<T, E> {
226
247
  */
227
248
  $mapOrElse<U, V = U>(this: ResultAsync<T, E>, def: (err: E) => U, f: (val: T) => V): ResultAsync<U | V, never>;
228
249
  /**
229
- * The same as {@link Retuple.$check|$andAssert}, except it returns
250
+ * The same as {@link Retuple.$assert|$assert}, except it returns
230
251
  * {@link ResultAsync}.
231
252
  */
232
- $assert(this: ResultAsync<T, E>): ResultAsync<Truthy<T>, E | RetupleCheckFailedError<T>>;
233
- $assert<F = E>(this: ResultAsync<T, E>, mapError: (val: T) => F): ResultAsync<Truthy<T>, E | F>;
253
+ $assert(this: ResultAsync<T, E>): ResultAsync<Truthy<T>, E | RetupleAssertFailed<T>>;
254
+ $assert<F = E>(this: ResultAsync<T, E>, mapError: (val: Falsey<T>) => F): ResultAsync<Truthy<T>, E | F>;
234
255
  /**
235
- * The same as {@link Retuple.$check|$andCheck}, except it returns
256
+ * The same as {@link Retuple.$filter|$filter}, except it returns
236
257
  * {@link ResultAsync}.
237
258
  */
238
- $check<U extends T = T>(this: ResultAsync<T, E>, predicate: (val: T) => val is U): ResultAsync<U, E | RetupleCheckFailedError<T>>;
239
- $check(this: ResultAsync<T, E>, check: (val: T) => unknown): ResultAsync<T, E | RetupleCheckFailedError<T>>;
240
- $check<U extends T = T, F = E>(this: ResultAsync<T, E>, predicate: (val: T) => val is U, mapError: (val: T) => F): ResultAsync<U, E | F>;
241
- $check<F = E>(this: ResultAsync<T, E>, check: (val: T) => unknown, mapError: (val: T) => F): ResultAsync<T, E | F>;
259
+ $filter<U extends T = T>(this: ResultAsync<T, E>, predicate: (val: T) => val is U): ResultAsync<U, E | RetupleFilterFailed<T>>;
260
+ $filter(this: ResultAsync<T, E>, check: (val: T) => unknown): ResultAsync<T, E | RetupleFilterFailed<T>>;
261
+ $filter<U extends T = T, F = E>(this: ResultAsync<T, E>, predicate: (val: T) => val is U, mapError: (val: T) => F): ResultAsync<U, E | F>;
262
+ $filter<F = E>(this: ResultAsync<T, E>, check: (val: T) => unknown, mapError: (val: T) => F): ResultAsync<T, E | F>;
242
263
  /**
243
264
  * The same as {@link Retuple.$atIndex|$atIndex}, except it returns
244
265
  * {@link ResultAsync}.
245
266
  */
246
- $atIndex<U>(this: ResultAsync<readonly U[], E>, index: number): ResultAsync<U, E | RetupleCheckFailedError<T>>;
267
+ $atIndex<U>(this: ResultAsync<readonly U[], E>, index: number): ResultAsync<U, E | RetupleFilterFailed<T>>;
247
268
  $atIndex<U, F = E>(this: ResultAsync<readonly U[], E>, index: number, mapError: (val: T) => F): ResultAsync<U, E | F>;
248
269
  /**
249
270
  * The same as {@link Retuple.$firstIndex|$firstIndex}, except it returns
250
271
  * {@link ResultAsync}.
251
272
  */
252
- $firstIndex<U>(this: ResultAsync<readonly U[], E>): ResultAsync<U, E | RetupleCheckFailedError<T>>;
273
+ $firstIndex<U>(this: ResultAsync<readonly U[], E>): ResultAsync<U, E | RetupleIndexFailed<T>>;
253
274
  $firstIndex<U, F = E>(this: ResultAsync<readonly U[], E>, mapError: (val: T) => F): ResultAsync<U, E | F>;
254
275
  /**
255
276
  * The same as {@link Retuple.$or|$or}, except it:
@@ -791,7 +812,7 @@ interface Retuple<T, E> extends ResultLike<T, E> {
791
812
  * Narrows the `T` type to include only truthy values;
792
813
  * - returning `Err` containing the return value of the map error function
793
814
  * when the ok value is falsey;
794
- * - returning `Err` containing {@link RetupleCheckFailedError} when the
815
+ * - returning `Err` containing {@link RetupleAssertFailed} when the
795
816
  * ok value is falsey, and when no map error function is provided.
796
817
  *
797
818
  * Otherwise returns `Err` containing the current error value.
@@ -800,7 +821,7 @@ interface Retuple<T, E> extends ResultLike<T, E> {
800
821
  *
801
822
  * ```ts
802
823
  * const result: Result<string | null, never> = Ok("test");
803
- * const asserted = result.$andAssert();
824
+ * const asserted = result.$assert();
804
825
  *
805
826
  * asserted satisfies Result<string, RetupleCheckFailedError<string | null>>;
806
827
  * assert.equal(asserted.$unwrap(), "test");
@@ -810,7 +831,7 @@ interface Retuple<T, E> extends ResultLike<T, E> {
810
831
  *
811
832
  * ```ts
812
833
  * const result: Result<string | null | undefined, never> = Ok(null);
813
- * const asserted = result.$andAssert(
834
+ * const asserted = result.$assert(
814
835
  * (val) => val === null ? "value was null" : "value was undefined"
815
836
  * );
816
837
  *
@@ -818,17 +839,17 @@ interface Retuple<T, E> extends ResultLike<T, E> {
818
839
  * assert.equal(asserted.$unwrapErr(), "value was null");
819
840
  * ```
820
841
  */
821
- $assert(this: Result<T, E>): Result<Truthy<T>, E | RetupleCheckFailedError<T>>;
822
- $assert<F = E>(this: Result<T, E>, mapError: (val: T) => F): Result<Truthy<T>, E | F>;
842
+ $assert(this: Result<T, E>): Result<Truthy<T>, E | RetupleFilterFailed<T>>;
843
+ $assert<F = E>(this: Result<T, E>, mapError: (val: Falsey<T>) => F): Result<Truthy<T>, E | F>;
823
844
  /**
824
845
  * Performs a check when this result is `Ok`:
825
846
  *
826
- * - returning `Ok` containing the current ok value when the predicate/check
827
- * function returns true. Narrows the `T` type based on the check function;
847
+ * - returning `Ok` containing the current ok value when the filter function
848
+ * returns true. Narrows the `T` type based on the filter function;
828
849
  * - returning `Err` containing the return value of the map error function
829
- * when the ok value fails the check;
830
- * - returning `Err` containing {@link RetupleCheckFailedError} when the
831
- * ok value fails the check, and when no map error function is provided.
850
+ * when the ok value fails the filter;
851
+ * - returning `Err` containing {@link RetupleFilterFailed} when the
852
+ * ok value fails the filter, and when no map error function is provided.
832
853
  *
833
854
  * Otherwise returns `Err` containing the current error value.
834
855
  *
@@ -836,7 +857,7 @@ interface Retuple<T, E> extends ResultLike<T, E> {
836
857
  *
837
858
  * ```ts
838
859
  * const result: Result<string, never> = Ok("test");
839
- * const asserted = result.$andCheck((val) => val === "test");
860
+ * const asserted = result.$filter((val) => val === "test");
840
861
  *
841
862
  * asserted satisfies Result<string, RetupleCheckFailedError<string>>;
842
863
  * assert.equal(asserted.$unwrap(), "test");
@@ -846,7 +867,7 @@ interface Retuple<T, E> extends ResultLike<T, E> {
846
867
  *
847
868
  * ```ts
848
869
  * const result: Result<string, never> = Ok("test");
849
- * const checked = result.$andCheck(
870
+ * const checked = result.$filter(
850
871
  * (val) => val === "value",
851
872
  * (val) => `value was ${val}`,
852
873
  * );
@@ -855,10 +876,10 @@ interface Retuple<T, E> extends ResultLike<T, E> {
855
876
  * assert.equal(checked.$unwrapErr(), "value was test");
856
877
  * ```
857
878
  */
858
- $check<U extends T = T>(this: Result<T, E>, predicate: (val: T) => val is U): Result<U, E | RetupleCheckFailedError<T>>;
859
- $check(this: Result<T, E>, check: (val: T) => unknown): Result<T, E | RetupleCheckFailedError<T>>;
860
- $check<U extends T = T, F = E>(this: Result<T, E>, predicate: (val: T) => val is U, mapError: (val: T) => F): Result<U, E | F>;
861
- $check<F = E>(this: Result<T, E>, check: (val: T) => unknown, mapError: (val: T) => F): Result<T, E | F>;
879
+ $filter<U extends T = T>(this: Result<T, E>, predicate: (val: T) => val is U): Result<U, E | RetupleFilterFailed<T>>;
880
+ $filter(this: Result<T, E>, filter: (val: T) => unknown): Result<T, E | RetupleFilterFailed<T>>;
881
+ $filter<U extends T = T, F = E>(this: Result<T, E>, predicate: (val: T) => val is U, mapError: (val: T) => F): Result<U, E | F>;
882
+ $filter<F = E>(this: Result<T, E>, filter: (val: T) => unknown, mapError: (val: T) => F): Result<T, E | F>;
862
883
  /**
863
884
  * Checks the specified element of the contained array when when this result
864
885
  * is `Ok`:
@@ -866,9 +887,9 @@ interface Retuple<T, E> extends ResultLike<T, E> {
866
887
  * - returning `Ok` containing the specified element when it is truthy.
867
888
  * Narrows the type to include only truthy values;
868
889
  * - returning `Err` containing the return value of the map error function
869
- * when the first array element fails the check;
870
- * - returning `Err` containing {@link RetupleCheckFailedError} when the
871
- * first array elemnt fails the check, and when no map error function is
890
+ * when the specified array element fails the check;
891
+ * - returning `Err` containing {@link RetupleIndexFailed} when the specified
892
+ * array element fails the check, and when no map error function is
872
893
  * provided.
873
894
  *
874
895
  * Otherwise returns `Err` containing the current error value.
@@ -876,8 +897,8 @@ interface Retuple<T, E> extends ResultLike<T, E> {
876
897
  * @example
877
898
  *
878
899
  * ```ts
879
- * const result: Result<(string | null)[], never> = Ok(["test", null]);
880
- * const first = result.$andFirst();
900
+ * const result: Result<(string | null)[], never> = Ok([null, "test"]);
901
+ * const first = result.$atIndex(1);
881
902
  *
882
903
  * first satisfies Result<string, RetupleCheckFailedError<string | null>>;
883
904
  * assert.equal(first.$unwrap(), "test");
@@ -886,8 +907,9 @@ interface Retuple<T, E> extends ResultLike<T, E> {
886
907
  * @example
887
908
  *
888
909
  * ```ts
889
- * const result: Result<(string | null | undefined)[], never> = Ok([null, "test"]);
890
- * const first = result.$andFirst(
910
+ * const result: Result<(string | null | undefined)[], never> = Ok(["test", null]);
911
+ * const first = result.$atIndex(
912
+ * 1,
891
913
  * (val) => val === null ? "value was null" : "value was undefined",
892
914
  * );
893
915
  *
@@ -895,12 +917,12 @@ interface Retuple<T, E> extends ResultLike<T, E> {
895
917
  * assert.equal(first.$unwrapErr(), "value was null");
896
918
  * ```
897
919
  */
898
- $atIndex<U>(this: Result<readonly U[], E>, index: number): Result<Truthy<U>, E | RetupleCheckFailedError<T>>;
920
+ $atIndex<U>(this: Result<readonly U[], E>, index: number): Result<Truthy<U>, E | RetupleIndexFailed<T>>;
899
921
  $atIndex<U, F = E>(this: Result<readonly U[], E>, index: number, mapError: (val: T) => F): Result<Truthy<U>, E | F>;
900
922
  /**
901
923
  * Equivalent to calling `result.$atIndex(0)`.
902
924
  */
903
- $firstIndex<U>(this: Result<readonly U[], E>): Result<Truthy<U>, E | RetupleCheckFailedError<T>>;
925
+ $firstIndex<U>(this: Result<readonly U[], E>): Result<Truthy<U>, E | RetupleFilterFailed<T>>;
904
926
  $firstIndex<U, F = E>(this: Result<readonly U[], E>, mapError: (val: T) => F): Result<Truthy<U>, E | F>;
905
927
  /**
906
928
  * Returns `Ok` containing the return value of the map function when this
@@ -1518,17 +1540,18 @@ type ObjectUnionErr<E> = {
1518
1540
  error: E;
1519
1541
  };
1520
1542
  type AllOk<TResults extends ResultLikeAwaitable<any, any>[]> = {
1521
- [K in keyof TResults]: TResults[K] extends Result<infer T, any> ? T : never;
1543
+ [K in keyof TResults]: TResults[K] extends ResultLikeAwaitable<infer T, any> ? T : never;
1522
1544
  };
1523
1545
  type AllErr<TResults extends ResultLikeAwaitable<any, any>[]> = TResults[number] extends ResultLikeAwaitable<any, infer E> ? E : never;
1524
1546
  type AnyOk<TResult extends ResultLikeAwaitable<any, any>[]> = TResult[number] extends ResultLikeAwaitable<infer T, any> ? T : never;
1525
1547
  type AnyErr<TResults extends ResultLikeAwaitable<any, any>[]> = {
1526
- [K in keyof TResults]: TResults[K] extends Result<any, infer E> ? E : never;
1548
+ [K in keyof TResults]: TResults[K] extends ResultLikeAwaitable<any, infer E> ? E : never;
1527
1549
  };
1528
1550
  type CollectOk<TResults extends Record<string, ResultLikeAwaitable<any, any>>> = {
1529
- [K in keyof TResults]: TResults[K] extends Result<infer T, any> ? T : never;
1551
+ [K in keyof TResults]: TResults[K] extends ResultLikeAwaitable<infer T, any> ? T : never;
1530
1552
  } & {};
1531
- type CollectErr<TResults extends Record<string, ResultLikeAwaitable<any, any>>> = TResults[keyof TResults] extends Result<any, infer E> ? E : never;
1553
+ type CollectErr<TResults extends Record<string, ResultLikeAwaitable<any, any>>> = TResults[keyof TResults] extends ResultLikeAwaitable<any, infer E> ? E : never;
1532
1554
  type Truthy<T> = Exclude<T, false | null | undefined | 0 | 0n | "">;
1555
+ type Falsey<T> = Extract<T, false | null | undefined | 0 | 0n | "">;
1533
1556
  type NonZero<N extends number> = N & (`${N}` extends "0" ? never : N);
1534
1557
  type NonNegativeOrDecimal<N extends number> = N & (`${N}` extends `-${string}` | `${string}.${string}` ? never : N);
package/dist/index.d.ts CHANGED
@@ -36,6 +36,37 @@ export declare class RetupleExpectFailed<const E = unknown> extends Error {
36
36
  value: E;
37
37
  constructor(value: E);
38
38
  }
39
+ /**
40
+ * ## Retuple Assert Failed
41
+ *
42
+ * This error is used as the error type of a `Result` when using $assert,
43
+ * when no map error function is provided.
44
+ */
45
+ export declare class RetupleAssertFailed<const T = unknown> extends Error {
46
+ value: T;
47
+ constructor(value: T);
48
+ }
49
+ /**
50
+ * ## Retuple Filter Failed
51
+ *
52
+ * This error is used as the error type of a `Result` when using $filter,
53
+ * when no map error function is provided.
54
+ */
55
+ export declare class RetupleFilterFailed<const T = unknown> extends Error {
56
+ value: T;
57
+ constructor(value: T);
58
+ }
59
+ /**
60
+ * ## Retuple Index Failed
61
+ *
62
+ * This error is used as the error type of a `Result` when using $atIndex or
63
+ * $firstIndex, when no map error function is provided.
64
+ */
65
+ export declare class RetupleIndexFailed<const T = unknown> extends Error {
66
+ index: number;
67
+ value: T;
68
+ constructor(index: number, value: T);
69
+ }
39
70
  /**
40
71
  * ## Retuple Thrown Value Error
41
72
  *
@@ -59,16 +90,6 @@ export declare class RetupleInvalidUnionError extends Error {
59
90
  value: unknown;
60
91
  constructor(value: unknown);
61
92
  }
62
- /**
63
- * ## Retuple Check Failed Error
64
- *
65
- * This error is used as the error type of a `Result` when using $andAssert
66
- * or $andCheck, when no custom error handler is provided.
67
- */
68
- export declare class RetupleCheckFailedError<const T = unknown> extends Error {
69
- value: T;
70
- constructor(value: T);
71
- }
72
93
  /**
73
94
  * ## Result
74
95
  *
@@ -226,30 +247,30 @@ declare class ResultAsync<T, E> {
226
247
  */
227
248
  $mapOrElse<U, V = U>(this: ResultAsync<T, E>, def: (err: E) => U, f: (val: T) => V): ResultAsync<U | V, never>;
228
249
  /**
229
- * The same as {@link Retuple.$check|$andAssert}, except it returns
250
+ * The same as {@link Retuple.$assert|$assert}, except it returns
230
251
  * {@link ResultAsync}.
231
252
  */
232
- $assert(this: ResultAsync<T, E>): ResultAsync<Truthy<T>, E | RetupleCheckFailedError<T>>;
233
- $assert<F = E>(this: ResultAsync<T, E>, mapError: (val: T) => F): ResultAsync<Truthy<T>, E | F>;
253
+ $assert(this: ResultAsync<T, E>): ResultAsync<Truthy<T>, E | RetupleAssertFailed<T>>;
254
+ $assert<F = E>(this: ResultAsync<T, E>, mapError: (val: Falsey<T>) => F): ResultAsync<Truthy<T>, E | F>;
234
255
  /**
235
- * The same as {@link Retuple.$check|$andCheck}, except it returns
256
+ * The same as {@link Retuple.$filter|$filter}, except it returns
236
257
  * {@link ResultAsync}.
237
258
  */
238
- $check<U extends T = T>(this: ResultAsync<T, E>, predicate: (val: T) => val is U): ResultAsync<U, E | RetupleCheckFailedError<T>>;
239
- $check(this: ResultAsync<T, E>, check: (val: T) => unknown): ResultAsync<T, E | RetupleCheckFailedError<T>>;
240
- $check<U extends T = T, F = E>(this: ResultAsync<T, E>, predicate: (val: T) => val is U, mapError: (val: T) => F): ResultAsync<U, E | F>;
241
- $check<F = E>(this: ResultAsync<T, E>, check: (val: T) => unknown, mapError: (val: T) => F): ResultAsync<T, E | F>;
259
+ $filter<U extends T = T>(this: ResultAsync<T, E>, predicate: (val: T) => val is U): ResultAsync<U, E | RetupleFilterFailed<T>>;
260
+ $filter(this: ResultAsync<T, E>, check: (val: T) => unknown): ResultAsync<T, E | RetupleFilterFailed<T>>;
261
+ $filter<U extends T = T, F = E>(this: ResultAsync<T, E>, predicate: (val: T) => val is U, mapError: (val: T) => F): ResultAsync<U, E | F>;
262
+ $filter<F = E>(this: ResultAsync<T, E>, check: (val: T) => unknown, mapError: (val: T) => F): ResultAsync<T, E | F>;
242
263
  /**
243
264
  * The same as {@link Retuple.$atIndex|$atIndex}, except it returns
244
265
  * {@link ResultAsync}.
245
266
  */
246
- $atIndex<U>(this: ResultAsync<readonly U[], E>, index: number): ResultAsync<U, E | RetupleCheckFailedError<T>>;
267
+ $atIndex<U>(this: ResultAsync<readonly U[], E>, index: number): ResultAsync<U, E | RetupleFilterFailed<T>>;
247
268
  $atIndex<U, F = E>(this: ResultAsync<readonly U[], E>, index: number, mapError: (val: T) => F): ResultAsync<U, E | F>;
248
269
  /**
249
270
  * The same as {@link Retuple.$firstIndex|$firstIndex}, except it returns
250
271
  * {@link ResultAsync}.
251
272
  */
252
- $firstIndex<U>(this: ResultAsync<readonly U[], E>): ResultAsync<U, E | RetupleCheckFailedError<T>>;
273
+ $firstIndex<U>(this: ResultAsync<readonly U[], E>): ResultAsync<U, E | RetupleIndexFailed<T>>;
253
274
  $firstIndex<U, F = E>(this: ResultAsync<readonly U[], E>, mapError: (val: T) => F): ResultAsync<U, E | F>;
254
275
  /**
255
276
  * The same as {@link Retuple.$or|$or}, except it:
@@ -791,7 +812,7 @@ interface Retuple<T, E> extends ResultLike<T, E> {
791
812
  * Narrows the `T` type to include only truthy values;
792
813
  * - returning `Err` containing the return value of the map error function
793
814
  * when the ok value is falsey;
794
- * - returning `Err` containing {@link RetupleCheckFailedError} when the
815
+ * - returning `Err` containing {@link RetupleAssertFailed} when the
795
816
  * ok value is falsey, and when no map error function is provided.
796
817
  *
797
818
  * Otherwise returns `Err` containing the current error value.
@@ -800,7 +821,7 @@ interface Retuple<T, E> extends ResultLike<T, E> {
800
821
  *
801
822
  * ```ts
802
823
  * const result: Result<string | null, never> = Ok("test");
803
- * const asserted = result.$andAssert();
824
+ * const asserted = result.$assert();
804
825
  *
805
826
  * asserted satisfies Result<string, RetupleCheckFailedError<string | null>>;
806
827
  * assert.equal(asserted.$unwrap(), "test");
@@ -810,7 +831,7 @@ interface Retuple<T, E> extends ResultLike<T, E> {
810
831
  *
811
832
  * ```ts
812
833
  * const result: Result<string | null | undefined, never> = Ok(null);
813
- * const asserted = result.$andAssert(
834
+ * const asserted = result.$assert(
814
835
  * (val) => val === null ? "value was null" : "value was undefined"
815
836
  * );
816
837
  *
@@ -818,17 +839,17 @@ interface Retuple<T, E> extends ResultLike<T, E> {
818
839
  * assert.equal(asserted.$unwrapErr(), "value was null");
819
840
  * ```
820
841
  */
821
- $assert(this: Result<T, E>): Result<Truthy<T>, E | RetupleCheckFailedError<T>>;
822
- $assert<F = E>(this: Result<T, E>, mapError: (val: T) => F): Result<Truthy<T>, E | F>;
842
+ $assert(this: Result<T, E>): Result<Truthy<T>, E | RetupleFilterFailed<T>>;
843
+ $assert<F = E>(this: Result<T, E>, mapError: (val: Falsey<T>) => F): Result<Truthy<T>, E | F>;
823
844
  /**
824
845
  * Performs a check when this result is `Ok`:
825
846
  *
826
- * - returning `Ok` containing the current ok value when the predicate/check
827
- * function returns true. Narrows the `T` type based on the check function;
847
+ * - returning `Ok` containing the current ok value when the filter function
848
+ * returns true. Narrows the `T` type based on the filter function;
828
849
  * - returning `Err` containing the return value of the map error function
829
- * when the ok value fails the check;
830
- * - returning `Err` containing {@link RetupleCheckFailedError} when the
831
- * ok value fails the check, and when no map error function is provided.
850
+ * when the ok value fails the filter;
851
+ * - returning `Err` containing {@link RetupleFilterFailed} when the
852
+ * ok value fails the filter, and when no map error function is provided.
832
853
  *
833
854
  * Otherwise returns `Err` containing the current error value.
834
855
  *
@@ -836,7 +857,7 @@ interface Retuple<T, E> extends ResultLike<T, E> {
836
857
  *
837
858
  * ```ts
838
859
  * const result: Result<string, never> = Ok("test");
839
- * const asserted = result.$andCheck((val) => val === "test");
860
+ * const asserted = result.$filter((val) => val === "test");
840
861
  *
841
862
  * asserted satisfies Result<string, RetupleCheckFailedError<string>>;
842
863
  * assert.equal(asserted.$unwrap(), "test");
@@ -846,7 +867,7 @@ interface Retuple<T, E> extends ResultLike<T, E> {
846
867
  *
847
868
  * ```ts
848
869
  * const result: Result<string, never> = Ok("test");
849
- * const checked = result.$andCheck(
870
+ * const checked = result.$filter(
850
871
  * (val) => val === "value",
851
872
  * (val) => `value was ${val}`,
852
873
  * );
@@ -855,10 +876,10 @@ interface Retuple<T, E> extends ResultLike<T, E> {
855
876
  * assert.equal(checked.$unwrapErr(), "value was test");
856
877
  * ```
857
878
  */
858
- $check<U extends T = T>(this: Result<T, E>, predicate: (val: T) => val is U): Result<U, E | RetupleCheckFailedError<T>>;
859
- $check(this: Result<T, E>, check: (val: T) => unknown): Result<T, E | RetupleCheckFailedError<T>>;
860
- $check<U extends T = T, F = E>(this: Result<T, E>, predicate: (val: T) => val is U, mapError: (val: T) => F): Result<U, E | F>;
861
- $check<F = E>(this: Result<T, E>, check: (val: T) => unknown, mapError: (val: T) => F): Result<T, E | F>;
879
+ $filter<U extends T = T>(this: Result<T, E>, predicate: (val: T) => val is U): Result<U, E | RetupleFilterFailed<T>>;
880
+ $filter(this: Result<T, E>, filter: (val: T) => unknown): Result<T, E | RetupleFilterFailed<T>>;
881
+ $filter<U extends T = T, F = E>(this: Result<T, E>, predicate: (val: T) => val is U, mapError: (val: T) => F): Result<U, E | F>;
882
+ $filter<F = E>(this: Result<T, E>, filter: (val: T) => unknown, mapError: (val: T) => F): Result<T, E | F>;
862
883
  /**
863
884
  * Checks the specified element of the contained array when when this result
864
885
  * is `Ok`:
@@ -866,9 +887,9 @@ interface Retuple<T, E> extends ResultLike<T, E> {
866
887
  * - returning `Ok` containing the specified element when it is truthy.
867
888
  * Narrows the type to include only truthy values;
868
889
  * - returning `Err` containing the return value of the map error function
869
- * when the first array element fails the check;
870
- * - returning `Err` containing {@link RetupleCheckFailedError} when the
871
- * first array elemnt fails the check, and when no map error function is
890
+ * when the specified array element fails the check;
891
+ * - returning `Err` containing {@link RetupleIndexFailed} when the specified
892
+ * array element fails the check, and when no map error function is
872
893
  * provided.
873
894
  *
874
895
  * Otherwise returns `Err` containing the current error value.
@@ -876,8 +897,8 @@ interface Retuple<T, E> extends ResultLike<T, E> {
876
897
  * @example
877
898
  *
878
899
  * ```ts
879
- * const result: Result<(string | null)[], never> = Ok(["test", null]);
880
- * const first = result.$andFirst();
900
+ * const result: Result<(string | null)[], never> = Ok([null, "test"]);
901
+ * const first = result.$atIndex(1);
881
902
  *
882
903
  * first satisfies Result<string, RetupleCheckFailedError<string | null>>;
883
904
  * assert.equal(first.$unwrap(), "test");
@@ -886,8 +907,9 @@ interface Retuple<T, E> extends ResultLike<T, E> {
886
907
  * @example
887
908
  *
888
909
  * ```ts
889
- * const result: Result<(string | null | undefined)[], never> = Ok([null, "test"]);
890
- * const first = result.$andFirst(
910
+ * const result: Result<(string | null | undefined)[], never> = Ok(["test", null]);
911
+ * const first = result.$atIndex(
912
+ * 1,
891
913
  * (val) => val === null ? "value was null" : "value was undefined",
892
914
  * );
893
915
  *
@@ -895,12 +917,12 @@ interface Retuple<T, E> extends ResultLike<T, E> {
895
917
  * assert.equal(first.$unwrapErr(), "value was null");
896
918
  * ```
897
919
  */
898
- $atIndex<U>(this: Result<readonly U[], E>, index: number): Result<Truthy<U>, E | RetupleCheckFailedError<T>>;
920
+ $atIndex<U>(this: Result<readonly U[], E>, index: number): Result<Truthy<U>, E | RetupleIndexFailed<T>>;
899
921
  $atIndex<U, F = E>(this: Result<readonly U[], E>, index: number, mapError: (val: T) => F): Result<Truthy<U>, E | F>;
900
922
  /**
901
923
  * Equivalent to calling `result.$atIndex(0)`.
902
924
  */
903
- $firstIndex<U>(this: Result<readonly U[], E>): Result<Truthy<U>, E | RetupleCheckFailedError<T>>;
925
+ $firstIndex<U>(this: Result<readonly U[], E>): Result<Truthy<U>, E | RetupleFilterFailed<T>>;
904
926
  $firstIndex<U, F = E>(this: Result<readonly U[], E>, mapError: (val: T) => F): Result<Truthy<U>, E | F>;
905
927
  /**
906
928
  * Returns `Ok` containing the return value of the map function when this
@@ -1518,17 +1540,18 @@ type ObjectUnionErr<E> = {
1518
1540
  error: E;
1519
1541
  };
1520
1542
  type AllOk<TResults extends ResultLikeAwaitable<any, any>[]> = {
1521
- [K in keyof TResults]: TResults[K] extends Result<infer T, any> ? T : never;
1543
+ [K in keyof TResults]: TResults[K] extends ResultLikeAwaitable<infer T, any> ? T : never;
1522
1544
  };
1523
1545
  type AllErr<TResults extends ResultLikeAwaitable<any, any>[]> = TResults[number] extends ResultLikeAwaitable<any, infer E> ? E : never;
1524
1546
  type AnyOk<TResult extends ResultLikeAwaitable<any, any>[]> = TResult[number] extends ResultLikeAwaitable<infer T, any> ? T : never;
1525
1547
  type AnyErr<TResults extends ResultLikeAwaitable<any, any>[]> = {
1526
- [K in keyof TResults]: TResults[K] extends Result<any, infer E> ? E : never;
1548
+ [K in keyof TResults]: TResults[K] extends ResultLikeAwaitable<any, infer E> ? E : never;
1527
1549
  };
1528
1550
  type CollectOk<TResults extends Record<string, ResultLikeAwaitable<any, any>>> = {
1529
- [K in keyof TResults]: TResults[K] extends Result<infer T, any> ? T : never;
1551
+ [K in keyof TResults]: TResults[K] extends ResultLikeAwaitable<infer T, any> ? T : never;
1530
1552
  } & {};
1531
- type CollectErr<TResults extends Record<string, ResultLikeAwaitable<any, any>>> = TResults[keyof TResults] extends Result<any, infer E> ? E : never;
1553
+ type CollectErr<TResults extends Record<string, ResultLikeAwaitable<any, any>>> = TResults[keyof TResults] extends ResultLikeAwaitable<any, infer E> ? E : never;
1532
1554
  type Truthy<T> = Exclude<T, false | null | undefined | 0 | 0n | "">;
1555
+ type Falsey<T> = Extract<T, false | null | undefined | 0 | 0n | "">;
1533
1556
  type NonZero<N extends number> = N & (`${N}` extends "0" ? never : N);
1534
1557
  type NonNegativeOrDecimal<N extends number> = N & (`${N}` extends `-${string}` | `${string}.${string}` ? never : N);
package/dist/index.js CHANGED
@@ -45,6 +45,43 @@ export class RetupleExpectFailed extends Error {
45
45
  this.value = value;
46
46
  }
47
47
  }
48
+ /**
49
+ * ## Retuple Assert Failed
50
+ *
51
+ * This error is used as the error type of a `Result` when using $assert,
52
+ * when no map error function is provided.
53
+ */
54
+ export class RetupleAssertFailed extends Error {
55
+ constructor(value) {
56
+ super("Assert failed");
57
+ this.value = value;
58
+ }
59
+ }
60
+ /**
61
+ * ## Retuple Filter Failed
62
+ *
63
+ * This error is used as the error type of a `Result` when using $filter,
64
+ * when no map error function is provided.
65
+ */
66
+ export class RetupleFilterFailed extends Error {
67
+ constructor(value) {
68
+ super("Filter failed");
69
+ this.value = value;
70
+ }
71
+ }
72
+ /**
73
+ * ## Retuple Index Failed
74
+ *
75
+ * This error is used as the error type of a `Result` when using $atIndex or
76
+ * $firstIndex, when no map error function is provided.
77
+ */
78
+ export class RetupleIndexFailed extends Error {
79
+ constructor(index, value) {
80
+ super("Index failed");
81
+ this.index = index;
82
+ this.value = value;
83
+ }
84
+ }
48
85
  /**
49
86
  * ## Retuple Thrown Value Error
50
87
  *
@@ -73,18 +110,6 @@ export class RetupleInvalidUnionError extends Error {
73
110
  this.value = value;
74
111
  }
75
112
  }
76
- /**
77
- * ## Retuple Check Failed Error
78
- *
79
- * This error is used as the error type of a `Result` when using $andAssert
80
- * or $andCheck, when no custom error handler is provided.
81
- */
82
- export class RetupleCheckFailedError extends Error {
83
- constructor(value) {
84
- super("Check failed");
85
- this.value = value;
86
- }
87
- }
88
113
  /**
89
114
  * ## Result
90
115
  *
@@ -567,19 +592,33 @@ class ResultOk extends Array {
567
592
  $mapOrElse(_def, f) {
568
593
  return Ok(f(this[1]));
569
594
  }
570
- $assert(mapError = mapCheckError) {
571
- return this[1] ? this : Err(mapError(this[1]));
595
+ $assert(mapError) {
596
+ return this[1]
597
+ ? this
598
+ : Err(mapError
599
+ ? mapError(this[1])
600
+ : new RetupleAssertFailed(this[1]));
572
601
  }
573
- $check(check, mapError = mapCheckError) {
574
- return check(this[1]) ? this : Err(mapError(this[1]));
602
+ $filter(filter, mapError) {
603
+ return filter(this[1])
604
+ ? this
605
+ : Err(mapError ? mapError(this[1]) : new RetupleFilterFailed(this[1]));
575
606
  }
576
- $atIndex(index, mapError = mapCheckError) {
607
+ $atIndex(index, mapError) {
577
608
  const element = this[1][index];
578
- return element ? Ok(element) : Err(mapError(this[1]));
609
+ return element
610
+ ? Ok(element)
611
+ : Err(mapError
612
+ ? mapError(this[1])
613
+ : new RetupleIndexFailed(index, this[1]));
579
614
  }
580
- $firstIndex(mapError = mapCheckError) {
615
+ $firstIndex(mapError) {
581
616
  const first = this[1][0];
582
- return first ? Ok(first) : Err(mapError(this[1]));
617
+ return first
618
+ ? Ok(first)
619
+ : Err(mapError
620
+ ? mapError(this[1])
621
+ : new RetupleIndexFailed(0, this[1]));
583
622
  }
584
623
  $or() {
585
624
  return this;
@@ -727,7 +766,7 @@ class ResultErr extends Array {
727
766
  $assert() {
728
767
  return this;
729
768
  }
730
- $check() {
769
+ $filter() {
731
770
  return this;
732
771
  }
733
772
  $atIndex() {
@@ -910,40 +949,54 @@ class ResultAsync {
910
949
  : new ResultOk(def(res[0]));
911
950
  }));
912
951
  }
913
- $assert(mapError = mapCheckError) {
952
+ $assert(mapError) {
914
953
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
915
954
  if (res instanceof ResultErr) {
916
955
  return res;
917
956
  }
918
957
  return res[1]
919
958
  ? res
920
- : Err(mapError(res[1]));
959
+ : Err(mapError
960
+ ? mapError(res[1])
961
+ : new RetupleAssertFailed(res[1]));
921
962
  }));
922
963
  }
923
- $check(check, mapError = mapCheckError) {
964
+ $filter(check, mapError) {
924
965
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
925
966
  if (res instanceof ResultErr) {
926
967
  return res;
927
968
  }
928
- return check(res[1]) ? res : Err(mapError(res[1]));
969
+ return check(res[1])
970
+ ? res
971
+ : Err(mapError
972
+ ? mapError(res[1])
973
+ : new RetupleFilterFailed(res[1]));
929
974
  }));
930
975
  }
931
- $atIndex(index, mapError = mapCheckError) {
976
+ $atIndex(index, mapError) {
932
977
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then((res) => {
933
978
  if (res instanceof ResultErr) {
934
979
  return res;
935
980
  }
936
981
  const element = res[1][index];
937
- return element ? Ok(element) : Err(mapError(res[1]));
982
+ return element
983
+ ? Ok(element)
984
+ : Err(mapError
985
+ ? mapError(res[1])
986
+ : new RetupleIndexFailed(index, res[1]));
938
987
  }));
939
988
  }
940
- $firstIndex(mapError = mapCheckError) {
989
+ $firstIndex(mapError) {
941
990
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then((res) => {
942
991
  if (res instanceof ResultErr) {
943
992
  return res;
944
993
  }
945
994
  const first = res[1][0];
946
- return first ? Ok(first) : Err(mapError(res[1]));
995
+ return first
996
+ ? Ok(first)
997
+ : Err(mapError
998
+ ? mapError(res[1])
999
+ : new RetupleIndexFailed(0, res[1]));
947
1000
  }));
948
1001
  }
949
1002
  $or(or) {
@@ -1260,6 +1313,3 @@ function ensureError(err) {
1260
1313
  function mapTrue() {
1261
1314
  return true;
1262
1315
  }
1263
- function mapCheckError(value) {
1264
- return new RetupleCheckFailedError(value);
1265
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "retuple",
3
- "version": "1.0.0-next.25",
3
+ "version": "1.0.0-next.27",
4
4
  "scripts": {
5
5
  "test": "vitest",
6
6
  "lint": "eslint . --ext .ts -c eslint.config.mjs --fix",