retuple 1.0.0-next.27 → 1.0.0-next.29

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
@@ -17,6 +17,7 @@ exports.Result = Result;
17
17
  exports.Ok = Ok;
18
18
  exports.Err = Err;
19
19
  const retuple_symbols_1 = require("retuple-symbols");
20
+ const RetupleStackSymbol = Symbol("RetupleStackSymbol");
20
21
  /**
21
22
  * ## Retuple Unwrap Failed
22
23
  *
@@ -664,9 +665,24 @@ class ResultOk extends Array {
664
665
  $andThen(f) {
665
666
  return asResult(f(this[1]));
666
667
  }
668
+ $andStack(f) {
669
+ const res = asResult(f(this[1]));
670
+ if (res instanceof ResultErr) {
671
+ return res;
672
+ }
673
+ if (this[1] instanceof RetupleStack) {
674
+ return Ok(new RetupleStack(...this[1], res[1]));
675
+ }
676
+ else {
677
+ return Ok(new RetupleStack(this[1], res[1]));
678
+ }
679
+ }
667
680
  $andThenAsync(f) {
668
681
  return this.$async().$andThen(f);
669
682
  }
683
+ $andStackAsync(f) {
684
+ return this.$async().$andStack(f);
685
+ }
670
686
  $andThrough(f) {
671
687
  const res = asResult(f(this[1]));
672
688
  return res instanceof ResultErr ? res : this;
@@ -824,9 +840,15 @@ class ResultErr extends Array {
824
840
  $andThen() {
825
841
  return this;
826
842
  }
843
+ $andStack() {
844
+ return this;
845
+ }
827
846
  $andThenAsync() {
828
847
  return this.$async();
829
848
  }
849
+ $andStackAsync() {
850
+ return this.$async();
851
+ }
830
852
  $andThrough() {
831
853
  return this;
832
854
  }
@@ -1067,6 +1089,21 @@ class ResultAsync {
1067
1089
  : res;
1068
1090
  }));
1069
1091
  }
1092
+ $andStack(f) {
1093
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
1094
+ if (res instanceof ResultErr) {
1095
+ return res;
1096
+ }
1097
+ const resThen = asResult(await f(res[1]));
1098
+ if (resThen instanceof ResultErr) {
1099
+ return resThen;
1100
+ }
1101
+ if (res[1] instanceof RetupleStack) {
1102
+ return Ok(new RetupleStack(...res[1], resThen[1]));
1103
+ }
1104
+ return Ok(new RetupleStack(res[1], resThen[1]));
1105
+ }));
1106
+ }
1070
1107
  $andThrough(f) {
1071
1108
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
1072
1109
  if (res instanceof ResultOk) {
@@ -1311,6 +1348,8 @@ class ResultRetry extends ResultAsync {
1311
1348
  _a = ResultRetry, _ResultRetry_f = new WeakMap(), _ResultRetry_promise = new WeakMap(), _ResultRetry_times = new WeakMap(), _ResultRetry_attempt = new WeakMap(), _ResultRetry_aborted = new WeakMap(), _ResultRetry_abort = new WeakMap(), _ResultRetry_getDelay = new WeakMap(), _ResultRetry_handler = new WeakMap();
1312
1349
  ResultRetry.MAX_TIMEOUT = 3600000;
1313
1350
  ResultRetry.MAX_RETRY = 100;
1351
+ class RetupleStack extends Array {
1352
+ }
1314
1353
  function asResult(resultLike) {
1315
1354
  if (resultLike instanceof ResultOk || resultLike instanceof ResultErr) {
1316
1355
  return resultLike;
package/dist/index.d.cts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { type ResultLike } from "retuple-symbols";
2
+ declare const RetupleStackSymbol: unique symbol;
2
3
  export type Ok = typeof Ok;
3
4
  export type Err = typeof Err;
4
5
  export type Result<T, E> = (OkTuple<T> | ErrTuple<E>) & Retuple<T, E>;
@@ -323,6 +324,14 @@ declare class ResultAsync<T, E> {
323
324
  * - returns {@link ResultAsync}.
324
325
  */
325
326
  $andThen<U = T, F = E>(this: ResultAsync<T, E>, f: (val: T) => ResultLikeAwaitable<U, F>): ResultAsync<U, E | F>;
327
+ /**
328
+ * The same as {@link Retuple.$andStack|$andStack}, except it:
329
+ *
330
+ * - can also accept an `async` and function;
331
+ * - returns {@link ResultAsync}.
332
+ */
333
+ $andStack<U = T, F = E, S extends [...any[], any] = [...any[], any]>(this: ResultAsync<Stack<S>, E>, f: (val: T) => ResultLikeAwaitable<U, F>): ResultAsync<Stack<[...S, U]>, E | F>;
334
+ $andStack<U = T, F = E>(this: ResultAsync<T, E>, f: (val: T) => ResultLikeAwaitable<U, F>): ResultAsync<Stack<[T, U]>, E | F>;
326
335
  /**
327
336
  * The same as {@link Retuple.$andThrough|$andThrough}, except it:
328
337
  *
@@ -504,6 +513,9 @@ declare class ResultRetry<T, E> extends ResultAsync<T, E> implements PromiseLike
504
513
  $handle(f: (controller: ResultRetryController<E>) => void): ResultRetry<T, E>;
505
514
  private drain;
506
515
  }
516
+ type Stack<T extends [...any[], any] = [...any[], any]> = T & {
517
+ [RetupleStackSymbol]: T;
518
+ };
507
519
  interface Retuple<T, E> extends ResultLike<T, E> {
508
520
  /**
509
521
  * Returns true when this result is `Ok`. Acts as a type guard.
@@ -1216,10 +1228,46 @@ interface Retuple<T, E> extends ResultLike<T, E> {
1216
1228
  * ```
1217
1229
  */
1218
1230
  $andThen<U = T, F = E>(this: Result<T, E>, f: (val: T) => ResultLike<U, F>): Result<U, E | F>;
1231
+ /**
1232
+ * Stacks the result of the and function on to the current contained
1233
+ * value, when this result and the result of the function is `Ok`.
1234
+ *
1235
+ * Otherwise, returns the current `Err` or the `Err` from the and
1236
+ * function.
1237
+ *
1238
+ * A stack is a special tuple which makes it possilble to pass multiple
1239
+ * values down a chain with minimal effort.
1240
+ *
1241
+ * @example
1242
+ *
1243
+ * ```ts
1244
+ * const identity = <T>(val: T) => Ok(val);
1245
+ *
1246
+ * const stack = Ok("test")
1247
+ * .$andStack((val) => identity(`${val}-2`))
1248
+ * .$andStack(([val, val2]) => identity(val.length + val2.length))
1249
+ * .$map(([val, val2, len]) => `'${val}${val2}' has length ${len}`);
1250
+ *
1251
+ * const nostack = Ok("test")
1252
+ * .$andThen((val) => identity(`${val}-2`).$map((val2) => [val, val2]))
1253
+ * .$andThen(([val, val2]) => {
1254
+ * return identity(val.length + val2.length)
1255
+ * .$map((len) => [val, val2, len]);
1256
+ * })
1257
+ * .$map(([val, val2, len]) => `'${val}${val2}' has length ${len}`);
1258
+ * ```
1259
+ */
1260
+ $andStack<U = T, F = E, S extends [...any[], any] = [...any[], any]>(this: Result<Stack<S>, E>, f: (val: T) => ResultLike<U, F>): Result<Stack<[...S, U]>, E | F>;
1261
+ $andStack<U = T, F = E>(this: Result<T, E>, f: (val: T) => ResultLike<U, F>): Result<Stack<[T, U]>, E | F>;
1219
1262
  /**
1220
1263
  * Shorthand for `result.$async().$andThen(...)`
1221
1264
  */
1222
1265
  $andThenAsync<U = T, F = E>(this: Result<T, E>, f: (val: T) => ResultLikeAwaitable<U, F>): ResultAsync<U, E | F>;
1266
+ /**
1267
+ * Shorthand for `result.$async().$andStack(...)`
1268
+ */
1269
+ $andStackAsync<U = T, F = E, S extends [...any[], any] = [...any[], any]>(this: Result<Stack<S>, E>, f: (val: T) => ResultLikeAwaitable<U, F>): ResultAsync<Stack<[...S, U]>, E | F>;
1270
+ $andStackAsync<U = T, F = E>(this: Result<T, E>, f: (val: T) => ResultLikeAwaitable<U, F>): ResultAsync<Stack<[T, U]>, E | F>;
1223
1271
  /**
1224
1272
  * Calls the through function when this result is `Ok` and returns:
1225
1273
  *
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { type ResultLike } from "retuple-symbols";
2
+ declare const RetupleStackSymbol: unique symbol;
2
3
  export type Ok = typeof Ok;
3
4
  export type Err = typeof Err;
4
5
  export type Result<T, E> = (OkTuple<T> | ErrTuple<E>) & Retuple<T, E>;
@@ -323,6 +324,14 @@ declare class ResultAsync<T, E> {
323
324
  * - returns {@link ResultAsync}.
324
325
  */
325
326
  $andThen<U = T, F = E>(this: ResultAsync<T, E>, f: (val: T) => ResultLikeAwaitable<U, F>): ResultAsync<U, E | F>;
327
+ /**
328
+ * The same as {@link Retuple.$andStack|$andStack}, except it:
329
+ *
330
+ * - can also accept an `async` and function;
331
+ * - returns {@link ResultAsync}.
332
+ */
333
+ $andStack<U = T, F = E, S extends [...any[], any] = [...any[], any]>(this: ResultAsync<Stack<S>, E>, f: (val: T) => ResultLikeAwaitable<U, F>): ResultAsync<Stack<[...S, U]>, E | F>;
334
+ $andStack<U = T, F = E>(this: ResultAsync<T, E>, f: (val: T) => ResultLikeAwaitable<U, F>): ResultAsync<Stack<[T, U]>, E | F>;
326
335
  /**
327
336
  * The same as {@link Retuple.$andThrough|$andThrough}, except it:
328
337
  *
@@ -504,6 +513,9 @@ declare class ResultRetry<T, E> extends ResultAsync<T, E> implements PromiseLike
504
513
  $handle(f: (controller: ResultRetryController<E>) => void): ResultRetry<T, E>;
505
514
  private drain;
506
515
  }
516
+ type Stack<T extends [...any[], any] = [...any[], any]> = T & {
517
+ [RetupleStackSymbol]: T;
518
+ };
507
519
  interface Retuple<T, E> extends ResultLike<T, E> {
508
520
  /**
509
521
  * Returns true when this result is `Ok`. Acts as a type guard.
@@ -1216,10 +1228,46 @@ interface Retuple<T, E> extends ResultLike<T, E> {
1216
1228
  * ```
1217
1229
  */
1218
1230
  $andThen<U = T, F = E>(this: Result<T, E>, f: (val: T) => ResultLike<U, F>): Result<U, E | F>;
1231
+ /**
1232
+ * Stacks the result of the and function on to the current contained
1233
+ * value, when this result and the result of the function is `Ok`.
1234
+ *
1235
+ * Otherwise, returns the current `Err` or the `Err` from the and
1236
+ * function.
1237
+ *
1238
+ * A stack is a special tuple which makes it possilble to pass multiple
1239
+ * values down a chain with minimal effort.
1240
+ *
1241
+ * @example
1242
+ *
1243
+ * ```ts
1244
+ * const identity = <T>(val: T) => Ok(val);
1245
+ *
1246
+ * const stack = Ok("test")
1247
+ * .$andStack((val) => identity(`${val}-2`))
1248
+ * .$andStack(([val, val2]) => identity(val.length + val2.length))
1249
+ * .$map(([val, val2, len]) => `'${val}${val2}' has length ${len}`);
1250
+ *
1251
+ * const nostack = Ok("test")
1252
+ * .$andThen((val) => identity(`${val}-2`).$map((val2) => [val, val2]))
1253
+ * .$andThen(([val, val2]) => {
1254
+ * return identity(val.length + val2.length)
1255
+ * .$map((len) => [val, val2, len]);
1256
+ * })
1257
+ * .$map(([val, val2, len]) => `'${val}${val2}' has length ${len}`);
1258
+ * ```
1259
+ */
1260
+ $andStack<U = T, F = E, S extends [...any[], any] = [...any[], any]>(this: Result<Stack<S>, E>, f: (val: T) => ResultLike<U, F>): Result<Stack<[...S, U]>, E | F>;
1261
+ $andStack<U = T, F = E>(this: Result<T, E>, f: (val: T) => ResultLike<U, F>): Result<Stack<[T, U]>, E | F>;
1219
1262
  /**
1220
1263
  * Shorthand for `result.$async().$andThen(...)`
1221
1264
  */
1222
1265
  $andThenAsync<U = T, F = E>(this: Result<T, E>, f: (val: T) => ResultLikeAwaitable<U, F>): ResultAsync<U, E | F>;
1266
+ /**
1267
+ * Shorthand for `result.$async().$andStack(...)`
1268
+ */
1269
+ $andStackAsync<U = T, F = E, S extends [...any[], any] = [...any[], any]>(this: Result<Stack<S>, E>, f: (val: T) => ResultLikeAwaitable<U, F>): ResultAsync<Stack<[...S, U]>, E | F>;
1270
+ $andStackAsync<U = T, F = E>(this: Result<T, E>, f: (val: T) => ResultLikeAwaitable<U, F>): ResultAsync<Stack<[T, U]>, E | F>;
1223
1271
  /**
1224
1272
  * Calls the through function when this result is `Ok` and returns:
1225
1273
  *
package/dist/index.js CHANGED
@@ -11,6 +11,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
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
13
  import { ResultLikeSymbol, } from "retuple-symbols";
14
+ const RetupleStackSymbol = Symbol("RetupleStackSymbol");
14
15
  /**
15
16
  * ## Retuple Unwrap Failed
16
17
  *
@@ -650,9 +651,24 @@ class ResultOk extends Array {
650
651
  $andThen(f) {
651
652
  return asResult(f(this[1]));
652
653
  }
654
+ $andStack(f) {
655
+ const res = asResult(f(this[1]));
656
+ if (res instanceof ResultErr) {
657
+ return res;
658
+ }
659
+ if (this[1] instanceof RetupleStack) {
660
+ return Ok(new RetupleStack(...this[1], res[1]));
661
+ }
662
+ else {
663
+ return Ok(new RetupleStack(this[1], res[1]));
664
+ }
665
+ }
653
666
  $andThenAsync(f) {
654
667
  return this.$async().$andThen(f);
655
668
  }
669
+ $andStackAsync(f) {
670
+ return this.$async().$andStack(f);
671
+ }
656
672
  $andThrough(f) {
657
673
  const res = asResult(f(this[1]));
658
674
  return res instanceof ResultErr ? res : this;
@@ -810,9 +826,15 @@ class ResultErr extends Array {
810
826
  $andThen() {
811
827
  return this;
812
828
  }
829
+ $andStack() {
830
+ return this;
831
+ }
813
832
  $andThenAsync() {
814
833
  return this.$async();
815
834
  }
835
+ $andStackAsync() {
836
+ return this.$async();
837
+ }
816
838
  $andThrough() {
817
839
  return this;
818
840
  }
@@ -1053,6 +1075,21 @@ class ResultAsync {
1053
1075
  : res;
1054
1076
  }));
1055
1077
  }
1078
+ $andStack(f) {
1079
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
1080
+ if (res instanceof ResultErr) {
1081
+ return res;
1082
+ }
1083
+ const resThen = asResult(await f(res[1]));
1084
+ if (resThen instanceof ResultErr) {
1085
+ return resThen;
1086
+ }
1087
+ if (res[1] instanceof RetupleStack) {
1088
+ return Ok(new RetupleStack(...res[1], resThen[1]));
1089
+ }
1090
+ return Ok(new RetupleStack(res[1], resThen[1]));
1091
+ }));
1092
+ }
1056
1093
  $andThrough(f) {
1057
1094
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
1058
1095
  if (res instanceof ResultOk) {
@@ -1297,6 +1334,8 @@ class ResultRetry extends ResultAsync {
1297
1334
  _a = ResultRetry, _ResultRetry_f = new WeakMap(), _ResultRetry_promise = new WeakMap(), _ResultRetry_times = new WeakMap(), _ResultRetry_attempt = new WeakMap(), _ResultRetry_aborted = new WeakMap(), _ResultRetry_abort = new WeakMap(), _ResultRetry_getDelay = new WeakMap(), _ResultRetry_handler = new WeakMap();
1298
1335
  ResultRetry.MAX_TIMEOUT = 3600000;
1299
1336
  ResultRetry.MAX_RETRY = 100;
1337
+ class RetupleStack extends Array {
1338
+ }
1300
1339
  function asResult(resultLike) {
1301
1340
  if (resultLike instanceof ResultOk || resultLike instanceof ResultErr) {
1302
1341
  return resultLike;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "retuple",
3
- "version": "1.0.0-next.27",
3
+ "version": "1.0.0-next.29",
4
4
  "scripts": {
5
5
  "test": "vitest",
6
6
  "lint": "eslint . --ext .ts -c eslint.config.mjs --fix",