@oscarpalmer/atoms 0.208.0 → 0.209.0

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.d.mts CHANGED
@@ -4592,6 +4592,12 @@ export declare function setValue<Data extends PlainObject>(data: Data, path: str
4592
4592
  * An asynchronous plan of execution that can yield intermediate results and eventually return a result
4593
4593
  */
4594
4594
  type AsyncPlan<Value, Error = unknown> = {
4595
+ /**
4596
+ * Get the asynchronous generator for the plan
4597
+ *
4598
+ * @returns Asynchronous generator
4599
+ */
4600
+ [Symbol.asyncIterator]: () => AsyncGenerator<unknown, Value, unknown>;
4595
4601
  /**
4596
4602
  * Run the plan to completion
4597
4603
  *
@@ -4603,6 +4609,12 @@ type AsyncPlan<Value, Error = unknown> = {
4603
4609
  * A plan of execution that can yield intermediate results and eventually return a result
4604
4610
  */
4605
4611
  type Plan<Value, Error = unknown> = {
4612
+ /**
4613
+ * Get the generator for the plan
4614
+ *
4615
+ * @returns Generator
4616
+ */
4617
+ [Symbol.iterator]: () => Generator<unknown, Value, unknown>;
4606
4618
  /**
4607
4619
  * Run the plan to completion
4608
4620
  *
package/dist/index.mjs CHANGED
@@ -2746,6 +2746,10 @@ function setValue(data, path, value, ignoreCase) {
2746
2746
  return data;
2747
2747
  }
2748
2748
  const SET_EXPRESSION_INDEX = /^\d+$/;
2749
+ //#endregion
2750
+ //#region src/internal/models/plan.model.ts
2751
+ const GENERATOR_NAME_ASYNC = "AsyncGeneratorFunction";
2752
+ const GENERATOR_NAME_SYNC = "GeneratorFunction";
2749
2753
  const PLAN_MESSAGE_PLAN_INPUT = "plan requires a generator function";
2750
2754
  const PLAN_MESSAGE_RUN_INPUT = "run requires a plan or a generator function";
2751
2755
  const PLAN_SYMBOL = Symbol("plan");
@@ -2759,6 +2763,9 @@ function AsyncPlan(generator) {
2759
2763
  type: PLAN_TYPE_PLAN_ASYNC
2760
2764
  };
2761
2765
  }
2766
+ AsyncPlan.prototype[Symbol.asyncIterator] = function() {
2767
+ return this[PLAN_SYMBOL].generator();
2768
+ };
2762
2769
  AsyncPlan.prototype.run = runAsyncPlan;
2763
2770
  function Plan(generator) {
2764
2771
  this[PLAN_SYMBOL] = {
@@ -2766,6 +2773,9 @@ function Plan(generator) {
2766
2773
  type: PLAN_TYPE_PLAN_SYNC
2767
2774
  };
2768
2775
  }
2776
+ Plan.prototype[Symbol.iterator] = function() {
2777
+ return this[PLAN_SYMBOL].generator();
2778
+ };
2769
2779
  Plan.prototype.run = runPlan;
2770
2780
  function asyncExecute(callback) {
2771
2781
  return asyncAttempt(async () => {
@@ -2776,6 +2786,8 @@ function asyncExecute(callback) {
2776
2786
  const { done } = next;
2777
2787
  let value = next.value;
2778
2788
  if (value instanceof Error) throw value;
2789
+ if (isError(value)) throw value.error;
2790
+ if (isOk(value)) value = value.value;
2779
2791
  lastValue = value;
2780
2792
  if (done === true) break;
2781
2793
  }
@@ -2806,6 +2818,8 @@ function execute(callback) {
2806
2818
  const { done } = next;
2807
2819
  let { value } = next;
2808
2820
  if (value instanceof Error) throw value;
2821
+ if (isError(value)) throw value.error;
2822
+ if (isOk(value)) value = value.value;
2809
2823
  lastValue = value;
2810
2824
  if (done === true) break;
2811
2825
  }
@@ -2828,7 +2842,7 @@ function isAsyncPlan(value) {
2828
2842
  * @returns `true` if the value is an asynchronous generator function, otherwise `false`
2829
2843
  */
2830
2844
  function isAsyncGenerator(value) {
2831
- return typeof value === "function" && value.constructor.name === "AsyncGeneratorFunction" && typeof value.prototype.next === "function" && typeof value.prototype.return === "function" && typeof value.prototype.throw === "function";
2845
+ return isGeneratorInstance(GENERATOR_NAME_ASYNC, value);
2832
2846
  }
2833
2847
  /**
2834
2848
  * Is the value a generator function?
@@ -2837,7 +2851,10 @@ function isAsyncGenerator(value) {
2837
2851
  * @returns `true` if the value is a generator function, otherwise `false`
2838
2852
  */
2839
2853
  function isGenerator(value) {
2840
- return typeof value === "function" && value.constructor.name === "GeneratorFunction" && typeof value.prototype.next === "function" && typeof value.prototype.return === "function" && typeof value.prototype.throw === "function";
2854
+ return isGeneratorInstance(GENERATOR_NAME_SYNC, value);
2855
+ }
2856
+ function isGeneratorInstance(name, value) {
2857
+ return typeof value === "function" && value !== null && value.constructor.name === name && typeof value.prototype.next === "function" && typeof value.prototype.return === "function" && typeof value.prototype.throw === "function";
2841
2858
  }
2842
2859
  /**
2843
2860
  * Is the value a plan?
@@ -4,6 +4,12 @@ import { Result } from "./result.model.mjs";
4
4
  * An asynchronous plan of execution that can yield intermediate results and eventually return a result
5
5
  */
6
6
  export type AsyncPlan<Value, Error = unknown> = {
7
+ /**
8
+ * Get the asynchronous generator for the plan
9
+ *
10
+ * @returns Asynchronous generator
11
+ */
12
+ [Symbol.asyncIterator]: () => AsyncGenerator<unknown, Value, unknown>;
7
13
  /**
8
14
  * Run the plan to completion
9
15
  *
@@ -27,6 +33,12 @@ export type InternalPlan = {
27
33
  * A plan of execution that can yield intermediate results and eventually return a result
28
34
  */
29
35
  export type Plan<Value, Error = unknown> = {
36
+ /**
37
+ * Get the generator for the plan
38
+ *
39
+ * @returns Generator
40
+ */
41
+ [Symbol.iterator]: () => Generator<unknown, Value, unknown>;
30
42
  /**
31
43
  * Run the plan to completion
32
44
  *
@@ -1,5 +1,7 @@
1
+ import { isError, isOk } from "../internal/result/misc.mjs";
2
+ import "../result/misc.mjs";
1
3
  import { asyncAttempt, attempt } from "../internal/result/attempt.mjs";
2
- import { PLAN_MESSAGE_PLAN_INPUT, PLAN_MESSAGE_RUN_INPUT, PLAN_SYMBOL, PLAN_TYPE_PLAN_ASYNC, PLAN_TYPE_PLAN_SYNC } from "../internal/models/plan.model.mjs";
4
+ import { GENERATOR_NAME_ASYNC, GENERATOR_NAME_SYNC, PLAN_MESSAGE_PLAN_INPUT, PLAN_MESSAGE_RUN_INPUT, PLAN_SYMBOL, PLAN_TYPE_PLAN_ASYNC, PLAN_TYPE_PLAN_SYNC } from "../internal/models/plan.model.mjs";
3
5
  //#region src/plan/index.ts
4
6
  function AsyncPlan(generator) {
5
7
  this[PLAN_SYMBOL] = {
@@ -7,6 +9,9 @@ function AsyncPlan(generator) {
7
9
  type: PLAN_TYPE_PLAN_ASYNC
8
10
  };
9
11
  }
12
+ AsyncPlan.prototype[Symbol.asyncIterator] = function() {
13
+ return this[PLAN_SYMBOL].generator();
14
+ };
10
15
  AsyncPlan.prototype.run = runAsyncPlan;
11
16
  function Plan(generator) {
12
17
  this[PLAN_SYMBOL] = {
@@ -14,6 +19,9 @@ function Plan(generator) {
14
19
  type: PLAN_TYPE_PLAN_SYNC
15
20
  };
16
21
  }
22
+ Plan.prototype[Symbol.iterator] = function() {
23
+ return this[PLAN_SYMBOL].generator();
24
+ };
17
25
  Plan.prototype.run = runPlan;
18
26
  function asyncExecute(callback) {
19
27
  return asyncAttempt(async () => {
@@ -24,6 +32,8 @@ function asyncExecute(callback) {
24
32
  const { done } = next;
25
33
  let value = next.value;
26
34
  if (value instanceof Error) throw value;
35
+ if (isError(value)) throw value.error;
36
+ if (isOk(value)) value = value.value;
27
37
  lastValue = value;
28
38
  if (done === true) break;
29
39
  }
@@ -54,6 +64,8 @@ function execute(callback) {
54
64
  const { done } = next;
55
65
  let { value } = next;
56
66
  if (value instanceof Error) throw value;
67
+ if (isError(value)) throw value.error;
68
+ if (isOk(value)) value = value.value;
57
69
  lastValue = value;
58
70
  if (done === true) break;
59
71
  }
@@ -76,7 +88,7 @@ function isAsyncPlan(value) {
76
88
  * @returns `true` if the value is an asynchronous generator function, otherwise `false`
77
89
  */
78
90
  function isAsyncGenerator(value) {
79
- return typeof value === "function" && value.constructor.name === "AsyncGeneratorFunction" && typeof value.prototype.next === "function" && typeof value.prototype.return === "function" && typeof value.prototype.throw === "function";
91
+ return isGeneratorInstance(GENERATOR_NAME_ASYNC, value);
80
92
  }
81
93
  /**
82
94
  * Is the value a generator function?
@@ -85,7 +97,10 @@ function isAsyncGenerator(value) {
85
97
  * @returns `true` if the value is a generator function, otherwise `false`
86
98
  */
87
99
  function isGenerator(value) {
88
- return typeof value === "function" && value.constructor.name === "GeneratorFunction" && typeof value.prototype.next === "function" && typeof value.prototype.return === "function" && typeof value.prototype.throw === "function";
100
+ return isGeneratorInstance(GENERATOR_NAME_SYNC, value);
101
+ }
102
+ function isGeneratorInstance(name, value) {
103
+ return typeof value === "function" && value !== null && value.constructor.name === name && typeof value.prototype.next === "function" && typeof value.prototype.return === "function" && typeof value.prototype.throw === "function";
89
104
  }
90
105
  /**
91
106
  * Is the value a plan?
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oscarpalmer/atoms",
3
- "version": "0.208.0",
3
+ "version": "0.209.0",
4
4
  "description": "Atomic utilities for making your JavaScript better.",
5
5
  "keywords": [
6
6
  "array",