atomic-fns 1.2.0 → 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 +397 -91
  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 -256
  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 -623
  27. package/dist/collections/index.js +14 -844
  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 -148
  55. package/dist/decimal/index.js +10 -410
  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 -53
  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 -224
  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 +99 -51
  171. package/dist/collections/BTree.d.ts +0 -230
  172. package/dist/collections/BTree.js +0 -1143
  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 -83
  176. package/dist/collections/LRUCache.js +0 -139
  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 -142
  183. package/dist/collections/SplayTree.d.ts +0 -107
  184. package/dist/collections/SplayTree.js +0 -351
  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 -33
  191. package/dist/functools/index.d.ts +0 -106
  192. package/dist/functools/index.js +0 -203
  193. package/dist/globals/index.d.ts +0 -268
  194. package/dist/globals/index.js +0 -447
  195. package/dist/i18n/currency.d.ts +0 -27
  196. package/dist/i18n/currency.js +0 -100
  197. package/dist/i18n/date/index.d.ts +0 -210
  198. package/dist/i18n/date/index.js +0 -552
  199. package/dist/i18n/date/utils.d.ts +0 -64
  200. package/dist/i18n/date/utils.js +0 -192
  201. package/dist/i18n/duration.d.ts +0 -113
  202. package/dist/i18n/duration.js +0 -398
  203. package/dist/i18n/format.d.ts +0 -34
  204. package/dist/i18n/format.js +0 -378
  205. package/dist/i18n/index.d.ts +0 -9
  206. package/dist/i18n/index.js +0 -8
  207. package/dist/itertools/index.d.ts +0 -251
  208. package/dist/itertools/index.js +0 -434
  209. package/dist/math/index.d.ts +0 -79
  210. package/dist/math/index.js +0 -81
  211. package/dist/mod.d.ts +0 -1
  212. package/dist/mod.js +0 -1
  213. package/dist/operators/index.d.ts +0 -148
  214. package/dist/operators/index.js +0 -325
  215. package/dist/types.d.ts +0 -19
  216. package/dist/types.js +0 -1
@@ -1,434 +0,0 @@
1
- /**
2
- * This module includes functions that deal with or produce iterators.
3
- *
4
- * @module itertools
5
- */
6
- import { call, isDate, isEmpty, isIterable, isObject, isRegExp, notNull, ValueError } from '../globals/index.js';
7
- import { add, bool, compare, eq, identity } from '../operators/index.js';
8
- /**
9
- * Make an iterator that returns accumulated sums, or accumulated results of other binary functions (specified via the optional `func` argument).
10
- * @param iterable The iterable to accumulate
11
- * @param func The accumulator function (default `add`).
12
- * @param initial Optional initial value (defaults to the first element in iterable)
13
- * @returns A sequence of accumulated values
14
- */
15
- export function* accumulate(iterable, func = add, initial) {
16
- const it = iter(iterable);
17
- let total = initial;
18
- if (initial == null) {
19
- const head = it.next();
20
- if (head.done)
21
- return;
22
- total = head.value;
23
- }
24
- while (true) {
25
- yield total;
26
- const current = it.next();
27
- if (current.done)
28
- return;
29
- total = func(total, current.value);
30
- }
31
- }
32
- /**
33
- * Returns `true` when all of the items in iterable are truthy. An optional predicate function can be used to define what truthiness means for this specific collection.
34
- * @param iterable
35
- * @param fn
36
- * @returns `True` if all elements pass the test or are truthy else `false`.
37
- * @example
38
- ```js
39
- all([0])
40
- // false
41
-
42
- all([1, 2, 3])
43
- // true
44
-
45
- all([2, 4, 6], n => n > 1)
46
- // true
47
- ```
48
- @see {@link any}
49
- */
50
- export function all(iterable, fn = bool) {
51
- for (const item of iterable) {
52
- if (!fn(item))
53
- return false;
54
- }
55
- return true;
56
- }
57
- /**
58
- * Returns true when any of the items in iterable are truthy. An optional predicate function can be used to define what truthiness means for this specific collection.
59
- * @param iterable
60
- * @param fn
61
- * @returns `True` if any element passes the test or is truthy else `false`.
62
- * @example
63
- ```js
64
- any([0])
65
- // false
66
-
67
- any([0, 1, null, undefined])
68
- // true
69
-
70
- any([1, 4, 5], n => n % 2 === 0)
71
- // true
72
- ```
73
- @see {@link all}
74
- */
75
- export function any(iterable, fn = bool) {
76
- for (const item of iterable) {
77
- if (fn(item))
78
- return true;
79
- }
80
- return false;
81
- }
82
- export function chain(...iterables) {
83
- return iflatten(iterables);
84
- }
85
- /**
86
- * Checks if the `value` is in `collection`.
87
- * @param collection The collection to inspect.
88
- * @param value The value to search for.
89
- * @returns Returns `true` if value is found, else `false`.
90
- * @example
91
- ```js
92
- contains([1, 2, 3], 1)
93
- // true
94
-
95
- contains({ 'a': 1, 'b': 2 }, 1)
96
- // true
97
-
98
- contains('abcd', 'bc')
99
- // true
100
- ```
101
- *
102
- * @see {@link collections.find}
103
- */
104
- export function contains(collection, value) {
105
- if (!collection)
106
- return false;
107
- const includes = call(collection, 'includes', value);
108
- if (includes != null)
109
- return includes;
110
- const items = isIterable(collection) ? collection : Object.values(collection);
111
- return any(items, (x) => eq(value, x));
112
- }
113
- /**
114
- * Returns a generator that drops elements from the iterable as long as the predicate is `true`; afterwards, returns every remaining element. Note, the generator does not produce any output until the predicate first becomes `false`.
115
- * @param iterable The iterable to inspect.
116
- * @param predicate A function invoked per element to check if it should be dropped.
117
- * @returns Elements
118
- */
119
- export function* dropWhile(iterable, predicate) {
120
- const it = iter(iterable);
121
- while (true) {
122
- const head = it.next();
123
- if (head.done)
124
- return head.value;
125
- if (!predicate(head.value)) {
126
- yield head.value;
127
- break;
128
- }
129
- }
130
- while (true) {
131
- const head = it.next();
132
- if (head.done)
133
- return head.value;
134
- yield head.value;
135
- }
136
- }
137
- export function iforEach(iterable, fun) {
138
- for (const [index, value] of enumerate(iterable)) {
139
- if (fun(value, index, iterable) === false) {
140
- return;
141
- }
142
- }
143
- }
144
- /**
145
- * Yields elements like `[index, item]` from an iterable. Index starts at zero by default.
146
- * @template T
147
- * @param iterable The iterable to inspect.
148
- * @param start The starting index.
149
- * @returns A generator with tuples like `[index, item]`.
150
- * @example
151
- ```js
152
- console.log([...enumerate(['hello', 'world'])])
153
- // [[0, 'hello'], [1, 'world']]
154
-
155
- // Start with index `1`
156
- console.log([...enumerate(['hello', 'world'], 1)])
157
- // [[1, 'hello'], [2, 'world']]
158
- ```
159
- */
160
- export function* enumerate(iterable, start = 0) {
161
- let index = start;
162
- for (const item of iterable) {
163
- yield [index++, item];
164
- }
165
- }
166
- /**
167
- * Returns the first element in an iterable.
168
- * @param iterable The iterable to inspect.
169
- * @returns The first element found or undefined if iterable is empty.
170
- */
171
- export function first(iterable) {
172
- return iter(iterable).next().value;
173
- }
174
- /**
175
- * Creates a new generator that filters all falsy and empty values from iterable.
176
- * @param iterable The iterable to compact
177
- * @returns A generator with the filtered values.
178
- * @example
179
- ```js
180
- [...icompact([0, 1, false, 2, '', 3])]
181
- // [1, 2, 3]
182
- ```
183
- */
184
- export function* icompact(iterable) {
185
- for (const value of iterable) {
186
- // `isEmpty` reads arity and keys, which say nothing about these values
187
- if (!isEmpty(value) || typeof value === 'function' || isDate(value) || isRegExp(value)) {
188
- yield value;
189
- }
190
- }
191
- }
192
- /**
193
- * Iterates over elements of collection, producing only those elements where predicate returns a `truthy` value.
194
- */
195
- export function* ifilter(iterable, predicate) {
196
- for (const value of iterable) {
197
- if (predicate(value))
198
- yield value;
199
- }
200
- }
201
- export function* imap(iterable, mapFn) {
202
- for (const item of iterable) {
203
- yield mapFn(item);
204
- }
205
- }
206
- /**
207
- * Returns a generator flattening one level of nesting in a list of lists.
208
- * @param iterables The iterable to flatten
209
- * @example
210
- ```js
211
- [...iflatten([[0, 1], [2, 3]])]
212
- // [0, 1, 2, 3]
213
- ```
214
- */
215
- export function* iflatten(iterables) {
216
- for (const iterator of iterables) {
217
- for (const item of iterator) {
218
- yield item;
219
- }
220
- }
221
- }
222
- export function flatMap(iterable, mapFn = identity) {
223
- const toIterable = (x) => {
224
- const mapped = mapFn(x);
225
- return isIterable(mapped) ? mapped : [mapped];
226
- };
227
- return iflatten(imap(iterable, toIterable));
228
- }
229
- export function* islice(iterable, start, stop, step = 1) {
230
- if (stop != null && stop < 0)
231
- throw new ValueError(`stop parameter can't be negative`);
232
- if (start < 0)
233
- throw new ValueError(`start parameter can't be negative`);
234
- if (stop != null && start > stop)
235
- throw new ValueError(`start parameter can't be after stop`);
236
- if (step < 0)
237
- throw new ValueError(`step parameter can't be negative`);
238
- for (const [i, value] of enumerate(iterable)) {
239
- if (stop != null && i >= stop)
240
- return;
241
- if (i === start) {
242
- yield value;
243
- start += step;
244
- }
245
- }
246
- }
247
- export function* itake(n, iterable) {
248
- const it = iter(iterable);
249
- while (n-- > 0) {
250
- const item = it.next();
251
- if (!item.done)
252
- yield item.value;
253
- else
254
- return;
255
- }
256
- }
257
- /**
258
- * Returns an Iterator from the elements of a collection or the object keys.
259
- * @param obj The given collection to iterate over.
260
- * @returns An Iterator type.
261
- */
262
- export function iter(obj) {
263
- const iterable = call(obj, Symbol.iterator);
264
- if (notNull(iterable))
265
- return iterable;
266
- return Object.keys(obj)[Symbol.iterator]();
267
- }
268
- export function partition(iterable, predicate) {
269
- const left = [];
270
- const right = [];
271
- for (const value of iterable) {
272
- if (predicate(value))
273
- left.push(value);
274
- else
275
- right.push(value);
276
- }
277
- return [left, right];
278
- }
279
- /**
280
- * Returns a generator for a sequence of numbers in the given range, starting from `start` (default 0) to `stop` (not-inclusive) in increments of `step` (default 1).
281
- *
282
- * `range(x)` is a shorthand for `range(0, x, 1)`, or `range(0, x, -1)` when `x` is negative.
283
- * @param args When called with a single argument, it is assigned to `stop` param. If two arguments are given, it will assign them to `(start, stop)`. When three arguments, it means `(start, stop, step)`.
284
- * @example
285
- ```js
286
- range(5)
287
- // [0, 1, 2, 3, 4]
288
-
289
- range(2, 5)
290
- // [2, 3, 4]
291
-
292
- range(0, 5, 2)
293
- // [0, 2, 4]
294
-
295
- range(5, 0, -1)
296
- // [5, 4, 3, 2, 1]
297
-
298
- range(-3)
299
- // [0, -1, -2]
300
- ```
301
- */
302
- export function* range(...args) {
303
- let start = 0;
304
- let stop = 0;
305
- let step = 1;
306
- if (args.length === 1) {
307
- stop = args[0];
308
- if (stop < 0)
309
- step = -1;
310
- }
311
- else if (args.length === 2) {
312
- start = args[0];
313
- stop = args[1];
314
- }
315
- else {
316
- start = args[0];
317
- stop = args[1];
318
- step = args[2] || 1;
319
- }
320
- for (; step > 0 ? start < stop : start > stop; start += step) {
321
- yield start;
322
- }
323
- }
324
- /**
325
- * Applies a `reducer` function on each element of the iterable, in order, passing the return value from the operation on the previous element. The final result of running the reducer across all elements of the iterable is a single value.
326
- * @param iterable The collection to reduce.
327
- * @param reducer A reducer function that will be called per item.
328
- * @param initial Optional initial value (defaults to the first element in the iterable).
329
- * @returns The result of calling reducer on each item.
330
- * @example
331
- ```js
332
- reduce([1, 2, 3, 4, 5], (x, y) => x + y, 0)
333
- // 15
334
- ```
335
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce Array.reduce()}
336
- */
337
- export function reduce(iterable, reducer, initial) {
338
- const it = iter(iterable);
339
- let output = initial ?? it.next().value;
340
- for (const [index, value] of enumerate({ [Symbol.iterator]: () => it })) {
341
- output = reducer(output, value, index);
342
- }
343
- return output;
344
- }
345
- /**
346
- * Creates a new array with the elements from the iterable in reverse order.
347
- * @param iterable The iterable to inspect.
348
- * @returns A new array with the elements reversed.
349
- */
350
- export function reversed(iterable) {
351
- return Array.from(iterable).reverse();
352
- }
353
- const compKey = (compare, key) => (x, y) => compare(key(x), key(y));
354
- export function sorted(args, key, reverse, compareFn) {
355
- if (isObject(args)) {
356
- args = Object.keys(args);
357
- }
358
- if (typeof key === 'boolean') {
359
- if (typeof reverse === 'function')
360
- compareFn = reverse;
361
- reverse = key || reverse === true;
362
- key = identity;
363
- }
364
- const copy = Array.from(args);
365
- const _compare = compKey(compareFn || compare, key || identity);
366
- copy.sort(_compare);
367
- if (reverse)
368
- copy.reverse();
369
- return copy;
370
- }
371
- export function sort(args, reverse, compareFn) {
372
- if (typeof reverse === 'function') {
373
- compareFn = reverse;
374
- reverse = false;
375
- }
376
- args.sort(compareFn || compare);
377
- if (reverse)
378
- args.reverse();
379
- return args;
380
- }
381
- export function take(n, iterable) {
382
- return [...itake(n, iterable)];
383
- }
384
- /**
385
- * Returns a generator that takes elements from the iterable as long as the predicate is `true`.
386
- * @param iterable The iterable to inspect.
387
- * @param predicate A function invoked per element to check if it should be taken.
388
- * @returns Elements
389
- */
390
- export function* takeWhile(iterable, predicate) {
391
- for (const value of iterable) {
392
- if (!predicate(value))
393
- return;
394
- yield value;
395
- }
396
- }
397
- /** Returns a generator that calls `fn(index)` for each index < n */
398
- /**
399
- * Returns a generator that produces `fn(index)` for each `index < n`.
400
- * @param n The number of times to invoke `fn`.
401
- * @param fn A function used to produce the results.
402
- */
403
- export function* times(n, fn) {
404
- for (let i = 0; i < n; i++)
405
- yield fn(i);
406
- }
407
- /**
408
- * Returns a generator that aggregates elements from the iterables producing tuples with the next element from each one.
409
- * @param args The iterables to zip
410
- * @returns Tuples with elements from each iterator
411
- * @example
412
- ```js
413
- for item of zip([1, 2, 3], ['sugar', 'spice', 'nice'])
414
- console.log(item)
415
- // [1, 'sugar']
416
- // [2, 'spice']
417
- // [3, 'nice']
418
- ```
419
- */
420
- export function* zip(...args) {
421
- if (args.length === 0)
422
- return;
423
- const iterables = args.map(iter);
424
- while (true) {
425
- const items = [];
426
- for (const iterator of iterables) {
427
- const { value, done } = iterator.next();
428
- if (done)
429
- return;
430
- items.push(value);
431
- }
432
- yield items;
433
- }
434
- }
@@ -1,79 +0,0 @@
1
- /**
2
- * Math and algebraic operations.
3
- *
4
- * @module math
5
- */
6
- import type { Predicate } from '../types.js';
7
- /**
8
- * Returns the absolute value of a number. The argument may be a number or an object implementing the `abs()`.
9
- * @param x The value.
10
- * @returns The absolute value.
11
- */
12
- export declare function abs(x: any): number;
13
- /**
14
- * Round `x` to the number of digits after the decimal point. If `digits` is
15
- * omitted, it returns the nearest integer to x.
16
- *
17
- * @param {number} x
18
- * @param {number} [digits=0]
19
- *
20
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round Math.round()}
21
- */
22
- export declare function round(x: number, digits?: number): number;
23
- /**
24
- * Returns a tuple like `[q, r]` with the floored quotient of `x / y` and a remainder that takes the sign of `y`, so `q * y + r` gives `x`, as in Python.
25
- *
26
- * @param x
27
- * @param y
28
- * @returns
29
- */
30
- export declare function divmod(x: number, y: number): [number, number];
31
- export declare function logBase(x: number, y: number): number;
32
- /**
33
- * Returns the smallest element in an iterable. The optional `key` argument specifies a transform on the elements before comparing them. If the elements in the iterable are objects that implement a custom `lt` method, this will be called to compare.
34
- * @param iterable The iterable to inspect.
35
- * @param key Optional key function to transform the elements (default `identity`).
36
- * @returns The smallest element in the iterable.
37
- */
38
- export declare function min(iterable: number[]): number;
39
- export declare function min<T>(iterable: Iterable<T>, key?: Predicate<T>): T;
40
- /**
41
- * Returns the largest element in an iterable. The optional `key` argument specifies a transform on the elements before comparing them. If the elements in the iterable are objects that implement a custom `gt` method, this will be called to compare.
42
- * @param iterable The iterable to inspect.
43
- * @param key Optional key function to transform the elements (default `identity`).
44
- * @returns The largest element in the iterable.
45
- */
46
- export declare function max(iterable: number[]): number;
47
- export declare function max<T>(iterable: Iterable<T>, key?: Predicate<T>): T;
48
- /**
49
- * Computes the mean of the values in `iterable` and accepts an optional `key` function which is invoked for each element to generate the values to be averaged.
50
- * @param {Array<number>|Iterable<T>} iterable
51
- * @param {Function} [key] Optional iteratee invoked per element.
52
- * @returns Returns the mean value or `NaN` if no elements.
53
- * @template T
54
- * @example
55
- ```js
56
- let objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }]
57
-
58
- mean(objects, o => o.n)
59
- // 5
60
- ```
61
- */
62
- export declare function mean(iterable: number[]): number;
63
- export declare function mean<T>(iterable: Iterable<T>, key?: Predicate<T>): number;
64
- /**
65
- * Sums the items of an iterable and returns the total. The optional `key` argument specifies a transform on the elements before adding them.
66
- * @param args The elements to add.
67
- * @param key Optional key function to transform the elements (default `identity`).
68
- * @returns The total sum of the iterable elements.
69
- * @example
70
- ```js
71
- sum([1, 2, 3, 4])
72
- // 10
73
-
74
- sum([{ n: 1 }, { n: 2 }], o => o.n)
75
- // 3
76
- ```
77
- */
78
- export declare function sum(args: number[]): number;
79
- export declare function sum<T>(args: Iterable<T>, key?: Predicate<T>): number;
@@ -1,81 +0,0 @@
1
- /**
2
- * Math and algebraic operations.
3
- *
4
- * @module math
5
- */
6
- import { call } from '../globals/index.js';
7
- import { reduce } from '../itertools/index.js';
8
- import { gt, identity, lt } from '../operators/index.js';
9
- /**
10
- * Returns the absolute value of a number. The argument may be a number or an object implementing the `abs()`.
11
- * @param x The value.
12
- * @returns The absolute value.
13
- */
14
- export function abs(x) {
15
- const op = call(x, 'abs');
16
- if (op != null)
17
- return op;
18
- return Math.abs(x);
19
- }
20
- /**
21
- * Round `x` to the number of digits after the decimal point. If `digits` is
22
- * omitted, it returns the nearest integer to x.
23
- *
24
- * @param {number} x
25
- * @param {number} [digits=0]
26
- *
27
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round Math.round()}
28
- */
29
- export function round(x, digits = 0) {
30
- const base = 10 ** digits;
31
- const scaled = x * base;
32
- // A number too large to scale has no digits left to round.
33
- return Number.isFinite(scaled) ? Math.round(scaled) / base : Number(x);
34
- }
35
- /**
36
- * Returns a tuple like `[q, r]` with the floored quotient of `x / y` and a remainder that takes the sign of `y`, so `q * y + r` gives `x`, as in Python.
37
- *
38
- * @param x
39
- * @param y
40
- * @returns
41
- */
42
- export function divmod(x, y) {
43
- let remainder = x % y;
44
- let quotient = (x - remainder) / y;
45
- if (remainder !== 0 && remainder < 0 !== y < 0) {
46
- remainder += y;
47
- quotient -= 1;
48
- }
49
- // A zero or NaN quotient keeps the sign of zero and the infinities of `x / y`.
50
- return [Math.round(quotient) || Math.floor(x / y), remainder];
51
- }
52
- export function logBase(x, y) {
53
- if (x > 0 && y > 0) {
54
- return Math.log(y) / Math.log(x);
55
- }
56
- return Number.NaN;
57
- }
58
- export function min(iterable, key = identity) {
59
- return reduce(iterable, (x, y) => (lt(key(x), key(y)) ? x : y));
60
- }
61
- export function max(iterable, key = identity) {
62
- return reduce(iterable, (x, y) => (gt(key(x), key(y)) ? x : y));
63
- }
64
- export function mean(iterable, key = identity) {
65
- let total = 0;
66
- let count = 0;
67
- for (const value of iterable) {
68
- total += key(value);
69
- count++;
70
- }
71
- if (count !== 0)
72
- return total / count;
73
- return Number.NaN;
74
- }
75
- export function sum(args, key = identity) {
76
- let initial = 0;
77
- for (const x of args) {
78
- initial += key(x);
79
- }
80
- return initial;
81
- }
package/dist/mod.d.ts DELETED
@@ -1 +0,0 @@
1
- export * from './index.js';
package/dist/mod.js DELETED
@@ -1 +0,0 @@
1
- export * from './index.js';