@oscarpalmer/atoms 0.209.0 → 0.211.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.
@@ -1,5 +1,6 @@
1
- import { Result } from "../internal/models/result.model.mjs";
2
1
  import { AsyncPlan, Plan } from "../internal/models/plan.model.mjs";
2
+ import { asyncAttemptRun, attemptRun } from "./result.mjs";
3
+ import { asyncRun, run } from "./run.mjs";
3
4
  //#region src/plan/index.d.ts
4
5
  /**
5
6
  * Create a plan for an asynchronous generator function
@@ -7,74 +8,13 @@ import { AsyncPlan, Plan } from "../internal/models/plan.model.mjs";
7
8
  * @param generator Generator to plan for
8
9
  * @returns Generator plan
9
10
  */
10
- export declare function asyncPlan<Value = unknown, Error = unknown>(generator: () => AsyncGenerator<unknown, Value, unknown>): AsyncPlan<Value, Error>;
11
- /**
12
- * Run an asynchronous generator to completion
13
- *
14
- * @param generator Generator to run
15
- * @returns Result
16
- */
17
- export declare function asyncRun<Value = unknown, Error = unknown>(generator: () => AsyncGenerator<unknown, Value, unknown>): Promise<Result<Value, Error>>;
18
- /**
19
- * Run an asynchronous plan to completion
20
- *
21
- * @param plan Plan to run
22
- * @returns Result
23
- */
24
- export declare function asyncRun<Value = unknown, Error = unknown>(plan: AsyncPlan<Value, Error>): Promise<Result<Value, Error>>;
25
- /**
26
- * Is the value an asynchronous plan?
27
- *
28
- * @param value Value to check
29
- * @returns `true` if the value is an asynchronous plan, otherwise `false`
30
- */
31
- export declare function isAsyncPlan<Value = unknown, Error = unknown>(value: unknown): value is AsyncPlan<Value, Error>;
32
- /**
33
- * Is the value an asynchronous generator function?
34
- *
35
- * @param value Value to check
36
- * @returns `true` if the value is an asynchronous generator function, otherwise `false`
37
- */
38
- export declare function isAsyncGenerator(value: unknown): value is () => AsyncGenerator;
39
- /**
40
- * Is the value a generator function?
41
- *
42
- * @param value Value to check
43
- * @returns `true` if the value is a generator function, otherwise `false`
44
- */
45
- export declare function isGenerator(value: unknown): value is () => Generator;
46
- /**
47
- * Is the value a plan?
48
- *
49
- * @param value Value to check
50
- * @returns `true` if the value is a plan, otherwise `false`
51
- */
52
- export declare function isPlan<Value = unknown, Error = unknown>(value: unknown): value is Plan<Value, Error>;
11
+ export declare function plan<Yielded, Returned, Parameters extends unknown[]>(generator: (...parameters: Parameters) => AsyncGenerator<Yielded, Returned>): AsyncPlan<Yielded, Returned, Parameters>;
53
12
  /**
54
13
  * Create a plan for a generator function
55
14
  *
56
15
  * @param generator Generator to plan for
57
16
  * @returns Generator plan
58
17
  */
59
- export declare function plan<Value = unknown, Error = unknown>(generator: () => Generator<unknown, Value, Error>): Plan<Value, Error>;
60
- /**
61
- * Run a generator to completion
62
- *
63
- * @param generator Generator to run
64
- * @returns Result
65
- */
66
- export declare function run<Value = unknown, Error = unknown>(generator: () => Generator<unknown, Value, unknown>): Result<Value, Error>;
67
- /**
68
- * Run a plan to completion
69
- *
70
- * @param plan Plan to run
71
- * @returns Result
72
- */
73
- export declare function run<Value = unknown, Error = unknown>(plan: Plan<Value, Error>): Result<Value, Error>;
74
- export declare namespace plan {
75
- export var async: typeof asyncPlan;
76
- }
77
- export declare namespace run {
78
- export var async: typeof asyncRun;
79
- }
80
- //#endregion
18
+ export declare function plan<Yielded, Returned, Parameters extends unknown[]>(generator: (...parameters: Parameters) => Generator<Yielded, Returned>): Plan<Yielded, Returned, Parameters>;
19
+ //#endregion
20
+ export { asyncAttemptRun, asyncRun, attemptRun, run };
@@ -1,7 +1,7 @@
1
- import { isError, isOk } from "../internal/result/misc.mjs";
2
- import "../result/misc.mjs";
3
- import { asyncAttempt, attempt } from "../internal/result/attempt.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";
1
+ import { PLAN_MESSAGE_PLAN_INPUT, PLAN_SYMBOL, PLAN_TYPE_PLAN_ASYNC, PLAN_TYPE_PLAN_SYNC } from "../internal/models/plan.model.mjs";
2
+ import { isAsyncGenerator, isGenerator } from "../internal/plan.mjs";
3
+ import { asyncAttemptRun, attemptRun, attemptRunAsyncPlan, attemptRunPlan } from "./result.mjs";
4
+ import { asyncRun, run, runAsyncPlan, runPlan } from "./run.mjs";
5
5
  //#region src/plan/index.ts
6
6
  function AsyncPlan(generator) {
7
7
  this[PLAN_SYMBOL] = {
@@ -12,6 +12,7 @@ function AsyncPlan(generator) {
12
12
  AsyncPlan.prototype[Symbol.asyncIterator] = function() {
13
13
  return this[PLAN_SYMBOL].generator();
14
14
  };
15
+ AsyncPlan.prototype.attempt = attemptRunAsyncPlan;
15
16
  AsyncPlan.prototype.run = runAsyncPlan;
16
17
  function Plan(generator) {
17
18
  this[PLAN_SYMBOL] = {
@@ -22,120 +23,12 @@ function Plan(generator) {
22
23
  Plan.prototype[Symbol.iterator] = function() {
23
24
  return this[PLAN_SYMBOL].generator();
24
25
  };
26
+ Plan.prototype.attempt = attemptRunPlan;
25
27
  Plan.prototype.run = runPlan;
26
- function asyncExecute(callback) {
27
- return asyncAttempt(async () => {
28
- const generator = callback();
29
- let lastValue;
30
- while (true) {
31
- const next = await generator.next();
32
- const { done } = next;
33
- let value = next.value;
34
- if (value instanceof Error) throw value;
35
- if (isError(value)) throw value.error;
36
- if (isOk(value)) value = value.value;
37
- lastValue = value;
38
- if (done === true) break;
39
- }
40
- return lastValue;
41
- });
42
- }
43
- /**
44
- * Create a plan for an asynchronous generator function
45
- *
46
- * @param generator Generator to plan for
47
- * @returns Generator plan
48
- */
49
- function asyncPlan(generator) {
50
- if (!isAsyncGenerator(generator)) throw new Error(PLAN_MESSAGE_PLAN_INPUT);
51
- return new AsyncPlan(generator);
52
- }
53
- async function asyncRun(input) {
54
- if (isAsyncPlan(input)) return input.run();
55
- if (!isAsyncGenerator(input)) throw new Error(PLAN_MESSAGE_RUN_INPUT);
56
- return asyncExecute(input);
57
- }
58
- function execute(callback) {
59
- return attempt(() => {
60
- const generator = callback();
61
- let lastValue;
62
- while (true) {
63
- const next = generator.next();
64
- const { done } = next;
65
- let { value } = next;
66
- if (value instanceof Error) throw value;
67
- if (isError(value)) throw value.error;
68
- if (isOk(value)) value = value.value;
69
- lastValue = value;
70
- if (done === true) break;
71
- }
72
- return lastValue;
73
- });
74
- }
75
- /**
76
- * Is the value an asynchronous plan?
77
- *
78
- * @param value Value to check
79
- * @returns `true` if the value is an asynchronous plan, otherwise `false`
80
- */
81
- function isAsyncPlan(value) {
82
- return isPlanInstance(PLAN_TYPE_PLAN_ASYNC, value);
83
- }
84
- /**
85
- * Is the value an asynchronous generator function?
86
- *
87
- * @param value Value to check
88
- * @returns `true` if the value is an asynchronous generator function, otherwise `false`
89
- */
90
- function isAsyncGenerator(value) {
91
- return isGeneratorInstance(GENERATOR_NAME_ASYNC, value);
92
- }
93
- /**
94
- * Is the value a generator function?
95
- *
96
- * @param value Value to check
97
- * @returns `true` if the value is a generator function, otherwise `false`
98
- */
99
- function isGenerator(value) {
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";
104
- }
105
- /**
106
- * Is the value a plan?
107
- *
108
- * @param value Value to check
109
- * @returns `true` if the value is a plan, otherwise `false`
110
- */
111
- function isPlan(value) {
112
- return isPlanInstance(PLAN_TYPE_PLAN_SYNC, value);
113
- }
114
- function isPlanInstance(type, value) {
115
- return typeof value === "object" && value !== null && PLAN_SYMBOL in value && value[PLAN_SYMBOL].type === type;
116
- }
117
- /**
118
- * Create a plan for a generator function
119
- *
120
- * @param generator Generator to plan for
121
- * @returns Generator plan
122
- */
123
28
  function plan(generator) {
124
- if (!isGenerator(generator)) throw new Error(PLAN_MESSAGE_PLAN_INPUT);
125
- return new Plan(generator);
126
- }
127
- function run(input) {
128
- if (isPlan(input)) return input.run();
129
- if (!isGenerator(input)) throw new Error(PLAN_MESSAGE_RUN_INPUT);
130
- return execute(input);
131
- }
132
- function runAsyncPlan() {
133
- return asyncExecute(this[PLAN_SYMBOL].generator);
134
- }
135
- function runPlan() {
136
- return execute(this[PLAN_SYMBOL].generator);
29
+ if (isAsyncGenerator(generator)) return new AsyncPlan(generator);
30
+ if (isGenerator(generator)) return new Plan(generator);
31
+ throw new Error(PLAN_MESSAGE_PLAN_INPUT);
137
32
  }
138
- plan.async = asyncPlan;
139
- run.async = asyncRun;
140
33
  //#endregion
141
- export { asyncPlan, asyncRun, isAsyncGenerator, isAsyncPlan, isGenerator, isPlan, plan, run };
34
+ export { asyncAttemptRun, asyncRun, attemptRun, plan, run };
@@ -0,0 +1,2 @@
1
+ import { isAsyncGenerator, isAsyncPlan, isGenerator, isPlan } from "../internal/plan.mjs";
2
+ export { isAsyncGenerator, isAsyncPlan, isGenerator, isPlan };
@@ -0,0 +1,2 @@
1
+ import { isAsyncGenerator, isAsyncPlan, isGenerator, isPlan } from "../internal/plan.mjs";
2
+ export { isAsyncGenerator, isAsyncPlan, isGenerator, isPlan };
@@ -0,0 +1,2 @@
1
+ import { fail, succeed } from "../internal/plan.mjs";
2
+ export { fail, succeed };
@@ -0,0 +1,2 @@
1
+ import { fail, succeed } from "../internal/plan.mjs";
2
+ export { fail, succeed };
@@ -1,2 +1,2 @@
1
- import { Plan } from "../internal/models/plan.model.mjs";
2
- export type { Plan };
1
+ import { AsyncPlan, Plan } from "../internal/models/plan.model.mjs";
2
+ export type { AsyncPlan, Plan };
@@ -0,0 +1,41 @@
1
+ import { Result } from "../internal/models/result.model.mjs";
2
+ import { AsyncPlan, InternalPlan, Plan, PlanError, PlanResult } from "../internal/models/plan.model.mjs";
3
+ //#region src/plan/result.d.ts
4
+ /**
5
+ * Attempt to run the generator to completion
6
+ *
7
+ * _Returns a promised {@link Result} instead of a raw value or throwing an error_
8
+ *
9
+ * @returns Promised result
10
+ */
11
+ export declare function asyncAttemptRun<Yielded, Returned, Parameters extends unknown[]>(generator: (...parameters: Parameters) => AsyncGenerator<Yielded, Returned, unknown>, ...parameters: Parameters): Promise<Result<PlanResult<Returned>, PlanError<Yielded, Returned>>>;
12
+ /**
13
+ * Attempt to run the plan to completion
14
+ *
15
+ * _Returns a promised {@link Result} instead of a raw value or throwing an error_
16
+ *
17
+ * @returns Promised result
18
+ */
19
+ export declare function asyncAttemptRun<Yielded, Returned, Parameters extends unknown[]>(plan: AsyncPlan<Yielded, Returned, Parameters>, ...parameters: Parameters): Promise<Result<PlanResult<Returned>, PlanError<Yielded, Returned>>>;
20
+ /**
21
+ * Attempt to run the generator to completion
22
+ *
23
+ * _Returns a {@link Result} instead of a raw value or throwing an error_
24
+ *
25
+ * @returns Result
26
+ */
27
+ export declare function attemptRun<Yielded, Returned>(generator: () => Generator<Yielded, Returned, unknown>, ...parameters: Parameters<typeof generator>): Result<PlanResult<Returned>, PlanError<Yielded, Returned>>;
28
+ /**
29
+ * Attempt to run the plan to completion
30
+ *
31
+ * _Returns a {@link Result} instead of a raw value or throwing an error_
32
+ *
33
+ * @returns Result
34
+ */
35
+ export declare function attemptRun<Yielded, Returned, Parameters extends unknown[]>(plan: Plan<Yielded, Returned, Parameters>, ...parameters: Parameters): Result<PlanResult<Returned>, PlanError<Yielded, Returned>>;
36
+ export declare function attemptRunAsyncPlan(this: InternalPlan, ...parameters: unknown[]): Promise<unknown>;
37
+ export declare function attemptRunPlan(this: InternalPlan, ...parameters: unknown[]): unknown;
38
+ export declare namespace attemptRun {
39
+ export var async: typeof asyncAttemptRun;
40
+ }
41
+ //#endregion
@@ -0,0 +1,24 @@
1
+ import { asyncAttempt, attempt } from "../internal/result/attempt.mjs";
2
+ import { PLAN_MESSAGE_RUN_INPUT } from "../internal/models/plan.model.mjs";
3
+ import { isAsyncGenerator, isAsyncPlan, isGenerator, isPlan } from "../internal/plan.mjs";
4
+ import { asyncGenerate, generate } from "./generate.mjs";
5
+ //#region src/plan/result.ts
6
+ function asyncAttemptRun(input, ...parameters) {
7
+ if (isAsyncPlan(input)) return input.attempt(...parameters);
8
+ if (isAsyncGenerator(input)) return asyncAttempt(() => asyncGenerate(input, parameters));
9
+ throw new Error(PLAN_MESSAGE_RUN_INPUT);
10
+ }
11
+ function attemptRun(input, ...parameters) {
12
+ if (isPlan(input)) return input.attempt(...parameters);
13
+ if (isGenerator(input)) return attempt(() => generate(input, parameters));
14
+ throw new Error(PLAN_MESSAGE_RUN_INPUT);
15
+ }
16
+ function attemptRunAsyncPlan(...parameters) {
17
+ return asyncAttempt(() => this.run(...parameters));
18
+ }
19
+ function attemptRunPlan(...parameters) {
20
+ return attempt(() => this.run(...parameters));
21
+ }
22
+ attemptRun.async = asyncAttemptRun;
23
+ //#endregion
24
+ export { asyncAttemptRun, attemptRun, attemptRunAsyncPlan, attemptRunPlan };
@@ -0,0 +1,41 @@
1
+ import { AsyncPlan, InternalAsyncPlan, InternalPlan, Plan, PlanError, PlanResult } from "../internal/models/plan.model.mjs";
2
+ import { asyncAttemptRun, attemptRun } from "./result.mjs";
3
+ //#region src/plan/run.d.ts
4
+ /**
5
+ * Run an asynchronous generator to completion
6
+ *
7
+ * @param generator Generator to run
8
+ * @returns Result
9
+ */
10
+ export declare function asyncRun<Yielded, Returned, Parameters extends unknown[]>(generator: (...parameters: Parameters) => AsyncGenerator<Yielded, Returned, unknown>, ...parameters: Parameters): Promise<PlanResult<Returned>>;
11
+ /**
12
+ * Run an asynchronous plan to completion
13
+ *
14
+ * @param plan Plan to run
15
+ * @returns Result
16
+ */
17
+ export declare function asyncRun<Yielded, Returned, Parameters extends unknown[]>(plan: AsyncPlan<Yielded, Returned, Parameters>, ...parameters: Parameters): Promise<PlanResult<Returned>>;
18
+ /**
19
+ * Run a generator to completion
20
+ *
21
+ * @param generator Generator to run
22
+ * @returns Result
23
+ */
24
+ export declare function run<Yielded, Returned, Parameters extends unknown[]>(generator: (...parameters: Parameters) => Generator<Yielded, Returned, unknown>, ...parameters: Parameters): PlanResult<Returned> | PlanError<Yielded, Returned>;
25
+ /**
26
+ * Run a plan to completion
27
+ *
28
+ * @param plan Plan to run
29
+ * @returns Result
30
+ */
31
+ export declare function run<Yielded, Returned, Parameters extends unknown[]>(plan: Plan<Yielded, Returned, Parameters>, ...parameters: Parameters): PlanResult<Returned> | PlanError<Yielded, Returned>;
32
+ export declare function runAsyncPlan(this: InternalAsyncPlan, ...parameters: unknown[]): Promise<unknown>;
33
+ export declare function runPlan(this: InternalPlan, ...parameters: unknown[]): unknown;
34
+ export declare namespace asyncRun {
35
+ export var attempt: typeof asyncAttemptRun;
36
+ }
37
+ export declare namespace run {
38
+ export var async: typeof asyncRun;
39
+ export var attempt: typeof attemptRun;
40
+ }
41
+ //#endregion
@@ -0,0 +1,26 @@
1
+ import { PLAN_MESSAGE_RUN_INPUT, PLAN_SYMBOL } from "../internal/models/plan.model.mjs";
2
+ import { isAsyncGenerator, isAsyncPlan, isGenerator, isPlan } from "../internal/plan.mjs";
3
+ import { asyncGenerate, generate } from "./generate.mjs";
4
+ import { asyncAttemptRun, attemptRun } from "./result.mjs";
5
+ //#region src/plan/run.ts
6
+ function asyncRun(input, ...parameters) {
7
+ if (isAsyncPlan(input)) return input.run(...parameters);
8
+ if (isAsyncGenerator(input)) return asyncGenerate(input, parameters);
9
+ throw new Error(PLAN_MESSAGE_RUN_INPUT);
10
+ }
11
+ function run(input, ...parameters) {
12
+ if (isPlan(input)) return input.run(...parameters);
13
+ if (isGenerator(input)) return generate(input, parameters);
14
+ throw new Error(PLAN_MESSAGE_RUN_INPUT);
15
+ }
16
+ function runAsyncPlan(...parameters) {
17
+ return asyncGenerate(this[PLAN_SYMBOL].generator, parameters);
18
+ }
19
+ function runPlan(...parameters) {
20
+ return generate(this[PLAN_SYMBOL].generator, parameters);
21
+ }
22
+ asyncRun.attempt = asyncAttemptRun;
23
+ run.async = asyncRun;
24
+ run.attempt = attemptRun;
25
+ //#endregion
26
+ export { asyncRun, run, runAsyncPlan, runPlan };
@@ -34,8 +34,8 @@ export type PromiseData = {
34
34
  result: ArrayOrPlainObject;
35
35
  };
36
36
  export type PromiseHandlers = {
37
- resolve: (value: unknown[]) => void;
38
- reject: (reason: unknown) => void;
37
+ resolve(value: unknown[]): void;
38
+ reject(reason: unknown): void;
39
39
  };
40
40
  /**
41
41
  * Options for a _Promise_-handling function
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oscarpalmer/atoms",
3
- "version": "0.209.0",
3
+ "version": "0.211.0",
4
4
  "description": "Atomic utilities for making your JavaScript better.",
5
5
  "keywords": [
6
6
  "array",
@@ -153,6 +153,14 @@
153
153
  "types": "./dist/plan/index.d.mts",
154
154
  "default": "./dist/plan/index.mjs"
155
155
  },
156
+ "./plan/is": {
157
+ "types": "./dist/plan/is.d.mts",
158
+ "default": "./dist/plan/is.mjs"
159
+ },
160
+ "./plan/misc": {
161
+ "types": "./dist/plan/misc.d.mts",
162
+ "default": "./dist/plan/misc.mjs"
163
+ },
156
164
  "./plan/models": {
157
165
  "default": "./dist/plan/models.mjs"
158
166
  },
@@ -297,7 +305,7 @@
297
305
  "devDependencies": {
298
306
  "@types/node": "^26.6",
299
307
  "@vitest/coverage-istanbul": "5.0.1",
300
- "eslint": "^10.11",
308
+ "eslint": "^10.12",
301
309
  "jsdom": "^30.1",
302
310
  "tsdown": "^0.23",
303
311
  "typescript": "^7",