@oscarpalmer/atoms 0.202.0 → 0.204.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 (187) hide show
  1. package/dist/array/difference.d.mts +4 -5
  2. package/dist/array/exists.d.mts +5 -6
  3. package/dist/array/filter.d.mts +10 -11
  4. package/dist/array/filter.mjs +0 -1
  5. package/dist/array/find.d.mts +10 -11
  6. package/dist/array/find.mjs +0 -1
  7. package/dist/array/first.d.mts +10 -11
  8. package/dist/array/first.mjs +0 -1
  9. package/dist/array/flatten.d.mts +2 -3
  10. package/dist/array/from.d.mts +7 -8
  11. package/dist/array/group-by.d.mts +7 -8
  12. package/dist/array/group-by.mjs +0 -1
  13. package/dist/array/insert.d.mts +3 -4
  14. package/dist/array/intersection.d.mts +4 -5
  15. package/dist/array/last.d.mts +10 -11
  16. package/dist/array/last.mjs +0 -1
  17. package/dist/array/move.d.mts +9 -10
  18. package/dist/array/move.mjs +0 -4
  19. package/dist/array/partition.d.mts +5 -6
  20. package/dist/array/push.d.mts +2 -3
  21. package/dist/array/reverse.d.mts +2 -3
  22. package/dist/array/select.d.mts +11 -12
  23. package/dist/array/select.mjs +0 -1
  24. package/dist/array/single.d.mts +4 -5
  25. package/dist/array/slice.d.mts +12 -13
  26. package/dist/array/sort.d.mts +31 -32
  27. package/dist/array/sort.mjs +38 -39
  28. package/dist/array/splice.d.mts +5 -6
  29. package/dist/array/swap.d.mts +8 -9
  30. package/dist/array/swap.mjs +0 -1
  31. package/dist/array/to-map.d.mts +7 -8
  32. package/dist/array/to-map.mjs +0 -1
  33. package/dist/array/to-record.d.mts +7 -8
  34. package/dist/array/to-record.mjs +0 -1
  35. package/dist/array/to-set.d.mts +4 -5
  36. package/dist/array/toggle.d.mts +4 -5
  37. package/dist/array/union.d.mts +4 -5
  38. package/dist/array/unique.d.mts +4 -5
  39. package/dist/array/update.d.mts +4 -5
  40. package/dist/beacon.d.mts +8 -9
  41. package/dist/beacon.mjs +96 -71
  42. package/dist/color/constants.d.mts +14 -13
  43. package/dist/color/constants.mjs +9 -2
  44. package/dist/color/index.d.mts +2 -2
  45. package/dist/color/instance.d.mts +3 -3
  46. package/dist/color/instance.mjs +91 -69
  47. package/dist/color/misc/alpha.d.mts +7 -6
  48. package/dist/color/misc/alpha.mjs +8 -2
  49. package/dist/color/misc/format.d.mts +6 -7
  50. package/dist/color/misc/format.mjs +15 -12
  51. package/dist/color/misc/get.d.mts +14 -15
  52. package/dist/color/misc/is.d.mts +16 -17
  53. package/dist/color/misc/is.mjs +7 -7
  54. package/dist/color/misc/state.d.mts +33 -10
  55. package/dist/color/misc/state.mjs +28 -48
  56. package/dist/color/models.d.mts +25 -19
  57. package/dist/color/space/hex.d.mts +14 -11
  58. package/dist/color/space/hex.mjs +21 -2
  59. package/dist/color/space/hsl.d.mts +13 -10
  60. package/dist/color/space/hsl.mjs +25 -4
  61. package/dist/color/space/hwb.d.mts +14 -11
  62. package/dist/color/space/hwb.mjs +24 -3
  63. package/dist/color/space/rgb.d.mts +16 -13
  64. package/dist/color/space/rgb.mjs +41 -20
  65. package/dist/function/limit.d.mts +9 -8
  66. package/dist/function/limit.mjs +5 -7
  67. package/dist/function/once.d.mts +26 -6
  68. package/dist/function/once.mjs +129 -76
  69. package/dist/function/retry.d.mts +5 -6
  70. package/dist/function/retry.mjs +3 -4
  71. package/dist/index.d.mts +184 -60
  72. package/dist/index.mjs +1292 -970
  73. package/dist/internal/aborter.d.mts +9 -0
  74. package/dist/internal/{abort.mjs → aborter.mjs} +1 -1
  75. package/dist/internal/array/callbacks.d.mts +4 -5
  76. package/dist/internal/array/chunk.d.mts +2 -3
  77. package/dist/internal/array/compact.d.mts +3 -4
  78. package/dist/internal/array/find.d.mts +10 -11
  79. package/dist/internal/array/get.d.mts +7 -8
  80. package/dist/internal/array/get.mjs +3 -2
  81. package/dist/internal/array/group.d.mts +2 -3
  82. package/dist/internal/array/index-of.d.mts +10 -11
  83. package/dist/internal/array/index-of.mjs +0 -1
  84. package/dist/internal/array/insert.d.mts +5 -6
  85. package/dist/internal/array/match.d.mts +17 -18
  86. package/dist/internal/array/overlap.d.mts +2 -3
  87. package/dist/internal/array/sets.d.mts +5 -6
  88. package/dist/internal/array/shuffle.d.mts +2 -3
  89. package/dist/internal/array/update.d.mts +2 -3
  90. package/dist/internal/defaults.d.mts +3 -4
  91. package/dist/internal/function/assert.d.mts +10 -11
  92. package/dist/internal/function/assert.mjs +0 -7
  93. package/dist/internal/function/limit.d.mts +41 -0
  94. package/dist/internal/function/limit.mjs +106 -0
  95. package/dist/internal/function/memoize.d.mts +4 -5
  96. package/dist/internal/function/memoize.mjs +55 -31
  97. package/dist/internal/function/misc.d.mts +2 -3
  98. package/dist/internal/function/work.d.mts +53 -54
  99. package/dist/internal/function/work.mjs +0 -2
  100. package/dist/internal/herald.d.mts +8 -9
  101. package/dist/internal/herald.mjs +38 -27
  102. package/dist/internal/is.d.mts +19 -20
  103. package/dist/internal/math/aggregate.d.mts +14 -15
  104. package/dist/internal/math/misc.d.mts +3 -4
  105. package/dist/internal/number.d.mts +4 -5
  106. package/dist/internal/random.d.mts +3 -4
  107. package/dist/internal/result/misc.d.mts +29 -14
  108. package/dist/internal/result/misc.mjs +4 -3
  109. package/dist/internal/result/models.d.mts +9 -10
  110. package/dist/internal/sized/map.d.mts +2 -3
  111. package/dist/internal/sized/misc.d.mts +2 -3
  112. package/dist/internal/string/case.d.mts +9 -10
  113. package/dist/internal/string/misc.d.mts +8 -9
  114. package/dist/internal/subscription.d.mts +10 -11
  115. package/dist/internal/subscription.mjs +72 -58
  116. package/dist/internal/value/compare.d.mts +17 -9
  117. package/dist/internal/value/compare.mjs +6 -11
  118. package/dist/internal/value/equal.d.mts +34 -15
  119. package/dist/internal/value/equal.mjs +20 -13
  120. package/dist/internal/value/get.d.mts +3 -4
  121. package/dist/internal/value/handlers.d.mts +19 -9
  122. package/dist/internal/value/handlers.mjs +48 -50
  123. package/dist/internal/value/has.d.mts +6 -7
  124. package/dist/internal/value/has.mjs +0 -1
  125. package/dist/internal/value/misc.d.mts +6 -7
  126. package/dist/internal/value/misc.mjs +4 -3
  127. package/dist/internal/value/partial.d.mts +3 -4
  128. package/dist/internal/value/partial.mjs +2 -1
  129. package/dist/internal/value/set.d.mts +4 -5
  130. package/dist/internal/value/set.mjs +2 -1
  131. package/dist/is.d.mts +13 -13
  132. package/dist/logger.d.mts +5 -4
  133. package/dist/logger.mjs +72 -64
  134. package/dist/math.d.mts +20 -20
  135. package/dist/models.d.mts +45 -49
  136. package/dist/promise/constants.d.mts +14 -15
  137. package/dist/promise/delay.d.mts +3 -4
  138. package/dist/promise/delay.mjs +5 -5
  139. package/dist/promise/helpers.d.mts +7 -8
  140. package/dist/promise/helpers.mjs +4 -3
  141. package/dist/promise/index.d.mts +13 -14
  142. package/dist/promise/index.mjs +1 -2
  143. package/dist/promise/misc.d.mts +7 -7
  144. package/dist/promise/models.d.mts +17 -18
  145. package/dist/promise/timed.d.mts +4 -5
  146. package/dist/promise/timed.mjs +6 -6
  147. package/dist/query.d.mts +3 -4
  148. package/dist/queue.d.mts +14 -15
  149. package/dist/queue.mjs +198 -136
  150. package/dist/random.d.mts +8 -8
  151. package/dist/result/index.d.mts +8 -9
  152. package/dist/result/index.mjs +0 -1
  153. package/dist/result/match.d.mts +6 -7
  154. package/dist/result/match.mjs +0 -1
  155. package/dist/result/misc.d.mts +3 -3
  156. package/dist/result/work/flow.d.mts +28 -29
  157. package/dist/result/work/flow.mjs +0 -1
  158. package/dist/result/work/pipe.d.mts +24 -25
  159. package/dist/result/work/pipe.mjs +0 -1
  160. package/dist/sized/set.d.mts +2 -3
  161. package/dist/string/fuzzy.d.mts +21 -12
  162. package/dist/string/fuzzy.mjs +77 -52
  163. package/dist/string/index.d.mts +8 -8
  164. package/dist/string/match.d.mts +4 -5
  165. package/dist/string/normalize.d.mts +8 -9
  166. package/dist/string/normalize.mjs +9 -5
  167. package/dist/string/template.d.mts +9 -9
  168. package/dist/string/template.mjs +11 -5
  169. package/dist/value/clone.d.mts +21 -13
  170. package/dist/value/clone.mjs +19 -25
  171. package/dist/value/collection.d.mts +4 -5
  172. package/dist/value/diff.d.mts +5 -6
  173. package/dist/value/freeze.d.mts +18 -19
  174. package/dist/value/freeze.mjs +1 -5
  175. package/dist/value/merge.d.mts +13 -14
  176. package/dist/value/merge.mjs +21 -7
  177. package/dist/value/omit.d.mts +2 -3
  178. package/dist/value/pick.d.mts +2 -3
  179. package/dist/value/shake.d.mts +3 -4
  180. package/dist/value/smush.d.mts +3 -4
  181. package/dist/value/transform.d.mts +15 -10
  182. package/dist/value/transform.mjs +12 -8
  183. package/dist/value/unsmush.d.mts +3 -4
  184. package/package.json +2 -2
  185. package/dist/internal/abort.d.mts +0 -10
  186. package/dist/internal/function/timer.d.mts +0 -10
  187. package/dist/internal/function/timer.mjs +0 -87
@@ -1,13 +1,46 @@
1
1
  import { isKey, isPlainObject } from "./is.mjs";
2
- import { createAborter } from "./abort.mjs";
2
+ import { createAborter } from "./aborter.mjs";
3
3
  //#region src/internal/subscription.ts
4
- function addSubscription(subscriptions, subscription, state) {
5
- const { items, values } = subscriptions;
4
+ const SUBSCRIPTION_NAME = "subscription";
5
+ const SUBSCRIPTION_PROPERTY = "$subscription";
6
+ const SUBSCRIPTION_STORE = "subscriptions";
7
+ function Subscription(state, property, parameters) {
8
+ this[SUBSCRIPTION_SYMBOL] = state;
9
+ this[property.key] = property.value;
10
+ state.aborter = createAborter(parameters.signal, () => this.unsubscribe(subscriptions, this, state));
11
+ addSubscription(this);
12
+ }
13
+ Subscription.prototype[SUBSCRIPTION_PROPERTY] = SUBSCRIPTION_NAME;
14
+ Subscription.prototype.unsubscribe = removeSubscription;
15
+ Object.defineProperty(Subscription.prototype, "active", {
16
+ enumerable: true,
17
+ get: getSubscriptionActive
18
+ });
19
+ function Subscriptions(parameters) {
20
+ const { keys, property } = createSubscriptionsParameters(parameters);
21
+ this[SUBSCRIPTION_SYMBOL] = createSubscriptionsState(property, keys);
22
+ }
23
+ Subscriptions.prototype[SUBSCRIPTION_PROPERTY] = SUBSCRIPTION_STORE;
24
+ Subscriptions.prototype.clear = clearSubscriptions;
25
+ Subscriptions.prototype.create = createSubscription;
26
+ Object.defineProperties(Subscriptions.prototype, {
27
+ items: {
28
+ enumerable: true,
29
+ get: getSubscriptionsItems
30
+ },
31
+ values: {
32
+ enumerable: true,
33
+ get: getSubscriptionsValues
34
+ }
35
+ });
36
+ function addSubscription(instance) {
37
+ const state = instance[SUBSCRIPTION_SYMBOL];
6
38
  const { key, value } = state.parameters;
39
+ const { items, values } = state.subscriptions;
7
40
  if (!isKey(key)) {
8
- items.any.add(subscription);
9
- values.from.any.set(value, subscription);
10
- values.to.any.set(subscription, value);
41
+ items.any.add(instance);
42
+ values.from.any.set(value, instance);
43
+ values.to.any.set(instance, value);
11
44
  return;
12
45
  }
13
46
  /* istanbul ignore if */
@@ -19,29 +52,32 @@ function addSubscription(subscriptions, subscription, state) {
19
52
  keyedItems = /* @__PURE__ */ new Set();
20
53
  items.keyed.set(key, keyedItems);
21
54
  }
22
- keyedItems.add(subscription);
55
+ keyedItems.add(instance);
23
56
  let keyedFrom = values.from.keyed.get(key);
24
57
  if (keyedFrom == null) {
25
58
  keyedFrom = /* @__PURE__ */ new Map();
26
59
  values.from.keyed.set(key, keyedFrom);
27
60
  }
28
- keyedFrom.set(value, subscription);
61
+ keyedFrom.set(value, instance);
29
62
  let keyedTo = values.to.keyed.get(key);
30
63
  if (keyedTo == null) {
31
64
  keyedTo = /* @__PURE__ */ new Map();
32
65
  values.to.keyed.set(key, keyedTo);
33
66
  }
34
- keyedTo.set(subscription, value);
67
+ keyedTo.set(instance, value);
35
68
  }
36
- function clearSubscriptions(store) {
37
- for (const susbcription of store.values.to.any.keys()) susbcription.unsubscribe();
38
- if (store.values.to.keyed != null) for (const values of store.values.to.keyed.values()) for (const susbcription of values.keys()) susbcription.unsubscribe();
69
+ function clearSubscriptions() {
70
+ const state = this[SUBSCRIPTION_SYMBOL];
71
+ for (const susbcription of state.values.to.any.keys()) susbcription.unsubscribe();
72
+ if (state.values.to.keyed != null && state.values.to.keyed.size > 0) for (const values of state.values.to.keyed.values()) for (const susbcription of values.keys()) susbcription.unsubscribe();
39
73
  }
40
- function createSubscription(subscriptions, property, params) {
74
+ function createSubscription(params) {
41
75
  const parameters = createSubscriptionParameters(params);
42
76
  if (parameters.signal?.aborted ?? false) throw new Error(parameters.signal?.reason);
77
+ const subscriptions = this[SUBSCRIPTION_SYMBOL];
43
78
  const state = {
44
79
  parameters,
80
+ subscriptions,
45
81
  active: true
46
82
  };
47
83
  const existing = getExistingSubscription(subscriptions, state);
@@ -50,24 +86,23 @@ function createSubscription(subscriptions, property, params) {
50
86
  if (subscriptions.keys === false) throw new Error(SUBSCRIPTION_DISALLOWED_KEY);
51
87
  else if (subscriptions.keys !== true && !subscriptions.keys.has(parameters.key)) throw new Error(SUBSCRIPTION_INVALID_KEY);
52
88
  }
53
- state.aborter = createAborter(parameters.signal, () => unsubscribe(subscriptions, instance, state));
54
- const instance = { unsubscribe: () => unsubscribe(subscriptions, instance, state) };
55
- Object.defineProperties(instance, {
56
- [SUBSCRIPTION_PROPERTY]: { value: SUBSCRIPTION_NAME },
57
- [property.key]: { value: property.value },
58
- active: {
59
- enumerable: true,
60
- get: () => (parameters.isActive?.() ?? true) && state.active
61
- }
62
- });
63
- addSubscription(subscriptions, instance, state);
64
- return [Object.freeze(instance), false];
89
+ return [new Subscription(state, subscriptions.property, parameters), false];
65
90
  }
66
91
  function getExistingSubscription(subscriptions, state) {
67
92
  const { values } = subscriptions;
68
93
  const { parameters } = state;
69
94
  return isKey(parameters.key) ? values.from.keyed?.get(parameters.key)?.get(parameters.value) : values.from.any.get(parameters.value);
70
95
  }
96
+ function getSubscriptionActive() {
97
+ const state = this[SUBSCRIPTION_SYMBOL];
98
+ return (state.parameters?.isActive?.() ?? true) && state.active;
99
+ }
100
+ function getSubscriptionsItems() {
101
+ return this[SUBSCRIPTION_SYMBOL].items;
102
+ }
103
+ function getSubscriptionsValues() {
104
+ return this[SUBSCRIPTION_SYMBOL].values;
105
+ }
71
106
  function createSubscriptionParameters(input) {
72
107
  const values = isPlainObject(input) ? input : {};
73
108
  if (values.value == null) throw new Error(SUBSCRIPTION_INVALID_VALUE);
@@ -95,9 +130,10 @@ function createSubscriptionsProperty(input) {
95
130
  value: values.value == null ? SUBSCRIPTION_NAME : values.value
96
131
  };
97
132
  }
98
- function createSubscriptionsState(keys) {
133
+ function createSubscriptionsState(property, keys) {
99
134
  return {
100
135
  keys,
136
+ property,
101
137
  items: {
102
138
  any: /* @__PURE__ */ new Set(),
103
139
  keyed: keys === false ? void 0 : /* @__PURE__ */ new Map()
@@ -124,7 +160,7 @@ function isSubscription(value) {
124
160
  return isSubscriptionInstance(value, SUBSCRIPTION_NAME);
125
161
  }
126
162
  function isSubscriptionInstance(value, name) {
127
- return isPlainObject(value) && SUBSCRIPTION_PROPERTY in value && value[SUBSCRIPTION_PROPERTY] === name;
163
+ return typeof value === "object" && value !== null && SUBSCRIPTION_PROPERTY in value && value[SUBSCRIPTION_PROPERTY] === name;
128
164
  }
129
165
  /**
130
166
  * Is the value a subscriptions store?
@@ -145,16 +181,17 @@ function removeFromStore(items, values, subscription, value, key) {
145
181
  values.to.keyed?.get(key)?.delete(subscription);
146
182
  }
147
183
  }
148
- function removeSubscription(subscriptionsState, subscription, state) {
184
+ function removeSubscription() {
185
+ const state = this[SUBSCRIPTION_SYMBOL];
149
186
  if (!state.active) return;
150
187
  state.aborter = void 0;
151
188
  state.active = false;
152
- state.parameters.isActive = void 0;
153
- state.parameters.signal = void 0;
154
- const { items, values } = subscriptionsState;
189
+ const { items, values } = state.subscriptions;
155
190
  const { key, value } = state.parameters;
191
+ state.parameters = void 0;
192
+ state.subscriptions = void 0;
156
193
  if (!isKey(key)) {
157
- removeFromStore(items.any, values, subscription, value);
194
+ removeFromStore(items.any, values, this, value);
158
195
  return;
159
196
  }
160
197
  const keyed = items.keyed?.get(key);
@@ -162,7 +199,7 @@ function removeSubscription(subscriptionsState, subscription, state) {
162
199
  if (items.keyed == null || keyed == null)
163
200
  // istanbul ignore next
164
201
  return;
165
- removeFromStore(keyed, values, subscription, value, key);
202
+ removeFromStore(keyed, values, this, value, key);
166
203
  if (keyed.size === 0) items.keyed.delete(key);
167
204
  }
168
205
  /**
@@ -172,34 +209,11 @@ function removeSubscription(subscriptionsState, subscription, state) {
172
209
  * @returns Subscription store
173
210
  */
174
211
  function subscriptions(parameters) {
175
- const { keys, property } = createSubscriptionsParameters(parameters);
176
- const state = createSubscriptionsState(keys);
177
- const instance = {
178
- clear: () => clearSubscriptions(state),
179
- create: (parameters) => createSubscription(state, property, parameters)
180
- };
181
- Object.defineProperties(instance, {
182
- [SUBSCRIPTION_PROPERTY]: { value: SUBSCRIPTION_STORE },
183
- items: {
184
- enumerable: true,
185
- value: state.items
186
- },
187
- values: {
188
- enumerable: true,
189
- value: state.values
190
- }
191
- });
192
- return Object.freeze(instance);
193
- }
194
- function unsubscribe(subscriptionsState, subscription, state) {
195
- state.aborter?.cancel();
196
- removeSubscription(subscriptionsState, subscription, state);
212
+ return new Subscriptions(parameters);
197
213
  }
198
214
  const SUBSCRIPTION_DISALLOWED_KEY = "Disallowed key for subscription";
199
215
  const SUBSCRIPTION_INVALID_KEY = "Invalid key for subscription";
200
216
  const SUBSCRIPTION_INVALID_VALUE = "A subscription requires a non-null value to identify it";
201
- const SUBSCRIPTION_NAME = "subscription";
202
- const SUBSCRIPTION_PROPERTY = "$subscriptions";
203
- const SUBSCRIPTION_STORE = "subscriptions";
217
+ const SUBSCRIPTION_SYMBOL = Symbol(SUBSCRIPTION_PROPERTY);
204
218
  //#endregion
205
219
  export { SUBSCRIPTION_NAME, isSubscription, isSubscriptions, subscriptions };
@@ -8,13 +8,22 @@ type Comparator<Value = any> = (first: Value, second: Value) => number;
8
8
  * @param second Second value
9
9
  * @returns `0` if equal; `-1` first comes before second; `1` first comes after second
10
10
  */
11
- declare function compare(first: unknown, second: unknown): number;
12
- declare namespace compare {
11
+ export declare function compare(first: unknown, second: unknown): number;
12
+ export declare namespace compare {
13
13
  var deregister: typeof deregisterComparator;
14
- var handlers: {
15
- deregister(constructor: Constructor): void;
14
+ var handler: {
15
+ base: {
16
+ handlers: WeakMap<WeakKey, string | GenericCallback>;
17
+ options: {
18
+ callback: GenericCallback;
19
+ method?: string;
20
+ };
21
+ owner: GenericCallback;
22
+ deregister: (constructor: Constructor) => void;
23
+ get: (first: unknown, second: unknown) => string | GenericCallback | undefined;
24
+ register: (constructor: Constructor, handler?: string | GenericCallback) => void;
25
+ };
16
26
  handle(first: unknown, second: unknown, ...parameters: unknown[]): number;
17
- register(constructor: Constructor, handler?: string | GenericCallback): void;
18
27
  };
19
28
  var register: typeof registerComparator;
20
29
  }
@@ -25,7 +34,7 @@ declare namespace compare {
25
34
  *
26
35
  * @param constructor Class constructor
27
36
  */
28
- declare function deregisterComparator<Instance>(constructor: Constructor<Instance>): void;
37
+ export declare function deregisterComparator<Instance>(constructor: Constructor<Instance>): void;
29
38
  /**
30
39
  * Register a custom comparison handler for a class
31
40
  *
@@ -34,6 +43,5 @@ declare function deregisterComparator<Instance>(constructor: Constructor<Instanc
34
43
  * @param constructor Class constructor
35
44
  * @param handler Method name or comparison function _(defaults to `compare`)_
36
45
  */
37
- declare function registerComparator<Instance>(constructor: Constructor<Instance>, handler?: string | Comparator<Instance>): void;
38
- //#endregion
39
- export { compare, deregisterComparator, registerComparator };
46
+ export declare function registerComparator<Instance>(constructor: Constructor<Instance>, handler?: string | Comparator<Instance>): void;
47
+ //#endregion
@@ -1,6 +1,6 @@
1
1
  import { max } from "../math/aggregate.mjs";
2
2
  import { getString, words } from "../string/misc.mjs";
3
- import { createCompareHandlers } from "./handlers.mjs";
3
+ import { createCompareHandler } from "./handlers.mjs";
4
4
  //#region src/internal/value/compare.ts
5
5
  const COMPARE_NAME = "compare";
6
6
  /**
@@ -45,7 +45,7 @@ function compareValue(first, second, compareStrings) {
45
45
  const firstType = typeof first;
46
46
  if (firstType === typeof second && firstType in comparators) return comparators[firstType](first, second);
47
47
  if (first instanceof Date && second instanceof Date) return compareNumbers(first.getTime(), second.getTime());
48
- return compare.handlers.handle(first, second, compareStrings);
48
+ return compare.handler.handle(first, second, compareStrings);
49
49
  }
50
50
  /**
51
51
  * Deregister a custom comparison handler for a class
@@ -55,7 +55,7 @@ function compareValue(first, second, compareStrings) {
55
55
  * @param constructor Class constructor
56
56
  */
57
57
  function deregisterComparator(constructor) {
58
- compare.handlers.deregister(constructor);
58
+ compare.handler.base.deregister(constructor);
59
59
  }
60
60
  function getComparisonParts(value) {
61
61
  if (Array.isArray(value)) return value;
@@ -70,7 +70,7 @@ function getComparisonParts(value) {
70
70
  * @param handler Method name or comparison function _(defaults to `compare`)_
71
71
  */
72
72
  function registerComparator(constructor, handler) {
73
- compare.handlers.register(constructor, handler);
73
+ compare.handler.base.register(constructor, handler);
74
74
  }
75
75
  const comparators = {
76
76
  bigint: compareNumbers,
@@ -78,17 +78,12 @@ const comparators = {
78
78
  number: compareNumbers,
79
79
  symbol: compareSymbols
80
80
  };
81
- const compareHandlers = createCompareHandlers(compare, {
81
+ const compareHandler = createCompareHandler(compare, {
82
82
  callback: (first, second, compareStrings) => compareStrings ? getString(first).localeCompare(getString(second)) : void 0,
83
83
  method: COMPARE_NAME
84
84
  });
85
85
  compare.deregister = deregisterComparator;
86
- compare.handlers = compareHandlers;
86
+ compare.handler = compareHandler;
87
87
  compare.register = registerComparator;
88
- Object.defineProperties(compare, {
89
- deregister: { value: deregisterComparator },
90
- handlers: { value: compareHandlers },
91
- register: { value: registerComparator }
92
- });
93
88
  //#endregion
94
89
  export { compare, deregisterComparator, registerComparator };
@@ -3,7 +3,7 @@ import { Constructor, GenericCallback } from "../../models.mjs";
3
3
  /**
4
4
  * Options for value equality comparison
5
5
  */
6
- type EqualOptions = {
6
+ export type EqualOptions = {
7
7
  /**
8
8
  * When `true`, strings are compared case-insensitively
9
9
  */
@@ -31,7 +31,7 @@ type Equalizer = {
31
31
  * @param ignoreCase If `true`, comparison will be case-insensitive
32
32
  * @returns `true` if the strings are equal, otherwise `false`
33
33
  */
34
- (first: string, second: string, ignoreCase?: boolean): boolean;
34
+ compare(first: string, second: string, ignoreCase?: boolean): boolean;
35
35
  /**
36
36
  * Are two values equal?
37
37
  *
@@ -39,7 +39,7 @@ type Equalizer = {
39
39
  * @param second Second value
40
40
  * @returns `true` if the values are equal, otherwise `false`
41
41
  */
42
- (first: unknown, second: unknown): boolean;
42
+ compare(first: unknown, second: unknown): boolean;
43
43
  /**
44
44
  * Deregister a equality comparison handler for a specific class
45
45
  *
@@ -54,6 +54,17 @@ type Equalizer = {
54
54
  */
55
55
  register: <Instance>(constructor: Constructor<Instance>, handler: (first: Instance, second: Instance) => boolean) => void;
56
56
  };
57
+ type Options = {
58
+ ignoreCase: boolean;
59
+ ignoreExpressions: OptionsKeys<RegExp[]>;
60
+ ignoreKeys: OptionsKeys<Set<string>>;
61
+ relaxedNullish: boolean;
62
+ };
63
+ type OptionsKeys<Values> = {
64
+ enabled: boolean;
65
+ values: Values;
66
+ };
67
+ declare function Equalizer(this: any, options: Options): void;
57
68
  /**
58
69
  * Deregister a equality comparison handler for a specific class
59
70
  *
@@ -61,7 +72,7 @@ type Equalizer = {
61
72
  *
62
73
  * @param constructor Class constructor
63
74
  */
64
- declare function deregisterEqualizer<Instance>(constructor: Constructor<Instance>): void;
75
+ export declare function deregisterEqualizer<Instance>(constructor: Constructor<Instance>): void;
65
76
  /**
66
77
  * Are two strings equal?
67
78
  *
@@ -70,7 +81,7 @@ declare function deregisterEqualizer<Instance>(constructor: Constructor<Instance
70
81
  * @param ignoreCase If `true`, comparison will be case-insensitive
71
82
  * @returns `true` if the strings are equal, otherwise `false`
72
83
  */
73
- declare function equal(first: string, second: string, ignoreCase?: boolean): boolean;
84
+ export declare function equal(first: string, second: string, ignoreCase?: boolean): boolean;
74
85
  /**
75
86
  * Are two values equal?
76
87
  *
@@ -79,14 +90,23 @@ declare function equal(first: string, second: string, ignoreCase?: boolean): boo
79
90
  * @param options Comparison options
80
91
  * @returns `true` if the values are equal, otherwise `false`
81
92
  */
82
- declare function equal(first: unknown, second: unknown, options?: EqualOptions): boolean;
83
- declare namespace equal {
84
- var handlers: {
85
- deregister(constructor: Constructor): void;
93
+ export declare function equal(first: unknown, second: unknown, options?: EqualOptions): boolean;
94
+ export declare namespace equal {
95
+ var deregister: typeof deregisterEqualizer;
96
+ var handler: {
97
+ base: {
98
+ handlers: WeakMap<WeakKey, string | GenericCallback>;
99
+ options: {
100
+ callback: GenericCallback;
101
+ method?: string;
102
+ };
103
+ owner: GenericCallback;
104
+ deregister: (constructor: Constructor) => void;
105
+ get: (first: unknown, second: unknown) => string | GenericCallback | undefined;
106
+ register: (constructor: Constructor, handler?: string | GenericCallback) => void;
107
+ };
86
108
  handle(first: unknown, second: unknown, ...parameters: unknown[]): boolean;
87
- register(constructor: Constructor, handler?: string | GenericCallback): void;
88
109
  };
89
- var deregister: typeof deregisterEqualizer;
90
110
  var initialize: typeof initializeEqualizer;
91
111
  var register: typeof registerEqualizer;
92
112
  }
@@ -98,7 +118,7 @@ declare namespace equal {
98
118
  * @param options Comparison options
99
119
  * @returns Equalizer function
100
120
  */
101
- declare function initializeEqualizer(options?: EqualOptions): Equalizer;
121
+ export declare function initializeEqualizer(options?: EqualOptions): Equalizer;
102
122
  /**
103
123
  * Register a equality comparison function for a specific class
104
124
  *
@@ -107,6 +127,5 @@ declare function initializeEqualizer(options?: EqualOptions): Equalizer;
107
127
  * @param constructor Class constructor
108
128
  * @param handler Comparison function
109
129
  */
110
- declare function registerEqualizer<Instance>(constructor: Constructor<Instance>, handler: (first: Instance, second: Instance) => boolean): void;
111
- //#endregion
112
- export { EqualOptions, deregisterEqualizer, equal, initializeEqualizer, registerEqualizer };
130
+ export declare function registerEqualizer<Instance>(constructor: Constructor<Instance>, handler: (first: Instance, second: Instance) => boolean): void;
131
+ //#endregion
@@ -1,6 +1,15 @@
1
1
  import { isNonPlainObject, isPlainObject, isPrimitive, isTypedArray } from "../is.mjs";
2
- import { createCompareHandlers } from "./handlers.mjs";
2
+ import { createCompareHandler } from "./handlers.mjs";
3
3
  //#region src/internal/value/equal.ts
4
+ function Equalizer(options) {
5
+ this[EQUAL_SYMBOL] = options;
6
+ }
7
+ Equalizer.prototype.compare = compare;
8
+ Equalizer.prototype.deregister = deregisterEqualizer;
9
+ Equalizer.prototype.register = registerEqualizer;
10
+ function compare(first, second) {
11
+ return equalValue(first, second, this[EQUAL_SYMBOL]);
12
+ }
4
13
  function createEqualOptions(input) {
5
14
  const options = {
6
15
  ignoreCase: false,
@@ -35,7 +44,7 @@ function createEqualOptions(input) {
35
44
  * @param constructor Class constructor
36
45
  */
37
46
  function deregisterEqualizer(constructor) {
38
- equal.handlers.deregister(constructor);
47
+ equalHandler.base.deregister(constructor);
39
48
  }
40
49
  function filterKey(key, options) {
41
50
  if (typeof key !== "string") return true;
@@ -46,10 +55,6 @@ function filterKey(key, options) {
46
55
  function equal(first, second, options) {
47
56
  return equalValue(first, second, createEqualOptions(options));
48
57
  }
49
- equal.handlers = createCompareHandlers(equal, { callback: Object.is });
50
- equal.deregister = deregisterEqualizer;
51
- equal.initialize = initializeEqualizer;
52
- equal.register = registerEqualizer;
53
58
  function equalArray(first, second, options) {
54
59
  const { length } = first;
55
60
  if (length !== second.length) return false;
@@ -138,7 +143,7 @@ function equalValue(first, second, options) {
138
143
  case Array.isArray(first) && Array.isArray(second): return equalArray(first, second, options);
139
144
  case isPlainObject(first) && isPlainObject(second): return equalPlainObject(first, second, options);
140
145
  case isTypedArray(first) && isTypedArray(second): return equalTypedArray(first, second);
141
- default: return equal.handlers.handle(first, second, options);
146
+ default: return equalHandler.handle(first, second, options);
142
147
  }
143
148
  }
144
149
  /**
@@ -150,11 +155,7 @@ function equalValue(first, second, options) {
150
155
  * @returns Equalizer function
151
156
  */
152
157
  function initializeEqualizer(options) {
153
- const actual = createEqualOptions(options);
154
- const equalizer = (first, second) => equalValue(first, second, actual);
155
- equalizer.deregister = deregisterEqualizer;
156
- equalizer.register = registerEqualizer;
157
- return equalizer;
158
+ return new Equalizer(createEqualOptions(options));
158
159
  }
159
160
  /**
160
161
  * Register a equality comparison function for a specific class
@@ -165,12 +166,18 @@ function initializeEqualizer(options) {
165
166
  * @param handler Comparison function
166
167
  */
167
168
  function registerEqualizer(constructor, handler) {
168
- equal.handlers.register(constructor, handler);
169
+ equalHandler.base.register(constructor, handler);
169
170
  }
170
171
  const EQUAL_ARRAY_PEEK_PERCENTAGE = 10;
171
172
  const EQUAL_ARRAY_THRESHOLD = 100;
172
173
  const EQUAL_ERROR_PROPERTIES = ["name", "message"];
173
174
  const EQUAL_EXPRESSION_PROPERTIES = ["source", "flags"];
174
175
  const EQUAL_MINIMUM_LENGTH_FOR_SET = 16;
176
+ const EQUAL_SYMBOL = Symbol("equal");
177
+ const equalHandler = createCompareHandler(equal, { callback: Object.is });
178
+ equal.deregister = deregisterEqualizer;
179
+ equal.handler = equalHandler;
180
+ equal.initialize = initializeEqualizer;
181
+ equal.register = registerEqualizer;
175
182
  //#endregion
176
183
  export { deregisterEqualizer, equal, initializeEqualizer, registerEqualizer };
@@ -17,7 +17,7 @@ import { NestedKeys, NestedValue, PlainObject } from "../../models.mjs";
17
17
  * getValue(data, 'foo.nope'); // => undefined
18
18
  * ```
19
19
  */
20
- declare function getValue<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path): NestedValue<Data, Path>;
20
+ export declare function getValue<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path): NestedValue<Data, Path>;
21
21
  /**
22
22
  * Get the value from an object using an unknown path
23
23
  *
@@ -36,6 +36,5 @@ declare function getValue<Data extends PlainObject, Path extends NestedKeys<Data
36
36
  * getValue(data, 'foo.nope'); // => undefined
37
37
  * ```
38
38
  */
39
- declare function getValue<Data extends PlainObject>(data: Data, path: string, ignoreCase?: boolean): unknown;
40
- //#endregion
41
- export { getValue };
39
+ export declare function getValue<Data extends PlainObject>(data: Data, path: string, ignoreCase?: boolean): unknown;
40
+ //#endregion
@@ -1,18 +1,28 @@
1
1
  import { Constructor, GenericCallback } from "../../models.mjs";
2
2
  //#region src/internal/value/handlers.d.ts
3
- type Options = {
3
+ type BaseHandler = {
4
+ handlers: WeakMap<WeakKey, string | GenericCallback>;
5
+ options: BaseHandlerOptions;
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;
10
+ };
11
+ type BaseHandlerOptions = {
4
12
  callback: GenericCallback;
5
13
  method?: string;
6
14
  };
7
- declare function createCompareHandlers<Value>(owner: GenericCallback, options: Options): {
8
- deregister(constructor: Constructor): void;
15
+ type CompareHandler<Value> = {
16
+ base: BaseHandler;
9
17
  handle(first: unknown, second: unknown, ...parameters: unknown[]): Value;
10
- register(constructor: Constructor, handler?: string | GenericCallback): void;
11
18
  };
12
- declare function createSelfHandlers(owner: GenericCallback, options: Options): {
13
- deregister(constructor: Constructor): void;
19
+ type ValueHandler = {
20
+ base: BaseHandler;
14
21
  handle(value: unknown, ...parameters: unknown[]): unknown;
15
- register(constructor: Constructor, handler?: string | GenericCallback): void;
16
22
  };
17
- //#endregion
18
- export { createCompareHandlers, createSelfHandlers };
23
+ declare function BaseHandler(this: any, owner: GenericCallback, options: BaseHandlerOptions): void;
24
+ declare function CompareHandler(this: any, base: BaseHandler): void;
25
+ declare function ValueHandler(this: any, value: BaseHandler): void;
26
+ export declare function createCompareHandler<Value>(owner: GenericCallback, options: BaseHandlerOptions): CompareHandler<Value>;
27
+ export declare function createValueHandler(owner: GenericCallback, options: BaseHandlerOptions): ValueHandler;
28
+ //#endregion
@@ -1,57 +1,55 @@
1
1
  import { isNonConstructor } from "../is.mjs";
2
2
  //#region src/internal/value/handlers.ts
3
- function createCompareHandlers(owner, options) {
4
- const handlers = createHandlers(owner, options);
5
- return {
6
- deregister(constructor) {
7
- handlers.deregister(constructor);
8
- },
9
- handle(first, second, ...parameters) {
10
- const handler = handlers.get(first, second);
11
- if (handler == null) return options.callback(first, second, ...parameters);
12
- return typeof handler === "function" ? handler(first, second) : first[handler](second);
13
- },
14
- register(constructor, handler) {
15
- handlers.register(constructor, handler);
16
- }
17
- };
18
- }
19
- function createHandlers(owner, options) {
20
- const handlers = /* @__PURE__ */ new WeakMap();
21
- return {
22
- deregister(constructor) {
23
- handlers.delete(constructor);
24
- },
25
- get(first, second) {
26
- if (isConstructable(first) && isConstructable(second) && first.constructor === second.constructor) return handlers.get(first.constructor);
27
- },
28
- register(constructor, handler) {
29
- if (isNonConstructor(constructor) || handler === owner) return;
30
- let actual = handler ?? options.method;
31
- if (typeof actual !== "function" && typeof actual !== "string") return;
32
- if (typeof actual === "string") actual = typeof constructor.prototype[actual] === "function" ? actual : void 0;
33
- if (actual != null) handlers.set(constructor, actual);
34
- }
35
- };
36
- }
37
- function createSelfHandlers(owner, options) {
38
- const handlers = createHandlers(owner, options);
39
- return {
40
- deregister(constructor) {
41
- handlers.deregister(constructor);
42
- },
43
- handle(value, ...parameters) {
44
- const handler = handlers.get(value, value);
45
- if (handler == null) return options.callback(value, ...parameters);
46
- return typeof handler === "function" ? handler(value) : value[handler]();
47
- },
48
- register(constructor, handler) {
49
- handlers.register(constructor, handler);
50
- }
51
- };
3
+ function BaseHandler(owner, options) {
4
+ this.handlers = /* @__PURE__ */ new WeakMap();
5
+ this.owner = owner;
6
+ this.options = options;
7
+ }
8
+ BaseHandler.prototype.deregister = deregisterHandler;
9
+ BaseHandler.prototype.get = getHandler;
10
+ BaseHandler.prototype.register = registerHandler;
11
+ function CompareHandler(base) {
12
+ this.base = base;
13
+ }
14
+ CompareHandler.prototype.handle = handleComparison;
15
+ function ValueHandler(value) {
16
+ this.base = value;
17
+ }
18
+ ValueHandler.prototype.handle = handleValue;
19
+ function createBaseHandler(owner, options) {
20
+ return new BaseHandler(owner, options);
21
+ }
22
+ function createCompareHandler(owner, options) {
23
+ return new CompareHandler(createBaseHandler(owner, options));
24
+ }
25
+ function createValueHandler(owner, options) {
26
+ return new ValueHandler(createBaseHandler(owner, options));
27
+ }
28
+ function deregisterHandler(value) {
29
+ this.handlers.delete(value);
30
+ }
31
+ function getHandler(first, second) {
32
+ if (isConstructable(first) && isConstructable(second) && first.constructor === second.constructor) return this.handlers.get(first.constructor);
33
+ }
34
+ function handleComparison(first, second, ...parameters) {
35
+ const handler = this.base.get(first, second);
36
+ if (handler == null) return this.base.options.callback(first, second, ...parameters);
37
+ return typeof handler === "function" ? handler(first, second) : first[handler](second);
52
38
  }
53
39
  function isConstructable(value) {
54
40
  return typeof value === "object" && value !== null;
55
41
  }
42
+ function registerHandler(constructor, handler) {
43
+ if (isNonConstructor(constructor) || handler === this.owner) return;
44
+ let actual = handler ?? this.options.method;
45
+ if (typeof actual !== "function" && typeof actual !== "string") return;
46
+ if (typeof actual === "string") actual = typeof constructor.prototype[actual] === "function" ? actual : void 0;
47
+ if (actual != null) this.handlers.set(constructor, actual);
48
+ }
49
+ function handleValue(value, ...parameters) {
50
+ const handler = this.base.get(value, value);
51
+ if (handler == null) return this.base.options.callback(value, ...parameters);
52
+ return typeof handler === "function" ? handler(value) : value[handler]();
53
+ }
56
54
  //#endregion
57
- export { createCompareHandlers, createSelfHandlers };
55
+ export { createCompareHandler, createValueHandler };