retuple 1.0.0-next.30 → 1.0.0-next.31

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
@@ -601,6 +601,9 @@ class ResultOk extends Array {
601
601
  $mapErr() {
602
602
  return this;
603
603
  }
604
+ $mapErrMatching() {
605
+ return this;
606
+ }
604
607
  $mapOr(_def, f) {
605
608
  return Ok(f(this[1]));
606
609
  }
@@ -787,6 +790,15 @@ class ResultErr extends Array {
787
790
  $mapErr(f) {
788
791
  return Err(f(this[0]));
789
792
  }
793
+ $mapErrMatching({ classes, with: f }) {
794
+ const err = this[0];
795
+ for (const ctor of classes) {
796
+ if (err instanceof ctor) {
797
+ return Err(f(err));
798
+ }
799
+ }
800
+ return this;
801
+ }
790
802
  $mapOr(def) {
791
803
  return Ok(def);
792
804
  }
@@ -1000,6 +1012,12 @@ class ResultAsync {
1000
1012
  : res;
1001
1013
  }));
1002
1014
  }
1015
+ /**
1016
+ * @TODO
1017
+ */
1018
+ $mapErrMatching(params) {
1019
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => res.$mapErrMatching(params)));
1020
+ }
1003
1021
  /**
1004
1022
  * The same as {@link Retuple.$mapOr|$mapOr}, except it returns
1005
1023
  * {@link ResultAsync}.
package/dist/index.d.cts CHANGED
@@ -265,6 +265,10 @@ declare class ResultAsync<T, E> {
265
265
  * {@link ResultAsync}.
266
266
  */
267
267
  $mapErr<F = E>(this: ResultAsync<T, E>, f: (err: E) => F): ResultAsync<T, F>;
268
+ /**
269
+ * @TODO
270
+ */
271
+ $mapErrMatching<F, C extends new (...args: any[]) => Error>(this: ResultAsync<T, E>, params: MapErrMatchingParams<F, C>): ResultAsync<T, E | F>;
268
272
  /**
269
273
  * The same as {@link Retuple.$mapOr|$mapOr}, except it returns
270
274
  * {@link ResultAsync}.
@@ -1018,6 +1022,13 @@ interface Retuple<T, E> extends ResultLike<T, E> {
1018
1022
  * ```
1019
1023
  */
1020
1024
  $mapErr<F = E>(this: Result<T, E>, f: (err: E) => F): Result<T, F>;
1025
+ /**
1026
+ * @TODO
1027
+ */
1028
+ $mapErrMatching<F, C extends new (...args: any[]) => Error>(this: Result<T, E>, mapper: {
1029
+ classes: [C, ...C[]];
1030
+ with: (err: InstanceType<C>) => F;
1031
+ }): Result<T, E | F>;
1021
1032
  /**
1022
1033
  * Returns `Ok` containing the return value of the map function when this
1023
1034
  * result is `Ok`.
@@ -1627,6 +1638,10 @@ type CollectOk<TResults extends Record<string, ResultLikeAwaitable<any, any>>> =
1627
1638
  [K in keyof TResults]: TResults[K] extends ResultLikeAwaitable<infer T, any> ? T : never;
1628
1639
  } & {};
1629
1640
  type CollectErr<TResults extends Record<string, ResultLikeAwaitable<any, any>>> = TResults[keyof TResults] extends ResultLikeAwaitable<any, infer E> ? E : never;
1641
+ interface MapErrMatchingParams<F, C extends new (...args: any[]) => Error> {
1642
+ classes: [C, ...C[]];
1643
+ with: (err: InstanceType<C>) => F;
1644
+ }
1630
1645
  type Truthy<T> = Exclude<T, false | null | undefined | 0 | 0n | "">;
1631
1646
  type Falsey<T> = Extract<T, false | null | undefined | 0 | 0n | "">;
1632
1647
  type NonZero<N extends number> = N & (`${N}` extends "0" ? never : N);
package/dist/index.d.ts CHANGED
@@ -265,6 +265,10 @@ declare class ResultAsync<T, E> {
265
265
  * {@link ResultAsync}.
266
266
  */
267
267
  $mapErr<F = E>(this: ResultAsync<T, E>, f: (err: E) => F): ResultAsync<T, F>;
268
+ /**
269
+ * @TODO
270
+ */
271
+ $mapErrMatching<F, C extends new (...args: any[]) => Error>(this: ResultAsync<T, E>, params: MapErrMatchingParams<F, C>): ResultAsync<T, E | F>;
268
272
  /**
269
273
  * The same as {@link Retuple.$mapOr|$mapOr}, except it returns
270
274
  * {@link ResultAsync}.
@@ -1018,6 +1022,13 @@ interface Retuple<T, E> extends ResultLike<T, E> {
1018
1022
  * ```
1019
1023
  */
1020
1024
  $mapErr<F = E>(this: Result<T, E>, f: (err: E) => F): Result<T, F>;
1025
+ /**
1026
+ * @TODO
1027
+ */
1028
+ $mapErrMatching<F, C extends new (...args: any[]) => Error>(this: Result<T, E>, mapper: {
1029
+ classes: [C, ...C[]];
1030
+ with: (err: InstanceType<C>) => F;
1031
+ }): Result<T, E | F>;
1021
1032
  /**
1022
1033
  * Returns `Ok` containing the return value of the map function when this
1023
1034
  * result is `Ok`.
@@ -1627,6 +1638,10 @@ type CollectOk<TResults extends Record<string, ResultLikeAwaitable<any, any>>> =
1627
1638
  [K in keyof TResults]: TResults[K] extends ResultLikeAwaitable<infer T, any> ? T : never;
1628
1639
  } & {};
1629
1640
  type CollectErr<TResults extends Record<string, ResultLikeAwaitable<any, any>>> = TResults[keyof TResults] extends ResultLikeAwaitable<any, infer E> ? E : never;
1641
+ interface MapErrMatchingParams<F, C extends new (...args: any[]) => Error> {
1642
+ classes: [C, ...C[]];
1643
+ with: (err: InstanceType<C>) => F;
1644
+ }
1630
1645
  type Truthy<T> = Exclude<T, false | null | undefined | 0 | 0n | "">;
1631
1646
  type Falsey<T> = Extract<T, false | null | undefined | 0 | 0n | "">;
1632
1647
  type NonZero<N extends number> = N & (`${N}` extends "0" ? never : N);
package/dist/index.js CHANGED
@@ -587,6 +587,9 @@ class ResultOk extends Array {
587
587
  $mapErr() {
588
588
  return this;
589
589
  }
590
+ $mapErrMatching() {
591
+ return this;
592
+ }
590
593
  $mapOr(_def, f) {
591
594
  return Ok(f(this[1]));
592
595
  }
@@ -773,6 +776,15 @@ class ResultErr extends Array {
773
776
  $mapErr(f) {
774
777
  return Err(f(this[0]));
775
778
  }
779
+ $mapErrMatching({ classes, with: f }) {
780
+ const err = this[0];
781
+ for (const ctor of classes) {
782
+ if (err instanceof ctor) {
783
+ return Err(f(err));
784
+ }
785
+ }
786
+ return this;
787
+ }
776
788
  $mapOr(def) {
777
789
  return Ok(def);
778
790
  }
@@ -986,6 +998,12 @@ class ResultAsync {
986
998
  : res;
987
999
  }));
988
1000
  }
1001
+ /**
1002
+ * @TODO
1003
+ */
1004
+ $mapErrMatching(params) {
1005
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => res.$mapErrMatching(params)));
1006
+ }
989
1007
  /**
990
1008
  * The same as {@link Retuple.$mapOr|$mapOr}, except it returns
991
1009
  * {@link ResultAsync}.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "retuple",
3
- "version": "1.0.0-next.30",
3
+ "version": "1.0.0-next.31",
4
4
  "scripts": {
5
5
  "test": "vitest",
6
6
  "lint": "eslint . --ext .ts -c eslint.config.mjs --fix",