@oscarpalmer/atoms 0.193.1 → 0.195.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 (121) hide show
  1. package/dist/array/difference.mjs +2 -2
  2. package/dist/array/index.d.mts +2 -2
  3. package/dist/array/index.mjs +2 -2
  4. package/dist/array/intersection.mjs +2 -2
  5. package/dist/array/move.mjs +1 -1
  6. package/dist/array/slice.mjs +5 -5
  7. package/dist/array/sort.d.mts +5 -1
  8. package/dist/array/sort.mjs +4 -2
  9. package/dist/array/swap.mjs +1 -1
  10. package/dist/array/union.mjs +2 -2
  11. package/dist/beacon.d.mts +8 -1
  12. package/dist/beacon.mjs +52 -60
  13. package/dist/color/constants.d.mts +1 -1
  14. package/dist/color/constants.mjs +3 -5
  15. package/dist/color/instance.mjs +34 -41
  16. package/dist/color/misc/format.d.mts +3 -2
  17. package/dist/color/misc/format.mjs +4 -1
  18. package/dist/color/misc/is.d.mts +9 -1
  19. package/dist/color/misc/is.mjs +11 -1
  20. package/dist/color/misc/state.d.mts +6 -6
  21. package/dist/color/misc/state.mjs +11 -14
  22. package/dist/color/models.d.mts +23 -68
  23. package/dist/color/space/hex.mjs +1 -1
  24. package/dist/color/space/rgb.mjs +1 -1
  25. package/dist/function/index.d.mts +1 -2
  26. package/dist/function/index.mjs +1 -2
  27. package/dist/function/limit.d.mts +0 -6
  28. package/dist/function/limit.mjs +2 -2
  29. package/dist/function/once.d.mts +0 -3
  30. package/dist/function/once.mjs +17 -14
  31. package/dist/function/retry.d.mts +1 -24
  32. package/dist/function/retry.mjs +1 -1
  33. package/dist/herald.d.mts +2 -72
  34. package/dist/herald.mjs +2 -92
  35. package/dist/index.d.mts +3332 -3418
  36. package/dist/index.mjs +1850 -1870
  37. package/dist/internal/abort.d.mts +10 -0
  38. package/dist/internal/abort.mjs +14 -0
  39. package/dist/internal/array/chunk.mjs +2 -2
  40. package/dist/internal/array/find.mjs +2 -2
  41. package/dist/{array → internal/array}/get.d.mts +2 -2
  42. package/dist/{array → internal/array}/get.mjs +2 -2
  43. package/dist/{array → internal/array}/match.d.mts +2 -2
  44. package/dist/{array → internal/array}/match.mjs +15 -15
  45. package/dist/internal/array/sets.d.mts +4 -4
  46. package/dist/internal/array/sets.mjs +7 -7
  47. package/dist/internal/defaults.d.mts +5 -0
  48. package/dist/internal/defaults.mjs +9 -0
  49. package/dist/{function → internal/function}/assert.d.mts +2 -2
  50. package/dist/{function → internal/function}/assert.mjs +4 -4
  51. package/dist/{function → internal/function}/memoize.d.mts +2 -2
  52. package/dist/{function → internal/function}/memoize.mjs +22 -35
  53. package/dist/internal/function/timer.mjs +19 -15
  54. package/dist/{function → internal/function}/work.d.mts +2 -2
  55. package/dist/{function → internal/function}/work.mjs +15 -13
  56. package/dist/internal/herald.d.mts +71 -0
  57. package/dist/internal/herald.mjs +110 -0
  58. package/dist/internal/number.d.mts +1 -2
  59. package/dist/internal/number.mjs +8 -11
  60. package/dist/internal/{result.d.mts → result/misc.d.mts} +42 -3
  61. package/dist/internal/result/misc.mjs +51 -0
  62. package/dist/{result → internal/result}/models.d.mts +2 -2
  63. package/dist/{sized → internal/sized}/map.d.mts +1 -1
  64. package/dist/{sized → internal/sized}/map.mjs +2 -2
  65. package/dist/internal/{sized.d.mts → sized/misc.d.mts} +1 -1
  66. package/dist/internal/sized/misc.mjs +12 -0
  67. package/dist/{string → internal/string}/case.d.mts +1 -1
  68. package/dist/{string → internal/string}/case.mjs +22 -22
  69. package/dist/internal/{string.d.mts → string/misc.d.mts} +1 -1
  70. package/dist/internal/{string.mjs → string/misc.mjs} +5 -5
  71. package/dist/internal/subscription.d.mts +24 -8
  72. package/dist/internal/subscription.mjs +49 -36
  73. package/dist/internal/value/compare.mjs +1 -1
  74. package/dist/internal/value/equal.mjs +6 -6
  75. package/dist/internal/value/has.d.mts +1 -1
  76. package/dist/internal/value/misc.d.mts +1 -1
  77. package/dist/internal/value/misc.mjs +16 -16
  78. package/dist/internal/value/set.mjs +2 -2
  79. package/dist/is.mjs +2 -2
  80. package/dist/logger.mjs +15 -39
  81. package/dist/promise/constants.d.mts +17 -0
  82. package/dist/promise/constants.mjs +14 -0
  83. package/dist/promise/delay.mjs +7 -10
  84. package/dist/promise/helpers.d.mts +1 -1
  85. package/dist/promise/helpers.mjs +3 -3
  86. package/dist/promise/index.d.mts +1 -1
  87. package/dist/promise/index.mjs +14 -17
  88. package/dist/promise/misc.d.mts +3 -2
  89. package/dist/promise/misc.mjs +10 -8
  90. package/dist/promise/models.d.mts +5 -17
  91. package/dist/promise/models.mjs +4 -13
  92. package/dist/promise/timed.mjs +9 -10
  93. package/dist/query.mjs +16 -16
  94. package/dist/queue.mjs +104 -120
  95. package/dist/random.mjs +8 -8
  96. package/dist/result/index.d.mts +1 -4
  97. package/dist/result/index.mjs +2 -2
  98. package/dist/result/match.d.mts +1 -4
  99. package/dist/result/match.mjs +5 -5
  100. package/dist/result/misc.d.mts +3 -42
  101. package/dist/result/misc.mjs +4 -30
  102. package/dist/result/work/flow.d.mts +1 -4
  103. package/dist/result/work/flow.mjs +6 -6
  104. package/dist/result/work/pipe.d.mts +1 -4
  105. package/dist/result/work/pipe.mjs +5 -5
  106. package/dist/sized/set.mjs +1 -1
  107. package/dist/string/fuzzy.mjs +25 -20
  108. package/dist/string/index.d.mts +1 -1
  109. package/dist/string/index.mjs +8 -8
  110. package/dist/string/match.mjs +1 -1
  111. package/dist/string/normalize.mjs +16 -16
  112. package/dist/string/template.mjs +3 -3
  113. package/dist/subscription.d.mts +2 -0
  114. package/dist/subscription.mjs +2 -0
  115. package/dist/value/clone.mjs +10 -10
  116. package/dist/value/diff.mjs +1 -1
  117. package/dist/value/smush.mjs +3 -3
  118. package/package.json +35 -14
  119. package/dist/internal/result.mjs +0 -25
  120. package/dist/internal/sized.mjs +0 -12
  121. /package/dist/{result → internal/result}/models.mjs +0 -0
@@ -0,0 +1,10 @@
1
+ import { GenericCallback } from "../models.mjs";
2
+ //#region src/internal/abort.d.ts
3
+ type Aborter = {
4
+ callback: GenericCallback;
5
+ signal: AbortSignal;
6
+ cancel: () => void;
7
+ };
8
+ declare function createAborter(value: unknown, onAbort: () => void): Aborter | undefined;
9
+ //#endregion
10
+ export { Aborter, createAborter };
@@ -0,0 +1,14 @@
1
+ //#region src/internal/abort.ts
2
+ function createAborter(value, onAbort) {
3
+ if (!(value instanceof AbortSignal)) return;
4
+ value.addEventListener(ABORT_EVENT, onAbort, ABORT_OPTIONS);
5
+ return {
6
+ callback: onAbort,
7
+ signal: value,
8
+ cancel: () => value.removeEventListener(ABORT_EVENT, onAbort)
9
+ };
10
+ }
11
+ const ABORT_EVENT = "abort";
12
+ const ABORT_OPTIONS = { once: true };
13
+ //#endregion
14
+ export { createAborter };
@@ -15,7 +15,7 @@ function chunk(array, size) {
15
15
  if (!Array.isArray(array)) return [];
16
16
  if (array.length === 0) return [];
17
17
  const { length } = array;
18
- const actualSize = typeof size === "number" && size > 0 && size <= MAX_SIZE ? size : MAX_SIZE;
18
+ const actualSize = typeof size === "number" && size > 0 && size <= CHUNK_MAX_SIZE ? size : CHUNK_MAX_SIZE;
19
19
  if (length <= actualSize) return [array];
20
20
  const chunks = [];
21
21
  let index = 0;
@@ -25,6 +25,6 @@ function chunk(array, size) {
25
25
  }
26
26
  return chunks;
27
27
  }
28
- const MAX_SIZE = 5e3;
28
+ const CHUNK_MAX_SIZE = 5e3;
29
29
  //#endregion
30
30
  export { chunk };
@@ -40,7 +40,7 @@ function findValues(type, array, parameters, mapper) {
40
40
  const { length } = array;
41
41
  const { bool, key, value } = getFindParameters(parameters);
42
42
  const callbacks = getArrayCallbacks(bool, key);
43
- if (type === "unique" && callbacks?.keyed == null && length >= UNIQUE_THRESHOLD) {
43
+ if (type === "unique" && callbacks?.keyed == null && length >= FIND_UNIQUE_THRESHOLD) {
44
44
  result.matched = [...new Set(array)];
45
45
  return result;
46
46
  }
@@ -77,6 +77,6 @@ const FIND_VALUE_INDEX = "index";
77
77
  const FIND_VALUE_ITEM = "item";
78
78
  const FIND_VALUES_ALL = "all";
79
79
  const FIND_VALUES_UNIQUE = "unique";
80
- const UNIQUE_THRESHOLD = 100;
80
+ const FIND_UNIQUE_THRESHOLD = 100;
81
81
  //#endregion
82
82
  export { FIND_VALUES_ALL, FIND_VALUES_UNIQUE, FIND_VALUE_INDEX, FIND_VALUE_ITEM, findAbsoluteValueOrDefault, findValue, findValues, getFindParameters };
@@ -1,5 +1,5 @@
1
- import { NumericalKeys, PlainObject } from "../models.mjs";
2
- //#region src/array/get.d.ts
1
+ import { NumericalKeys, PlainObject } from "../../models.mjs";
2
+ //#region src/internal/array/get.d.ts
3
3
  /**
4
4
  * Get an array from an object, where only values with numerical keys will be included
5
5
  *
@@ -1,5 +1,5 @@
1
- import { isNonPlainObject } from "../internal/is.mjs";
2
- //#region src/array/get.ts
1
+ import { isNonPlainObject } from "../is.mjs";
2
+ //#region src/internal/array/get.ts
3
3
  function getArray(value, indiced) {
4
4
  if (Array.isArray(value)) return value;
5
5
  if (value instanceof Map) return [...value.entries()];
@@ -1,5 +1,5 @@
1
- import { PlainObject } from "../models.mjs";
2
- //#region src/array/match.d.ts
1
+ import { PlainObject } from "../../models.mjs";
2
+ //#region src/internal/array/match.d.ts
3
3
  /**
4
4
  * Comparison of an array within another array
5
5
  */
@@ -1,5 +1,5 @@
1
- import { getArrayCallback } from "../internal/array/callbacks.mjs";
2
- //#region src/array/match.ts
1
+ import { getArrayCallback } from "./callbacks.mjs";
2
+ //#region src/internal/array/match.ts
3
3
  function endsWithArray(haystack, needle, key) {
4
4
  return endings.has(getPosition(haystack, needle, key)[1]);
5
5
  }
@@ -7,8 +7,8 @@ function getArrayComparison(haystack, needle, key) {
7
7
  return getPosition(haystack, needle, key)[1];
8
8
  }
9
9
  function getName(start, haystack, needle) {
10
- if (start === 0) return haystack === needle ? COMPARISON_SAME : COMPARISON_START;
11
- return start + needle === haystack ? COMPARISON_END : COMPARISON_INSIDE;
10
+ if (start === 0) return haystack === needle ? MATCH_COMPARISON_SAME : MATCH_COMPARISON_START;
11
+ return start + needle === haystack ? MATCH_COMPARISON_END : MATCH_COMPARISON_INSIDE;
12
12
  }
13
13
  function getPosition(haystack, needle, key) {
14
14
  if (!Array.isArray(haystack) || !Array.isArray(needle)) return invalid;
@@ -43,16 +43,16 @@ function indexOfArray(haystack, needle, key) {
43
43
  function startsWithArray(haystack, needle, key) {
44
44
  return starts.has(getPosition(haystack, needle, key)[1]);
45
45
  }
46
- const COMPARISON_END = "end";
47
- const COMPARISON_INSIDE = "inside";
48
- const COMPARISON_INVALID = "invalid";
49
- const COMPARISON_OUTSIDE = "outside";
50
- const COMPARISON_SAME = "same";
51
- const COMPARISON_START = "start";
52
- const endings = /* @__PURE__ */ new Set([COMPARISON_END, COMPARISON_SAME]);
53
- const invalid = [-1, COMPARISON_INVALID];
54
- const outside = [-1, COMPARISON_OUTSIDE];
55
- const outsides = /* @__PURE__ */ new Set([COMPARISON_INVALID, COMPARISON_OUTSIDE]);
56
- const starts = /* @__PURE__ */ new Set([COMPARISON_START, COMPARISON_SAME]);
46
+ const MATCH_COMPARISON_END = "end";
47
+ const MATCH_COMPARISON_INSIDE = "inside";
48
+ const MATCH_COMPARISON_INVALID = "invalid";
49
+ const MATCH_COMPARISON_OUTSIDE = "outside";
50
+ const MATCH_COMPARISON_SAME = "same";
51
+ const MATCH_COMPARISON_START = "start";
52
+ const endings = /* @__PURE__ */ new Set([MATCH_COMPARISON_END, MATCH_COMPARISON_SAME]);
53
+ const invalid = [-1, MATCH_COMPARISON_INVALID];
54
+ const outside = [-1, MATCH_COMPARISON_OUTSIDE];
55
+ const outsides = /* @__PURE__ */ new Set([MATCH_COMPARISON_INVALID, MATCH_COMPARISON_OUTSIDE]);
56
+ const starts = /* @__PURE__ */ new Set([MATCH_COMPARISON_START, MATCH_COMPARISON_SAME]);
57
57
  //#endregion
58
58
  export { endsWithArray, getArrayComparison, includesArray, indexOfArray, startsWithArray };
@@ -1,8 +1,8 @@
1
1
  //#region src/internal/array/sets.d.ts
2
2
  type CompareSetsType = 'difference' | 'intersection' | 'union';
3
3
  declare function compareSets(type: CompareSetsType, first: unknown[], second: unknown[], key?: unknown): unknown[];
4
- declare const COMPARE_SETS_DIFFERENCE: CompareSetsType;
5
- declare const COMPARE_SETS_INTERSECTION: CompareSetsType;
6
- declare const COMPARE_SETS_UNION: CompareSetsType;
4
+ declare const SETS_COMPARE_DIFFERENCE: CompareSetsType;
5
+ declare const SETS_COMPARE_INTERSECTION: CompareSetsType;
6
+ declare const SETS_COMPARE_UNION: CompareSetsType;
7
7
  //#endregion
8
- export { COMPARE_SETS_DIFFERENCE, COMPARE_SETS_INTERSECTION, COMPARE_SETS_UNION, compareSets };
8
+ export { SETS_COMPARE_DIFFERENCE, SETS_COMPARE_INTERSECTION, SETS_COMPARE_UNION, compareSets };
@@ -2,9 +2,9 @@ import { getArrayCallback } from "./callbacks.mjs";
2
2
  //#region src/internal/array/sets.ts
3
3
  function compareSets(type, first, second, key) {
4
4
  if (!Array.isArray(first)) return [];
5
- const isDifference = type === COMPARE_SETS_DIFFERENCE;
6
- const isIntersection = type === COMPARE_SETS_INTERSECTION;
7
- const isUnion = type === COMPARE_SETS_UNION;
5
+ const isDifference = type === SETS_COMPARE_DIFFERENCE;
6
+ const isIntersection = type === SETS_COMPARE_INTERSECTION;
7
+ const isUnion = type === SETS_COMPARE_UNION;
8
8
  if (first.length === 0) return isDifference ? first.slice() : isIntersection ? [] : second.slice();
9
9
  if (!Array.isArray(second) || second.length === 0) return isIntersection ? [] : first.slice();
10
10
  const callback = getArrayCallback(key);
@@ -25,8 +25,8 @@ function compareSets(type, first, second, key) {
25
25
  }
26
26
  return result;
27
27
  }
28
- const COMPARE_SETS_DIFFERENCE = "difference";
29
- const COMPARE_SETS_INTERSECTION = "intersection";
30
- const COMPARE_SETS_UNION = "union";
28
+ const SETS_COMPARE_DIFFERENCE = "difference";
29
+ const SETS_COMPARE_INTERSECTION = "intersection";
30
+ const SETS_COMPARE_UNION = "union";
31
31
  //#endregion
32
- export { COMPARE_SETS_DIFFERENCE, COMPARE_SETS_INTERSECTION, COMPARE_SETS_UNION, compareSets };
32
+ export { SETS_COMPARE_DIFFERENCE, SETS_COMPARE_INTERSECTION, SETS_COMPARE_UNION, compareSets };
@@ -0,0 +1,5 @@
1
+ //#region src/internal/defaults.d.ts
2
+ declare function getBooleanOrDefault(value: unknown, defaultValue: boolean): boolean;
3
+ declare function getNumberOrDefault(value: unknown, defaultValue: number, minimum?: number): number;
4
+ //#endregion
5
+ export { getBooleanOrDefault, getNumberOrDefault };
@@ -0,0 +1,9 @@
1
+ //#region src/internal/defaults.ts
2
+ function getBooleanOrDefault(value, defaultValue) {
3
+ return typeof value === "boolean" ? value : defaultValue;
4
+ }
5
+ function getNumberOrDefault(value, defaultValue, minimum) {
6
+ return typeof value === "number" && !Number.isNaN(value) && value >= (minimum ?? 0) ? value : defaultValue;
7
+ }
8
+ //#endregion
9
+ export { getBooleanOrDefault, getNumberOrDefault };
@@ -1,5 +1,5 @@
1
- import { Constructor, NestedKeys, NestedValue, PlainObject } from "../models.mjs";
2
- //#region src/function/assert.d.ts
1
+ import { Constructor, NestedKeys, NestedValue, PlainObject } from "../../models.mjs";
2
+ //#region src/internal/function/assert.d.ts
3
3
  /**
4
4
  * Asserter for a property of a value
5
5
  */
@@ -1,5 +1,5 @@
1
- import { hasValueResult } from "../internal/value/has.mjs";
2
- //#region src/function/assert.ts
1
+ import { hasValueResult } from "../value/has.mjs";
2
+ //#region src/internal/function/assert.ts
3
3
  /**
4
4
  * Asserts that a condition is true, throwing an error if it is not
5
5
  *
@@ -40,7 +40,7 @@ function assertCondition(condition, message, error) {
40
40
  * @param error Error constructor _(defaults to `Error`)_
41
41
  */
42
42
  function assertDefined(value, message, error) {
43
- assert(() => value != null, message ?? MESSAGE_VALUE_DEFINED, error);
43
+ assert(() => value != null, message ?? ASSERT_MESSAGE_VALUE_DEFINED, error);
44
44
  }
45
45
  /**
46
46
  * Creates an _Asserter_ that asserts a value is an instance of a constructor, throwing an error if it is not
@@ -91,6 +91,6 @@ function assertProperty(path, condition, message, error) {
91
91
  }, message, error);
92
92
  };
93
93
  }
94
- const MESSAGE_VALUE_DEFINED = "Expected value to be defined";
94
+ const ASSERT_MESSAGE_VALUE_DEFINED = "Expected value to be defined";
95
95
  //#endregion
96
96
  export { assert, assertCondition, assertDefined, assertInstanceOf, assertIs, assertProperty };
@@ -1,5 +1,5 @@
1
- import { GenericCallback } from "../models.mjs";
2
- //#region src/function/memoize.d.ts
1
+ import { GenericCallback } from "../../models.mjs";
2
+ //#region src/internal/function/memoize.d.ts
3
3
  /**
4
4
  * A _Memoized_ function instance, caching and retrieving results based on the its parameters _(or a custom cache key)_
5
5
  */
@@ -1,12 +1,12 @@
1
- import { isPlainObject } from "../internal/is.mjs";
2
- import { getString, join } from "../internal/string.mjs";
1
+ import { isPlainObject } from "../is.mjs";
2
+ import { getString, join } from "../string/misc.mjs";
3
3
  import { SizedMap } from "../sized/map.mjs";
4
- //#region src/function/memoize.ts
4
+ //#region src/internal/function/memoize.ts
5
5
  function getMemoizationOptions(input) {
6
6
  const { cacheKey, cacheSize } = isPlainObject(input) ? input : {};
7
7
  return {
8
8
  cacheKey: typeof cacheKey === "function" ? cacheKey : void 0,
9
- cacheSize: typeof cacheSize === "number" && cacheSize > 0 ? cacheSize : DEFAULT_CACHE_SIZE
9
+ cacheSize: typeof cacheSize === "number" && cacheSize > 0 ? cacheSize : MEMOIZED_CACHE_SIZE_DEFAULT
10
10
  };
11
11
  }
12
12
  /**
@@ -17,54 +17,41 @@ function getMemoizationOptions(input) {
17
17
  * @returns _Memoized_ instance
18
18
  */
19
19
  function memoize(callback, options) {
20
- if (typeof callback !== "function") throw new TypeError(MEMOIZED_ERROR_CALLBACK);
21
- const state = { options: getMemoizationOptions(options) };
20
+ if (typeof callback !== "function") throw new TypeError(MEMOIZED_CALLBACK);
21
+ const state = {
22
+ cache: void 0,
23
+ getter: void 0,
24
+ options: getMemoizationOptions(options)
25
+ };
22
26
  state.cache = new SizedMap(state.options.cacheSize);
23
27
  state.getter = (...parameters) => {
24
- const key = state.options.cacheKey?.(...parameters) ?? (parameters.length === 1 ? parameters[0] : join(parameters.map(getString), SEPARATOR));
28
+ const key = state.options.cacheKey?.(...parameters) ?? (parameters.length === 1 ? parameters[0] : join(parameters.map(getString), MEMOIZED_KEY_SEPARATOR));
25
29
  if (state.cache.has(key)) return state.cache.get(key);
26
30
  const value = callback(...parameters);
27
- state.cache?.set(key, value);
31
+ state.cache.set(key, value);
28
32
  return value;
29
33
  };
30
34
  const instance = {
31
- clear: () => {
32
- state.cache?.clear();
33
- },
34
- delete: (key) => {
35
- return state.cache?.delete(key) ?? false;
36
- },
37
- destroy: () => {
38
- state.cache?.clear();
39
- state.cache = void 0;
40
- state.getter = void 0;
41
- },
42
- get: (key) => {
43
- return state.cache?.get(key);
44
- },
45
- has: (key) => {
46
- return state.cache?.has(key) ?? false;
47
- },
48
- run: (...parameters) => {
49
- if (state.cache == null || state.getter == null) throw new Error(MEMOIZED_ERROR_DESTROYED);
50
- return state.getter(...parameters);
51
- }
35
+ clear: () => state.cache.clear(),
36
+ delete: (key) => state.cache.delete(key),
37
+ get: (key) => state.cache.get(key),
38
+ has: (key) => state.cache.has(key),
39
+ run: (...parameters) => state.getter(...parameters)
52
40
  };
53
41
  Object.defineProperties(instance, {
54
42
  maximum: {
55
43
  enumerable: true,
56
- get: () => state.cache?.maximum ?? NaN
44
+ get: () => state.cache.maximum
57
45
  },
58
46
  size: {
59
47
  enumerable: true,
60
- get: () => state.cache?.size ?? NaN
48
+ get: () => state.cache.size
61
49
  }
62
50
  });
63
51
  return Object.freeze(instance);
64
52
  }
65
- const DEFAULT_CACHE_SIZE = 1024;
66
- const MEMOIZED_ERROR_CALLBACK = "Memoized requires a callback function";
67
- const MEMOIZED_ERROR_DESTROYED = "The Memoized instance has been destroyed";
68
- const SEPARATOR = "_";
53
+ const MEMOIZED_CACHE_SIZE_DEFAULT = 1024;
54
+ const MEMOIZED_CALLBACK = "Memoized requires a callback function";
55
+ const MEMOIZED_KEY_SEPARATOR = "_";
69
56
  //#endregion
70
57
  export { memoize };
@@ -1,9 +1,16 @@
1
1
  //#region src/internal/function/timer.ts
2
2
  function getAsyncTimer(type, callback, time) {
3
+ function cancel() {
4
+ clearTimer(id);
5
+ if (last != null && !last.running) {
6
+ last.reject();
7
+ last = void 0;
8
+ }
9
+ }
3
10
  async function run(item) {
4
11
  const now = performance.now();
5
12
  start ??= now;
6
- if (interval === 0 || now - start >= interval - OFFSET) {
13
+ if (interval === 0 || now - start >= interval - TIMER_OFFSET) {
7
14
  start = throttle ? now : void 0;
8
15
  item.running = true;
9
16
  try {
@@ -23,7 +30,7 @@ function getAsyncTimer(type, callback, time) {
23
30
  let last;
24
31
  let start;
25
32
  const timer = (...parameters) => {
26
- timer.cancel();
33
+ cancel();
27
34
  const next = {
28
35
  parameters,
29
36
  running: false
@@ -34,25 +41,23 @@ function getAsyncTimer(type, callback, time) {
34
41
  });
35
42
  last = next;
36
43
  if (throttle) run(next);
37
- else id = startTimer(() => {
38
- run(next);
39
- });
44
+ else id = startTimer(() => run(next));
40
45
  return next.promise;
41
46
  };
42
- timer.cancel = () => {
43
- clearTimer(id);
44
- if (last != null && !last.running) last.reject();
45
- };
47
+ Object.defineProperty(timer, TIMER_CANCEL, { value: () => cancel() });
46
48
  return timer;
47
49
  }
48
50
  function getInterval(value) {
49
51
  return typeof value === "number" && value > 0 ? value : 0;
50
52
  }
51
53
  function getTimer(type, callback, time) {
54
+ function cancel() {
55
+ clearTimer(id);
56
+ }
52
57
  function run() {
53
58
  const now = performance.now();
54
59
  start ??= now;
55
- if (interval === 0 || now - start >= interval - OFFSET) {
60
+ if (interval === 0 || now - start >= interval - TIMER_OFFSET) {
56
61
  start = throttle ? now : void 0;
57
62
  callback(...args);
58
63
  } else id = startTimer(run);
@@ -63,18 +68,17 @@ function getTimer(type, callback, time) {
63
68
  let id;
64
69
  let start;
65
70
  const timer = (...parameters) => {
66
- timer.cancel();
71
+ cancel();
67
72
  args = parameters;
68
73
  if (throttle) run();
69
74
  else id = startTimer(run);
70
75
  };
71
- timer.cancel = () => {
72
- clearTimer(id);
73
- };
76
+ Object.defineProperty(timer, TIMER_CANCEL, { value: () => cancel() });
74
77
  return timer;
75
78
  }
76
- const OFFSET = 5;
79
+ const TIMER_CANCEL = "cancel";
77
80
  const TIMER_DEBOUNCE = "debounce";
81
+ const TIMER_OFFSET = 5;
78
82
  const TIMER_THROTTLE = "throttle";
79
83
  const TIMER_WAIT = "wait";
80
84
  // istanbul ignore next
@@ -1,6 +1,6 @@
1
- import { GenericCallback } from "../models.mjs";
1
+ import { GenericCallback } from "../../models.mjs";
2
2
  import { UnwrapValue } from "../result/models.mjs";
3
- //#region src/function/work.d.ts
3
+ //#region src/internal/function/work.d.ts
4
4
  /**
5
5
  * A synchronous _Flow_, a function that pipe a value through a series of functions
6
6
  */
@@ -1,6 +1,6 @@
1
- import { isError, isOk } from "../internal/result.mjs";
1
+ import { isError, isOk } from "../result/misc.mjs";
2
2
  import { assert } from "./assert.mjs";
3
- //#region src/function/work.ts
3
+ //#region src/internal/function/work.ts
4
4
  function asyncFlow(...fns) {
5
5
  assertFlowFunctions(fns);
6
6
  return (...args) => asyncWork(args.map((value) => {
@@ -15,7 +15,6 @@ function flow(...fns) {
15
15
  return isOk(value) ? value.value : value;
16
16
  }), fns, true);
17
17
  }
18
- flow.async = asyncFlow;
19
18
  async function asyncPipe(value, ...pipes) {
20
19
  assertPipeFunctions(pipes);
21
20
  return asyncWork(value, pipes, false);
@@ -24,7 +23,6 @@ function pipe(value, ...pipes) {
24
23
  assertPipeFunctions(pipes);
25
24
  return work(value, pipes, false);
26
25
  }
27
- pipe.async = asyncPipe;
28
26
  async function asyncWork(initial, functions, flow) {
29
27
  const { length } = functions;
30
28
  let transformed = unwrapValue(initial);
@@ -36,10 +34,10 @@ async function asyncWork(initial, functions, flow) {
36
34
  }
37
35
  function unwrapValue(value, flow, nested) {
38
36
  if (typeof value === "function") {
39
- if (nested != null) throw new TypeError(MESSAGE_NESTING);
37
+ if (nested != null) throw new TypeError(WORK_MESSAGE_NESTING);
40
38
  return unwrapValue(value(), flow, true);
41
39
  }
42
- if (flow != null && value instanceof Promise) throw new TypeError(flow ? MESSAGE_FLOW_PROMISE : MESSAGE_PIPE_PROMISE);
40
+ if (flow != null && value instanceof Promise) throw new TypeError(flow ? WORK_MESSAGE_FLOW_PROMISE : WORK_MESSAGE_PIPE_PROMISE);
43
41
  if (isError(value)) throw value.error;
44
42
  return isOk(value) ? value.value : value;
45
43
  }
@@ -52,12 +50,16 @@ function work(initial, functions, flow) {
52
50
  }
53
51
  return transformed;
54
52
  }
55
- const MESSAGE_FLOW_ARRAY = "Flow expected to receive an array of functions";
56
- const MESSAGE_FLOW_PROMISE = "Synchronous Flow received a promise. Use `flow.async` instead.";
57
- const MESSAGE_NESTING = "Return values are too deeply nested.";
58
- const MESSAGE_PIPE_ARRAY = "Pipe expected to receive an array of functions";
59
- const MESSAGE_PIPE_PROMISE = "Synchronous Pipe received a promise. Use `pipe.async` instead.";
60
- const assertFlowFunctions = assert.condition((value) => Array.isArray(value) && value.every((item) => typeof item === "function"), MESSAGE_FLOW_ARRAY, TypeError);
61
- const assertPipeFunctions = assert.condition((value) => Array.isArray(value) && value.every((item) => typeof item === "function"), MESSAGE_PIPE_ARRAY, TypeError);
53
+ const WORK_MESSAGE_FLOW_ARRAY = "Flow expected to receive an array of functions";
54
+ const WORK_MESSAGE_FLOW_PROMISE = "Synchronous Flow received a promise. Use `flow.async` instead.";
55
+ const WORK_MESSAGE_NESTING = "Return values are too deeply nested.";
56
+ const WORK_MESSAGE_PIPE_ARRAY = "Pipe expected to receive an array of functions";
57
+ const WORK_MESSAGE_PIPE_PROMISE = "Synchronous Pipe received a promise. Use `pipe.async` instead.";
58
+ const assertFlowFunctions = assert.condition((value) => Array.isArray(value) && value.every((item) => typeof item === "function"), WORK_MESSAGE_FLOW_ARRAY, TypeError);
59
+ const assertPipeFunctions = assert.condition((value) => Array.isArray(value) && value.every((item) => typeof item === "function"), WORK_MESSAGE_PIPE_ARRAY, TypeError);
60
+ flow.async = asyncFlow;
61
+ pipe.async = asyncPipe;
62
+ Object.defineProperty(flow, "async", { value: asyncFlow });
63
+ Object.defineProperty(pipe, "async", { value: asyncPipe });
62
64
  //#endregion
63
65
  export { asyncFlow, asyncPipe, flow, pipe };
@@ -0,0 +1,71 @@
1
+ import { GenericCallback } from "../models.mjs";
2
+ import { Subscription, SubscriptionProperty } from "./subscription.mjs";
3
+ //#region src/internal/herald.d.ts
4
+ type Herald<Events extends Record<string, GenericCallback>> = {
5
+ readonly events: HeraldEvents<Events>;
6
+ /**
7
+ * Remove all event subscribers
8
+ */
9
+ clear(): void;
10
+ /**
11
+ * Emit an event with parameters
12
+ *
13
+ * @param event Event name
14
+ * @param parameters Event parameters
15
+ */
16
+ emit<Event extends keyof Events>(event: Event, ...parameters: Parameters<Events[Event]>): void;
17
+ /**
18
+ * Is the event observed by any subscribers?
19
+ *
20
+ * @param event Event name
21
+ * @returns `true` if the event is observed, otherwise `false`
22
+ */
23
+ observed<Event extends keyof Events>(event: Event): boolean;
24
+ /**
25
+ * Subscribe to an event with a callback
26
+ *
27
+ * @param event Event name
28
+ * @param callback Callback function
29
+ * @param signal Optional abort signal to cancel the subscription
30
+ * @returns Subscription instance
31
+ */
32
+ subscribe<Event extends keyof Events>(event: Event, callback: Events[Event], signal?: AbortSignal): Subscription;
33
+ };
34
+ type HeraldEvents<Events extends Record<string, GenericCallback>> = { [Key in keyof Events]: Key extends '*' ? never : (callback: Events[Key], signal?: AbortSignal) => void; };
35
+ type HeraldOnCreate<Events extends Record<string, GenericCallback>> = <Event extends keyof Events>(event: Event, callback: Events[Event], subscription: Subscription) => void;
36
+ type HeraldOptions<Events extends Record<string, GenericCallback>> = {
37
+ names: Array<keyof Events>;
38
+ property?: SubscriptionProperty;
39
+ onCreate?: HeraldOnCreate<Events>;
40
+ };
41
+ /**
42
+ * Create a _Herald_ for announcing named events
43
+ *
44
+ * @param names Event names
45
+ * @param property Optional property for subscription identification _(defaults to `$herald`)_
46
+ * @returns _Herald_ instance
47
+ */
48
+ declare function herald<Events extends Record<string, GenericCallback>>(options: HeraldOptions<Events>): Herald<Events>;
49
+ /**
50
+ * Is the value a herald?
51
+ *
52
+ * @param value Value to check
53
+ * @returns `true` if the value is a herald, otherwise `false`
54
+ */
55
+ declare function isHerald<Events extends Record<string, GenericCallback> = Record<string, GenericCallback>>(value: unknown): value is Herald<Events>;
56
+ /**
57
+ * Is the value events for a herald?
58
+ *
59
+ * @param value Value to check
60
+ * @returns `true` if the value is events for a herald, otherwise `false`
61
+ */
62
+ declare function isHeraldEvents<Events extends Record<string, GenericCallback> = Record<string, GenericCallback>>(value: unknown): value is HeraldEvents<Events>;
63
+ /**
64
+ * Is the value a herald subscription?
65
+ *
66
+ * @param value Value to check
67
+ * @returns `true` if the value is a herald subscription, otherwise `false`
68
+ */
69
+ declare function isHeraldSubscription(value: unknown): value is Subscription;
70
+ //#endregion
71
+ export { Herald, HeraldEvents, HeraldOptions, herald, isHerald, isHeraldEvents, isHeraldSubscription };