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