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
@@ -0,0 +1,205 @@
1
+ import { defaultCompare, sameValueZero } from '../internal/compare.js';
2
+ import { arrayFromIterable } from './internal/iterable.js';
3
+ /**
4
+ * Hole-based sift-up: compare while finding the final index, then shift and place once, so a
5
+ * throwing comparator writes nothing.
6
+ * @returns The index where `item` landed.
7
+ */
8
+ function siftUp(heap, start, item, compareFn) {
9
+ let pos = start;
10
+ while (pos > 0) {
11
+ const parent = (pos - 1) >> 1;
12
+ if (compareFn(heap[parent], item) <= 0)
13
+ break;
14
+ pos = parent;
15
+ }
16
+ let hole = start;
17
+ while (hole > pos) {
18
+ const parent = (hole - 1) >> 1;
19
+ heap[hole] = heap[parent];
20
+ hole = parent;
21
+ }
22
+ heap[hole] = item;
23
+ return pos;
24
+ }
25
+ /**
26
+ * Undo the last `siftUp(heap, start, ...)`, whose item landed at `pos`: move each ancestor on the
27
+ * path back down to its old slot and drop slot `start`. Makes no comparisons.
28
+ */
29
+ function undoSiftUp(heap, start, pos) {
30
+ const path = [];
31
+ for (let index = start; index > pos; index = (index - 1) >> 1)
32
+ path.push(index);
33
+ path.push(pos);
34
+ for (let k = path.length - 1; k > 0; --k)
35
+ heap[path[k]] = heap[path[k - 1]];
36
+ heap.length = start;
37
+ }
38
+ /** Hole-based sift-down over `heap[0..end)`, placing `item` at `start`. */
39
+ function siftDown(heap, start, end, item, compareFn) {
40
+ const path = [];
41
+ let pos = start;
42
+ const half = end >> 1;
43
+ while (pos < half) {
44
+ let child = (pos << 1) | 1;
45
+ const right = child + 1;
46
+ if (right < end && compareFn(heap[child], heap[right]) > 0)
47
+ child = right;
48
+ if (compareFn(heap[child], item) >= 0)
49
+ break;
50
+ path.push(child);
51
+ pos = child;
52
+ }
53
+ let hole = start;
54
+ for (const child of path) {
55
+ heap[hole] = heap[child];
56
+ hole = child;
57
+ }
58
+ heap[hole] = item;
59
+ }
60
+ /**
61
+ * A binary min-heap: `peek` and `pop` give the smallest item by the comparator, so a reversed
62
+ * comparator makes a max-heap. Iteration is in heap order, not sorted order. `has` and `delete`
63
+ * use SameValueZero, as `Set` does. A comparator that throws leaves the heap unchanged, and a `push`
64
+ * of several items then adds none of them. With the default comparator, items are numbers, bigints
65
+ * or strings, checked as they enter: `NaN` throws `RangeError`, and `null`, `undefined` and other
66
+ * values throw `TypeError`; pass a comparator to order them.
67
+ *
68
+ * @example
69
+ * ```ts
70
+ * const heap = new Heap([5, 1, 3])
71
+ * heap.push(2)
72
+ * heap.pop() // 1
73
+ * heap.peek() // 2
74
+ * ```
75
+ */
76
+ export class Heap {
77
+ #heap;
78
+ #compare;
79
+ #checksItems;
80
+ /**
81
+ * Build a heap from `items` in linear time.
82
+ * @param items Initial items, copied. `null` means none.
83
+ * @param compareFn Comparator; the smallest item comes out first. Defaults to the primitive
84
+ * order (numbers, bigints and strings).
85
+ * @throws {TypeError} when `items` is not iterable, `compareFn` is not a function, or the default
86
+ * comparator cannot order an item.
87
+ * @throws {RangeError} when `items` is a huge sparse array, or holds `NaN` under the default
88
+ * comparator.
89
+ */
90
+ constructor(items, compareFn) {
91
+ if (compareFn != null && typeof compareFn !== 'function') {
92
+ throw new TypeError('comparator must be a function');
93
+ }
94
+ this.#compare = compareFn ?? defaultCompare;
95
+ this.#checksItems = compareFn == null;
96
+ const heap = arrayFromIterable(items ?? []);
97
+ if (this.#checksItems)
98
+ for (const item of heap)
99
+ defaultCompare(item, item);
100
+ for (let parent = (heap.length - 1) >> 1; parent >= 0; --parent) {
101
+ siftDown(heap, parent, heap.length, heap[parent], this.#compare);
102
+ }
103
+ this.#heap = heap;
104
+ }
105
+ /** The number of items. */
106
+ get size() {
107
+ return this.#heap.length;
108
+ }
109
+ /** `'Heap'`, for `Object.prototype.toString`. */
110
+ get [Symbol.toStringTag]() {
111
+ return 'Heap';
112
+ }
113
+ /**
114
+ * Add one or more items. When the comparator throws, none of them is added.
115
+ * @returns The new size.
116
+ * @throws {TypeError} when the default comparator cannot order an item.
117
+ * @throws {RangeError} when an item is `NaN` under the default comparator.
118
+ */
119
+ push(...items) {
120
+ const heap = this.#heap;
121
+ const firstStart = heap.length;
122
+ const landed = [];
123
+ try {
124
+ for (const item of items) {
125
+ if (this.#checksItems)
126
+ defaultCompare(item, item);
127
+ landed.push(siftUp(heap, heap.length, item, this.#compare));
128
+ }
129
+ }
130
+ catch (error) {
131
+ for (let k = landed.length - 1; k >= 0; --k)
132
+ undoSiftUp(heap, firstStart + k, landed[k]);
133
+ throw error;
134
+ }
135
+ return heap.length;
136
+ }
137
+ /** Remove and return the smallest item, or `undefined` when empty. */
138
+ pop() {
139
+ const heap = this.#heap;
140
+ if (heap.length === 0)
141
+ return undefined;
142
+ const top = heap[0];
143
+ siftDown(heap, 0, heap.length - 1, heap[heap.length - 1], this.#compare);
144
+ heap.pop();
145
+ return top;
146
+ }
147
+ /** The smallest item without removing it, or `undefined` when empty. */
148
+ peek() {
149
+ return this.#heap[0];
150
+ }
151
+ /** Whether `item` is in the heap (SameValueZero). */
152
+ has(item) {
153
+ return this.#heap.includes(item);
154
+ }
155
+ /**
156
+ * Remove one occurrence of `item` (SameValueZero).
157
+ * @returns Whether it was present.
158
+ */
159
+ delete(item) {
160
+ const heap = this.#heap;
161
+ const index = heap.findIndex((x) => sameValueZero(x, item));
162
+ if (index < 0)
163
+ return false;
164
+ if (index === 0) {
165
+ this.pop();
166
+ return true;
167
+ }
168
+ const end = heap.length - 1;
169
+ if (index < end) {
170
+ const last = heap[end];
171
+ const parent = (index - 1) >> 1;
172
+ if (this.#compare(heap[parent], last) > 0)
173
+ siftUp(heap, index, last, this.#compare);
174
+ else
175
+ siftDown(heap, index, end, last, this.#compare);
176
+ }
177
+ heap.pop();
178
+ return true;
179
+ }
180
+ /** Remove every item. */
181
+ clear() {
182
+ this.#heap.length = 0;
183
+ }
184
+ /** The items, in heap order. */
185
+ values() {
186
+ return this.#heap.values();
187
+ }
188
+ /** The items, in heap order, as `Set.prototype.keys` gives them. */
189
+ keys() {
190
+ return this.values();
191
+ }
192
+ /** `[item, item]` pairs, in heap order, as `Set.prototype.entries` gives them. */
193
+ entries() {
194
+ return this.values().map((v) => [v, v]);
195
+ }
196
+ /** Call `callback(item, item, heap)` for each item, in heap order. */
197
+ forEach(callback, thisArg) {
198
+ for (const v of this.values())
199
+ Reflect.apply(callback, thisArg, [v, v, this]);
200
+ }
201
+ /** The items, in heap order. */
202
+ [Symbol.iterator]() {
203
+ return this.values();
204
+ }
205
+ }