@oscarpalmer/atoms 0.216.0 → 0.218.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.
Files changed (77) hide show
  1. package/dist/array/insert.mjs +1 -1
  2. package/dist/array/slice.mjs +27 -14
  3. package/dist/array/sort.mjs +2 -1
  4. package/dist/array/swap.d.mts +2 -1
  5. package/dist/color/constants.d.mts +2 -1
  6. package/dist/color/misc/get.mjs +3 -1
  7. package/dist/color/space/hex.mjs +1 -1
  8. package/dist/color/space/rgb.d.mts +1 -0
  9. package/dist/function/index.mjs +1 -2
  10. package/dist/function/retry.d.mts +1 -1
  11. package/dist/function/retry.mjs +5 -2
  12. package/dist/herald.mjs +1 -2
  13. package/dist/index.d.mts +335 -113
  14. package/dist/index.mjs +483 -241
  15. package/dist/internal/array/find.mjs +57 -34
  16. package/dist/internal/array/insert.mjs +1 -1
  17. package/dist/internal/array/match.mjs +21 -17
  18. package/dist/internal/array/sets.mjs +7 -1
  19. package/dist/internal/math/aggregate.mjs +4 -1
  20. package/dist/internal/number.d.mts +1 -1
  21. package/dist/internal/number.mjs +4 -2
  22. package/dist/internal/plan/index.d.mts +7 -0
  23. package/dist/internal/plan/index.mjs +18 -0
  24. package/dist/{plan/generate.d.mts → internal/plan/plan.generate.d.mts} +2 -2
  25. package/dist/{plan/generate.mjs → internal/plan/plan.generate.mjs} +7 -7
  26. package/dist/internal/random.d.mts +6 -0
  27. package/dist/internal/random.mjs +13 -2
  28. package/dist/internal/result/attempt.mjs +4 -4
  29. package/dist/internal/string/misc.mjs +2 -1
  30. package/dist/internal/subscription.mjs +1 -1
  31. package/dist/internal/value/equal.mjs +2 -1
  32. package/dist/internal/value/partial.mjs +3 -1
  33. package/dist/models/color.model.d.mts +2 -1
  34. package/dist/models/function/once.model.d.mts +2 -1
  35. package/dist/models/index.d.mts +2 -1
  36. package/dist/models/plan.model.d.mts +160 -29
  37. package/dist/models/plan.model.mjs +15 -5
  38. package/dist/models/queue.model.d.mts +2 -1
  39. package/dist/models/result.model.d.mts +2 -1
  40. package/dist/models/string/string.fuzzy.model.d.mts +1 -1
  41. package/dist/models/string/string.misc.model.mjs +1 -1
  42. package/dist/models/value/value.equal.model.d.mts +2 -1
  43. package/dist/models/value/value.merge.model.d.mts +3 -2
  44. package/dist/models/value/value.misc.model.d.mts +2 -1
  45. package/dist/number.mjs +1 -2
  46. package/dist/plan/index.d.mts +1 -4
  47. package/dist/plan/index.mjs +14 -15
  48. package/dist/plan/is.d.mts +59 -3
  49. package/dist/plan/is.mjs +82 -10
  50. package/dist/plan/misc.d.mts +51 -3
  51. package/dist/plan/misc.mjs +47 -30
  52. package/dist/plan/models.d.mts +2 -2
  53. package/dist/plan/result.d.mts +15 -3
  54. package/dist/plan/result.mjs +5 -11
  55. package/dist/plan/run.d.mts +1 -3
  56. package/dist/plan/run.mjs +5 -11
  57. package/dist/plan/use.d.mts +36 -0
  58. package/dist/plan/use.mjs +69 -0
  59. package/dist/promise/index.d.mts +6 -4
  60. package/dist/promise/index.mjs +4 -4
  61. package/dist/promise/misc.mjs +5 -2
  62. package/dist/promise/models.mjs +1 -2
  63. package/dist/promise/timed.mjs +2 -2
  64. package/dist/queue.mjs +4 -5
  65. package/dist/random.d.mts +2 -2
  66. package/dist/random.mjs +3 -3
  67. package/dist/result/index.mjs +1 -2
  68. package/dist/result/match.mjs +2 -2
  69. package/dist/result/misc.mjs +3 -10
  70. package/dist/result/work/pipe.d.mts +26 -23
  71. package/dist/result/work/pipe.mjs +7 -3
  72. package/dist/string/fuzzy.mjs +1 -1
  73. package/dist/string/index.mjs +1 -1
  74. package/dist/subscription.mjs +1 -2
  75. package/dist/value/clone.mjs +1 -1
  76. package/dist/value/merge.mjs +17 -4
  77. package/package.json +8 -10
@@ -1,17 +1,65 @@
1
- import { PlanFailure, PlanSuccess } from "../models/plan.model.mjs";
1
+ import { AsyncFailure, AsyncSuccess, Failure, Success } from "../models/plan.model.mjs";
2
2
  //#region src/plan/misc.d.ts
3
+ /**
4
+ * Create an immediately failing plan
5
+ *
6
+ * @param callback Failure callback
7
+ * @returns Plan failure
8
+ */
9
+ export declare function asyncFail<Value>(callback: () => Value): AsyncFailure<Awaited<Value>>;
10
+ /**
11
+ * Create an immediately failing plan
12
+ *
13
+ * @param value Failure value
14
+ * @returns Plan failure
15
+ */
16
+ export declare function asyncFail<Value>(value: Value): AsyncFailure<Awaited<Value>>;
17
+ /**
18
+ * Create an immediately succeeding plan
19
+ *
20
+ * @param callback Success callback
21
+ * @returns Plan success
22
+ */
23
+ export declare function asyncSucceed<Value>(callback: () => Value): AsyncSuccess<Awaited<Value>>;
24
+ /**
25
+ * Create an immediately succeeding plan
26
+ *
27
+ * @param value Success value
28
+ * @returns Plan success
29
+ */
30
+ export declare function asyncSucceed<Value>(value: Value): AsyncSuccess<Awaited<Value>>;
31
+ /**
32
+ * Create an immediately failing plan
33
+ *
34
+ * @param callback Failure callback
35
+ * @returns Plan failure
36
+ */
37
+ export declare function fail<Value>(callback: () => Value): Failure<Value>;
3
38
  /**
4
39
  * Create an immediately failing plan
5
40
  *
6
41
  * @param value Failure value
7
42
  * @returns Plan failure
8
43
  */
9
- export declare function fail<Value>(value: Value): PlanFailure<Value>;
44
+ export declare function fail<Value>(value: Value): Failure<Value>;
45
+ /**
46
+ * Create an immediately succeeding plan
47
+ *
48
+ * @param callback Success callback
49
+ * @returns Plan success
50
+ */
51
+ export declare function succeed<Value>(callback: () => Value): Success<Value>;
10
52
  /**
11
53
  * Create an immediately succeeding plan
12
54
  *
13
55
  * @param value Success value
14
56
  * @returns Plan success
15
57
  */
16
- export declare function succeed<Value>(value: Value): PlanSuccess<Value>;
58
+ export declare function succeed<Value>(value: Value): Success<Value>;
59
+ export declare namespace fail {
60
+ export var async: typeof asyncFail;
61
+ }
62
+ export declare namespace succeed {
63
+ export var async: typeof asyncSucceed;
64
+ }
17
65
  //#endregion
@@ -1,39 +1,56 @@
1
1
  import { error } from "../internal/result/misc.mjs";
2
- import { PLAN_SYMBOL } from "../models/plan.model.mjs";
2
+ import { PLAN_FAILURE_ASYNC, PLAN_FAILURE_SYNC, PLAN_SUCCESS_ASYNC, PLAN_SUCCESS_SYNC, PLAN_SYMBOL } from "../models/plan.model.mjs";
3
+ import { getGenerator } from "../internal/plan/index.mjs";
3
4
  //#region src/plan/misc.ts
4
- function PlanFailure(generator) {
5
- this[PLAN_SYMBOL] = { generator };
6
- }
7
- PlanFailure.prototype[Symbol.iterator] = function() {
8
- return this[PLAN_SYMBOL].generator();
9
- };
10
- function PlanSuccess(generator) {
11
- this[PLAN_SYMBOL] = { generator };
12
- }
13
- PlanSuccess.prototype[Symbol.iterator] = function() {
14
- return this[PLAN_SYMBOL].generator();
15
- };
16
- /**
17
- * Create an immediately failing plan
18
- *
19
- * @param value Failure value
20
- * @returns Plan failure
21
- */
5
+ function AsyncFailure(generator) {
6
+ this[PLAN_SYMBOL] = {
7
+ generator,
8
+ type: PLAN_FAILURE_ASYNC
9
+ };
10
+ }
11
+ AsyncFailure.prototype[Symbol.asyncIterator] = getGenerator;
12
+ function AsyncSuccess(generator) {
13
+ this[PLAN_SYMBOL] = {
14
+ generator,
15
+ type: PLAN_SUCCESS_ASYNC
16
+ };
17
+ }
18
+ AsyncSuccess.prototype[Symbol.asyncIterator] = getGenerator;
19
+ function Failure(generator) {
20
+ this[PLAN_SYMBOL] = {
21
+ generator,
22
+ type: PLAN_FAILURE_SYNC
23
+ };
24
+ }
25
+ Failure.prototype[Symbol.iterator] = getGenerator;
26
+ function Success(generator) {
27
+ this[PLAN_SYMBOL] = {
28
+ generator,
29
+ type: PLAN_SUCCESS_SYNC
30
+ };
31
+ }
32
+ Success.prototype[Symbol.iterator] = getGenerator;
33
+ function asyncFail(value) {
34
+ return new AsyncFailure(async function* () {
35
+ yield error(await (typeof value === "function" ? value() : value));
36
+ });
37
+ }
38
+ function asyncSucceed(value) {
39
+ return new AsyncSuccess(async function* () {
40
+ return typeof value === "function" ? value() : value;
41
+ });
42
+ }
22
43
  function fail(value) {
23
- return new PlanFailure(function* () {
24
- yield error(value);
44
+ return new Failure(function* () {
45
+ yield error(typeof value === "function" ? value() : value);
25
46
  });
26
47
  }
27
- /**
28
- * Create an immediately succeeding plan
29
- *
30
- * @param value Success value
31
- * @returns Plan success
32
- */
33
48
  function succeed(value) {
34
- return new PlanSuccess(function* () {
35
- return value;
49
+ return new Success(function* () {
50
+ return typeof value === "function" ? value() : value;
36
51
  });
37
52
  }
53
+ fail.async = asyncFail;
54
+ succeed.async = asyncSucceed;
38
55
  //#endregion
39
- export { fail, succeed };
56
+ export { asyncFail, asyncSucceed, fail, succeed };
@@ -1,2 +1,2 @@
1
- import { AsyncPlan, Plan, PlanFailure, PlanResult, PlanSuccess } from "../models/plan.model.mjs";
2
- export type { AsyncPlan, Plan, PlanFailure, PlanResult, PlanSuccess };
1
+ import { AsyncFailure, AsyncPlan, AsyncPlanned, AsyncStep, AsyncSuccess, Failure, Plan, PlanError, PlanOk, PlanReturned, Planned, Step, Success } from "../models/plan.model.mjs";
2
+ export type { AsyncFailure, AsyncPlan, AsyncPlanned, AsyncStep, AsyncSuccess, Failure, Plan, PlanError, PlanOk, PlanReturned, Planned, Step, Success };
@@ -1,11 +1,14 @@
1
1
  import { Result } from "../models/result.model.mjs";
2
- import { AsyncPlan, InternalPlan, Plan, PlanError, PlanOk } from "../models/plan.model.mjs";
2
+ import { AsyncPlan, Plan, PlanError, PlanOk } from "../models/plan.model.mjs";
3
3
  //#region src/plan/result.d.ts
4
4
  /**
5
5
  * Attempt to run the generator to completion
6
6
  *
7
7
  * _Returns a promised {@link Result} instead of a raw value or throwing an error_
8
8
  *
9
+ * @param generator Generator to run
10
+ * @param parameters Parameters for starting the run
11
+ * @param signal Optional abort signal
9
12
  * @returns Promised result
10
13
  */
11
14
  export declare function asyncAttemptRun<Yielded, Returned, Parameters extends unknown[]>(generator: (...parameters: Parameters) => AsyncGenerator<Yielded, Returned>, parameters: Parameters, signal?: AbortSignal): Promise<Result<PlanOk<Returned>, PlanError<Yielded, Returned>>>;
@@ -14,6 +17,8 @@ export declare function asyncAttemptRun<Yielded, Returned, Parameters extends un
14
17
  *
15
18
  * _Returns a promised {@link Result} instead of a raw value or throwing an error_
16
19
  *
20
+ * @param generator Generator to run
21
+ * @param signal Optional abort signal
17
22
  * @returns Promised result
18
23
  */
19
24
  export declare function asyncAttemptRun<Yielded, Returned>(generator: () => AsyncGenerator<Yielded, Returned>, signal?: AbortSignal): Promise<Result<PlanOk<Returned>, PlanError<Yielded, Returned>>>;
@@ -22,6 +27,9 @@ export declare function asyncAttemptRun<Yielded, Returned>(generator: () => Asyn
22
27
  *
23
28
  * _Returns a promised {@link Result} instead of a raw value or throwing an error_
24
29
  *
30
+ * @param plan Plan to run
31
+ * @param parameters Parameters for starting the run
32
+ * @param signal Optional abort signal
25
33
  * @returns Promised result
26
34
  */
27
35
  export declare function asyncAttemptRun<Yielded, Returned, Parameters extends unknown[]>(plan: AsyncPlan<Yielded, Returned, Parameters>, parameters: Parameters, signal?: AbortSignal): Promise<Result<PlanOk<Returned>, PlanError<Yielded, Returned>>>;
@@ -30,6 +38,8 @@ export declare function asyncAttemptRun<Yielded, Returned, Parameters extends un
30
38
  *
31
39
  * _Returns a promised {@link Result} instead of a raw value or throwing an error_
32
40
  *
41
+ * @param plan Plan to run
42
+ * @param signal Optional abort signal
33
43
  * @returns Promised result
34
44
  */
35
45
  export declare function asyncAttemptRun<Yielded, Returned>(plan: AsyncPlan<Yielded, Returned, []>, signal?: AbortSignal): Promise<Result<PlanOk<Returned>, PlanError<Yielded, Returned>>>;
@@ -38,6 +48,8 @@ export declare function asyncAttemptRun<Yielded, Returned>(plan: AsyncPlan<Yield
38
48
  *
39
49
  * _Returns a {@link Result} instead of a raw value or throwing an error_
40
50
  *
51
+ * @param generator Generator to run
52
+ * @param parameters Parameters for starting the run
41
53
  * @returns Result
42
54
  */
43
55
  export declare function attemptRun<Yielded, Returned, Parameters extends unknown[]>(generator: (...parameters: Parameters) => Generator<Yielded, Returned>, ...parameters: Parameters): Result<PlanOk<Returned>, PlanError<Yielded, Returned>>;
@@ -46,11 +58,11 @@ export declare function attemptRun<Yielded, Returned, Parameters extends unknown
46
58
  *
47
59
  * _Returns a {@link Result} instead of a raw value or throwing an error_
48
60
  *
61
+ * @param plan Plan to run
62
+ * @param parameters Parameters for starting the run
49
63
  * @returns Result
50
64
  */
51
65
  export declare function attemptRun<Yielded, Returned, Parameters extends unknown[]>(plan: Plan<Yielded, Returned, Parameters>, ...parameters: Parameters): Result<PlanOk<Returned>, PlanError<Yielded, Returned>>;
52
- export declare function attemptRunAsyncPlan(this: never, first: never, second: never): Promise<unknown>;
53
- export declare function attemptRunPlan(this: InternalPlan, ...parameters: unknown[]): unknown;
54
66
  export declare namespace attemptRun {
55
67
  export var async: typeof asyncAttemptRun;
56
68
  }
@@ -1,28 +1,22 @@
1
1
  import { asyncAttempt, attempt } from "../internal/result/attempt.mjs";
2
2
  import { PLAN_MESSAGE_RUN_INPUT, PLAN_SYMBOL } from "../models/plan.model.mjs";
3
- import { isAsyncGenerator, isAsyncPlan, isGenerator, isPlan } from "./is.mjs";
4
- import { asyncGenerate, generate } from "./generate.mjs";
3
+ import { asyncGenerate, generate } from "../internal/plan/plan.generate.mjs";
4
+ import { isAsyncGeneratorFunction, isAsyncPlan, isGeneratorFunction, isPlan } from "./is.mjs";
5
5
  //#region src/plan/result.ts
6
6
  function asyncAttemptRun(input, first, second) {
7
7
  let generator;
8
8
  if (isAsyncPlan(input)) generator = input[PLAN_SYMBOL];
9
- else if (isAsyncGenerator(input)) generator = input;
9
+ else if (isAsyncGeneratorFunction(input)) generator = input;
10
10
  if (generator == null) throw new Error(PLAN_MESSAGE_RUN_INPUT);
11
11
  return asyncAttempt(() => asyncGenerate(generator, true, first, second));
12
12
  }
13
13
  function attemptRun(input, ...parameters) {
14
14
  let generator;
15
15
  if (isPlan(input)) generator = input[PLAN_SYMBOL];
16
- else if (isGenerator(input)) generator = input;
16
+ else if (isGeneratorFunction(input)) generator = input;
17
17
  if (generator == null) throw new Error(PLAN_MESSAGE_RUN_INPUT);
18
18
  return attempt(() => generate(generator, parameters, true));
19
19
  }
20
- function attemptRunAsyncPlan(first, second) {
21
- return asyncAttemptRun(this, first, second);
22
- }
23
- function attemptRunPlan(...parameters) {
24
- return attempt(() => generate(this[PLAN_SYMBOL], parameters, true));
25
- }
26
20
  attemptRun.async = asyncAttemptRun;
27
21
  //#endregion
28
- export { asyncAttemptRun, attemptRun, attemptRunAsyncPlan, attemptRunPlan };
22
+ export { asyncAttemptRun, attemptRun };
@@ -1,4 +1,4 @@
1
- import { AsyncPlan, InternalAsyncPlan, InternalPlan, Plan, PlanReturned } from "../models/plan.model.mjs";
1
+ import { AsyncPlan, Plan, PlanReturned } from "../models/plan.model.mjs";
2
2
  import { asyncAttemptRun, attemptRun } from "./result.mjs";
3
3
  //#region src/plan/run.d.ts
4
4
  /**
@@ -51,8 +51,6 @@ export declare function run<Yielded, Returned, Parameters extends unknown[]>(gen
51
51
  * @returns Result
52
52
  */
53
53
  export declare function run<Yielded, Returned, Parameters extends unknown[]>(plan: Plan<Yielded, Returned, Parameters>, ...parameters: Parameters): PlanReturned<Returned>;
54
- export declare function runAsyncPlan(this: InternalAsyncPlan, first: never, second: never): Promise<unknown>;
55
- export declare function runPlan(this: InternalPlan, ...parameters: unknown[]): unknown;
56
54
  export declare namespace asyncRun {
57
55
  export var attempt: typeof asyncAttemptRun;
58
56
  }
package/dist/plan/run.mjs CHANGED
@@ -1,26 +1,20 @@
1
1
  import { PLAN_MESSAGE_RUN_INPUT, PLAN_SYMBOL } from "../models/plan.model.mjs";
2
- import { isAsyncGenerator, isAsyncPlan, isGenerator, isPlan } from "./is.mjs";
3
- import { asyncGenerate, generate } from "./generate.mjs";
2
+ import { asyncGenerate, generate } from "../internal/plan/plan.generate.mjs";
3
+ import { isAsyncGeneratorFunction, isAsyncPlan, isGeneratorFunction, isPlan } from "./is.mjs";
4
4
  import { asyncAttemptRun, attemptRun } from "./result.mjs";
5
5
  //#region src/plan/run.ts
6
6
  function asyncRun(input, first, second) {
7
7
  if (isAsyncPlan(input)) return asyncGenerate(input[PLAN_SYMBOL], false, first, second);
8
- if (isAsyncGenerator(input)) return asyncGenerate(input, false, first, second);
8
+ if (isAsyncGeneratorFunction(input)) return asyncGenerate(input, false, first, second);
9
9
  throw new Error(PLAN_MESSAGE_RUN_INPUT);
10
10
  }
11
11
  function run(input, ...parameters) {
12
12
  if (isPlan(input)) return input.run(...parameters);
13
- if (isGenerator(input)) return generate(input, parameters, false);
13
+ if (isGeneratorFunction(input)) return generate(input, parameters, false);
14
14
  throw new Error(PLAN_MESSAGE_RUN_INPUT);
15
15
  }
16
- function runAsyncPlan(first, second) {
17
- return asyncGenerate(this[PLAN_SYMBOL], false, first, second);
18
- }
19
- function runPlan(...parameters) {
20
- return generate(this[PLAN_SYMBOL], parameters, false);
21
- }
22
16
  asyncRun.attempt = asyncAttemptRun;
23
17
  run.async = asyncRun;
24
18
  run.attempt = attemptRun;
25
19
  //#endregion
26
- export { asyncRun, run, runAsyncPlan, runPlan };
20
+ export { asyncRun, run };
@@ -0,0 +1,36 @@
1
+ import { AsyncStep, AsyncStepValue, Step, StepValue } from "../models/plan.model.mjs";
2
+ //#region src/plan/use.d.ts
3
+ /**
4
+ * Create an asynchronous step from a callback
5
+ *
6
+ * @param callback Callback
7
+ * @returns An asynchronous step
8
+ */
9
+ export declare function asyncUse<Value>(callback: () => Value & AsyncStepValue<Value>): AsyncStep<Value>;
10
+ /**
11
+ * Create an asynchronous step from a value
12
+ *
13
+ * @param value Value
14
+ * @returns An asynchronous step
15
+ */
16
+ export declare function asyncUse<Value>(value: Value & AsyncStepValue<Value>): AsyncStep<Value>;
17
+ /**
18
+ * Create a step from a callback
19
+ *
20
+ * @param callback Callback
21
+ * @returns A step
22
+ * @throws Error If the input is invalid
23
+ */
24
+ export declare function use<Value>(callback: () => Value & StepValue<Value>): Step<Value>;
25
+ /**
26
+ * Create a step from a value
27
+ *
28
+ * @param value Value
29
+ * @returns A step
30
+ * @throws Error If the input is invalid
31
+ */
32
+ export declare function use<Value>(value: Value & StepValue<Value>): Step<Value>;
33
+ export declare namespace use {
34
+ export var async: typeof asyncUse;
35
+ }
36
+ //#endregion
@@ -0,0 +1,69 @@
1
+ import { isError, isOk } from "../internal/result/misc.mjs";
2
+ import { PLAN_MESSAGE_USE_NESTED_FN, PLAN_MESSAGE_USE_SYNC_ASYNC, PLAN_STEP_ASYNC, PLAN_STEP_SYNC, PLAN_SYMBOL } from "../models/plan.model.mjs";
3
+ import { getGenerator } from "../internal/plan/index.mjs";
4
+ import { isAsyncFailure, isAsyncGenerator, isAsyncGeneratorFunction, isAsyncPlan, isAsyncStep, isAsyncSuccess, isGenerator } from "./is.mjs";
5
+ import { fail } from "./misc.mjs";
6
+ //#region src/plan/use.ts
7
+ function AsyncStep(generator) {
8
+ this[PLAN_SYMBOL] = {
9
+ generator,
10
+ type: PLAN_STEP_ASYNC
11
+ };
12
+ }
13
+ AsyncStep.prototype[Symbol.asyncIterator] = getGenerator;
14
+ function Step(generator) {
15
+ this[PLAN_SYMBOL] = {
16
+ generator,
17
+ type: PLAN_STEP_SYNC
18
+ };
19
+ }
20
+ Step.prototype[Symbol.iterator] = getGenerator;
21
+ function asyncUse(value) {
22
+ return new AsyncStep(async function* () {
23
+ let actual = value;
24
+ if (typeof actual === "function") actual = actual();
25
+ actual = await actual;
26
+ if (typeof actual === "function") throw new TypeError(PLAN_MESSAGE_USE_NESTED_FN);
27
+ if (typeof actual === "object" && actual !== null && PLAN_SYMBOL in actual) return yield* actual;
28
+ if (isAsyncGenerator(actual) || isGenerator(actual)) return yield* actual;
29
+ if (isError(actual)) {
30
+ yield* fail(actual.error);
31
+ return;
32
+ }
33
+ if (isOk(actual)) return actual.value;
34
+ if (actual instanceof Error) {
35
+ yield* fail(actual);
36
+ return;
37
+ }
38
+ return actual;
39
+ });
40
+ }
41
+ function use(value) {
42
+ validateUseInput(value, false);
43
+ return new Step(function* () {
44
+ let actual = value;
45
+ if (typeof actual === "function") {
46
+ actual = actual();
47
+ validateUseInput(actual, true);
48
+ }
49
+ if (typeof actual === "object" && actual !== null && PLAN_SYMBOL in actual) return yield* actual;
50
+ if (isGenerator(actual)) return yield* actual;
51
+ if (isError(actual)) {
52
+ yield* fail(actual.error);
53
+ return;
54
+ }
55
+ if (isOk(actual)) return actual.value;
56
+ if (actual instanceof Error) {
57
+ yield* fail(actual);
58
+ return;
59
+ }
60
+ return actual;
61
+ });
62
+ }
63
+ function validateUseInput(value, resolved) {
64
+ if (value instanceof Promise || isAsyncFailure(value) || isAsyncGenerator(value) || isAsyncGeneratorFunction(value) || isAsyncPlan(value) || isAsyncStep(value) || isAsyncSuccess(value)) throw new Error(PLAN_MESSAGE_USE_SYNC_ASYNC);
65
+ if (resolved && typeof value === "function") throw new Error(PLAN_MESSAGE_USE_NESTED_FN);
66
+ }
67
+ use.async = asyncUse;
68
+ //#endregion
69
+ export { asyncUse, use };
@@ -1,6 +1,7 @@
1
1
  import { PlainObject } from "../models/index.mjs";
2
2
  import { PromiseOptions, PromiseStrategy, PromisesItems, PromisesOptions, PromisesResult, PromisesUnwrapped, PromisesValues } from "../models/promise.model.mjs";
3
3
  //#region src/promise/index.d.ts
4
+ type AttemptPromiseOptions = PromiseOptions | AbortSignal | number;
4
5
  /**
5
6
  * Wrap a _Promise_ with safety handlers
6
7
  *
@@ -8,7 +9,7 @@ import { PromiseOptions, PromiseStrategy, PromisesItems, PromisesOptions, Promis
8
9
  * @param options Options for the _Promise_
9
10
  * @returns Wrapped _Promise_
10
11
  */
11
- export declare function attemptPromise<Value>(promise: Promise<Value>, options?: PromiseOptions | AbortSignal | number): Promise<Value>;
12
+ export declare function attemptPromise<Value>(promise: Promise<Value>, options?: AttemptPromiseOptions): Promise<Value>;
12
13
  /**
13
14
  * Wrap a _Promise_-returning callback with safety handlers
14
15
  *
@@ -16,7 +17,7 @@ export declare function attemptPromise<Value>(promise: Promise<Value>, options?:
16
17
  * @param options Options for the _Promise_
17
18
  * @returns _Promise_-wrapped callback
18
19
  */
19
- export declare function attemptPromise<Value>(callback: () => Promise<Value>, options?: PromiseOptions | AbortSignal | number): Promise<Value>;
20
+ export declare function attemptPromise<Value>(callback: () => Promise<Value>, options?: AttemptPromiseOptions): Promise<Value>;
20
21
  /**
21
22
  * Wrap a callback with a _Promise_ and safety handlers
22
23
  *
@@ -24,7 +25,7 @@ export declare function attemptPromise<Value>(callback: () => Promise<Value>, op
24
25
  * @param options Options for the _Promise_
25
26
  * @returns _Promise_-wrapped callback
26
27
  */
27
- export declare function attemptPromise<Value>(callback: () => Value, options?: PromiseOptions | AbortSignal | number): Promise<Value>;
28
+ export declare function attemptPromise<Value>(callback: () => Value, options?: AttemptPromiseOptions): Promise<Value>;
28
29
  /**
29
30
  * Handle a list of _Promises_, returning their results in an ordered array
30
31
  *
@@ -106,4 +107,5 @@ export declare function resultPromises<Items extends PlainObject>(items: Items,
106
107
  export declare namespace promises {
107
108
  export var result: typeof resultPromises;
108
109
  }
109
- //#endregion
110
+ //#endregion
111
+ export {};
@@ -8,9 +8,9 @@ import { getTimedPromise } from "./timed.mjs";
8
8
  //#region src/promise/index.ts
9
9
  async function attemptPromise(value, options) {
10
10
  const isFunction = typeof value === "function";
11
- if (!isFunction && !(value instanceof Promise)) return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_ATTEMPT));
11
+ if (!isFunction && !(value instanceof Promise)) throw new TypeError(PROMISE_MESSAGE_EXPECTATION_ATTEMPT);
12
12
  const { signal, time } = createPromiseOptions(options);
13
- if (signal?.aborted ?? false) return Promise.reject(signal?.reason);
13
+ if (signal?.aborted ?? false) throw signal?.reason;
14
14
  const aborter = createAborter(signal, () => {
15
15
  rejector(signal?.reason);
16
16
  });
@@ -32,8 +32,8 @@ async function attemptPromise(value, options) {
32
32
  }
33
33
  async function promises(items, options) {
34
34
  const { signal, strategy } = createPromisesOptions(options);
35
- if (signal?.aborted ?? false) return Promise.reject(signal?.reason);
36
- if (!isArrayOrPlainObject(items)) return Promise.reject(PROMISE_MESSAGE_EXPECTATION_ITEMS_TYPE);
35
+ if (signal?.aborted ?? false) throw signal?.reason;
36
+ if (!isArrayOrPlainObject(items)) throw new TypeError(PROMISE_MESSAGE_EXPECTATION_ITEMS_TYPE);
37
37
  const isArray = Array.isArray(items);
38
38
  const actual = (isArray ? items.map((item, index) => [index, item]) : Object.entries(items)).map(([key, value]) => [key, typeof value === "function" ? value() : value]).filter(([, value]) => value instanceof Promise);
39
39
  const aborter = createAborter(signal, () => {
@@ -20,13 +20,16 @@ function handleResult(status, parameters) {
20
20
  handlers.reject(value);
21
21
  return;
22
22
  }
23
- data.result[key] = !complete ? value : status === "fulfilled" ? {
23
+ let resultValue;
24
+ if (complete) resultValue = status === "fulfilled" ? {
24
25
  status,
25
26
  value
26
27
  } : {
27
28
  status,
28
29
  reason: value
29
30
  };
31
+ else resultValue = value;
32
+ data.result[key] = resultValue;
30
33
  if (index === data.last) {
31
34
  aborter?.cancel();
32
35
  handlers.resolve(data.result);
@@ -38,7 +41,7 @@ function settlePromise(settler, value, aborter) {
38
41
  }
39
42
  async function toResult(value) {
40
43
  const actual = typeof value === "function" ? value() : value;
41
- if (!(actual instanceof Promise)) return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_RESULT));
44
+ if (!(actual instanceof Promise)) throw new TypeError(PROMISE_MESSAGE_EXPECTATION_RESULT);
42
45
  return actual.then((result) => ok(result)).catch((reason) => error(reason));
43
46
  }
44
47
  //#endregion
@@ -1,2 +1 @@
1
- import { CancelablePromise, PromiseTimeoutError } from "../models/promise.model.mjs";
2
- export { CancelablePromise, PromiseTimeoutError };
1
+ export { CancelablePromise, PromiseTimeoutError } from "../models/promise.model.mjs";
@@ -23,9 +23,9 @@ async function getTimedPromise(promise, time, signal) {
23
23
  });
24
24
  }
25
25
  async function timed(promise, options) {
26
- if (!(promise instanceof Promise)) return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_TIMED));
26
+ if (!(promise instanceof Promise)) throw new TypeError(PROMISE_MESSAGE_EXPECTATION_TIMED);
27
27
  const { signal, time } = createPromiseOptions(options);
28
- if (signal?.aborted ?? false) return Promise.reject(signal?.reason);
28
+ if (signal?.aborted ?? false) throw signal?.reason;
29
29
  return time > 0 ? getTimedPromise(promise, time, signal) : promise;
30
30
  }
31
31
  //#endregion
package/dist/queue.mjs CHANGED
@@ -335,14 +335,13 @@ async function run(state) {
335
335
  state.runners += 1;
336
336
  let item = state.items.shift();
337
337
  while (item != null) {
338
+ const next = item;
338
339
  let error = false;
339
340
  let result;
340
341
  const attempted = await asyncAttempt(async () => {
341
- if (item != null && !(item.aborter?.signal?.aborted ?? false)) {
342
- const parameters = item.key == null ? item.parameters : [item.key, ...item.parameters];
343
- return await state.callback(...parameters);
344
- }
345
- throw new Error();
342
+ if (next.aborter?.signal?.aborted) throw next.aborter.signal.reason;
343
+ const parameters = next.key == null ? next.parameters : [next.key, ...next.parameters];
344
+ return await state.callback(...parameters);
346
345
  });
347
346
  if (attempted.ok) result = attempted.value;
348
347
  else {
package/dist/random.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { getRandomFloatingNumber, getRandomInteger } from "./internal/random.mjs";
1
+ import { getRandom, getRandomFloatingNumber, getRandomInteger } from "./internal/random.mjs";
2
2
  //#region src/random.d.ts
3
3
  /**
4
4
  * Get a random boolean
@@ -50,4 +50,4 @@ export declare function getRandomItems<Value>(array: Value[], amount: number): V
50
50
  */
51
51
  export declare function getRandomItems<Value>(array: Value[]): Value[];
52
52
  //#endregion
53
- export { getRandomFloatingNumber as getRandomFloat, getRandomInteger };
53
+ export { getRandom, getRandomFloatingNumber as getRandomFloat, getRandomInteger };
package/dist/random.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { getRandomFloatingNumber, getRandomInteger } from "./internal/random.mjs";
1
+ import { getRandom, getRandomFloatingNumber, getRandomInteger } from "./internal/random.mjs";
2
2
  import { shuffle } from "./internal/array/shuffle.mjs";
3
3
  import { join } from "./internal/string/misc.mjs";
4
4
  import { RANDOM_ALPHABET, RANDOM_BOOLEAN_MODIFIER, RANDOM_HEX_CHARACTERS } from "./models/random.model.mjs";
@@ -9,7 +9,7 @@ import { RANDOM_ALPHABET, RANDOM_BOOLEAN_MODIFIER, RANDOM_HEX_CHARACTERS } from
9
9
  * @returns Random boolean
10
10
  */
11
11
  function getRandomBoolean() {
12
- return Math.random() > RANDOM_BOOLEAN_MODIFIER;
12
+ return getRandom() > RANDOM_BOOLEAN_MODIFIER;
13
13
  }
14
14
  /**
15
15
  * Get a random string of characters with a specified length
@@ -59,4 +59,4 @@ function getRandomItems(array, amount) {
59
59
  return typeof amount !== "number" || amount <= 0 || amount >= array.length ? shuffle(array) : shuffle(array).slice(0, amount);
60
60
  }
61
61
  //#endregion
62
- export { getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloatingNumber as getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems };
62
+ export { getRandom, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloatingNumber as getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems };
@@ -1,2 +1 @@
1
- import { asyncAttempt, attempt } from "../internal/result/attempt.mjs";
2
- export { asyncAttempt, attempt };
1
+ export { asyncAttempt, attempt } from "../internal/result/attempt.mjs";
@@ -20,10 +20,10 @@ function handleResult(result, first, second) {
20
20
  error = second;
21
21
  }
22
22
  if (isOk(result)) {
23
- if (typeof ok !== "function") throw new Error(MATCH_MESSAGE_OK);
23
+ if (typeof ok !== "function") throw new TypeError(MATCH_MESSAGE_OK);
24
24
  return ok(result.value);
25
25
  }
26
- if (typeof error !== "function") throw new Error(MATCH_MESSAGE_ERROR);
26
+ if (typeof error !== "function") throw new TypeError(MATCH_MESSAGE_ERROR);
27
27
  return error(result.error, result.original);
28
28
  }
29
29
  function matchResult(result, first, second) {
@@ -1,17 +1,10 @@
1
1
  import { error, isError, isOk, isResult, ok, unwrap } from "../internal/result/misc.mjs";
2
2
  //#region src/result/misc.ts
3
- /**
4
- * Converts a _Result_ to a _Promise_
5
- *
6
- * Resolves if ok, rejects for error
7
- *
8
- * @param result _Result_ to convert
9
- * @returns Promised result
10
- */
11
3
  async function toPromise(result) {
12
4
  const actual = typeof result === "function" ? result() : result;
13
- if (!isResult(actual)) return Promise.reject(new Error(RESULT_MESSAGE_PROMISE));
14
- return isOk(actual) ? Promise.resolve(actual.value) : Promise.reject(actual.error);
5
+ if (!isResult(actual)) throw new TypeError(RESULT_MESSAGE_PROMISE);
6
+ if (isOk(actual)) return actual.value;
7
+ throw actual.error;
15
8
  }
16
9
  const RESULT_MESSAGE_PROMISE = "toPromise expected to receive a Result";
17
10
  //#endregion