atomic-fns 0.2.0 → 0.2.3

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 (64) hide show
  1. package/.esdoc.json +19 -1
  2. package/.eslintrc.json +1 -0
  3. package/README.md +8 -9
  4. package/package.json +9 -9
  5. package/src/collections/index.d.ts +658 -0
  6. package/src/collections/index.js +1068 -0
  7. package/src/collections/index.ts +1162 -0
  8. package/src/datetime/index.d.ts +6 -0
  9. package/src/datetime/index.js +6 -0
  10. package/src/datetime/index.ts +7 -0
  11. package/src/{functools.d.ts → functools/index.d.ts} +10 -5
  12. package/src/{functools.js → functools/index.js} +9 -4
  13. package/src/{functools.ts → functools/index.ts} +11 -5
  14. package/src/globals/index.d.ts +291 -0
  15. package/src/globals/index.js +391 -0
  16. package/src/globals/index.ts +456 -0
  17. package/src/index.d.ts +8 -7
  18. package/src/index.js +8 -7
  19. package/src/index.ts +8 -7
  20. package/src/itertools/index.d.ts +254 -0
  21. package/src/itertools/index.js +485 -0
  22. package/src/itertools/index.ts +511 -0
  23. package/src/math/index.d.ts +21 -0
  24. package/src/math/index.js +39 -0
  25. package/src/math/index.ts +44 -0
  26. package/src/{operators.d.ts → operators/index.d.ts} +8 -6
  27. package/src/{operators.js → operators/index.js} +69 -26
  28. package/src/{operators.ts → operators/index.ts} +77 -28
  29. package/src/random/index.d.ts +42 -0
  30. package/src/random/index.js +65 -0
  31. package/src/random/index.ts +66 -0
  32. package/tests/collections.spec.ts +119 -30
  33. package/tests/globals.spec.ts +91 -49
  34. package/tests/itertools.spec.ts +207 -14
  35. package/tests/operators.spec.ts +7 -29
  36. package/tests/random.spec.ts +46 -0
  37. package/tsconfig.json +19 -3
  38. package/src/collections.d.ts +0 -224
  39. package/src/collections.js +0 -570
  40. package/src/collections.ts +0 -637
  41. package/src/datetime.d.ts +0 -1
  42. package/src/datetime.js +0 -1
  43. package/src/datetime.ts +0 -1
  44. package/src/globals.d.ts +0 -175
  45. package/src/globals.js +0 -329
  46. package/src/globals.ts +0 -373
  47. package/src/itertools.d.ts +0 -16
  48. package/src/itertools.js +0 -101
  49. package/src/itertools.ts +0 -112
  50. package/src/math.d.ts +0 -16
  51. package/src/math.js +0 -67
  52. package/src/math.ts +0 -73
  53. package/src/objects.d.ts +0 -0
  54. package/src/objects.js +0 -1
  55. package/src/objects.ts +0 -0
  56. package/src/random.d.ts +0 -22
  57. package/src/random.js +0 -48
  58. package/src/random.ts +0 -49
  59. package/src/strings.d.ts +0 -0
  60. package/src/strings.js +0 -1
  61. package/src/strings.ts +0 -0
  62. package/src/util.d.ts +0 -0
  63. package/src/util.js +0 -1
  64. package/src/util.ts +0 -0
@@ -0,0 +1,254 @@
1
+ /**
2
+ * This module includes functions that deal with or produce iterators.
3
+ *
4
+ * @module Iterators
5
+ */
6
+ import { Comp, Function, Predicate } from '../globals/index.js';
7
+ import { add } 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 declare function accumulate<T>(iterable: Iterable<T>, func?: typeof add, initial?: any): Generator<any, void, unknown>;
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 const 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 const 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): any;
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): Generator<unknown, any, unknown>;
85
+ /**
86
+ * Yields elements like `[index, item]` from an iterable. Index starts at zero by default.
87
+ * @template T
88
+ * @param iterable The iterable to inspect.
89
+ * @param start The starting index.
90
+ * @returns A generator with tuples like `[index, item]`.
91
+ * @example
92
+ ```js
93
+ console.log([...enumerate(['hello', 'world'])])
94
+ // => [[0, 'hello'], [1, 'world']]
95
+
96
+ // Start with index `1`
97
+ console.log([...enumerate(['hello', 'world'], 1)])
98
+ // => [[1, 'hello'], [2, 'world']]
99
+ ```
100
+ */
101
+ export declare function enumerate<T = any>(iterable: Iterable<T>, start?: number): Generator<(number | T)[], void, unknown>;
102
+ /**
103
+ * Returns the first element in an iterable.
104
+ * @param iterable The iterable to inspect.
105
+ * @returns The first element found or undefined if iterable is empty.
106
+ */
107
+ export declare function first<T>(iterable: Iterable<T>): any;
108
+ /**
109
+ * Creates a new generator that filters all `falsey` and empty values from iterable.
110
+ * @param arr The array to compact
111
+ * @returns A new array with the filtered values.
112
+ * @example
113
+ ```js
114
+ compact([0, 1, false, 2, '', 3])
115
+ // => [1, 2, 3]
116
+ ```
117
+ */
118
+ export declare function icompact<T>(iterable: Iterable<T>): Generator<T, void, unknown>;
119
+ /**
120
+ * Iterates over elements of collection, producing only those elements where predicate returns a `truthy` value.
121
+ */
122
+ export declare function ifilter<T>(iterable: Iterable<T>, predicate: Predicate): Generator<T, void, unknown>;
123
+ export declare function imap<T>(iterable: Iterable<T>, mapFn: (x: T) => any): Generator<any, void, unknown>;
124
+ /**
125
+ * Returns a generator flattening one level of nesting in a list of lists.
126
+ * @param iterables The iterable to flatten
127
+ * @example
128
+ ```js
129
+ [...iflatten([[0, 1], [2, 3]])]
130
+ // => [0, 1, 2, 3]
131
+ ```
132
+ */
133
+ export declare function iflatten<T = any>(iterables: Iterable<Iterable<T>>): Generator<T, void, unknown>;
134
+ export declare function flatMap<T>(iterable: Iterable<T>, mapFn?: (x: T) => any): Generator<T, void, unknown>;
135
+ export declare function islice<T>(iterable: Iterable<T>, start: number, stop?: number, step?: number): Generator<number | T, number | T | undefined, unknown>;
136
+ export declare function itake<T>(n: number, iterable: Iterable<T>): Generator<unknown, void, unknown>;
137
+ /**
138
+ * Returns an Iterator from the elements of a collection or the object keys.
139
+ * @param obj The given collection to iterate over.
140
+ * @returns An Iterator type.
141
+ */
142
+ export declare function iter<T>(obj: any): Iterator<T>;
143
+ export declare function partition<T>(iterable: Iterable<T>, predicate: Predicate<T>): T[][];
144
+ /**
145
+ * 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 `le` method, this will be called to compare.
146
+ * @param iterable The iterable to inspect.
147
+ * @param key Optional key function to transform the elements (default `id`).
148
+ * @returns The smallest element in the iterable.
149
+ */
150
+ export declare function min<T>(iterable: Iterable<T>, key?: (x: any) => any): any;
151
+ /**
152
+ * 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.
153
+ * @param iterable The iterable to inspect.
154
+ * @param key Optional key function to transform the elements (default `id`).
155
+ * @returns The largest element in the iterable.
156
+ */
157
+ export declare function max<T>(iterable: Iterable<T>, key?: (x: any) => any): any;
158
+ /**
159
+ * 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).
160
+ *
161
+ * `range(x)` is a shorthand for `range(0, x, 1)`.
162
+ * @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)`.
163
+ * @example
164
+ ```js
165
+ range(5)
166
+ // => [0, 1, 2, 3, 4]
167
+
168
+ range(2, 5)
169
+ // => [2, 3, 4]
170
+
171
+ range(0, 5, 2)
172
+ // => [0, 2, 4]
173
+
174
+ range(5, 0, -1)
175
+ // => [5, 4, 3, 2, 1]
176
+
177
+ range(-3)
178
+ // => []
179
+ ```
180
+ */
181
+ export declare function range(...args: number[]): Generator<number, void, unknown>;
182
+ /**
183
+ * 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.
184
+ * @param iterable The collection to reduce.
185
+ * @param reducer A reducer function that will be called per item.
186
+ * @param initial Optional initial value (defaults to the first element in the iterable).
187
+ * @returns The result of calling reducer on each item.
188
+ * @example
189
+ ```js
190
+ reduce([1, 2, 3, 4, 5], (x, y) => x + y, 0)
191
+ // => 15
192
+ ```
193
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce Array.reduce()}
194
+ */
195
+ export declare function reduce<T>(iterable: Iterable<T>, reducer: (prev: T, value: T, index: any) => T, initial?: any): any;
196
+ /**
197
+ * Creates a new array with the elements from the iterable in reverse order.
198
+ * @param iterable The iterable to inspect.
199
+ * @returns A new array with the elements reversed.
200
+ */
201
+ export declare function reversed<T>(iterable: Iterable<T>): T[];
202
+ export declare function sorted(args: any[]): any[];
203
+ export declare function sorted(args: any[], reverse: boolean): any[];
204
+ export declare function sorted(args: any[], reverse: boolean, comp: Comp): any[];
205
+ export declare function sorted(args: any[], key: Function): any[];
206
+ export declare function sorted(args: any[], key: Function, reverse: boolean): any[];
207
+ export declare function sort(args: any[]): any[];
208
+ export declare function sort(args: any[], reverse: boolean): any[];
209
+ export declare function sort(args: any[], compareFn: Comp): any[];
210
+ export declare function sort(args: any[], reverse: boolean, compareFn: Comp): any[];
211
+ /**
212
+ * Sums `initial` and the items of an iterable and returns the total.
213
+ * @param args The elements to add to `initial`.
214
+ * @param initial The initial value to add to (default `0`).
215
+ * @returns The total sum of initial and iterable elements.
216
+ * @example
217
+ ```js
218
+ sum([1, 2, 3, 4])
219
+ // => 10
220
+
221
+ sum([1, 2, 3, 4], 5)
222
+ // => 15
223
+ ```
224
+ */
225
+ export declare function sum(args: Iterable<number>, initial?: number): number;
226
+ export declare function take<T>(n: number, iterable: Iterable<T>): unknown[];
227
+ /**
228
+ * Returns a generator that takes elements from the iterable as long as the predicate is `true`.
229
+ * @param iterable The iterable to inspect.
230
+ * @param predicate A function invoked per element to check if it should be taken.
231
+ * @returns Elements
232
+ */
233
+ export declare function takeWhile<T>(iterable: Iterable<T>, predicate: Predicate): Generator<T, void, unknown>;
234
+ /** Returns a generator that calls `fn(index)` for each index < n */
235
+ /**
236
+ * Returns a generator that produces `fn(index)` for each `index < n`.
237
+ * @param n The number of times to invoke `fn`.
238
+ * @param fn A function used to produce the results.
239
+ */
240
+ export declare function times(n: number, fn: (index: number) => any): Generator<any, void, unknown>;
241
+ /**
242
+ * Returns a generator that aggregates elements from the iterables producing tuples with the next element from each one.
243
+ * @param args The iterables to zip
244
+ * @returns Tuples with elements from each iterator
245
+ * @example
246
+ ```js
247
+ for item of zip([1, 2, 3], ['sugar', 'spice', 'nice'])
248
+ console.log(item)
249
+ // => [1, 'sugar']
250
+ // => [2, 'spice']
251
+ // => [3, 'nice']
252
+ ```
253
+ */
254
+ export declare function zip(...args: Array<Iterable<any>>): Generator<any[], void, unknown>;