@oscarpalmer/atoms 0.201.0 → 0.203.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 (156) 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/find.d.mts +10 -11
  5. package/dist/array/first.d.mts +10 -11
  6. package/dist/array/flatten.d.mts +2 -3
  7. package/dist/array/from.d.mts +7 -8
  8. package/dist/array/group-by.d.mts +16 -185
  9. package/dist/array/insert.d.mts +3 -4
  10. package/dist/array/intersection.d.mts +4 -5
  11. package/dist/array/last.d.mts +10 -11
  12. package/dist/array/move.d.mts +9 -10
  13. package/dist/array/partition.d.mts +5 -6
  14. package/dist/array/push.d.mts +2 -3
  15. package/dist/array/reverse.d.mts +2 -3
  16. package/dist/array/select.d.mts +11 -12
  17. package/dist/array/single.d.mts +4 -5
  18. package/dist/array/slice.d.mts +12 -13
  19. package/dist/array/sort.d.mts +31 -32
  20. package/dist/array/sort.mjs +41 -33
  21. package/dist/array/splice.d.mts +5 -6
  22. package/dist/array/swap.d.mts +8 -9
  23. package/dist/array/to-map.d.mts +18 -175
  24. package/dist/array/to-record.d.mts +18 -175
  25. package/dist/array/to-set.d.mts +4 -5
  26. package/dist/array/toggle.d.mts +4 -5
  27. package/dist/array/union.d.mts +4 -5
  28. package/dist/array/unique.d.mts +4 -5
  29. package/dist/array/update.d.mts +4 -5
  30. package/dist/beacon.d.mts +8 -9
  31. package/dist/beacon.mjs +90 -65
  32. package/dist/color/constants.d.mts +14 -13
  33. package/dist/color/constants.mjs +9 -2
  34. package/dist/color/index.d.mts +2 -2
  35. package/dist/color/instance.d.mts +3 -3
  36. package/dist/color/instance.mjs +125 -61
  37. package/dist/color/misc/alpha.d.mts +4 -5
  38. package/dist/color/misc/format.d.mts +6 -7
  39. package/dist/color/misc/format.mjs +15 -12
  40. package/dist/color/misc/get.d.mts +14 -15
  41. package/dist/color/misc/is.d.mts +16 -17
  42. package/dist/color/misc/is.mjs +3 -4
  43. package/dist/color/misc/state.d.mts +8 -10
  44. package/dist/color/misc/state.mjs +22 -20
  45. package/dist/color/models.d.mts +25 -19
  46. package/dist/color/space/hex.d.mts +9 -10
  47. package/dist/color/space/hsl.d.mts +8 -9
  48. package/dist/color/space/hwb.d.mts +9 -10
  49. package/dist/color/space/rgb.d.mts +11 -12
  50. package/dist/function/limit.d.mts +9 -8
  51. package/dist/function/limit.mjs +5 -5
  52. package/dist/function/once.d.mts +26 -6
  53. package/dist/function/once.mjs +135 -76
  54. package/dist/function/retry.d.mts +5 -6
  55. package/dist/function/retry.mjs +3 -3
  56. package/dist/index.d.mts +244 -584
  57. package/dist/index.mjs +1191 -810
  58. package/dist/internal/abort.d.mts +3 -4
  59. package/dist/internal/array/callbacks.d.mts +4 -5
  60. package/dist/internal/array/chunk.d.mts +2 -3
  61. package/dist/internal/array/compact.d.mts +3 -4
  62. package/dist/internal/array/find.d.mts +10 -11
  63. package/dist/internal/array/get.d.mts +7 -8
  64. package/dist/internal/array/group.d.mts +2 -3
  65. package/dist/internal/array/index-of.d.mts +10 -11
  66. package/dist/internal/array/insert.d.mts +5 -6
  67. package/dist/internal/array/match.d.mts +17 -18
  68. package/dist/internal/array/overlap.d.mts +2 -3
  69. package/dist/internal/array/sets.d.mts +5 -6
  70. package/dist/internal/array/shuffle.d.mts +2 -3
  71. package/dist/internal/array/update.d.mts +2 -3
  72. package/dist/internal/defaults.d.mts +3 -4
  73. package/dist/internal/function/assert.d.mts +10 -11
  74. package/dist/internal/function/limit.d.mts +41 -0
  75. package/dist/internal/function/limit.mjs +110 -0
  76. package/dist/internal/function/memoize.d.mts +4 -5
  77. package/dist/internal/function/memoize.mjs +53 -31
  78. package/dist/internal/function/misc.d.mts +2 -3
  79. package/dist/internal/function/work.d.mts +53 -54
  80. package/dist/internal/herald.d.mts +8 -9
  81. package/dist/internal/herald.mjs +38 -26
  82. package/dist/internal/is.d.mts +19 -20
  83. package/dist/internal/math/aggregate.d.mts +14 -15
  84. package/dist/internal/math/misc.d.mts +3 -4
  85. package/dist/internal/number.d.mts +4 -5
  86. package/dist/internal/random.d.mts +3 -4
  87. package/dist/internal/result/misc.d.mts +29 -14
  88. package/dist/internal/result/misc.mjs +4 -0
  89. package/dist/internal/result/models.d.mts +9 -10
  90. package/dist/internal/sized/map.d.mts +2 -3
  91. package/dist/internal/sized/misc.d.mts +2 -3
  92. package/dist/internal/string/case.d.mts +9 -10
  93. package/dist/internal/string/misc.d.mts +8 -9
  94. package/dist/internal/subscription.d.mts +14 -15
  95. package/dist/internal/subscription.mjs +69 -57
  96. package/dist/internal/value/compare.d.mts +17 -9
  97. package/dist/internal/value/compare.mjs +7 -7
  98. package/dist/internal/value/equal.d.mts +34 -15
  99. package/dist/internal/value/equal.mjs +28 -13
  100. package/dist/internal/value/get.d.mts +3 -4
  101. package/dist/internal/value/handlers.d.mts +19 -9
  102. package/dist/internal/value/handlers.mjs +50 -50
  103. package/dist/internal/value/has.d.mts +6 -7
  104. package/dist/internal/value/misc.d.mts +6 -7
  105. package/dist/internal/value/partial.d.mts +3 -4
  106. package/dist/internal/value/set.d.mts +4 -5
  107. package/dist/is.d.mts +13 -13
  108. package/dist/logger.d.mts +5 -4
  109. package/dist/logger.mjs +61 -54
  110. package/dist/math.d.mts +20 -20
  111. package/dist/models.d.mts +58 -46
  112. package/dist/promise/constants.d.mts +14 -15
  113. package/dist/promise/delay.d.mts +3 -4
  114. package/dist/promise/delay.mjs +4 -4
  115. package/dist/promise/helpers.d.mts +7 -8
  116. package/dist/promise/index.d.mts +13 -14
  117. package/dist/promise/misc.d.mts +6 -6
  118. package/dist/promise/models.d.mts +16 -17
  119. package/dist/promise/timed.d.mts +4 -5
  120. package/dist/promise/timed.mjs +5 -5
  121. package/dist/query.d.mts +3 -4
  122. package/dist/queue.d.mts +14 -15
  123. package/dist/queue.mjs +185 -129
  124. package/dist/random.d.mts +8 -8
  125. package/dist/result/index.d.mts +8 -9
  126. package/dist/result/match.d.mts +6 -7
  127. package/dist/result/misc.d.mts +3 -3
  128. package/dist/result/work/flow.d.mts +28 -29
  129. package/dist/result/work/pipe.d.mts +24 -25
  130. package/dist/sized/set.d.mts +2 -3
  131. package/dist/string/fuzzy.d.mts +21 -12
  132. package/dist/string/fuzzy.mjs +77 -49
  133. package/dist/string/index.d.mts +8 -8
  134. package/dist/string/match.d.mts +4 -5
  135. package/dist/string/normalize.d.mts +8 -9
  136. package/dist/string/normalize.mjs +9 -4
  137. package/dist/string/template.d.mts +9 -9
  138. package/dist/string/template.mjs +11 -4
  139. package/dist/value/clone.d.mts +21 -13
  140. package/dist/value/clone.mjs +22 -20
  141. package/dist/value/collection.d.mts +4 -5
  142. package/dist/value/diff.d.mts +5 -6
  143. package/dist/value/freeze.d.mts +18 -19
  144. package/dist/value/freeze.mjs +1 -1
  145. package/dist/value/merge.d.mts +13 -14
  146. package/dist/value/merge.mjs +21 -5
  147. package/dist/value/omit.d.mts +2 -3
  148. package/dist/value/pick.d.mts +2 -3
  149. package/dist/value/shake.d.mts +3 -4
  150. package/dist/value/smush.d.mts +3 -4
  151. package/dist/value/transform.d.mts +15 -10
  152. package/dist/value/transform.mjs +12 -7
  153. package/dist/value/unsmush.d.mts +3 -4
  154. package/package.json +1 -1
  155. package/dist/internal/function/timer.d.mts +0 -10
  156. package/dist/internal/function/timer.mjs +0 -87
@@ -1,13 +1,13 @@
1
- import { Key, KeyedValue, PlainObject, Simplify } from "../models.mjs";
1
+ import { Key, KeyOrCallback, KeyOrCallbackKey, KeyOrCallbackValue, Simplify } from "../models.mjs";
2
2
  //#region src/array/to-record.d.ts
3
3
  /**
4
- * Create a record from an array of items using callbacks
4
+ * Create a record from an array of items using a specific key
5
5
  *
6
6
  * If multiple items have the same key, the latest item will be used
7
7
  *
8
8
  * @param array Array to convert
9
- * @param key Callback to get an item's grouping key
10
- * @param value Callback to get an item's value
9
+ * @param key Callback or key to use for grouping
10
+ * @param value Callback or key to use for value, defaulting to the item itself
11
11
  * @returns Record of keyed values
12
12
  *
13
13
  * @example
@@ -19,74 +19,14 @@ import { Key, KeyedValue, PlainObject, Simplify } from "../models.mjs";
19
19
  * ); // => { 10: 3, 20: 2 }
20
20
  * ```
21
21
  */
22
- declare function toRecord<Item, KeyCallback extends (item: Item, index: number, array: Item[]) => Key, ValueCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], key: KeyCallback, value: ValueCallback): Record<ReturnType<KeyCallback>, ReturnType<ValueCallback>>;
22
+ export declare function toRecord<Item, KeyArg extends KeyOrCallback<Item, Key>, ValueArg extends KeyOrCallback<Item>>(array: Item[], key: KeyArg, value: ValueArg): Simplify<Record<KeyOrCallbackKey<Item, KeyArg>, KeyOrCallbackValue<Item, ValueArg>>>;
23
23
  /**
24
- * Create a record from an array of items using a callback and value
24
+ * Create a record from an array of items using a specific key
25
25
  *
26
26
  * If multiple items have the same key, the latest item will be used
27
27
  *
28
28
  * @param array Array to convert
29
- * @param callback Callback to get an item's grouping key
30
- * @param value Key to use for value
31
- * @returns Record with keys
32
- *
33
- * @example
34
- * ```typescript
35
- * toRecord(
36
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
37
- * item => item.value,
38
- * 'id',
39
- * ); // => { 10: 3, 20: 2 }
40
- * ```
41
- */
42
- declare function toRecord<Item extends PlainObject, Callback extends (item: Item, index: number, array: Item[]) => Key, ItemValue extends keyof Item>(array: Item[], callback: Callback, value: ItemValue): Record<ReturnType<Callback>, Item[ItemValue]>;
43
- /**
44
- * Create a record from an array of items using a key and callback
45
- *
46
- * If multiple items have the same key, the latest item will be used
47
- *
48
- * @param array Array to convert
49
- * @param key Key to use for grouping
50
- * @param callback Callback to get an item's value
51
- * @returns Record with keys
52
- *
53
- * @example
54
- * ```typescript
55
- * toRecord(
56
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
57
- * 'value',
58
- * item => item.id,
59
- * ); // => { 10: 3, 20: 2 }
60
- * ```
61
- */
62
- declare function toRecord<Item extends PlainObject, ItemKey extends keyof Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], key: ItemKey, callback: Callback): Simplify<Record<KeyedValue<Item, ItemKey>, ReturnType<Callback>>>;
63
- /**
64
- * Create a record from an array of items using a key and value
65
- *
66
- * If multiple items have the same key, the latest item will be used
67
- *
68
- * @param array Array to convert
69
- * @param key Key to use for grouping
70
- * @param value Key to use for value
71
- * @returns Record of keyed values
72
- *
73
- * @example
74
- * ```typescript
75
- * toRecord(
76
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
77
- * 'value',
78
- * 'id',
79
- * ); // => { 10: 3, 20: 2 }
80
- * ```
81
- */
82
- declare function toRecord<Item extends PlainObject, ItemKey extends keyof Item, ItemValue extends keyof Item>(array: Item[], key: ItemKey, value: ItemValue): Simplify<Record<KeyedValue<Item, ItemKey>, Item[ItemValue]>>;
83
- /**
84
- * Create a record from an array of items using a callback
85
- *
86
- * If multiple items have the same key, the latest item will be used
87
- *
88
- * @param array Array to convert
89
- * @param callback Callback to get an item's grouping key
29
+ * @param key Callback or key to use for grouping
90
30
  * @returns Record of keyed values
91
31
  *
92
32
  * @example
@@ -97,25 +37,7 @@ declare function toRecord<Item extends PlainObject, ItemKey extends keyof Item,
97
37
  * ); // => { 10: {id: 3, value: 10}, 20: {id: 2, value: 20} }
98
38
  * ```
99
39
  */
100
- declare function toRecord<Item, Callback extends (item: Item, index: number, array: Item[]) => Key>(array: Item[], callback: Callback): Record<ReturnType<Callback>, Item>;
101
- /**
102
- * Create a record from an array of items using a key
103
- *
104
- * If multiple items have the same key, the latest item will be used
105
- *
106
- * @param array Array to convert
107
- * @param key Key to use for grouping
108
- * @returns Record of keyed values
109
- *
110
- * @example
111
- * ```typescript
112
- * toRecord(
113
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
114
- * 'value',
115
- * ); // => { 10: {id: 3, value: 10}, 20: {id: 2, value: 20} }
116
- * ```
117
- */
118
- declare function toRecord<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey): Simplify<Record<KeyedValue<Item, ItemKey>, Item>>;
40
+ export declare function toRecord<Item, KeyArg extends KeyOrCallback<Item, Key>>(array: Item[], key: KeyArg): Simplify<Record<KeyOrCallbackKey<Item, KeyArg>, Item>>;
119
41
  /**
120
42
  * Create a record from an array of items _(using indices as keys)_
121
43
  *
@@ -129,38 +51,18 @@ declare function toRecord<Item extends PlainObject, ItemKey extends keyof Item>(
129
51
  * ); // => { 0: {id: 1, value: 10}, 1: {id: 2, value: 20}, 2: {id: 3, value: 10} }
130
52
  * ```
131
53
  */
132
- declare function toRecord<Item>(array: Item[]): Record<number, Item>;
133
- declare namespace toRecord {
54
+ export declare function toRecord<Item>(array: Item[]): Record<number, Item>;
55
+ export declare namespace toRecord {
134
56
  var arrays: typeof toRecordArrays;
135
57
  }
136
58
  /**
137
- * Create a record from an array of items using callbacks, grouping values into arrays
138
- *
139
- * _Available as `toRecordArrays` and `toRecord.arrays`_
140
- *
141
- * @param array Array to convert
142
- * @param key Callback to get an item's grouping key
143
- * @param value Callback to get an item's value
144
- * @returns Record of keyed arrays of values
145
- *
146
- * @example
147
- * ```typescript
148
- * toRecordArrays(
149
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
150
- * item => item.value,
151
- * item => item.id,
152
- * ); // => { 10: [1, 3], 20: [2] }
153
- * ```
154
- */
155
- declare function toRecordArrays<Item, KeyCallback extends (item: Item, index: number, array: Item[]) => Key, ValueCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], key: KeyCallback, value: ValueCallback): Record<ReturnType<KeyCallback>, Array<ReturnType<ValueCallback>>>;
156
- /**
157
- * Create a record from an array of items using a callback and value, grouping values into arrays
59
+ * Create a record from an array of items using a specific key, grouping values into arrays
158
60
  *
159
61
  * _Available as `toRecordArrays` and `toRecord.arrays`_
160
62
  *
161
63
  * @param array Array to convert
162
- * @param callback Callback to get an item's grouping key
163
- * @param value Key to use for value
64
+ * @param key Callback or key to use for grouping
65
+ * @param value Callback or key to use for value, defaulting to the item itself
164
66
  * @returns Record of keyed arrays of values
165
67
  *
166
68
  * @example
@@ -168,58 +70,18 @@ declare function toRecordArrays<Item, KeyCallback extends (item: Item, index: nu
168
70
  * toRecordArrays(
169
71
  * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
170
72
  * item => item.value,
171
- * 'id',
172
- * ); // => { 10: [1, 3], 20: [2] }
173
- * ```
174
- */
175
- declare function toRecordArrays<Item extends PlainObject, Callback extends (item: Item, index: number, array: Item[]) => Key, ItemValue extends keyof Item>(array: Item[], callback: Callback, value: ItemValue): Record<ReturnType<Callback>, Item[ItemValue][]>;
176
- /**
177
- * Create a record from an array of items using a key and callback, grouping values into arrays
178
- *
179
- * _Available as `toRecordArrays` and `toRecord.arrays`_
180
- *
181
- * @param array Array to convert
182
- * @param key Key to use for grouping
183
- * @param callback Callback to get an item's value
184
- * @returns Record of keyed arrays of values
185
- *
186
- * @example
187
- * ```typescript
188
- * toRecordArrays(
189
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
190
- * 'value',
191
73
  * item => item.id,
192
74
  * ); // => { 10: [1, 3], 20: [2] }
193
75
  * ```
194
76
  */
195
- declare function toRecordArrays<Item extends PlainObject, ItemKey extends keyof Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], key: ItemKey, callback: Callback): Simplify<Record<KeyedValue<Item, ItemKey>, Array<ReturnType<Callback>>>>;
77
+ export declare function toRecordArrays<Item, KeyArg extends KeyOrCallback<Item, Key>, ValueArg extends KeyOrCallback<Item>>(array: Item[], key: KeyArg, value: ValueArg): Simplify<Record<KeyOrCallbackKey<Item, KeyArg>, Array<KeyOrCallbackValue<Item, ValueArg>>>>;
196
78
  /**
197
- * Create a record from an array of items using a key and value, grouping values into arrays
79
+ * Create a record from an array of items using a specific key, grouping items into arrays
198
80
  *
199
81
  * _Available as `toRecordArrays` and `toRecord.arrays`_
200
82
  *
201
83
  * @param array Array to convert
202
- * @param key Key to use for grouping
203
- * @param value Key to use for value
204
- * @returns Record of keyed arrays of values
205
- *
206
- * @example
207
- * ```typescript
208
- * toRecordArrays(
209
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
210
- * 'value',
211
- * 'id',
212
- * ); // => { 10: [1, 3], 20: [2] }
213
- * ```
214
- */
215
- declare function toRecordArrays<Item extends PlainObject, ItemKey extends keyof Item, ItemValue extends keyof Item>(array: Item[], key: ItemKey, value: ItemValue): Simplify<Record<KeyedValue<Item, ItemKey>, Item[ItemValue][]>>;
216
- /**
217
- * Create a record from an array of items using a callback, grouping items into arrays
218
- *
219
- * _Available as `toRecordArrays` and `toRecord.arrays`_
220
- *
221
- * @param array Array to convert
222
- * @param callback Callback to get an item's grouping key
84
+ * @param key Callback or key to use for grouping
223
85
  * @returns Record of keyed arrays of items
224
86
  *
225
87
  * @example
@@ -230,24 +92,5 @@ declare function toRecordArrays<Item extends PlainObject, ItemKey extends keyof
230
92
  * ); // => { 10: [{id: 1, value: 10}, {id: 3, value: 10}], 20: [{id: 2, value: 20}] }
231
93
  * ```
232
94
  */
233
- declare function toRecordArrays<Item, Callback extends (item: Item, index: number, array: Item[]) => Key>(array: Item[], callback: Callback): Record<ReturnType<Callback>, Item[]>;
234
- /**
235
- * Create a record from an array of items using a key, grouping items into arrays
236
- *
237
- * _Available as `toRecordArrays` and `toRecord.arrays`_
238
- *
239
- * @param array Array to convert
240
- * @param key Key to use for grouping
241
- * @returns Record of keyed arrays of items
242
- *
243
- * @example
244
- * ```typescript
245
- * toRecordArrays(
246
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
247
- * 'value',
248
- * ); // => { 10: [{id: 1, value: 10}, {id: 3, value: 10}], 20: [{id: 2, value: 20}] }
249
- * ```
250
- */
251
- declare function toRecordArrays<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey): Simplify<Record<KeyedValue<Item, ItemKey>, Item[]>>;
252
- //#endregion
253
- export { toRecord, toRecordArrays };
95
+ export declare function toRecordArrays<Item, KeyArg extends KeyOrCallback<Item, Key>>(array: Item[], key: KeyArg): Simplify<Record<KeyOrCallbackKey<Item, KeyArg>, Item[]>>;
96
+ //#endregion
@@ -15,7 +15,7 @@ import { PlainObject } from "../models.mjs";
15
15
  * ); // => Set { 1, 2, 3 }
16
16
  * ```
17
17
  */
18
- declare function toSet<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], callback: Callback): Set<ReturnType<Callback>>;
18
+ export declare function toSet<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], callback: Callback): Set<ReturnType<Callback>>;
19
19
  /**
20
20
  * Create a _Set_ from an array of items using a key
21
21
  *
@@ -31,7 +31,7 @@ declare function toSet<Item, Callback extends (item: Item, index: number, array:
31
31
  * ); // => Set { 1, 2, 3 }
32
32
  * ```
33
33
  */
34
- declare function toSet<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey): Set<Item[ItemKey]>;
34
+ export declare function toSet<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey): Set<Item[ItemKey]>;
35
35
  /**
36
36
  * Create a _Set_ from an array of items
37
37
  *
@@ -43,6 +43,5 @@ declare function toSet<Item extends PlainObject, ItemKey extends keyof Item>(arr
43
43
  * toSet([1, 2, 3]); // => Set { 1, 2, 3 }
44
44
  * ```
45
45
  */
46
- declare function toSet<Item>(array: Item[]): Set<Item>;
47
- //#endregion
48
- export { toSet };
46
+ export declare function toSet<Item>(array: Item[]): Set<Item>;
47
+ //#endregion
@@ -19,7 +19,7 @@ import { PlainObject } from "../models.mjs";
19
19
  * ); // => [{id: 1}, {id: 3}, {id: 4}]
20
20
  * ```
21
21
  */
22
- declare function toggle<Item>(destination: Item[], toggled: Item[], callback: (item: Item, index: number, array: Item[]) => unknown): Item[];
22
+ export declare function toggle<Item>(destination: Item[], toggled: Item[], callback: (item: Item, index: number, array: Item[]) => unknown): Item[];
23
23
  /**
24
24
  * Toggle an item in an array
25
25
  *
@@ -39,7 +39,7 @@ declare function toggle<Item>(destination: Item[], toggled: Item[], callback: (i
39
39
  * ); // => [{id: 1}, {id: 3}, {id: 4}]
40
40
  * ```
41
41
  */
42
- declare function toggle<Item extends PlainObject, ItemKey extends keyof Item>(destination: Item[], toggled: Item[], key: ItemKey): Item[];
42
+ export declare function toggle<Item extends PlainObject, ItemKey extends keyof Item>(destination: Item[], toggled: Item[], key: ItemKey): Item[];
43
43
  /**
44
44
  * Toggle an item in an array
45
45
  *
@@ -57,6 +57,5 @@ declare function toggle<Item extends PlainObject, ItemKey extends keyof Item>(de
57
57
  * ); // => [1, 3, 4]
58
58
  * ```
59
59
  */
60
- declare function toggle<Item>(destination: Item[], toggled: Item[]): Item[];
61
- //#endregion
62
- export { toggle };
60
+ export declare function toggle<Item>(destination: Item[], toggled: Item[]): Item[];
61
+ //#endregion
@@ -16,7 +16,7 @@
16
16
  * ); // => [{id: 1}, {id: 2}, {id: 3}, {id: 4}]
17
17
  * ```
18
18
  */
19
- declare function union<First, Second>(first: First[], second: Second[], callback: (item: First | Second) => unknown): (First | Second)[];
19
+ export declare function union<First, Second>(first: First[], second: Second[], callback: (item: First | Second) => unknown): (First | Second)[];
20
20
  /**
21
21
  * Get the combined, unique values from two arrays
22
22
  *
@@ -34,7 +34,7 @@ declare function union<First, Second>(first: First[], second: Second[], callback
34
34
  * ); // => [{id: 1}, {id: 2}, {id: 3}, {id: 4}]
35
35
  * ```
36
36
  */
37
- declare function union<First extends Record<string, unknown>, Second extends Record<string, unknown>, SharedKey extends keyof First & keyof Second>(first: First[], second: Second[], key: SharedKey): (First | Second)[];
37
+ export declare function union<First extends Record<string, unknown>, Second extends Record<string, unknown>, SharedKey extends keyof First & keyof Second>(first: First[], second: Second[], key: SharedKey): (First | Second)[];
38
38
  /**
39
39
  * Get the combined, unique values from two arrays
40
40
  *
@@ -50,6 +50,5 @@ declare function union<First extends Record<string, unknown>, Second extends Rec
50
50
  * ); // => [1, 2, 3, 4]
51
51
  * ```
52
52
  */
53
- declare function union<First, Second>(first: First[], second: Second[]): (First | Second)[];
54
- //#endregion
55
- export { union };
53
+ export declare function union<First, Second>(first: First[], second: Second[]): (First | Second)[];
54
+ //#endregion
@@ -15,7 +15,7 @@ import { PlainObject } from "../models.mjs";
15
15
  * ); // => [{id: 1}, {id: 2}, {id: 3}]
16
16
  * ```
17
17
  */
18
- declare function unique<Item>(array: Item[], callback: (item: Item, index: number, array: Item[]) => unknown): Item[];
18
+ export declare function unique<Item>(array: Item[], callback: (item: Item, index: number, array: Item[]) => unknown): Item[];
19
19
  /**
20
20
  * Get an array of unique items
21
21
  *
@@ -31,7 +31,7 @@ declare function unique<Item>(array: Item[], callback: (item: Item, index: numbe
31
31
  * ); // => [{id: 1}, {id: 2}, {id: 3}]
32
32
  * ```
33
33
  */
34
- declare function unique<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey): Item[];
34
+ export declare function unique<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey): Item[];
35
35
  /**
36
36
  * Get an array of unique items
37
37
  *
@@ -43,6 +43,5 @@ declare function unique<Item extends PlainObject, ItemKey extends keyof Item>(ar
43
43
  * unique([1, 2, 3, 2]); // => [1, 2, 3]
44
44
  * ```
45
45
  */
46
- declare function unique<Item>(array: Item[]): Item[];
47
- //#endregion
48
- export { unique };
46
+ export declare function unique<Item>(array: Item[]): Item[];
47
+ //#endregion
@@ -19,7 +19,7 @@ import { PlainObject } from "../models.mjs";
19
19
  * ); // => [{id: 1}, {id: 2, name: 'Updated'}, {id: 3}, {id: 4, name: 'New'}]
20
20
  * ```
21
21
  */
22
- declare function update<Item>(destination: Item[], updated: Item[], callback: (item: Item, index: number, array: Item[]) => unknown): Item[];
22
+ export declare function update<Item>(destination: Item[], updated: Item[], callback: (item: Item, index: number, array: Item[]) => unknown): Item[];
23
23
  /**
24
24
  * Update an item in an array
25
25
  *
@@ -39,7 +39,7 @@ declare function update<Item>(destination: Item[], updated: Item[], callback: (i
39
39
  * ); // => [{id: 1}, {id: 2, name: 'Updated'}, {id: 3}, {id: 4, name: 'New'}]
40
40
  * ```
41
41
  */
42
- declare function update<Item extends PlainObject, ItemKey extends keyof Item>(destination: Item[], updated: Item[], key: ItemKey): Item[];
42
+ export declare function update<Item extends PlainObject, ItemKey extends keyof Item>(destination: Item[], updated: Item[], key: ItemKey): Item[];
43
43
  /**
44
44
  * Update an item in an array
45
45
  *
@@ -57,6 +57,5 @@ declare function update<Item extends PlainObject, ItemKey extends keyof Item>(de
57
57
  * ); // => [1, 2, 3, 4]
58
58
  * ```
59
59
  */
60
- declare function update<Item>(destination: Item[], updated: Item[]): Item[];
61
- //#endregion
62
- export { update };
60
+ export declare function update<Item>(destination: Item[], updated: Item[]): Item[];
61
+ //#endregion
package/dist/beacon.d.mts CHANGED
@@ -3,7 +3,7 @@ import { Subscription } from "./internal/subscription.mjs";
3
3
  /**
4
4
  * A beacon is a lighthouse, holding an observable value that can be subscribed to and emitted from
5
5
  */
6
- type Beacon<Value> = {
6
+ export type Beacon<Value> = {
7
7
  /**
8
8
  * Is the beacon active?
9
9
  */
@@ -39,7 +39,7 @@ type Beacon<Value> = {
39
39
  */
40
40
  finish(): void;
41
41
  };
42
- type BeaconOptions<Value> = {
42
+ export type BeaconOptions<Value> = {
43
43
  /**
44
44
  * Method for comparing values for equality
45
45
  *
@@ -50,7 +50,7 @@ type BeaconOptions<Value> = {
50
50
  */
51
51
  equal?: (first: Value, second: Value) => boolean;
52
52
  };
53
- type Observable<Value> = {
53
+ export type Observable<Value> = {
54
54
  /**
55
55
  * Is the observable active?
56
56
  */
@@ -97,27 +97,26 @@ type Observer<Value> = {
97
97
  * @param options Beacon options
98
98
  * @returns Beacon instance
99
99
  */
100
- declare function beacon<Value>(value: Value, options?: BeaconOptions<Value>): Beacon<Value>;
100
+ export declare function beacon<Value>(value: Value, options?: BeaconOptions<Value>): Beacon<Value>;
101
101
  /**
102
102
  * Is the value a beacon?
103
103
  *
104
104
  * @param value Value to check
105
105
  * @returns `true` if the value is a beacon, otherwise `false`
106
106
  */
107
- declare function isBeacon<Value = unknown>(value: unknown): value is Beacon<Value>;
107
+ export declare function isBeacon<Value = unknown>(value: unknown): value is Beacon<Value>;
108
108
  /**
109
109
  * Is the value a beacon subscription?
110
110
  *
111
111
  * @param value Value to check
112
112
  * @returns `true` if the value is a beacon subscription, otherwise `false`
113
113
  */
114
- declare function isBeaconSubscription(value: unknown): value is Subscription;
114
+ export declare function isBeaconSubscription(value: unknown): value is Subscription;
115
115
  /**
116
116
  * Is the value an observable?
117
117
  *
118
118
  * @param value Value to check
119
119
  * @returns `true` if the value is an observable, otherwise `false`
120
120
  */
121
- declare function isObservable<Value = unknown>(value: unknown): value is Observable<Value>;
122
- //#endregion
123
- export { Beacon, BeaconOptions, Observable, beacon, isBeacon, isBeaconSubscription, isObservable };
121
+ export declare function isObservable<Value = unknown>(value: unknown): value is Observable<Value>;
122
+ //#endregion
package/dist/beacon.mjs CHANGED
@@ -2,6 +2,59 @@ import { noop } from "./internal/function/misc.mjs";
2
2
  import { isPlainObject } from "./internal/is.mjs";
3
3
  import { SUBSCRIPTION_NAME, isSubscription, subscriptions } from "./internal/subscription.mjs";
4
4
  //#region src/beacon.ts
5
+ const BEACON_PROPERTY = "$beacon";
6
+ const BEACON_NAME = "beacon";
7
+ const BEACON_OBSERVABLE = "observable";
8
+ function Beacon(value, options) {
9
+ Object.defineProperty(this, BEACON_SYMBOL, { value: {
10
+ value,
11
+ active: true,
12
+ options: createBeaconOptions(options)
13
+ } });
14
+ }
15
+ Object.defineProperties(Beacon.prototype, {
16
+ [BEACON_PROPERTY]: { value: BEACON_NAME },
17
+ active: {
18
+ enumerable: true,
19
+ get() {
20
+ return this[BEACON_SYMBOL].active;
21
+ }
22
+ },
23
+ deactivate: { value: deactivateBeacon },
24
+ emit: { value: emitBeaconValue },
25
+ error: { value: emitBeaconError },
26
+ finish: { value: finishBeacon },
27
+ observable: {
28
+ enumerable: true,
29
+ get() {
30
+ return getObservable(this[BEACON_SYMBOL]);
31
+ }
32
+ },
33
+ value: {
34
+ enumerable: true,
35
+ get() {
36
+ return this[BEACON_SYMBOL].value;
37
+ }
38
+ }
39
+ });
40
+ function Observable(beacon) {
41
+ Object.defineProperty(this, BEACON_SYMBOL, { value: {
42
+ beacon,
43
+ active: beacon.active
44
+ } });
45
+ }
46
+ Object.defineProperties(Observable.prototype, {
47
+ [BEACON_PROPERTY]: { value: BEACON_OBSERVABLE },
48
+ active: {
49
+ enumerable: true,
50
+ get() {
51
+ const state = this[BEACON_SYMBOL];
52
+ return state.beacon.active && state.active;
53
+ }
54
+ },
55
+ deactivate: { value: deactiveateObservable },
56
+ subscribe: { value: subscribeToObservable }
57
+ });
5
58
  /**
6
59
  * Create a new beacon
7
60
  *
@@ -10,61 +63,24 @@ import { SUBSCRIPTION_NAME, isSubscription, subscriptions } from "./internal/sub
10
63
  * @returns Beacon instance
11
64
  */
12
65
  function beacon(value, options) {
13
- const state = {
14
- value,
15
- active: true,
16
- observable: void 0,
17
- options: createBeaconOptions(options),
18
- subscriptions: subscriptions({ property: beaconSubscription })
19
- };
20
- const instance = {
21
- deactivate: () => finishBeacon(state, false),
22
- emit: (value, finish) => updateBeacon(BEACON_TYPE_NEXT, state, value, finish),
23
- error: (value, finish) => updateBeacon(BEACON_TYPE_ERROR, state, value, finish),
24
- finish: () => finishBeacon(state, true)
25
- };
26
- Object.defineProperties(instance, {
27
- [BEACON_PROPERTY]: { value: BEACON_NAME },
28
- active: {
29
- enumerable: true,
30
- get: () => state.active
31
- },
32
- observable: {
33
- enumerable: true,
34
- get: () => getObservable(state)
35
- },
36
- value: {
37
- enumerable: true,
38
- get: () => state.value
39
- }
40
- });
41
- return Object.freeze(instance);
66
+ return new Beacon(value, options);
67
+ }
68
+ function closeBeacon(state, emit) {
69
+ if (!state.active) return;
70
+ state.active = false;
71
+ const subscriptions = state.subscriptions?.values.to.any;
72
+ if (subscriptions != null && subscriptions.size > 0) {
73
+ for (const [, observer] of subscriptions) if (emit) observer.complete?.();
74
+ }
75
+ state.subscriptions?.clear();
76
+ state.observable?.deactivate();
77
+ state.observable = void 0;
42
78
  }
43
79
  function createBeaconOptions(input) {
44
80
  const options = isPlainObject(input) ? input : {};
45
81
  options.equal = typeof options.equal === "function" ? options.equal : Object.is;
46
82
  return options;
47
83
  }
48
- function createObservable(beacon) {
49
- const state = {
50
- beacon,
51
- active: beacon.active
52
- };
53
- const instance = {
54
- deactivate: () => {
55
- state.active = false;
56
- },
57
- subscribe: (first, second, third) => subscribeToObservable(state, first, second, third)
58
- };
59
- Object.defineProperties(instance, {
60
- [BEACON_PROPERTY]: { value: BEACON_OBSERVABLE },
61
- active: {
62
- enumerable: true,
63
- get: () => beacon.active && state.active
64
- }
65
- });
66
- return Object.freeze(instance);
67
- }
68
84
  function createObserver(first, second, third) {
69
85
  let observer = { next: noop };
70
86
  if (typeof first === "function") observer = {
@@ -79,17 +95,24 @@ function createObserver(first, second, third) {
79
95
  }
80
96
  return observer;
81
97
  }
82
- function finishBeacon(state, emit) {
83
- if (!state.active) return;
84
- state.active = false;
85
- for (const [, observer] of state.subscriptions.values.to.any) if (emit) observer.complete?.();
86
- state.subscriptions.clear();
87
- state.observable?.deactivate();
88
- state.observable = void 0;
98
+ function deactivateBeacon() {
99
+ closeBeacon(this[BEACON_SYMBOL], false);
100
+ }
101
+ function deactiveateObservable() {
102
+ this[BEACON_SYMBOL].active = false;
103
+ }
104
+ function emitBeaconError(error, finish) {
105
+ updateBeacon(this, BEACON_TYPE_ERROR, error, finish ?? false);
106
+ }
107
+ function emitBeaconValue(value, finish) {
108
+ updateBeacon(this, BEACON_TYPE_NEXT, value, finish ?? false);
109
+ }
110
+ function finishBeacon() {
111
+ closeBeacon(this[BEACON_SYMBOL], true);
89
112
  }
90
113
  function getObservable(state) {
91
114
  if (!state.active) throw new Error(BEACON_MESSAGE_RETRIEVE);
92
- state.observable ??= createObservable(state);
115
+ state.observable ??= new Observable(state);
93
116
  return state.observable;
94
117
  }
95
118
  function getObservableCallback(value) {
@@ -105,7 +128,7 @@ function isBeacon(value) {
105
128
  return isBeaconInstance(BEACON_NAME, value);
106
129
  }
107
130
  function isBeaconInstance(name, value) {
108
- return isPlainObject(value) && value[BEACON_PROPERTY] === name;
131
+ return typeof value === "object" && value !== null && value[BEACON_PROPERTY] === name;
109
132
  }
110
133
  /**
111
134
  * Is the value a beacon subscription?
@@ -125,8 +148,10 @@ function isBeaconSubscription(value) {
125
148
  function isObservable(value) {
126
149
  return isBeaconInstance(BEACON_OBSERVABLE, value);
127
150
  }
128
- function subscribeToObservable(state, first, second, third) {
151
+ function subscribeToObservable(first, second, third) {
152
+ const state = this[BEACON_SYMBOL];
129
153
  if (!state.beacon.active || !state.active) throw new Error(BEACON_MESSAGE_SUBSCRIBE);
154
+ state.beacon.subscriptions ??= subscriptions({ property: beaconSubscription });
130
155
  const observer = createObserver(first, second, third);
131
156
  const [subscription] = state.beacon.subscriptions.create({
132
157
  isActive: () => state.beacon.active && state.active,
@@ -135,20 +160,20 @@ function subscribeToObservable(state, first, second, third) {
135
160
  observer.next?.(state.beacon.value);
136
161
  return subscription;
137
162
  }
138
- function updateBeacon(type, state, value, finish) {
163
+ function updateBeacon(instance, type, value, finish) {
164
+ const state = instance[BEACON_SYMBOL];
139
165
  if (!state.active) return;
140
166
  if (type === BEACON_TYPE_NEXT) {
141
167
  if (state.options.equal(state.value, value)) return;
142
168
  state.value = value;
143
169
  }
144
- for (const [, observer] of state.subscriptions.values.to.any) observer[type]?.(value);
145
- if (finish === true) finishBeacon(state, true);
170
+ const subscriptions = state.subscriptions?.values.to.any;
171
+ if (subscriptions != null && subscriptions.size > 0) for (const [, observer] of subscriptions) observer[type]?.(value);
172
+ if (finish === true) closeBeacon(state, true);
146
173
  }
147
174
  const BEACON_MESSAGE_RETRIEVE = "Cannot retrieve observable from a closed beacon";
148
175
  const BEACON_MESSAGE_SUBSCRIBE = "Cannot subscribe to a closed observable";
149
- const BEACON_PROPERTY = "$beacon";
150
- const BEACON_NAME = "beacon";
151
- const BEACON_OBSERVABLE = "observable";
176
+ const BEACON_SYMBOL = Symbol(BEACON_PROPERTY);
152
177
  const BEACON_TYPE_ERROR = "error";
153
178
  const BEACON_TYPE_NEXT = "next";
154
179
  const beaconSubscription = {