atomic-fns 1.1.7 → 2.0.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 (216) hide show
  1. package/README.md +405 -60
  2. package/dist/array/index.d.ts +9 -0
  3. package/dist/array/index.js +9 -0
  4. package/dist/array/list.d.ts +99 -0
  5. package/dist/array/list.js +133 -0
  6. package/dist/array/sets.d.ts +130 -0
  7. package/dist/array/sets.js +213 -0
  8. package/dist/async/index.d.ts +6 -0
  9. package/dist/async/index.js +6 -0
  10. package/dist/async/retry.d.ts +38 -0
  11. package/dist/async/retry.js +56 -0
  12. package/dist/async/sleep.d.ts +42 -0
  13. package/dist/async/sleep.js +76 -0
  14. package/dist/collections/bisect.d.ts +103 -0
  15. package/dist/collections/bisect.js +157 -0
  16. package/dist/collections/deque.d.ts +67 -97
  17. package/dist/collections/deque.js +197 -249
  18. package/dist/collections/entries.d.ts +89 -0
  19. package/dist/collections/entries.js +131 -0
  20. package/dist/collections/frozen-set.d.ts +42 -0
  21. package/dist/collections/frozen-set.js +64 -0
  22. package/dist/collections/group.d.ts +141 -0
  23. package/dist/collections/group.js +241 -0
  24. package/dist/collections/heap.d.ts +64 -0
  25. package/dist/collections/heap.js +205 -0
  26. package/dist/collections/index.d.ts +15 -642
  27. package/dist/collections/index.js +14 -716
  28. package/dist/collections/internal/arrayKeys.d.ts +19 -0
  29. package/dist/collections/internal/arrayKeys.js +83 -0
  30. package/dist/collections/internal/invariants.d.ts +4 -0
  31. package/dist/collections/internal/invariants.js +12 -0
  32. package/dist/collections/internal/iterable.d.ts +25 -0
  33. package/dist/collections/internal/iterable.js +62 -0
  34. package/dist/collections/iterate.d.ts +162 -0
  35. package/dist/collections/iterate.js +188 -0
  36. package/dist/collections/lru-cache.d.ts +58 -0
  37. package/dist/collections/lru-cache.js +164 -0
  38. package/dist/collections/order.d.ts +19 -0
  39. package/dist/collections/order.js +38 -0
  40. package/dist/collections/shape.d.ts +32 -0
  41. package/dist/collections/shape.js +86 -0
  42. package/dist/collections/sorted-map.d.ts +171 -0
  43. package/dist/collections/sorted-map.js +845 -0
  44. package/dist/collections/sorted-set.d.ts +103 -0
  45. package/dist/collections/sorted-set.js +147 -0
  46. package/dist/collections/splay-tree.d.ts +82 -0
  47. package/dist/collections/splay-tree.js +354 -0
  48. package/dist/collections/trie.d.ts +58 -0
  49. package/dist/collections/trie.js +196 -0
  50. package/dist/decimal/currency.d.ts +211 -0
  51. package/dist/decimal/currency.js +321 -0
  52. package/dist/decimal/decimal.d.ts +299 -0
  53. package/dist/decimal/decimal.js +660 -0
  54. package/dist/decimal/index.d.ts +11 -145
  55. package/dist/decimal/index.js +10 -374
  56. package/dist/decimal/internal/bits.d.ts +2 -0
  57. package/dist/decimal/internal/bits.js +7 -0
  58. package/dist/decimal/internal/echo.d.ts +4 -0
  59. package/dist/decimal/internal/echo.js +6 -0
  60. package/dist/decimal/internal/isqrt.d.ts +9 -0
  61. package/dist/decimal/internal/isqrt.js +34 -0
  62. package/dist/decimal/internal/locale.d.ts +7 -0
  63. package/dist/decimal/internal/locale.js +24 -0
  64. package/dist/decimal/internal/minor-units.d.ts +2 -0
  65. package/dist/decimal/internal/minor-units.js +33 -0
  66. package/dist/decimal/internal/pow10.d.ts +24 -0
  67. package/dist/decimal/internal/pow10.js +96 -0
  68. package/dist/encoding/base64.d.ts +39 -0
  69. package/dist/encoding/base64.js +53 -0
  70. package/dist/encoding/hex.d.ts +23 -0
  71. package/dist/encoding/hex.js +34 -0
  72. package/dist/encoding/index.d.ts +6 -0
  73. package/dist/encoding/index.js +6 -0
  74. package/dist/function/debounce.d.ts +68 -0
  75. package/dist/function/debounce.js +203 -0
  76. package/dist/function/index.d.ts +6 -0
  77. package/dist/function/index.js +6 -0
  78. package/dist/function/shared.d.ts +2 -0
  79. package/dist/function/shared.js +5 -0
  80. package/dist/function/wrap.d.ts +130 -0
  81. package/dist/function/wrap.js +113 -0
  82. package/dist/hash/digest.d.ts +18 -0
  83. package/dist/hash/digest.js +26 -0
  84. package/dist/hash/hash.d.ts +22 -0
  85. package/dist/hash/hash.js +252 -0
  86. package/dist/hash/hashString.d.ts +13 -0
  87. package/dist/hash/hashString.js +53 -0
  88. package/dist/hash/index.d.ts +8 -0
  89. package/dist/hash/index.js +8 -0
  90. package/dist/hash/stableStringify.d.ts +19 -0
  91. package/dist/hash/stableStringify.js +102 -0
  92. package/dist/id/index.d.ts +7 -0
  93. package/dist/id/index.js +7 -0
  94. package/dist/id/nanoid.d.ts +16 -0
  95. package/dist/id/nanoid.js +65 -0
  96. package/dist/id/uniqueId.d.ts +14 -0
  97. package/dist/id/uniqueId.js +20 -0
  98. package/dist/id/uuid.d.ts +25 -0
  99. package/dist/id/uuid.js +64 -0
  100. package/dist/index.d.ts +15 -8
  101. package/dist/index.js +15 -8
  102. package/dist/internal/bytes.d.ts +11 -0
  103. package/dist/internal/bytes.js +33 -0
  104. package/dist/internal/codec.d.ts +11 -0
  105. package/dist/internal/codec.js +84 -0
  106. package/dist/internal/compare.d.ts +10 -0
  107. package/dist/internal/compare.js +26 -0
  108. package/dist/internal/crypto.d.ts +5 -0
  109. package/dist/internal/crypto.js +11 -0
  110. package/dist/internal/keys.d.ts +12 -0
  111. package/dist/internal/keys.js +23 -0
  112. package/dist/internal/kind.d.ts +44 -0
  113. package/dist/internal/kind.js +139 -0
  114. package/dist/internal/sparse.d.ts +51 -0
  115. package/dist/internal/sparse.js +104 -0
  116. package/dist/internal/text.d.ts +7 -0
  117. package/dist/internal/text.js +15 -0
  118. package/dist/internal/timer.d.ts +10 -0
  119. package/dist/internal/timer.js +17 -0
  120. package/dist/internal/truncate.d.ts +10 -0
  121. package/dist/internal/truncate.js +22 -0
  122. package/dist/internal/walk.d.ts +47 -0
  123. package/dist/internal/walk.js +100 -0
  124. package/dist/is/index.d.ts +8 -0
  125. package/dist/is/index.js +8 -0
  126. package/dist/is/isArrayLike.d.ts +18 -0
  127. package/dist/is/isArrayLike.js +25 -0
  128. package/dist/is/isError.d.ts +13 -0
  129. package/dist/is/isError.js +18 -0
  130. package/dist/is/isNil.d.ts +12 -0
  131. package/dist/is/isNil.js +14 -0
  132. package/dist/is/isPlainObject.d.ts +15 -0
  133. package/dist/is/isPlainObject.js +34 -0
  134. package/dist/number/index.d.ts +7 -0
  135. package/dist/number/index.js +7 -0
  136. package/dist/number/round.d.ts +86 -0
  137. package/dist/number/round.js +130 -0
  138. package/dist/number/stats.d.ts +52 -0
  139. package/dist/number/stats.js +115 -0
  140. package/dist/object/cloneDeep.d.ts +28 -0
  141. package/dist/object/cloneDeep.js +200 -0
  142. package/dist/object/index.d.ts +14 -0
  143. package/dist/object/index.js +13 -0
  144. package/dist/object/isEqual.d.ts +64 -0
  145. package/dist/object/isEqual.js +289 -0
  146. package/dist/object/matches.d.ts +40 -0
  147. package/dist/object/matches.js +284 -0
  148. package/dist/object/merge.d.ts +66 -0
  149. package/dist/object/merge.js +272 -0
  150. package/dist/object/parsePath.d.ts +22 -0
  151. package/dist/object/parsePath.js +111 -0
  152. package/dist/object/path.d.ts +85 -0
  153. package/dist/object/path.js +208 -0
  154. package/dist/object/pick.d.ts +91 -0
  155. package/dist/object/pick.js +180 -0
  156. package/dist/object/reshape.d.ts +73 -0
  157. package/dist/object/reshape.js +144 -0
  158. package/dist/random/index.d.ts +96 -32
  159. package/dist/random/index.js +215 -50
  160. package/dist/string/case.d.ts +71 -0
  161. package/dist/string/case.js +152 -0
  162. package/dist/string/fold.d.ts +35 -0
  163. package/dist/string/fold.js +100 -0
  164. package/dist/string/index.d.ts +6 -187
  165. package/dist/string/index.js +6 -226
  166. package/dist/string/truncate.d.ts +25 -0
  167. package/dist/string/truncate.js +44 -0
  168. package/dist/string/words.d.ts +27 -0
  169. package/dist/string/words.js +241 -0
  170. package/package.json +101 -58
  171. package/dist/collections/BTree.d.ts +0 -229
  172. package/dist/collections/BTree.js +0 -1165
  173. package/dist/collections/Heap.d.ts +0 -69
  174. package/dist/collections/Heap.js +0 -181
  175. package/dist/collections/LRUCache.d.ts +0 -80
  176. package/dist/collections/LRUCache.js +0 -126
  177. package/dist/collections/SortedMap.d.ts +0 -92
  178. package/dist/collections/SortedMap.js +0 -131
  179. package/dist/collections/SortedSet.d.ts +0 -69
  180. package/dist/collections/SortedSet.js +0 -99
  181. package/dist/collections/SortedTree.d.ts +0 -103
  182. package/dist/collections/SortedTree.js +0 -141
  183. package/dist/collections/SplayTree.d.ts +0 -105
  184. package/dist/collections/SplayTree.js +0 -338
  185. package/dist/collections/Trie.d.ts +0 -42
  186. package/dist/collections/Trie.js +0 -104
  187. package/dist/collections/abc.d.ts +0 -152
  188. package/dist/collections/abc.js +0 -39
  189. package/dist/collections/frozenset.d.ts +0 -19
  190. package/dist/collections/frozenset.js +0 -30
  191. package/dist/functools/index.d.ts +0 -130
  192. package/dist/functools/index.js +0 -235
  193. package/dist/globals/index.d.ts +0 -285
  194. package/dist/globals/index.js +0 -463
  195. package/dist/i18n/currency.d.ts +0 -24
  196. package/dist/i18n/currency.js +0 -85
  197. package/dist/i18n/date/index.d.ts +0 -203
  198. package/dist/i18n/date/index.js +0 -531
  199. package/dist/i18n/date/utils.d.ts +0 -65
  200. package/dist/i18n/date/utils.js +0 -290
  201. package/dist/i18n/duration.d.ts +0 -112
  202. package/dist/i18n/duration.js +0 -392
  203. package/dist/i18n/format.d.ts +0 -31
  204. package/dist/i18n/format.js +0 -506
  205. package/dist/i18n/index.d.ts +0 -8
  206. package/dist/i18n/index.js +0 -8
  207. package/dist/itertools/index.d.ts +0 -255
  208. package/dist/itertools/index.js +0 -437
  209. package/dist/math/index.d.ts +0 -96
  210. package/dist/math/index.js +0 -98
  211. package/dist/mod.d.ts +0 -1
  212. package/dist/mod.js +0 -1
  213. package/dist/operators/index.d.ts +0 -152
  214. package/dist/operators/index.js +0 -305
  215. package/dist/types.d.ts +0 -19
  216. package/dist/types.js +0 -1
@@ -1,719 +1,17 @@
1
1
  /**
2
- * This module includes classes and functions to work with Container types.
3
- *
2
+ * Iteration over objects, arrays, strings, maps, sets and other iterables.
3
+ * Sorted containers, trees, heaps, deques, caches, tries and bisect.
4
4
  * @module collections
5
5
  */
6
- import { call, get, isArray, isArrayLike, isBool, isEmpty, isFunction, isIterable, isNull, isNumber, isObject, isString, keys } from '../globals/index.js';
7
- import { enumerate } from '../itertools/index.js';
8
- import { compare, eq, id } from '../operators/index.js';
9
- export * from './abc.js';
10
- export * from './BTree.js';
11
- export * from './deque.js';
12
- export * from './frozenset.js';
13
- export * from './Heap.js';
14
- export * from './LRUCache.js';
15
- export * from './SortedMap.js';
16
- export * from './SortedSet.js';
17
- export * from './SortedTree.js';
18
- export * from './SplayTree.js';
19
- export * from './Trie.js';
20
- export function compact(arr) {
21
- if (!arr)
22
- return;
23
- if (Array.isArray(arr))
24
- return arr.filter((x) => !isEmpty(x));
25
- const result = {};
26
- for (const key of Object.keys(arr)) {
27
- if (!isEmpty(arr[key])) {
28
- result[key] = arr[key];
29
- }
30
- }
31
- return result;
32
- }
33
- export function count(obj, func) {
34
- const counters = {};
35
- forEach(obj, (value, key) => {
36
- const result = func(value, key);
37
- counters[result] = get(result, counters, 0) + 1;
38
- });
39
- return counters;
40
- }
41
- export function filter(arr, fn = isNull) {
42
- if (!arr)
43
- return;
44
- const predicate = isFunction(fn) ? fn : isObject(fn) ? matches(fn) : (obj) => get(fn, obj);
45
- const results = [];
46
- forEach(arr, (value, key) => {
47
- if (predicate(value, key, arr))
48
- results.push(value);
49
- });
50
- return results;
51
- }
52
- export function find(arr, fn) {
53
- const it = isFunction(fn) ? fn : isObject(fn) ? matches(fn) : (obj) => get(fn, obj);
54
- if (Array.isArray(arr)) {
55
- return arr.find(it);
56
- }
57
- else if (isObject(arr)) {
58
- for (const key of Object.keys(arr)) {
59
- if (it(arr[key], key, arr)) {
60
- return arr[key];
61
- }
62
- }
63
- }
64
- }
65
- /**
66
- * Performs an efficient array insert operation in the given array. If the index or the array is invalid, it just returns the given array.
67
- *
68
- * **Note:** Uses the same behavior as `Array.splice`.
69
- *
70
- * @param {Array<*>} arr The given array to insert into
71
- * @param {number} index The index of the array insert operation.
72
- * @param {*} value The value to insert in the array at the given `index`.
73
- * @returns {Array<*>} The given array.
74
- */
75
- export function insert(arr, index, value) {
76
- if (!arr || index < 0)
77
- return arr;
78
- arr.splice(index, 0, value);
79
- return arr;
80
- }
81
- /**
82
- * Creates a function that performs a partial deep comparison between a given object and `shape`, returning `true` if the given object has equivalent property values, else `false`.
83
- * @example
84
- ```js
85
- let objects = [
86
- { a: 1, b: 2, c: 3 },
87
- { a: 4, b: 5, c: 6 }
88
- ]
89
-
90
- filter(objects, matches({ a: 4, c: 6 }))
91
- // [{ a: 4, b: 5, c: 6 }]
92
- ```
93
- * @param shape
94
- * @returns
95
- */
96
- export function matches(shape) {
97
- return function (obj) {
98
- if (!shape || !obj)
99
- return false;
100
- for (const key in shape) {
101
- if (obj[key] !== shape[key])
102
- return false;
103
- }
104
- return true;
105
- };
106
- }
107
- export function forEach(collection, fn) {
108
- if (Array.isArray(collection)) {
109
- for (let i = 0; i < collection.length; i++) {
110
- const res = fn(collection[i], i, collection);
111
- if (res === false)
112
- return;
113
- }
114
- }
115
- else if (isIterable(collection)) {
116
- for (const [index, value] of enumerate(collection)) {
117
- const res = fn(value, index, collection);
118
- if (res === false)
119
- return;
120
- }
121
- }
122
- else {
123
- for (const key of keys(collection)) {
124
- const res = fn(collection[key], key, collection);
125
- if (res === false)
126
- return;
127
- }
128
- }
129
- }
130
- export function findLast(arr, fn) {
131
- let result;
132
- const it = isFunction(fn) ? fn : matches(fn);
133
- forEachRight(arr, (val, key) => {
134
- if (it(val, key, arr)) {
135
- result = val;
136
- return false;
137
- }
138
- });
139
- return result;
140
- }
141
- export function forEachRight(collection, fn) {
142
- if (Array.isArray(collection)) {
143
- for (let i = collection.length - 1; i >= 0; i--) {
144
- const value = collection[i];
145
- const res = fn(value, i, collection);
146
- if (res === false)
147
- return;
148
- }
149
- }
150
- else if (isObject(collection)) {
151
- const keys = Object.keys(collection);
152
- for (let i = keys.length - 1; i >= 0; i--) {
153
- const key = keys[i];
154
- const value = collection[key];
155
- const res = fn(value, key, collection);
156
- if (res === false)
157
- return;
158
- }
159
- }
160
- }
161
- export function flatten(arr, depth = 1) {
162
- if (!depth)
163
- return arr;
164
- if (Array.isArray(arr))
165
- return flattenArray(arr, depth, []);
166
- if (isObject(arr))
167
- return flattenObj(arr);
168
- return arr;
169
- }
170
- function flattenArray(arr, depth = 1, result = []) {
171
- for (const value of arr) {
172
- if (depth && Array.isArray(value)) {
173
- flattenArray(value, typeof depth === 'number' ? depth - 1 : depth, result);
174
- }
175
- else {
176
- result.push(value);
177
- }
178
- }
179
- return result;
180
- }
181
- function flattenObj(obj, prefix = '', result = {}, keepNull = false) {
182
- if (isString(obj) || isNumber(obj) || isBool(obj) || (keepNull && isNull(obj))) {
183
- result[prefix] = obj;
184
- return result;
185
- }
186
- if (isArray(obj) || isObject(obj)) {
187
- for (const i of Object.keys(obj)) {
188
- let pref = prefix;
189
- if (isArray(obj)) {
190
- pref = `${pref}[${i}]`;
191
- }
192
- else {
193
- if (prefix) {
194
- pref = `${prefix}.${i}`;
195
- }
196
- else {
197
- pref = i;
198
- }
199
- }
200
- flattenObj(obj[i], pref, result, keepNull);
201
- }
202
- return result;
203
- }
204
- return result;
205
- }
206
- export function map(arr, fn) {
207
- const it = isFunction(fn) ? fn : (obj) => get(fn, obj);
208
- if (Array.isArray(arr)) {
209
- return arr.map((x, i) => it(x, i, arr));
210
- }
211
- if (isObject(arr)) {
212
- return Object.keys(arr).map((key) => it(arr[key], key, arr));
213
- }
214
- }
215
- export function pick(obj, paths) {
216
- if (!obj)
217
- return {};
218
- const result = {};
219
- if (isFunction(paths)) {
220
- for (const key of Object.keys(obj)) {
221
- if (paths(obj[key], key)) {
222
- result[key] = obj[key];
223
- }
224
- }
225
- return result;
226
- }
227
- for (const key of paths) {
228
- result[key] = get(key, obj);
229
- }
230
- return result;
231
- }
232
- export function omit(obj, paths) {
233
- if (!obj)
234
- return {};
235
- const result = {};
236
- if (isFunction(paths)) {
237
- for (const key of Object.keys(obj)) {
238
- if (!paths(obj[key], key)) {
239
- result[key] = obj[key];
240
- }
241
- }
242
- return result;
243
- }
244
- Object.assign(result, obj);
245
- for (const key of paths) {
246
- delete result[key];
247
- }
248
- return result;
249
- }
250
- export function findIndex(obj, fn, start = 0) {
251
- if (!obj)
252
- return -1;
253
- const length = obj.length;
254
- if (length && start < 0) {
255
- start = Math.max(start + length, 0);
256
- }
257
- if (!length || start >= length)
258
- return -1;
259
- const it = isFunction(fn) ? fn : isObject(fn) ? matches(fn) : (obj) => get(fn, obj);
260
- for (; start < length; start++) {
261
- if (it(obj[start], start, obj))
262
- return start;
263
- }
264
- return -1;
265
- }
266
- /**
267
- * This method is like {@link findIndex} except that it searches for a given value directly, instead of using a predicate function.
268
- * @param {Array} obj The array to inspect.
269
- * @param {*} value The value to find
270
- * @param {number} [start=0] The index to search from.
271
- * @returns {number} The index of the found value, else -1
272
- * @example
273
- ```js
274
- indexOf([1, 2, 1, 2], 2)
275
- // 1
276
-
277
- // Search from a `start` index.
278
- indexOf([1, 2, 1, 2], 2, 2)
279
- // 3
280
- ```
281
- * @see {@link findIndex}
282
- * @see {@link lastIndexOf}
283
- */
284
- export function indexOf(obj, value, start = 0) {
285
- if (!obj)
286
- return -1;
287
- const op = call(obj, 'indexOf', value, start);
288
- if (op != null)
289
- return op;
290
- const length = obj.length;
291
- if (length && start < 0) {
292
- start = Math.max(start + length, 0);
293
- }
294
- if (!length || start >= length)
295
- return -1;
296
- for (; start < length; start++) {
297
- if (eq(value, obj[start]))
298
- return start;
299
- }
300
- return -1;
301
- }
302
- export function findLastIndex(arr, fn, start) {
303
- if (arr == null)
304
- return -1;
305
- const length = arr.length;
306
- if (start == null)
307
- start = length - 1;
308
- if (!length || start < 0)
309
- return -1;
310
- const it = isFunction(fn) ? fn : isObject(fn) ? matches(fn) : (obj) => get(fn, obj);
311
- for (; start >= 0; start--) {
312
- if (it(arr[start], start, arr))
313
- return start;
314
- }
315
- return -1;
316
- }
317
- /**
318
- * This method is like {@link indexOf} except that it iterates the collection from right to left.
319
- * @param {Array} obj The array to inspect.
320
- * @param {*} value The value to find
321
- * @param {number} [start] The index to search from.
322
- * @returns {number} The index of the found value, else -1
323
- * @example
324
- ```js
325
- lastIndexOf([1, 2, 1, 2], 2)
326
- // 3
327
-
328
- // Search from the `fromIndex`.
329
- lastIndexOf([1, 2, 1, 2], 2, 2)
330
- // 1
331
- ```
332
- * @see {@link findLastIndex}
333
- * @see {@link indexOf}
334
- */
335
- export function lastIndexOf(obj, value, start) {
336
- if (obj == null)
337
- return -1;
338
- const op = call(obj, 'lastIndexOf', value, start);
339
- if (op != null)
340
- return op;
341
- const length = obj.length;
342
- if (start == null)
343
- start = length - 1;
344
- if (!length || start < 0)
345
- return -1;
346
- for (; start >= 0; start--) {
347
- if (eq(value, obj[start]))
348
- return start;
349
- }
350
- return -1;
351
- }
352
- export function clone(value, deep = false) {
353
- if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer) {
354
- return cloneTypedArray(value, deep);
355
- }
356
- if (isArray(value) || isArrayLike(value)) {
357
- return cloneArray(value, deep);
358
- }
359
- if (isObject(value)) {
360
- if (isFunction(value.clone)) {
361
- return value.clone();
362
- }
363
- const copy = {};
364
- for (const key in value) {
365
- if (deep) {
366
- copy[key] = clone(value[key], deep);
367
- }
368
- else {
369
- copy[key] = value[key];
370
- }
371
- }
372
- return copy;
373
- }
374
- return value;
375
- }
376
- /**
377
- * Clones an array. If `deep` is `false` (default) the clone will be shallow. Otherwise {@link https://developer.mozilla.org/en-US/docs/Web/API/structuredClone structuredClone} is used.
378
- * @param arr The array to clone
379
- * @param [deep=false] Creates a deep clone using `structuredClone` if true.
380
- * @returns The new array
381
- * @see {@link clone}
382
- */
383
- export function cloneArray(arr, deep = false) {
384
- if (!deep)
385
- return Array.from(arr);
386
- return structuredClone(arr);
387
- }
388
- /**
389
- * Clones a typed array. If `deep` is `false` (default) the clone will be shallow. Otherwise {@link https://developer.mozilla.org/en-US/docs/Web/API/structuredClone structuredClone} is used.
390
- * @param arr The array to clone
391
- * @param [deep=false] Creates a deep clone using `structuredClone` if true.
392
- * @returns The new array
393
- * @see {@link clone}
394
- */
395
- export function cloneTypedArray(typedArray, isDeep = false) {
396
- const buffer = isDeep ? structuredClone(typedArray) : typedArray.buffer;
397
- return new typedArray.constructor(buffer, typedArray.byteOffset, typedArray.length);
398
- }
399
- export function uniq(arr, fn = id) {
400
- const keys = new Set();
401
- const it = isFunction(fn) ? fn : (x) => get(fn, x);
402
- for (const x of arr) {
403
- keys.add(it(x));
404
- }
405
- return Array.from(keys.values());
406
- }
407
- export function sortedUniq(arr, fn = id) {
408
- const values = uniq(arr, fn);
409
- values.sort(compare);
410
- return values;
411
- }
412
- function baseMergeDeep(obj, source, key, stack) {
413
- const objValue = obj[key];
414
- const srcValue = source[key];
415
- const stacked = stack.get(srcValue);
416
- if (stacked && (objValue !== stacked || !(key in obj))) {
417
- obj[key] = stacked;
418
- return;
419
- }
420
- let newValue;
421
- let isCommon = newValue === undefined;
422
- const isArray = Array.isArray(srcValue);
423
- const isTyped = ArrayBuffer.isView(srcValue);
424
- if (isArray || isTyped) {
425
- if (Array.isArray(objValue)) {
426
- newValue = objValue;
427
- }
428
- else if (isArrayLike(objValue)) {
429
- newValue = Array.from(objValue);
430
- }
431
- else if (isTyped) {
432
- isCommon = false;
433
- newValue = cloneTypedArray(srcValue, true);
434
- }
435
- else {
436
- newValue = [];
437
- }
438
- }
439
- else if (isObject(srcValue)) {
440
- newValue = objValue;
441
- if (!isObject(objValue)) {
442
- newValue = srcValue;
443
- }
444
- }
445
- else {
446
- isCommon = false;
447
- }
448
- if (isCommon) {
449
- stack.set(srcValue, newValue);
450
- baseMerge(newValue, srcValue, stack);
451
- stack.delete(srcValue);
452
- }
453
- obj[key] = newValue;
454
- }
455
- function baseMerge(obj, source, stack) {
456
- if (obj === source)
457
- return obj;
458
- for (const key of Object.keys(source)) {
459
- const srcValue = source[key];
460
- const objValue = obj[key];
461
- if (typeof srcValue === 'object' && srcValue !== null) {
462
- baseMergeDeep(obj, source, key, stack || new WeakMap());
463
- }
464
- else {
465
- if (objValue !== srcValue) {
466
- obj[key] = srcValue;
467
- }
468
- }
469
- }
470
- }
471
- /**
472
- * Recursively merges own and inherited enumerable string keyed properties of source objects into the destination object. Source properties that resolve to `undefined` are skipped if a destination value exists. Array and plain object properties are merged recursively. Other objects and value types are overridden by assignment. Source objects are applied from left to right. Subsequent sources overwrite property assignments of previous sources.
473
- *
474
- * Note: this method mutates `object`
475
- *
476
- *
477
- * @param {Object} object The destination object.
478
- * @param {...Object} sources The source objects.
479
- * @returns Returns `object`.
480
- *
481
- * @example
482
- ```js
483
- let object = {
484
- 'a': [{ 'b': 2 }, { 'd': 4 }]
485
- }
486
-
487
- let other = {
488
- 'a': [{ 'c': 3 }, { 'e': 5 }]
489
- }
490
-
491
- merge(object, other)
492
- // { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] }
493
- ```
494
- */
495
- export function merge(object, ...sources) {
496
- for (const source of sources) {
497
- baseMerge(object, source);
498
- }
499
- return object;
500
- }
501
- /**
502
- * Assigns own and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to `undefined`. Source objects are applied from left to right. Once a property is set, additional values of the same property are ignored.
503
- * **Note:** This method mutates `object`.
504
-
505
- * @param {Object} object The destination object.
506
- * @param {...Object} [sources] The source objects.
507
- * @returns Returns `object`.
508
- * @example
509
- ```js
510
- defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 })
511
- // { 'a': 1, 'b': 2 }
512
- ```
513
- */
514
- export function defaults(object, ...sources) {
515
- for (const source of sources) {
516
- for (const key in source) {
517
- if (object[key] === undefined) {
518
- object[key] = source[key];
519
- }
520
- }
521
- }
522
- return object;
523
- }
524
- /**
525
- * Returns a generator of array values not included in the other given arrays using a `Set` for equality comparisons. The order and references of result values are not guaranteed.
526
- * @param args The initial arrays
527
- * @example
528
- ```js
529
- [...difference([2, 1], [2, 3])]
530
- // [1]
531
- ```
532
- * @see {@link union}
533
- * @see {@link intersection}
534
- */
535
- export function* difference(...args) {
536
- const sets = args.map((arr) => new Set(arr));
537
- const setA = sets[0];
538
- for (let i = 1; i < sets.length; i++) {
539
- for (const x of sets[i].values()) {
540
- if (!setA.delete(x))
541
- setA.add(x);
542
- }
543
- }
544
- for (const x of setA.values()) {
545
- yield x;
546
- }
547
- }
548
- /**
549
- * Creates a generator of unique values that are included in all given arrays.
550
- * @param args The arrays to inspect
551
- * @example
552
- ```js
553
- [...intersection([2, 1], [2, 3])]
554
- // [2]
555
- ```
556
- * @see {@link difference}
557
- * @see {@link union}
558
- */
559
- export function* intersection(...args) {
560
- const sets = args.map((arr) => new Set(arr));
561
- // build a counter map to find items in all
562
- const results = new Map();
563
- const total = sets.length;
564
- for (let i = 0; i < total; i++) {
565
- for (const x of sets[i].values()) {
566
- const count = results.get(x) || 0;
567
- results.set(x, count + 1);
568
- }
569
- }
570
- for (const [key, value] of results.entries()) {
571
- if (value === total)
572
- yield key;
573
- }
574
- }
575
- /**
576
- * Creates a generator of unique values from all given arrays using `Set` for equality comparisons.
577
- * @param args The arrays to perform union on.
578
- * @example
579
- ```js
580
- [...union([2], [1, 2])]
581
- // [2, 1]
582
- ```
583
- * @see {@link difference}
584
- * @see {@link intersection}
585
- */
586
- export function* union(...args) {
587
- const results = new Set();
588
- for (const arr of args) {
589
- for (const item of arr) {
590
- results.add(item);
591
- }
592
- }
593
- for (const item of results.values()) {
594
- yield item;
595
- }
596
- }
597
- export function groupBy(arr, func = id) {
598
- const useKey = isFunction(func) ? func : (obj) => get(func, obj);
599
- const results = {};
600
- forEach(arr, (value) => {
601
- const groupKey = useKey(value);
602
- const values = results[groupKey] || [];
603
- values.push(value);
604
- results[groupKey] = values;
605
- });
606
- return results;
607
- }
608
- export function groupByMap(arr, func = id) {
609
- const results = new Map();
610
- const useKey = isFunction(func) ? func : (obj) => get(func, obj);
611
- forEach(arr, (value) => {
612
- const groupKey = useKey(value);
613
- const values = results.get(groupKey) || [];
614
- values.push(value);
615
- results.set(groupKey, values);
616
- });
617
- return results;
618
- }
619
- export function remove(arr, func) {
620
- const it = isFunction(func) ? func : isArray(func) ? (x) => func.includes(x) : (x) => func === x;
621
- return arr.filter((x, i) => !it(x, i, arr));
622
- }
623
- /**
624
- * Returns the index of `x` in a **sorted** array if found, in `O(log n)` using binary search.
625
- * If the element is not found, returns a negative integer.
626
- * @param arr The array to sort
627
- * @param x The element to find
628
- * @param lo The starting index
629
- * @param hi The end index to search within
630
- * @param comp The compare function to check for `x`
631
- * @returns {number} The index if the element is found or a negative integer.
632
- */
633
- export function binarySearch(arr, x, lo = 0, hi, comp = compare) {
634
- hi = hi ?? arr.length - 1;
635
- while (lo < hi) {
636
- const mid = (lo + hi) >> 1;
637
- const check = comp(x, arr[mid]);
638
- if (check === 0)
639
- return mid;
640
- if (check > 0)
641
- lo = mid + 1;
642
- else
643
- hi = mid - 1;
644
- }
645
- if (comp(x, arr[lo]) === 0)
646
- return lo;
647
- // Returns (-lo - 1) where n is the insertion point for new element in the range
648
- return -lo - 1;
649
- }
650
- /**
651
- * Returns an insertion index which comes after any existing entries of `x` in a **sorted** array, using binary search.
652
- * @param arr The array to sort
653
- * @param x The element to find
654
- * @param lo The starting index
655
- * @param hi The end index to search within
656
- * @param comp The compare function to check for `x`
657
- * @returns {number}
658
- */
659
- export function bisect(arr, x, lo = 0, hi, comp = compare) {
660
- hi = hi ?? arr.length;
661
- while (lo < hi) {
662
- const mid = (lo + hi) >> 1;
663
- const check = comp(x, arr[mid]);
664
- if (check >= 0)
665
- lo = mid + 1;
666
- else
667
- hi = mid - 1;
668
- }
669
- return lo;
670
- }
671
- /**
672
- * Returns an insertion index which comes before any existing entries of `x` in a **sorted** array, using binary search.
673
- * @param arr The array to sort
674
- * @param x The element to find
675
- * @param lo The starting index
676
- * @param hi The end index to search within
677
- * @param comp The compare function to check for `x`
678
- * @returns {number}
679
- */
680
- export function bisectLeft(arr, x, lo = 0, hi, comp = compare) {
681
- hi = hi ?? arr.length;
682
- while (lo < hi) {
683
- const mid = (lo + hi) >> 1;
684
- const check = comp(x, arr[mid]);
685
- if (check <= 0)
686
- hi = mid - 1;
687
- else
688
- lo = mid + 1;
689
- }
690
- return lo;
691
- }
692
- /**
693
- * Runs {@link bisect} first to locate an insertion point, and inserts the value `x` in the sorted array after any existing entries of `x` to maintain sort order.
694
- * Please note this method is `O(n)` because insertion resizes the array.
695
- * @param arr The array to insert into
696
- * @param x The element to insert
697
- * @param lo The starting index
698
- * @param hi The end index to search within
699
- * @param comp The compare function to check for `x`
700
- * @returns {Array}
701
- */
702
- export function insort(arr, x, lo = 0, hi, comp = compare) {
703
- const index = bisect(arr, x, lo, hi, comp);
704
- return insert(arr, index, x);
705
- }
706
- /**
707
- * Runs {@link bisectLeft} first to locate an insertion point, and inserts the value `x` in the sorted array before any existing entries of `x` to maintain sort order.
708
- * Please note this method is `O(n)` because insertion resizes the array.
709
- * @param arr The array to insert into
710
- * @param x The element to insert
711
- * @param lo The starting index
712
- * @param hi The end index to search within
713
- * @param comp The compare function to check for `x`
714
- * @returns {Array}
715
- */
716
- export function insortLeft(arr, x, lo = 0, hi, comp = compare) {
717
- const index = bisectLeft(arr, x, lo, hi, comp);
718
- return insert(arr, index, x);
719
- }
6
+ export { entries, includes, isEmpty, keys, size, values } from './entries.js';
7
+ export { countBy, groupBy, keyBy, maxBy, meanBy, minBy, orderBy, partition, sortBy, sumBy } from './group.js';
8
+ export { every, filter, find, findLast, flatMap, forEach, forEachRight, map, reduce, reduceRight, reject, some } from './iterate.js';
9
+ export { binarySearch, bisect, bisectLeft, bisectRight, insort, insortLeft } from './bisect.js';
10
+ export { Deque } from './deque.js';
11
+ export { FrozenSet } from './frozen-set.js';
12
+ export { Heap } from './heap.js';
13
+ export { LRUCache } from './lru-cache.js';
14
+ export { SortedMap } from './sorted-map.js';
15
+ export { SortedSet } from './sorted-set.js';
16
+ export { SplayTree } from './splay-tree.js';
17
+ export { Trie } from './trie.js';