@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.
package/dist/index.mjs CHANGED
@@ -2737,7 +2737,7 @@ function setValue(data, path, value, ignoreCase) {
2737
2737
  let next = unwrap(handleValue(target, currentPath, null, true, shouldIgnoreCase), void 0);
2738
2738
  if (typeof next !== "object" || next === null) {
2739
2739
  const nextPath = paths[index + 1];
2740
- if (SET_EXPRESSION_INDEX.test(nextPath)) next = Array.from({ length: Number(nextPath) + 1 }, () => void 0);
2740
+ if (SET_EXPRESSION_INDEX.test(nextPath)) next = Array.from({ length: +nextPath + 1 }, () => void 0);
2741
2741
  else next = {};
2742
2742
  target[currentPath] = next;
2743
2743
  }
@@ -2756,93 +2756,36 @@ const PLAN_SYMBOL = Symbol("plan");
2756
2756
  const PLAN_TYPE_PLAN_ASYNC = "asyncPlan";
2757
2757
  const PLAN_TYPE_PLAN_SYNC = "plan";
2758
2758
  //#endregion
2759
- //#region src/plan/index.ts
2760
- function AsyncPlan(generator) {
2761
- this[PLAN_SYMBOL] = {
2762
- generator,
2763
- type: PLAN_TYPE_PLAN_ASYNC
2764
- };
2765
- }
2766
- AsyncPlan.prototype[Symbol.asyncIterator] = function() {
2767
- return this[PLAN_SYMBOL].generator();
2768
- };
2769
- AsyncPlan.prototype.run = runAsyncPlan;
2770
- function Plan(generator) {
2771
- this[PLAN_SYMBOL] = {
2772
- generator,
2773
- type: PLAN_TYPE_PLAN_SYNC
2774
- };
2775
- }
2776
- Plan.prototype[Symbol.iterator] = function() {
2777
- return this[PLAN_SYMBOL].generator();
2778
- };
2779
- Plan.prototype.run = runPlan;
2780
- function asyncExecute(callback) {
2781
- return asyncAttempt(async () => {
2782
- const generator = callback();
2783
- let lastValue;
2784
- while (true) {
2785
- const next = await generator.next();
2786
- const { done } = next;
2787
- let value = next.value;
2788
- if (value instanceof Error) throw value;
2789
- if (isError(value)) throw value.error;
2790
- if (isOk(value)) value = value.value;
2791
- lastValue = value;
2792
- if (done === true) break;
2793
- }
2794
- return lastValue;
2795
- });
2796
- }
2759
+ //#region src/internal/plan.ts
2797
2760
  /**
2798
- * Create a plan for an asynchronous generator function
2761
+ * Create an immediately failing generator
2799
2762
  *
2800
- * @param generator Generator to plan for
2801
- * @returns Generator plan
2763
+ * @param reason Failure reason
2764
+ * @returns Failing generator
2802
2765
  */
2803
- function asyncPlan(generator) {
2804
- if (!isAsyncGenerator(generator)) throw new Error(PLAN_MESSAGE_PLAN_INPUT);
2805
- return new AsyncPlan(generator);
2806
- }
2807
- async function asyncRun(input) {
2808
- if (isAsyncPlan(input)) return input.run();
2809
- if (!isAsyncGenerator(input)) throw new Error(PLAN_MESSAGE_RUN_INPUT);
2810
- return asyncExecute(input);
2811
- }
2812
- function execute(callback) {
2813
- return attempt(() => {
2814
- const generator = callback();
2815
- let lastValue;
2816
- while (true) {
2817
- const next = generator.next();
2818
- const { done } = next;
2819
- let { value } = next;
2820
- if (value instanceof Error) throw value;
2821
- if (isError(value)) throw value.error;
2822
- if (isOk(value)) value = value.value;
2823
- lastValue = value;
2824
- if (done === true) break;
2825
- }
2826
- return lastValue;
2827
- });
2766
+ function fail(reason) {
2767
+ return (function* () {
2768
+ yield;
2769
+ throw reason;
2770
+ })();
2828
2771
  }
2829
2772
  /**
2830
- * Is the value an asynchronous plan?
2773
+ * Is the value an asynchronous generator function?
2831
2774
  *
2832
2775
  * @param value Value to check
2833
- * @returns `true` if the value is an asynchronous plan, otherwise `false`
2776
+ * @returns `true` if the value is an asynchronous generator function, otherwise `false`
2834
2777
  */
2835
- function isAsyncPlan(value) {
2836
- return isPlanInstance(PLAN_TYPE_PLAN_ASYNC, value);
2778
+ function isAsyncGenerator(value) {
2779
+ return isGeneratorInstance(GENERATOR_NAME_ASYNC, value);
2837
2780
  }
2838
2781
  /**
2839
- * Is the value an asynchronous generator function?
2782
+ * Is the value an asynchronous plan?
2840
2783
  *
2841
2784
  * @param value Value to check
2842
- * @returns `true` if the value is an asynchronous generator function, otherwise `false`
2785
+ * @returns `true` if the value is an asynchronous plan, otherwise `false`
2843
2786
  */
2844
- function isAsyncGenerator(value) {
2845
- return isGeneratorInstance(GENERATOR_NAME_ASYNC, value);
2787
+ function isAsyncPlan(value) {
2788
+ return isPlanInstance(PLAN_TYPE_PLAN_ASYNC, value);
2846
2789
  }
2847
2790
  /**
2848
2791
  * Is the value a generator function?
@@ -2869,28 +2812,134 @@ function isPlanInstance(type, value) {
2869
2812
  return typeof value === "object" && value !== null && PLAN_SYMBOL in value && value[PLAN_SYMBOL].type === type;
2870
2813
  }
2871
2814
  /**
2872
- * Create a plan for a generator function
2815
+ * Create an immediately succeeding generator
2873
2816
  *
2874
- * @param generator Generator to plan for
2875
- * @returns Generator plan
2817
+ * @param value Success value
2818
+ * @returns Succeeding generator
2876
2819
  */
2877
- function plan(generator) {
2878
- if (!isGenerator(generator)) throw new Error(PLAN_MESSAGE_PLAN_INPUT);
2879
- return new Plan(generator);
2820
+ function succeed(value) {
2821
+ return (function* () {
2822
+ yield;
2823
+ return value;
2824
+ })();
2825
+ }
2826
+ //#endregion
2827
+ //#region src/plan/generate.ts
2828
+ async function asyncGenerate(callback, parameters) {
2829
+ const generator = callback(...parameters);
2830
+ let success = true;
2831
+ let lastValue;
2832
+ try {
2833
+ while (true) {
2834
+ const next = await generator.next(lastValue);
2835
+ const { done } = next;
2836
+ let { value } = next;
2837
+ if (value instanceof Error) throw value;
2838
+ if (isError(value)) throw value.error;
2839
+ lastValue = value;
2840
+ if (done === true) break;
2841
+ }
2842
+ } catch (error) {
2843
+ lastValue = error;
2844
+ success = false;
2845
+ } finally {
2846
+ await generator?.return(lastValue);
2847
+ }
2848
+ if (success) return lastValue;
2849
+ throw lastValue;
2850
+ }
2851
+ function generate(callback, parameters) {
2852
+ const generator = callback(...parameters);
2853
+ let success = true;
2854
+ let lastValue;
2855
+ try {
2856
+ while (true) {
2857
+ const next = generator.next(lastValue);
2858
+ const done = next.done === true;
2859
+ const { value } = next;
2860
+ if (value instanceof Error) throw value;
2861
+ if (isError(value)) throw value.error;
2862
+ lastValue = value;
2863
+ if (done) break;
2864
+ }
2865
+ } catch (error) {
2866
+ lastValue = error;
2867
+ success = false;
2868
+ } finally {
2869
+ generator.return(lastValue);
2870
+ }
2871
+ if (success) return lastValue;
2872
+ throw lastValue;
2873
+ }
2874
+ //#endregion
2875
+ //#region src/plan/result.ts
2876
+ function asyncAttemptRun(input, ...parameters) {
2877
+ if (isAsyncPlan(input)) return input.attempt(...parameters);
2878
+ if (isAsyncGenerator(input)) return asyncAttempt(() => asyncGenerate(input, parameters));
2879
+ throw new Error(PLAN_MESSAGE_RUN_INPUT);
2880
+ }
2881
+ function attemptRun(input, ...parameters) {
2882
+ if (isPlan(input)) return input.attempt(...parameters);
2883
+ if (isGenerator(input)) return attempt(() => generate(input, parameters));
2884
+ throw new Error(PLAN_MESSAGE_RUN_INPUT);
2885
+ }
2886
+ function attemptRunAsyncPlan(...parameters) {
2887
+ return asyncAttempt(() => this.run(...parameters));
2880
2888
  }
2881
- function run(input) {
2882
- if (isPlan(input)) return input.run();
2883
- if (!isGenerator(input)) throw new Error(PLAN_MESSAGE_RUN_INPUT);
2884
- return execute(input);
2889
+ function attemptRunPlan(...parameters) {
2890
+ return attempt(() => this.run(...parameters));
2885
2891
  }
2886
- function runAsyncPlan() {
2887
- return asyncExecute(this[PLAN_SYMBOL].generator);
2892
+ attemptRun.async = asyncAttemptRun;
2893
+ //#endregion
2894
+ //#region src/plan/run.ts
2895
+ function asyncRun(input, ...parameters) {
2896
+ if (isAsyncPlan(input)) return input.run(...parameters);
2897
+ if (isAsyncGenerator(input)) return asyncGenerate(input, parameters);
2898
+ throw new Error(PLAN_MESSAGE_RUN_INPUT);
2899
+ }
2900
+ function run(input, ...parameters) {
2901
+ if (isPlan(input)) return input.run(...parameters);
2902
+ if (isGenerator(input)) return generate(input, parameters);
2903
+ throw new Error(PLAN_MESSAGE_RUN_INPUT);
2888
2904
  }
2889
- function runPlan() {
2890
- return execute(this[PLAN_SYMBOL].generator);
2905
+ function runAsyncPlan(...parameters) {
2906
+ return asyncGenerate(this[PLAN_SYMBOL].generator, parameters);
2891
2907
  }
2892
- plan.async = asyncPlan;
2908
+ function runPlan(...parameters) {
2909
+ return generate(this[PLAN_SYMBOL].generator, parameters);
2910
+ }
2911
+ asyncRun.attempt = asyncAttemptRun;
2893
2912
  run.async = asyncRun;
2913
+ run.attempt = attemptRun;
2914
+ //#endregion
2915
+ //#region src/plan/index.ts
2916
+ function AsyncPlan(generator) {
2917
+ this[PLAN_SYMBOL] = {
2918
+ generator,
2919
+ type: PLAN_TYPE_PLAN_ASYNC
2920
+ };
2921
+ }
2922
+ AsyncPlan.prototype[Symbol.asyncIterator] = function() {
2923
+ return this[PLAN_SYMBOL].generator();
2924
+ };
2925
+ AsyncPlan.prototype.attempt = attemptRunAsyncPlan;
2926
+ AsyncPlan.prototype.run = runAsyncPlan;
2927
+ function Plan(generator) {
2928
+ this[PLAN_SYMBOL] = {
2929
+ generator,
2930
+ type: PLAN_TYPE_PLAN_SYNC
2931
+ };
2932
+ }
2933
+ Plan.prototype[Symbol.iterator] = function() {
2934
+ return this[PLAN_SYMBOL].generator();
2935
+ };
2936
+ Plan.prototype.attempt = attemptRunPlan;
2937
+ Plan.prototype.run = runPlan;
2938
+ function plan(generator) {
2939
+ if (isAsyncGenerator(generator)) return new AsyncPlan(generator);
2940
+ if (isGenerator(generator)) return new Plan(generator);
2941
+ throw new Error(PLAN_MESSAGE_PLAN_INPUT);
2942
+ }
2894
2943
  //#endregion
2895
2944
  //#region src/internal/aborter.ts
2896
2945
  function createAborter(value, onAbort) {
@@ -6992,4 +7041,4 @@ var SizedSet = class extends Set {
6992
7041
  }
6993
7042
  };
6994
7043
  //#endregion
6995
- export { CancelablePromise, Logger, PromiseTimeoutError, QueueError, RetryError, SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, SizedMap, SizedSet, push as append, push, assert, assertCondition, assertDefined, assertInstanceOf, assertIs, assertProperty, assign, asyncAttempt, asyncDebounce, asyncFlow, asyncMatchResult, asyncOnce, asyncPipe, asyncPlan, asyncRun, asyncThrottle, attempt, attemptAsyncFlow, attemptAsyncPipe, attemptFlow, attemptPipe, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, copy, count, debounce, deburr, dedent, delay, deregisterCloner, deregisterComparator, deregisterEqualizer, diff, difference, drop, endsWith, endsWithArray, equal, error, exclude, exists, filter, filterAndMap, filterAndMap as select, find, findLast, first, firstOrDefault, flatFreeze, flatten, floor, flow, freeze, fromQuery, toPromise as fromResult, toPromise, fuzzy, fuzzyMatch, getArray, getArrayComparison, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getHwbColor, getHwbaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloatingNumber as getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getSortedIndex, getString, getTimedPromise, getUuid, getValue, groupArraysBy, groupBy, handleResult, hasValue, hasValueResult, herald, hexToHsl, hexToHsla, hexToHwb, hexToHwba, hexToRgb, hexToRgba, hslToHex, hslToHwb, hslToHwba, hslToRgb, hslToRgba, hwbToHex, hwbToHsl, hwbToHsla, hwbToRgb, hwbToRgba, ignoreKey, inMap, inSet, includes, includesArray, indexOf, indexOfArray, initializeAssigner, initializeCloner, initializeEqualizer, initializeMerger, initializeNormalizer, initializeSorter, initializeTemplater, initializeTransformer, insert, interpolate, intersection, isArrayOrPlainObject, isAsyncGenerator, isAsyncOnce, isAsyncPlan, isBeacon, isBeaconSubscription, isColor, isConstructor, isEmpty, isError, isFrozen, isFulfilled, isFuzzy, isGenerator, isHerald, isHeraldEvents, isHeraldSubscription, isHexColor, isHslColor, isHslaColor, isHwbColor, isHwbaColor, isInstanceOf, isKey, isKeyedQueue, isLogger, isNonArrayOrPlainObject, isNonConstructor, isNonEmpty, isNonInstanceOf, isNonKey, isNonNullable, isNonNullableOrEmpty, isNonNullableOrWhitespace, isNonNumber, isNonNumerical, isNonObject, isNonPlainObject, isNonPrimitive, isNonTemplateStringsArray, isNonTypedArray, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isObservable, isOk, isOnce, isPlainObject, isPlan, isPrimitive, isQueue, isQueueInstance, isRejected, isResult, isRgbColor, isRgbaColor, isSorted, isSubscription, isSubscriptions, isTemplateStringsArray, isTimeLogger, isTypedArray, join, kebabCase, keyedQueue, last, lastIndexOf, lastOrDefault, lowerCase, mapAndFilter, mapAndFilter as reverseSelect, matchResult, max, median, memoize, merge, min, move, moveIndices, moveToIndex, noop, normalize, ok, omit, once, parse, partition, pascalCase, pick, pipe, plan, promises, queue, range, registerCloner, registerComparator, registerEqualizer, resultPromises, retry, reverse, rgbToHex, rgbToHsl, rgbToHsla, rgbToHwb, rgbToHwba, round, run, setValue, settlePromise, shake, shuffle, single, slice, smush, snakeCase, sort, splice, startsWith, startsWithArray, subscriptions, sum, swap, take, template, throttle, timed, times, titleCase, toMap, toMapArrays, toQuery, toRecord, toRecordArrays, toResult, toSet, toggle, transform, trim, truncate, tryDecode, tryEncode, union, unique, unsmush, unwrap, update, upperCase, words };
7044
+ export { CancelablePromise, Logger, PromiseTimeoutError, QueueError, RetryError, SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, SizedMap, SizedSet, push as append, push, assert, assertCondition, assertDefined, assertInstanceOf, assertIs, assertProperty, assign, asyncAttempt, asyncAttemptRun, asyncDebounce, asyncFlow, asyncMatchResult, asyncOnce, asyncPipe, asyncRun, asyncThrottle, attempt, attemptAsyncFlow, attemptAsyncPipe, attemptFlow, attemptPipe, attemptPromise, attemptRun, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, copy, count, debounce, deburr, dedent, delay, deregisterCloner, deregisterComparator, deregisterEqualizer, diff, difference, drop, endsWith, endsWithArray, equal, error, exclude, exists, fail, filter, filterAndMap, filterAndMap as select, find, findLast, first, firstOrDefault, flatFreeze, flatten, floor, flow, freeze, fromQuery, toPromise as fromResult, toPromise, fuzzy, fuzzyMatch, getArray, getArrayComparison, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getHwbColor, getHwbaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloatingNumber as getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getSortedIndex, getString, getTimedPromise, getUuid, getValue, groupArraysBy, groupBy, handleResult, hasValue, hasValueResult, herald, hexToHsl, hexToHsla, hexToHwb, hexToHwba, hexToRgb, hexToRgba, hslToHex, hslToHwb, hslToHwba, hslToRgb, hslToRgba, hwbToHex, hwbToHsl, hwbToHsla, hwbToRgb, hwbToRgba, ignoreKey, inMap, inSet, includes, includesArray, indexOf, indexOfArray, initializeAssigner, initializeCloner, initializeEqualizer, initializeMerger, initializeNormalizer, initializeSorter, initializeTemplater, initializeTransformer, insert, interpolate, intersection, isArrayOrPlainObject, isAsyncGenerator, isAsyncOnce, isAsyncPlan, isBeacon, isBeaconSubscription, isColor, isConstructor, isEmpty, isError, isFrozen, isFulfilled, isFuzzy, isGenerator, isHerald, isHeraldEvents, isHeraldSubscription, isHexColor, isHslColor, isHslaColor, isHwbColor, isHwbaColor, isInstanceOf, isKey, isKeyedQueue, isLogger, isNonArrayOrPlainObject, isNonConstructor, isNonEmpty, isNonInstanceOf, isNonKey, isNonNullable, isNonNullableOrEmpty, isNonNullableOrWhitespace, isNonNumber, isNonNumerical, isNonObject, isNonPlainObject, isNonPrimitive, isNonTemplateStringsArray, isNonTypedArray, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isObservable, isOk, isOnce, isPlainObject, isPlan, isPrimitive, isQueue, isQueueInstance, isRejected, isResult, isRgbColor, isRgbaColor, isSorted, isSubscription, isSubscriptions, isTemplateStringsArray, isTimeLogger, isTypedArray, join, kebabCase, keyedQueue, last, lastIndexOf, lastOrDefault, lowerCase, mapAndFilter, mapAndFilter as reverseSelect, matchResult, max, median, memoize, merge, min, move, moveIndices, moveToIndex, noop, normalize, ok, omit, once, parse, partition, pascalCase, pick, pipe, plan, promises, queue, range, registerCloner, registerComparator, registerEqualizer, resultPromises, retry, reverse, rgbToHex, rgbToHsl, rgbToHsla, rgbToHwb, rgbToHwba, round, run, setValue, settlePromise, shake, shuffle, single, slice, smush, snakeCase, sort, splice, startsWith, startsWithArray, subscriptions, succeed, sum, swap, take, template, throttle, timed, times, titleCase, toMap, toMapArrays, toQuery, toRecord, toRecordArrays, toResult, toSet, toggle, transform, trim, truncate, tryDecode, tryEncode, union, unique, unsmush, unwrap, update, upperCase, words };
@@ -2,8 +2,8 @@ import { GenericCallback } from "../models.mjs";
2
2
  //#region src/internal/aborter.d.ts
3
3
  export type Aborter = {
4
4
  callback: GenericCallback;
5
+ cancel(): void;
5
6
  signal: AbortSignal;
6
- cancel: () => void;
7
7
  };
8
8
  export declare function createAborter(value: unknown, onAbort: () => void): Aborter | undefined;
9
9
  //#endregion
@@ -1,55 +1,76 @@
1
- import { Result } from "./result.model.mjs";
1
+ import { Err, Ok, Result } from "./result.model.mjs";
2
2
  //#region src/internal/models/plan.model.d.ts
3
3
  /**
4
4
  * An asynchronous plan of execution that can yield intermediate results and eventually return a result
5
5
  */
6
- export type AsyncPlan<Value, Error = unknown> = {
6
+ export type AsyncPlan<Yielded, Returned, Parameters extends unknown[]> = {
7
7
  /**
8
8
  * Get the asynchronous generator for the plan
9
9
  *
10
10
  * @returns Asynchronous generator
11
11
  */
12
- [Symbol.asyncIterator]: () => AsyncGenerator<unknown, Value, unknown>;
12
+ [Symbol.asyncIterator]: () => AsyncGenerator<Yielded, Returned, unknown>;
13
+ /**
14
+ * Attempt to run the plan to completion
15
+ *
16
+ * _Returns a promised {@link Result} instead of a raw value or throwing an error_
17
+ *
18
+ * @returns Promised result
19
+ */
20
+ attempt(...parameters: Parameters): Promise<Result<PlanResult<Returned>, PlanError<Yielded, Returned>>>;
13
21
  /**
14
22
  * Run the plan to completion
15
23
  *
24
+ * @throws {PlanError<Yielded, Returned>}
16
25
  * @returns Result
17
26
  */
18
- run: () => Promise<Result<Value, Error>>;
27
+ run(...parameters: Parameters): Promise<PlanResult<Returned>>;
19
28
  };
20
29
  type AsyncPlanState = {
21
- generator: () => AsyncGenerator;
30
+ generator(...parameters: unknown[]): AsyncGenerator;
22
31
  } & BaseState<typeof PLAN_TYPE_PLAN_ASYNC>;
23
32
  type BaseState<Type extends PlanType> = {
24
33
  type: Type;
25
34
  };
26
35
  export type InternalAsyncPlan = {
27
36
  [PLAN_SYMBOL]: AsyncPlanState;
28
- } & AsyncPlan<never, never>;
37
+ } & AsyncPlan<never, never, unknown[]>;
29
38
  export type InternalPlan = {
30
39
  [PLAN_SYMBOL]: PlanState;
31
- } & Plan<never, never>;
40
+ } & Plan<never, never, unknown[]>;
32
41
  /**
33
42
  * A plan of execution that can yield intermediate results and eventually return a result
34
43
  */
35
- export type Plan<Value, Error = unknown> = {
44
+ export type Plan<Yielded, Returned, Parameters extends unknown[]> = {
36
45
  /**
37
46
  * Get the generator for the plan
38
47
  *
39
48
  * @returns Generator
40
49
  */
41
- [Symbol.iterator]: () => Generator<unknown, Value, unknown>;
50
+ [Symbol.iterator]: () => Generator<Yielded, Returned, unknown>;
51
+ /**
52
+ * Attempt to run the plan to completion
53
+ *
54
+ * _Returns a {@link Result} instead of a raw value or throwing an error_
55
+ *
56
+ * @returns Result
57
+ */
58
+ attempt(...parameters: Parameters): Result<PlanResult<Returned>, PlanError<Yielded, Returned>>;
42
59
  /**
43
60
  * Run the plan to completion
44
61
  *
62
+ * @throws {PlanError<Yielded, Returned>}
45
63
  * @returns Result
46
64
  */
47
- run: () => Result<Value, Error>;
65
+ run(...parameters: Parameters): PlanResult<Returned>;
48
66
  };
67
+ type PlanErrorValues<Original> = Original extends Error ? Original : Original extends Err<infer Error> ? Error : Original extends Result<infer _, infer Error> ? Error : never;
68
+ export type PlanError<Yielded, Returned> = PlanErrorValues<Yielded> | PlanErrorValues<Returned> | Error;
69
+ export type PlanResult<Returned> = Returned extends Error ? never : Returned extends Err<infer _> ? never : Returned extends Ok<infer Value> ? Value : Returned extends Result<infer Value, infer _> ? Value : Returned;
49
70
  type PlanState = {
50
- generator: () => Generator;
71
+ generator(...parameters: unknown[]): Generator;
51
72
  } & BaseState<typeof PLAN_TYPE_PLAN_SYNC>;
52
- export type PlanType = "asyncPlan" | "plan";
73
+ export type PlanType = "asyncPlan" | "plan" | "stop";
53
74
  export declare const GENERATOR_NAME_ASYNC = "AsyncGeneratorFunction";
54
75
  export declare const GENERATOR_NAME_SYNC = "GeneratorFunction";
55
76
  export declare const PLAN_MESSAGE_PLAN_INPUT = "plan requires a generator function";
@@ -43,14 +43,14 @@ export type ResultMatch<Value, Returned, E = Error> = {
43
43
  * @param original Original error, if available
44
44
  * @returns Value to return
45
45
  */
46
- error: (error: E, original?: Error) => Returned;
46
+ error(error: E, original?: Error): Returned;
47
47
  /**
48
48
  * Callback for ok result
49
49
  *
50
50
  * @param value Ok value
51
51
  * @returns Value to return
52
52
  */
53
- ok: (value: Value) => Returned;
53
+ ok(value: Value): Returned;
54
54
  };
55
55
  /**
56
56
  * Unwrap a result value
@@ -0,0 +1,45 @@
1
+ import { AsyncPlan, Plan } from "./models/plan.model.mjs";
2
+ //#region src/internal/plan.d.ts
3
+ /**
4
+ * Create an immediately failing generator
5
+ *
6
+ * @param reason Failure reason
7
+ * @returns Failing generator
8
+ */
9
+ export declare function fail<Reason>(reason: Reason): Generator<unknown, never>;
10
+ /**
11
+ * Is the value an asynchronous generator function?
12
+ *
13
+ * @param value Value to check
14
+ * @returns `true` if the value is an asynchronous generator function, otherwise `false`
15
+ */
16
+ export declare function isAsyncGenerator(value: unknown): value is () => AsyncGenerator;
17
+ /**
18
+ * Is the value an asynchronous plan?
19
+ *
20
+ * @param value Value to check
21
+ * @returns `true` if the value is an asynchronous plan, otherwise `false`
22
+ */
23
+ export declare function isAsyncPlan<Yielded = unknown, Returned = unknown, Parameters extends unknown[] = unknown[]>(value: unknown): value is AsyncPlan<Yielded, Returned, Parameters>;
24
+ /**
25
+ * Is the value a generator function?
26
+ *
27
+ * @param value Value to check
28
+ * @returns `true` if the value is a generator function, otherwise `false`
29
+ */
30
+ export declare function isGenerator(value: unknown): value is () => Generator;
31
+ /**
32
+ * Is the value a plan?
33
+ *
34
+ * @param value Value to check
35
+ * @returns `true` if the value is a plan, otherwise `false`
36
+ */
37
+ export declare function isPlan<Yielded = unknown, Returned = unknown, Parameters extends unknown[] = unknown[]>(value: unknown): value is Plan<Yielded, Returned, Parameters>;
38
+ /**
39
+ * Create an immediately succeeding generator
40
+ *
41
+ * @param value Success value
42
+ * @returns Succeeding generator
43
+ */
44
+ export declare function succeed<Value>(value: Value): Generator<unknown, Value>;
45
+ //#endregion
@@ -0,0 +1,70 @@
1
+ import { GENERATOR_NAME_ASYNC, GENERATOR_NAME_SYNC, PLAN_SYMBOL, PLAN_TYPE_PLAN_ASYNC, PLAN_TYPE_PLAN_SYNC } from "./models/plan.model.mjs";
2
+ //#region src/internal/plan.ts
3
+ /**
4
+ * Create an immediately failing generator
5
+ *
6
+ * @param reason Failure reason
7
+ * @returns Failing generator
8
+ */
9
+ function fail(reason) {
10
+ return (function* () {
11
+ yield;
12
+ throw reason;
13
+ })();
14
+ }
15
+ /**
16
+ * Is the value an asynchronous generator function?
17
+ *
18
+ * @param value Value to check
19
+ * @returns `true` if the value is an asynchronous generator function, otherwise `false`
20
+ */
21
+ function isAsyncGenerator(value) {
22
+ return isGeneratorInstance(GENERATOR_NAME_ASYNC, value);
23
+ }
24
+ /**
25
+ * Is the value an asynchronous plan?
26
+ *
27
+ * @param value Value to check
28
+ * @returns `true` if the value is an asynchronous plan, otherwise `false`
29
+ */
30
+ function isAsyncPlan(value) {
31
+ return isPlanInstance(PLAN_TYPE_PLAN_ASYNC, value);
32
+ }
33
+ /**
34
+ * Is the value a generator function?
35
+ *
36
+ * @param value Value to check
37
+ * @returns `true` if the value is a generator function, otherwise `false`
38
+ */
39
+ function isGenerator(value) {
40
+ return isGeneratorInstance(GENERATOR_NAME_SYNC, value);
41
+ }
42
+ function isGeneratorInstance(name, value) {
43
+ 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";
44
+ }
45
+ /**
46
+ * Is the value a plan?
47
+ *
48
+ * @param value Value to check
49
+ * @returns `true` if the value is a plan, otherwise `false`
50
+ */
51
+ function isPlan(value) {
52
+ return isPlanInstance(PLAN_TYPE_PLAN_SYNC, value);
53
+ }
54
+ function isPlanInstance(type, value) {
55
+ return typeof value === "object" && value !== null && PLAN_SYMBOL in value && value[PLAN_SYMBOL].type === type;
56
+ }
57
+ /**
58
+ * Create an immediately succeeding generator
59
+ *
60
+ * @param value Success value
61
+ * @returns Succeeding generator
62
+ */
63
+ function succeed(value) {
64
+ return (function* () {
65
+ yield;
66
+ return value;
67
+ })();
68
+ }
69
+ //#endregion
70
+ export { fail, isAsyncGenerator, isAsyncPlan, isGenerator, isPlan, succeed };
@@ -65,14 +65,14 @@ export type Subscriptions<Value = unknown> = {
65
65
  /**
66
66
  * Clear all subscriptions
67
67
  */
68
- clear: () => void;
68
+ clear(): void;
69
69
  /**
70
70
  * Create _(or retrieve)_ a subscription
71
71
  *
72
72
  * @param parameters Subscription parameters
73
73
  * @returns Tuple holding subsccription and existing boolean
74
74
  */
75
- create: (parameters: SubscriptionParameters) => [Subscription, boolean];
75
+ create(parameters: SubscriptionParameters): [Subscription, boolean];
76
76
  };
77
77
  /**
78
78
  * Parameters for creating a subscription store
@@ -4,9 +4,9 @@ type BaseHandler = {
4
4
  handlers: WeakMap<WeakKey, string | GenericCallback>;
5
5
  options: BaseHandlerOptions;
6
6
  owner: GenericCallback;
7
- deregister: (constructor: Constructor) => void;
8
- get: (first: unknown, second: unknown) => string | GenericCallback | undefined;
9
- register: (constructor: Constructor, handler?: string | GenericCallback) => void;
7
+ deregister(constructor: Constructor): void;
8
+ get(first: unknown, second: unknown): string | GenericCallback | undefined;
9
+ register(constructor: Constructor, handler?: string | GenericCallback): void;
10
10
  };
11
11
  type BaseHandlerOptions = {
12
12
  callback: GenericCallback;
@@ -21,7 +21,7 @@ function setValue(data, path, value, ignoreCase) {
21
21
  let next = unwrap(handleValue(target, currentPath, null, true, shouldIgnoreCase), void 0);
22
22
  if (typeof next !== "object" || next === null) {
23
23
  const nextPath = paths[index + 1];
24
- if (SET_EXPRESSION_INDEX.test(nextPath)) next = Array.from({ length: Number(nextPath) + 1 }, () => void 0);
24
+ if (SET_EXPRESSION_INDEX.test(nextPath)) next = Array.from({ length: +nextPath + 1 }, () => void 0);
25
25
  else next = {};
26
26
  target[currentPath] = next;
27
27
  }
package/dist/models.d.mts CHANGED
@@ -90,7 +90,7 @@ export type OnceAsync<Callback extends GenericAsyncCallback> = {
90
90
  * @param parameters Callback parameters
91
91
  * @returns Call result
92
92
  */
93
- run: (...parameters: Parameters<Callback>) => ReturnType<Callback>;
93
+ run(...parameters: Parameters<Callback>): ReturnType<Callback>;
94
94
  } & OnceProperties;
95
95
  /**
96
96
  * A callback function that can only be called once, returning the same value on subsequent calls
@@ -102,7 +102,7 @@ export type Once<Callback extends GenericCallback> = {
102
102
  * @param parameters Callback parameters
103
103
  * @returns Call result
104
104
  */
105
- run: (...parameters: Parameters<Callback>) => ReturnType<Callback>;
105
+ run(...parameters: Parameters<Callback>): ReturnType<Callback>;
106
106
  } & OnceProperties;
107
107
  type OnceProperties = {
108
108
  /**
@@ -116,7 +116,7 @@ type OnceProperties = {
116
116
  /**
117
117
  * Clear the callback's cached value
118
118
  */
119
- clear: () => void;
119
+ clear(): void;
120
120
  };
121
121
  /**
122
122
  * A generic object
@@ -0,0 +1,4 @@
1
+ //#region src/plan/generate.d.ts
2
+ export declare function asyncGenerate(callback: (...parameters: unknown[]) => AsyncGenerator, parameters: unknown[]): Promise<unknown>;
3
+ export declare function generate(callback: (...parameters: unknown[]) => Generator, parameters: unknown[]): unknown;
4
+ //#endregion
@@ -0,0 +1,50 @@
1
+ import { isError } from "../internal/result/misc.mjs";
2
+ //#region src/plan/generate.ts
3
+ async function asyncGenerate(callback, parameters) {
4
+ const generator = callback(...parameters);
5
+ let success = true;
6
+ let lastValue;
7
+ try {
8
+ while (true) {
9
+ const next = await generator.next(lastValue);
10
+ const { done } = next;
11
+ let { value } = next;
12
+ if (value instanceof Error) throw value;
13
+ if (isError(value)) throw value.error;
14
+ lastValue = value;
15
+ if (done === true) break;
16
+ }
17
+ } catch (error) {
18
+ lastValue = error;
19
+ success = false;
20
+ } finally {
21
+ await generator?.return(lastValue);
22
+ }
23
+ if (success) return lastValue;
24
+ throw lastValue;
25
+ }
26
+ function generate(callback, parameters) {
27
+ const generator = callback(...parameters);
28
+ let success = true;
29
+ let lastValue;
30
+ try {
31
+ while (true) {
32
+ const next = generator.next(lastValue);
33
+ const done = next.done === true;
34
+ const { value } = next;
35
+ if (value instanceof Error) throw value;
36
+ if (isError(value)) throw value.error;
37
+ lastValue = value;
38
+ if (done) break;
39
+ }
40
+ } catch (error) {
41
+ lastValue = error;
42
+ success = false;
43
+ } finally {
44
+ generator.return(lastValue);
45
+ }
46
+ if (success) return lastValue;
47
+ throw lastValue;
48
+ }
49
+ //#endregion
50
+ export { asyncGenerate, generate };