itertools 2.3.1 → 2.4.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.
- package/README.md +1 -1
- package/dist/index.cjs +55 -29
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +73 -60
- package/dist/index.d.ts +73 -60
- package/dist/index.js +54 -28
- package/dist/index.js.map +1 -1
- package/package.json +20 -16
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
type Predicate<T> = (value: T) => boolean;
|
|
1
|
+
type Predicate<T> = (value: T, index: number) => boolean;
|
|
2
2
|
type Primitive = string | number | boolean;
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -6,7 +6,7 @@ type Primitive = string | number | boolean;
|
|
|
6
6
|
* any. If no predicate is given, it will return the first value returned by
|
|
7
7
|
* the iterable.
|
|
8
8
|
*/
|
|
9
|
-
declare function find<T>(iterable: Iterable<T>,
|
|
9
|
+
declare function find<T>(iterable: Iterable<T>, predicate?: Predicate<T>): T | undefined;
|
|
10
10
|
/**
|
|
11
11
|
* Returns true when all of the items in iterable are truthy. An optional key
|
|
12
12
|
* function can be used to define what truthiness means for this specific
|
|
@@ -25,7 +25,7 @@ declare function find<T>(iterable: Iterable<T>, keyFn?: Predicate<T>): T | undef
|
|
|
25
25
|
* all([2, 4, 5], n => n % 2 === 0) // => false
|
|
26
26
|
*
|
|
27
27
|
*/
|
|
28
|
-
declare function every<T>(iterable: Iterable<T>,
|
|
28
|
+
declare function every<T>(iterable: Iterable<T>, predicate?: Predicate<T>): boolean;
|
|
29
29
|
/**
|
|
30
30
|
* Returns true when some of the items in iterable are truthy. An optional key
|
|
31
31
|
* function can be used to define what truthiness means for this specific
|
|
@@ -43,7 +43,7 @@ declare function every<T>(iterable: Iterable<T>, keyFn?: Predicate<T>): boolean;
|
|
|
43
43
|
* some([{name: 'Bob'}, {name: 'Alice'}], person => person.name.startsWith('C')) // => false
|
|
44
44
|
*
|
|
45
45
|
*/
|
|
46
|
-
declare function some<T>(iterable: Iterable<T>,
|
|
46
|
+
declare function some<T>(iterable: Iterable<T>, predicate?: Predicate<T>): boolean;
|
|
47
47
|
/**
|
|
48
48
|
* Alias of `every()`.
|
|
49
49
|
*/
|
|
@@ -81,7 +81,7 @@ declare function enumerate<T>(iterable: Iterable<T>, start?: number): IterableIt
|
|
|
81
81
|
/**
|
|
82
82
|
* Non-lazy version of ifilter().
|
|
83
83
|
*/
|
|
84
|
-
declare function filter<T, N extends T>(iterable: Iterable<T>, predicate: (item: T) => item is N): N[];
|
|
84
|
+
declare function filter<T, N extends T>(iterable: Iterable<T>, predicate: (item: T, index: number) => item is N): N[];
|
|
85
85
|
declare function filter<T>(iterable: Iterable<T>, predicate: Predicate<T>): T[];
|
|
86
86
|
/**
|
|
87
87
|
* Returns an iterator object for the given iterable. This can be used to
|
|
@@ -189,11 +189,60 @@ declare function sum(iterable: Iterable<number>): number;
|
|
|
189
189
|
/**
|
|
190
190
|
* See izip.
|
|
191
191
|
*/
|
|
192
|
-
declare function zip<T1, T2>(xs: Iterable<T1>, ys: Iterable<T2>):
|
|
192
|
+
declare function zip<T1, T2>(xs: Iterable<T1>, ys: Iterable<T2>): [T1, T2][];
|
|
193
193
|
/**
|
|
194
194
|
* See izip3.
|
|
195
195
|
*/
|
|
196
|
-
declare function zip3<T1, T2, T3>(xs: Iterable<T1>, ys: Iterable<T2>, zs: Iterable<T3>):
|
|
196
|
+
declare function zip3<T1, T2, T3>(xs: Iterable<T1>, ys: Iterable<T2>, zs: Iterable<T3>): [T1, T2, T3][];
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Returns an iterable, filtering out any "nullish" values from the iterable.
|
|
200
|
+
*
|
|
201
|
+
* >>> compact([1, 2, undefined, 3, null])
|
|
202
|
+
* [1, 2, 3]
|
|
203
|
+
*
|
|
204
|
+
* For an eager version, @see compact().
|
|
205
|
+
*/
|
|
206
|
+
declare function icompact<T>(iterable: Iterable<T | null | undefined>): IterableIterator<T>;
|
|
207
|
+
/**
|
|
208
|
+
* Returns an array, filtering out any "nullish" values from the iterable.
|
|
209
|
+
*
|
|
210
|
+
* >>> compact([1, 2, undefined, 3, null])
|
|
211
|
+
* [1, 2, 3]
|
|
212
|
+
*
|
|
213
|
+
* For a lazy version, @see icompact().
|
|
214
|
+
*/
|
|
215
|
+
declare function compact<T>(iterable: Iterable<T | null | undefined>): T[];
|
|
216
|
+
/**
|
|
217
|
+
* Removes all "nullish" values from the given object. Returns a new object.
|
|
218
|
+
*
|
|
219
|
+
* >>> compactObject({ a: 1, b: undefined, c: 0, d: null })
|
|
220
|
+
* { a: 1, c: 0 }
|
|
221
|
+
*
|
|
222
|
+
*/
|
|
223
|
+
declare function compactObject<K extends string, V>(obj: Record<K, V | null | undefined>): Record<K, V>;
|
|
224
|
+
/**
|
|
225
|
+
* Almost an alias of find(). There only is a difference if no key fn is
|
|
226
|
+
* provided. In that case, `find()` will return the first item in the iterable,
|
|
227
|
+
* whereas `first()` will return the first non-`undefined` value in the
|
|
228
|
+
* iterable.
|
|
229
|
+
*/
|
|
230
|
+
declare function first<T>(iterable: Iterable<T>, keyFn?: Predicate<T>): T | undefined;
|
|
231
|
+
/**
|
|
232
|
+
* Returns 0 or more values for every value in the given iterable.
|
|
233
|
+
* Technically, it's just calling map(), followed by flatten(), but it's a very
|
|
234
|
+
* useful operation if you want to map over a structure, but not have a 1:1
|
|
235
|
+
* input-output mapping. Instead, if you want to potentially return 0 or more
|
|
236
|
+
* values per input element, use flatmap():
|
|
237
|
+
*
|
|
238
|
+
* For example, to return all numbers `n` in the input iterable `n` times:
|
|
239
|
+
*
|
|
240
|
+
* >>> const repeatN = n => repeat(n, n);
|
|
241
|
+
* >>> [...flatmap([0, 1, 2, 3, 4], repeatN)]
|
|
242
|
+
* [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] // note: no 0
|
|
243
|
+
*
|
|
244
|
+
*/
|
|
245
|
+
declare function flatmap<T, S>(iterable: Iterable<T>, mapper: (item: T) => Iterable<S>): IterableIterator<S>;
|
|
197
246
|
|
|
198
247
|
/**
|
|
199
248
|
* Returns an iterator that returns elements from the first iterable until it
|
|
@@ -272,6 +321,10 @@ declare const izip2: typeof izip;
|
|
|
272
321
|
* fillvalue. Iteration continues until the longest iterable is exhausted.
|
|
273
322
|
*/
|
|
274
323
|
declare function izipLongest2<T1, T2, D>(xs: Iterable<T1>, ys: Iterable<T2>, filler?: D): IterableIterator<[T1 | D, T2 | D]>;
|
|
324
|
+
/**
|
|
325
|
+
* See izipLongest2, but for three.
|
|
326
|
+
*/
|
|
327
|
+
declare function izipLongest3<T1, T2, T3, D = undefined>(xs: Iterable<T1>, ys: Iterable<T2>, zs: Iterable<T3>, filler?: D): IterableIterator<[T1 | D, T2 | D, T3 | D]>;
|
|
275
328
|
/**
|
|
276
329
|
* Like the other izips (`izip`, `izip3`, etc), but generalized to take an
|
|
277
330
|
* unlimited amount of input iterables. Think `izip(*iterables)` in Python.
|
|
@@ -305,7 +358,8 @@ declare function repeat<T>(thing: T, times?: number): IterableIterator<T>;
|
|
|
305
358
|
* predicate is true.
|
|
306
359
|
*/
|
|
307
360
|
declare function takewhile<T>(iterable: Iterable<T>, predicate: Predicate<T>): IterableIterator<T>;
|
|
308
|
-
declare function zipLongest2<T1, T2, D>(xs: Iterable<T1>, ys: Iterable<T2>, filler?: D):
|
|
361
|
+
declare function zipLongest2<T1, T2, D>(xs: Iterable<T1>, ys: Iterable<T2>, filler?: D): [T1 | D, T2 | D][];
|
|
362
|
+
declare function zipLongest3<T1, T2, T3, D>(xs: Iterable<T1>, ys: Iterable<T2>, zs: Iterable<T3>, filler?: D): [T1 | D, T2 | D, T3 | D][];
|
|
309
363
|
declare const izipLongest: typeof izipLongest2;
|
|
310
364
|
declare const zipLongest: typeof zipLongest2;
|
|
311
365
|
declare function zipMany<T>(...iters: Iterable<T>[]): T[][];
|
|
@@ -331,6 +385,14 @@ declare function chunked<T>(iterable: Iterable<T>, size: number): IterableIterat
|
|
|
331
385
|
*
|
|
332
386
|
*/
|
|
333
387
|
declare function flatten<T>(iterableOfIterables: Iterable<Iterable<T>>): IterableIterator<T>;
|
|
388
|
+
/**
|
|
389
|
+
* Intersperse filler element `value` among the items in `iterable`.
|
|
390
|
+
*
|
|
391
|
+
* >>> [...intersperse(-1, range(1, 5))]
|
|
392
|
+
* [1, -1, 2, -1, 3, -1, 4]
|
|
393
|
+
*
|
|
394
|
+
*/
|
|
395
|
+
declare function intersperse<T, V>(value: V, iterable: Iterable<T>): IterableIterator<T | V>;
|
|
334
396
|
/**
|
|
335
397
|
* Returns an iterable containing only the first `n` elements of the given
|
|
336
398
|
* iterable.
|
|
@@ -360,7 +422,7 @@ declare function pairwise<T>(iterable: Iterable<T>): IterableIterator<[T, T]>;
|
|
|
360
422
|
* [0, 2, 4, 6, 8]
|
|
361
423
|
*
|
|
362
424
|
*/
|
|
363
|
-
declare function partition<T, N extends T>(iterable: Iterable<T>, predicate: (item: T) => item is N): [N[], Exclude<T, N>[]];
|
|
425
|
+
declare function partition<T, N extends T>(iterable: Iterable<T>, predicate: (item: T, index: number) => item is N): [N[], Exclude<T, N>[]];
|
|
364
426
|
declare function partition<T>(iterable: Iterable<T>, predicate: Predicate<T>): [T[], T[]];
|
|
365
427
|
/**
|
|
366
428
|
* Yields the next item from each iterable in turn, alternating between them.
|
|
@@ -381,7 +443,7 @@ declare function roundrobin<T>(...iters: Iterable<T>[]): IterableIterator<T>;
|
|
|
381
443
|
* This is also different from `zipLongest()`, since the number of items in
|
|
382
444
|
* each round can decrease over time, rather than being filled with a filler.
|
|
383
445
|
*/
|
|
384
|
-
declare function heads<T>(...iters:
|
|
446
|
+
declare function heads<T>(...iters: Iterable<T>[]): IterableIterator<T[]>;
|
|
385
447
|
/**
|
|
386
448
|
* Non-lazy version of itake().
|
|
387
449
|
*/
|
|
@@ -418,53 +480,4 @@ declare function dupes<T>(iterable: Iterable<T>, keyFn?: (item: T) => Primitive)
|
|
|
418
480
|
*/
|
|
419
481
|
declare function uniqueJustseen<T>(iterable: Iterable<T>, keyFn?: (item: T) => Primitive): IterableIterator<T>;
|
|
420
482
|
|
|
421
|
-
|
|
422
|
-
* Returns an iterable, filtering out any "nullish" values from the iterable.
|
|
423
|
-
*
|
|
424
|
-
* >>> compact([1, 2, undefined, 3, null])
|
|
425
|
-
* [1, 2, 3]
|
|
426
|
-
*
|
|
427
|
-
* For an eager version, @see compact().
|
|
428
|
-
*/
|
|
429
|
-
declare function icompact<T>(iterable: Iterable<T | null | undefined>): IterableIterator<T>;
|
|
430
|
-
/**
|
|
431
|
-
* Returns an array, filtering out any "nullish" values from the iterable.
|
|
432
|
-
*
|
|
433
|
-
* >>> compact([1, 2, undefined, 3, null])
|
|
434
|
-
* [1, 2, 3]
|
|
435
|
-
*
|
|
436
|
-
* For a lazy version, @see icompact().
|
|
437
|
-
*/
|
|
438
|
-
declare function compact<T>(iterable: Iterable<T | null | undefined>): T[];
|
|
439
|
-
/**
|
|
440
|
-
* Removes all "nullish" values from the given object. Returns a new object.
|
|
441
|
-
*
|
|
442
|
-
* >>> compactObject({ a: 1, b: undefined, c: 0, d: null })
|
|
443
|
-
* { a: 1, c: 0 }
|
|
444
|
-
*
|
|
445
|
-
*/
|
|
446
|
-
declare function compactObject<K extends string, V>(obj: Record<K, V | null | undefined>): Record<K, V>;
|
|
447
|
-
/**
|
|
448
|
-
* Almost an alias of find(). There only is a difference if no key fn is
|
|
449
|
-
* provided. In that case, `find()` will return the first item in the iterable,
|
|
450
|
-
* whereas `first()` will return the first non-`undefined` value in the
|
|
451
|
-
* iterable.
|
|
452
|
-
*/
|
|
453
|
-
declare function first<T>(iterable: Iterable<T>, keyFn?: Predicate<T>): T | undefined;
|
|
454
|
-
/**
|
|
455
|
-
* Returns 0 or more values for every value in the given iterable.
|
|
456
|
-
* Technically, it's just calling map(), followed by flatten(), but it's a very
|
|
457
|
-
* useful operation if you want to map over a structure, but not have a 1:1
|
|
458
|
-
* input-output mapping. Instead, if you want to potentially return 0 or more
|
|
459
|
-
* values per input element, use flatmap():
|
|
460
|
-
*
|
|
461
|
-
* For example, to return all numbers `n` in the input iterable `n` times:
|
|
462
|
-
*
|
|
463
|
-
* >>> const repeatN = n => repeat(n, n);
|
|
464
|
-
* >>> [...flatmap([0, 1, 2, 3, 4], repeatN)]
|
|
465
|
-
* [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] // note: no 0
|
|
466
|
-
*
|
|
467
|
-
*/
|
|
468
|
-
declare function flatmap<T, S>(iterable: Iterable<T>, mapper: (item: T) => Iterable<S>): IterableIterator<S>;
|
|
469
|
-
|
|
470
|
-
export { type Predicate, type Primitive, all, any, chain, chunked, compact, compactObject, compress, contains, count, cycle, dropwhile, dupes, enumerate, every, filter, find, first, flatmap, flatten, groupby, heads, icompact, icompress, ifilter, imap, islice, itake, iter, izip, izip2, izip3, izipLongest, izipMany, map, max, min, pairwise, partition, permutations, range, reduce, repeat, roundrobin, some, sorted, sum, take, takewhile, uniqueEverseen, uniqueJustseen, zip, zip3, zipLongest, zipMany };
|
|
483
|
+
export { type Predicate, type Primitive, all, any, chain, chunked, compact, compactObject, compress, contains, count, cycle, dropwhile, dupes, enumerate, every, filter, find, first, flatmap, flatten, groupby, heads, icompact, icompress, ifilter, imap, intersperse, islice, itake, iter, izip, izip2, izip3, izipLongest, izipLongest3, izipMany, map, max, min, pairwise, partition, permutations, range, reduce, repeat, roundrobin, some, sorted, sum, take, takewhile, uniqueEverseen, uniqueJustseen, zip, zip3, zipLongest, zipLongest3, zipMany };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
type Predicate<T> = (value: T) => boolean;
|
|
1
|
+
type Predicate<T> = (value: T, index: number) => boolean;
|
|
2
2
|
type Primitive = string | number | boolean;
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -6,7 +6,7 @@ type Primitive = string | number | boolean;
|
|
|
6
6
|
* any. If no predicate is given, it will return the first value returned by
|
|
7
7
|
* the iterable.
|
|
8
8
|
*/
|
|
9
|
-
declare function find<T>(iterable: Iterable<T>,
|
|
9
|
+
declare function find<T>(iterable: Iterable<T>, predicate?: Predicate<T>): T | undefined;
|
|
10
10
|
/**
|
|
11
11
|
* Returns true when all of the items in iterable are truthy. An optional key
|
|
12
12
|
* function can be used to define what truthiness means for this specific
|
|
@@ -25,7 +25,7 @@ declare function find<T>(iterable: Iterable<T>, keyFn?: Predicate<T>): T | undef
|
|
|
25
25
|
* all([2, 4, 5], n => n % 2 === 0) // => false
|
|
26
26
|
*
|
|
27
27
|
*/
|
|
28
|
-
declare function every<T>(iterable: Iterable<T>,
|
|
28
|
+
declare function every<T>(iterable: Iterable<T>, predicate?: Predicate<T>): boolean;
|
|
29
29
|
/**
|
|
30
30
|
* Returns true when some of the items in iterable are truthy. An optional key
|
|
31
31
|
* function can be used to define what truthiness means for this specific
|
|
@@ -43,7 +43,7 @@ declare function every<T>(iterable: Iterable<T>, keyFn?: Predicate<T>): boolean;
|
|
|
43
43
|
* some([{name: 'Bob'}, {name: 'Alice'}], person => person.name.startsWith('C')) // => false
|
|
44
44
|
*
|
|
45
45
|
*/
|
|
46
|
-
declare function some<T>(iterable: Iterable<T>,
|
|
46
|
+
declare function some<T>(iterable: Iterable<T>, predicate?: Predicate<T>): boolean;
|
|
47
47
|
/**
|
|
48
48
|
* Alias of `every()`.
|
|
49
49
|
*/
|
|
@@ -81,7 +81,7 @@ declare function enumerate<T>(iterable: Iterable<T>, start?: number): IterableIt
|
|
|
81
81
|
/**
|
|
82
82
|
* Non-lazy version of ifilter().
|
|
83
83
|
*/
|
|
84
|
-
declare function filter<T, N extends T>(iterable: Iterable<T>, predicate: (item: T) => item is N): N[];
|
|
84
|
+
declare function filter<T, N extends T>(iterable: Iterable<T>, predicate: (item: T, index: number) => item is N): N[];
|
|
85
85
|
declare function filter<T>(iterable: Iterable<T>, predicate: Predicate<T>): T[];
|
|
86
86
|
/**
|
|
87
87
|
* Returns an iterator object for the given iterable. This can be used to
|
|
@@ -189,11 +189,60 @@ declare function sum(iterable: Iterable<number>): number;
|
|
|
189
189
|
/**
|
|
190
190
|
* See izip.
|
|
191
191
|
*/
|
|
192
|
-
declare function zip<T1, T2>(xs: Iterable<T1>, ys: Iterable<T2>):
|
|
192
|
+
declare function zip<T1, T2>(xs: Iterable<T1>, ys: Iterable<T2>): [T1, T2][];
|
|
193
193
|
/**
|
|
194
194
|
* See izip3.
|
|
195
195
|
*/
|
|
196
|
-
declare function zip3<T1, T2, T3>(xs: Iterable<T1>, ys: Iterable<T2>, zs: Iterable<T3>):
|
|
196
|
+
declare function zip3<T1, T2, T3>(xs: Iterable<T1>, ys: Iterable<T2>, zs: Iterable<T3>): [T1, T2, T3][];
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Returns an iterable, filtering out any "nullish" values from the iterable.
|
|
200
|
+
*
|
|
201
|
+
* >>> compact([1, 2, undefined, 3, null])
|
|
202
|
+
* [1, 2, 3]
|
|
203
|
+
*
|
|
204
|
+
* For an eager version, @see compact().
|
|
205
|
+
*/
|
|
206
|
+
declare function icompact<T>(iterable: Iterable<T | null | undefined>): IterableIterator<T>;
|
|
207
|
+
/**
|
|
208
|
+
* Returns an array, filtering out any "nullish" values from the iterable.
|
|
209
|
+
*
|
|
210
|
+
* >>> compact([1, 2, undefined, 3, null])
|
|
211
|
+
* [1, 2, 3]
|
|
212
|
+
*
|
|
213
|
+
* For a lazy version, @see icompact().
|
|
214
|
+
*/
|
|
215
|
+
declare function compact<T>(iterable: Iterable<T | null | undefined>): T[];
|
|
216
|
+
/**
|
|
217
|
+
* Removes all "nullish" values from the given object. Returns a new object.
|
|
218
|
+
*
|
|
219
|
+
* >>> compactObject({ a: 1, b: undefined, c: 0, d: null })
|
|
220
|
+
* { a: 1, c: 0 }
|
|
221
|
+
*
|
|
222
|
+
*/
|
|
223
|
+
declare function compactObject<K extends string, V>(obj: Record<K, V | null | undefined>): Record<K, V>;
|
|
224
|
+
/**
|
|
225
|
+
* Almost an alias of find(). There only is a difference if no key fn is
|
|
226
|
+
* provided. In that case, `find()` will return the first item in the iterable,
|
|
227
|
+
* whereas `first()` will return the first non-`undefined` value in the
|
|
228
|
+
* iterable.
|
|
229
|
+
*/
|
|
230
|
+
declare function first<T>(iterable: Iterable<T>, keyFn?: Predicate<T>): T | undefined;
|
|
231
|
+
/**
|
|
232
|
+
* Returns 0 or more values for every value in the given iterable.
|
|
233
|
+
* Technically, it's just calling map(), followed by flatten(), but it's a very
|
|
234
|
+
* useful operation if you want to map over a structure, but not have a 1:1
|
|
235
|
+
* input-output mapping. Instead, if you want to potentially return 0 or more
|
|
236
|
+
* values per input element, use flatmap():
|
|
237
|
+
*
|
|
238
|
+
* For example, to return all numbers `n` in the input iterable `n` times:
|
|
239
|
+
*
|
|
240
|
+
* >>> const repeatN = n => repeat(n, n);
|
|
241
|
+
* >>> [...flatmap([0, 1, 2, 3, 4], repeatN)]
|
|
242
|
+
* [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] // note: no 0
|
|
243
|
+
*
|
|
244
|
+
*/
|
|
245
|
+
declare function flatmap<T, S>(iterable: Iterable<T>, mapper: (item: T) => Iterable<S>): IterableIterator<S>;
|
|
197
246
|
|
|
198
247
|
/**
|
|
199
248
|
* Returns an iterator that returns elements from the first iterable until it
|
|
@@ -272,6 +321,10 @@ declare const izip2: typeof izip;
|
|
|
272
321
|
* fillvalue. Iteration continues until the longest iterable is exhausted.
|
|
273
322
|
*/
|
|
274
323
|
declare function izipLongest2<T1, T2, D>(xs: Iterable<T1>, ys: Iterable<T2>, filler?: D): IterableIterator<[T1 | D, T2 | D]>;
|
|
324
|
+
/**
|
|
325
|
+
* See izipLongest2, but for three.
|
|
326
|
+
*/
|
|
327
|
+
declare function izipLongest3<T1, T2, T3, D = undefined>(xs: Iterable<T1>, ys: Iterable<T2>, zs: Iterable<T3>, filler?: D): IterableIterator<[T1 | D, T2 | D, T3 | D]>;
|
|
275
328
|
/**
|
|
276
329
|
* Like the other izips (`izip`, `izip3`, etc), but generalized to take an
|
|
277
330
|
* unlimited amount of input iterables. Think `izip(*iterables)` in Python.
|
|
@@ -305,7 +358,8 @@ declare function repeat<T>(thing: T, times?: number): IterableIterator<T>;
|
|
|
305
358
|
* predicate is true.
|
|
306
359
|
*/
|
|
307
360
|
declare function takewhile<T>(iterable: Iterable<T>, predicate: Predicate<T>): IterableIterator<T>;
|
|
308
|
-
declare function zipLongest2<T1, T2, D>(xs: Iterable<T1>, ys: Iterable<T2>, filler?: D):
|
|
361
|
+
declare function zipLongest2<T1, T2, D>(xs: Iterable<T1>, ys: Iterable<T2>, filler?: D): [T1 | D, T2 | D][];
|
|
362
|
+
declare function zipLongest3<T1, T2, T3, D>(xs: Iterable<T1>, ys: Iterable<T2>, zs: Iterable<T3>, filler?: D): [T1 | D, T2 | D, T3 | D][];
|
|
309
363
|
declare const izipLongest: typeof izipLongest2;
|
|
310
364
|
declare const zipLongest: typeof zipLongest2;
|
|
311
365
|
declare function zipMany<T>(...iters: Iterable<T>[]): T[][];
|
|
@@ -331,6 +385,14 @@ declare function chunked<T>(iterable: Iterable<T>, size: number): IterableIterat
|
|
|
331
385
|
*
|
|
332
386
|
*/
|
|
333
387
|
declare function flatten<T>(iterableOfIterables: Iterable<Iterable<T>>): IterableIterator<T>;
|
|
388
|
+
/**
|
|
389
|
+
* Intersperse filler element `value` among the items in `iterable`.
|
|
390
|
+
*
|
|
391
|
+
* >>> [...intersperse(-1, range(1, 5))]
|
|
392
|
+
* [1, -1, 2, -1, 3, -1, 4]
|
|
393
|
+
*
|
|
394
|
+
*/
|
|
395
|
+
declare function intersperse<T, V>(value: V, iterable: Iterable<T>): IterableIterator<T | V>;
|
|
334
396
|
/**
|
|
335
397
|
* Returns an iterable containing only the first `n` elements of the given
|
|
336
398
|
* iterable.
|
|
@@ -360,7 +422,7 @@ declare function pairwise<T>(iterable: Iterable<T>): IterableIterator<[T, T]>;
|
|
|
360
422
|
* [0, 2, 4, 6, 8]
|
|
361
423
|
*
|
|
362
424
|
*/
|
|
363
|
-
declare function partition<T, N extends T>(iterable: Iterable<T>, predicate: (item: T) => item is N): [N[], Exclude<T, N>[]];
|
|
425
|
+
declare function partition<T, N extends T>(iterable: Iterable<T>, predicate: (item: T, index: number) => item is N): [N[], Exclude<T, N>[]];
|
|
364
426
|
declare function partition<T>(iterable: Iterable<T>, predicate: Predicate<T>): [T[], T[]];
|
|
365
427
|
/**
|
|
366
428
|
* Yields the next item from each iterable in turn, alternating between them.
|
|
@@ -381,7 +443,7 @@ declare function roundrobin<T>(...iters: Iterable<T>[]): IterableIterator<T>;
|
|
|
381
443
|
* This is also different from `zipLongest()`, since the number of items in
|
|
382
444
|
* each round can decrease over time, rather than being filled with a filler.
|
|
383
445
|
*/
|
|
384
|
-
declare function heads<T>(...iters:
|
|
446
|
+
declare function heads<T>(...iters: Iterable<T>[]): IterableIterator<T[]>;
|
|
385
447
|
/**
|
|
386
448
|
* Non-lazy version of itake().
|
|
387
449
|
*/
|
|
@@ -418,53 +480,4 @@ declare function dupes<T>(iterable: Iterable<T>, keyFn?: (item: T) => Primitive)
|
|
|
418
480
|
*/
|
|
419
481
|
declare function uniqueJustseen<T>(iterable: Iterable<T>, keyFn?: (item: T) => Primitive): IterableIterator<T>;
|
|
420
482
|
|
|
421
|
-
|
|
422
|
-
* Returns an iterable, filtering out any "nullish" values from the iterable.
|
|
423
|
-
*
|
|
424
|
-
* >>> compact([1, 2, undefined, 3, null])
|
|
425
|
-
* [1, 2, 3]
|
|
426
|
-
*
|
|
427
|
-
* For an eager version, @see compact().
|
|
428
|
-
*/
|
|
429
|
-
declare function icompact<T>(iterable: Iterable<T | null | undefined>): IterableIterator<T>;
|
|
430
|
-
/**
|
|
431
|
-
* Returns an array, filtering out any "nullish" values from the iterable.
|
|
432
|
-
*
|
|
433
|
-
* >>> compact([1, 2, undefined, 3, null])
|
|
434
|
-
* [1, 2, 3]
|
|
435
|
-
*
|
|
436
|
-
* For a lazy version, @see icompact().
|
|
437
|
-
*/
|
|
438
|
-
declare function compact<T>(iterable: Iterable<T | null | undefined>): T[];
|
|
439
|
-
/**
|
|
440
|
-
* Removes all "nullish" values from the given object. Returns a new object.
|
|
441
|
-
*
|
|
442
|
-
* >>> compactObject({ a: 1, b: undefined, c: 0, d: null })
|
|
443
|
-
* { a: 1, c: 0 }
|
|
444
|
-
*
|
|
445
|
-
*/
|
|
446
|
-
declare function compactObject<K extends string, V>(obj: Record<K, V | null | undefined>): Record<K, V>;
|
|
447
|
-
/**
|
|
448
|
-
* Almost an alias of find(). There only is a difference if no key fn is
|
|
449
|
-
* provided. In that case, `find()` will return the first item in the iterable,
|
|
450
|
-
* whereas `first()` will return the first non-`undefined` value in the
|
|
451
|
-
* iterable.
|
|
452
|
-
*/
|
|
453
|
-
declare function first<T>(iterable: Iterable<T>, keyFn?: Predicate<T>): T | undefined;
|
|
454
|
-
/**
|
|
455
|
-
* Returns 0 or more values for every value in the given iterable.
|
|
456
|
-
* Technically, it's just calling map(), followed by flatten(), but it's a very
|
|
457
|
-
* useful operation if you want to map over a structure, but not have a 1:1
|
|
458
|
-
* input-output mapping. Instead, if you want to potentially return 0 or more
|
|
459
|
-
* values per input element, use flatmap():
|
|
460
|
-
*
|
|
461
|
-
* For example, to return all numbers `n` in the input iterable `n` times:
|
|
462
|
-
*
|
|
463
|
-
* >>> const repeatN = n => repeat(n, n);
|
|
464
|
-
* >>> [...flatmap([0, 1, 2, 3, 4], repeatN)]
|
|
465
|
-
* [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] // note: no 0
|
|
466
|
-
*
|
|
467
|
-
*/
|
|
468
|
-
declare function flatmap<T, S>(iterable: Iterable<T>, mapper: (item: T) => Iterable<S>): IterableIterator<S>;
|
|
469
|
-
|
|
470
|
-
export { type Predicate, type Primitive, all, any, chain, chunked, compact, compactObject, compress, contains, count, cycle, dropwhile, dupes, enumerate, every, filter, find, first, flatmap, flatten, groupby, heads, icompact, icompress, ifilter, imap, islice, itake, iter, izip, izip2, izip3, izipLongest, izipMany, map, max, min, pairwise, partition, permutations, range, reduce, repeat, roundrobin, some, sorted, sum, take, takewhile, uniqueEverseen, uniqueJustseen, zip, zip3, zipLongest, zipMany };
|
|
483
|
+
export { type Predicate, type Primitive, all, any, chain, chunked, compact, compactObject, compress, contains, count, cycle, dropwhile, dupes, enumerate, every, filter, find, first, flatmap, flatten, groupby, heads, icompact, icompress, ifilter, imap, intersperse, islice, itake, iter, izip, izip2, izip3, izipLongest, izipLongest3, izipMany, map, max, min, pairwise, partition, permutations, range, reduce, repeat, roundrobin, some, sorted, sum, take, takewhile, uniqueEverseen, uniqueJustseen, zip, zip3, zipLongest, zipLongest3, zipMany };
|
package/dist/index.js
CHANGED
|
@@ -53,6 +53,11 @@ function* flatten(iterableOfIterables) {
|
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
|
+
function intersperse(value, iterable) {
|
|
57
|
+
const stream = flatten(izip(repeat(value), iterable));
|
|
58
|
+
stream.next();
|
|
59
|
+
return stream;
|
|
60
|
+
}
|
|
56
61
|
function* itake(n, iterable) {
|
|
57
62
|
const it = iter(iterable);
|
|
58
63
|
let count2 = n;
|
|
@@ -80,8 +85,9 @@ function* pairwise(iterable) {
|
|
|
80
85
|
function partition(iterable, predicate) {
|
|
81
86
|
const good = [];
|
|
82
87
|
const bad = [];
|
|
88
|
+
let index = 0;
|
|
83
89
|
for (const item of iterable) {
|
|
84
|
-
if (predicate(item)) {
|
|
90
|
+
if (predicate(item, index++)) {
|
|
85
91
|
good.push(item);
|
|
86
92
|
} else {
|
|
87
93
|
bad.push(item);
|
|
@@ -195,11 +201,12 @@ function* cycle(iterable) {
|
|
|
195
201
|
}
|
|
196
202
|
}
|
|
197
203
|
function* dropwhile(iterable, predicate) {
|
|
204
|
+
let index = 0;
|
|
198
205
|
const it = iter(iterable);
|
|
199
206
|
let res;
|
|
200
207
|
while (!(res = it.next()).done) {
|
|
201
208
|
const value = res.value;
|
|
202
|
-
if (!predicate(value)) {
|
|
209
|
+
if (!predicate(value, index++)) {
|
|
203
210
|
yield value;
|
|
204
211
|
break;
|
|
205
212
|
}
|
|
@@ -217,8 +224,7 @@ function* groupby(iterable, keyFn = primitiveIdentity) {
|
|
|
217
224
|
while (currentKey === tgtKey) {
|
|
218
225
|
yield currentValue;
|
|
219
226
|
const nextVal = it.next();
|
|
220
|
-
if (nextVal.done)
|
|
221
|
-
return;
|
|
227
|
+
if (nextVal.done) return;
|
|
222
228
|
currentValue = nextVal.value;
|
|
223
229
|
currentKey = keyFn(currentValue);
|
|
224
230
|
}
|
|
@@ -245,8 +251,9 @@ function* icompress(data, selectors) {
|
|
|
245
251
|
}
|
|
246
252
|
}
|
|
247
253
|
function* ifilter(iterable, predicate) {
|
|
254
|
+
let index = 0;
|
|
248
255
|
for (const value of iterable) {
|
|
249
|
-
if (predicate(value)) {
|
|
256
|
+
if (predicate(value, index++)) {
|
|
250
257
|
yield value;
|
|
251
258
|
}
|
|
252
259
|
}
|
|
@@ -265,24 +272,18 @@ function* islice(iterable, stopOrStart, possiblyStop, step = 1) {
|
|
|
265
272
|
start = 0;
|
|
266
273
|
stop = stopOrStart;
|
|
267
274
|
}
|
|
268
|
-
if (start < 0)
|
|
269
|
-
|
|
270
|
-
if (
|
|
271
|
-
throw new Error("stop cannot be negative");
|
|
272
|
-
if (step <= 0)
|
|
273
|
-
throw new Error("step cannot be negative");
|
|
275
|
+
if (start < 0) throw new Error("start cannot be negative");
|
|
276
|
+
if (stop !== null && stop < 0) throw new Error("stop cannot be negative");
|
|
277
|
+
if (step <= 0) throw new Error("step cannot be negative");
|
|
274
278
|
let i = -1;
|
|
275
279
|
const it = iter(iterable);
|
|
276
280
|
let res;
|
|
277
281
|
while (true) {
|
|
278
282
|
i++;
|
|
279
|
-
if (stop !== null && i >= stop)
|
|
280
|
-
return;
|
|
283
|
+
if (stop !== null && i >= stop) return;
|
|
281
284
|
res = it.next();
|
|
282
|
-
if (res.done)
|
|
283
|
-
|
|
284
|
-
if (i < start)
|
|
285
|
-
continue;
|
|
285
|
+
if (res.done) return;
|
|
286
|
+
if (i < start) continue;
|
|
286
287
|
if ((i - start) % step === 0) {
|
|
287
288
|
yield res.value;
|
|
288
289
|
}
|
|
@@ -331,6 +332,22 @@ function* izipLongest2(xs, ys, filler) {
|
|
|
331
332
|
}
|
|
332
333
|
}
|
|
333
334
|
}
|
|
335
|
+
function* izipLongest3(xs, ys, zs, filler) {
|
|
336
|
+
const filler_ = filler;
|
|
337
|
+
const ixs = iter(xs);
|
|
338
|
+
const iys = iter(ys);
|
|
339
|
+
const izs = iter(zs);
|
|
340
|
+
for (; ; ) {
|
|
341
|
+
const x = ixs.next();
|
|
342
|
+
const y = iys.next();
|
|
343
|
+
const z = izs.next();
|
|
344
|
+
if (x.done && y.done && z.done) {
|
|
345
|
+
return;
|
|
346
|
+
} else {
|
|
347
|
+
yield [!x.done ? x.value : filler_, !y.done ? y.value : filler_, !z.done ? z.value : filler_];
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
334
351
|
function* izipMany(...iters) {
|
|
335
352
|
const iterables = iters.map(iter);
|
|
336
353
|
for (; ; ) {
|
|
@@ -345,7 +362,7 @@ function* izipMany(...iters) {
|
|
|
345
362
|
function* permutations(iterable, r) {
|
|
346
363
|
const pool = Array.from(iterable);
|
|
347
364
|
const n = pool.length;
|
|
348
|
-
const x = r
|
|
365
|
+
const x = r != null ? r : n;
|
|
349
366
|
if (x > n) {
|
|
350
367
|
return;
|
|
351
368
|
}
|
|
@@ -387,18 +404,21 @@ function* repeat(thing, times) {
|
|
|
387
404
|
}
|
|
388
405
|
}
|
|
389
406
|
function* takewhile(iterable, predicate) {
|
|
407
|
+
let index = 0;
|
|
390
408
|
const it = iter(iterable);
|
|
391
409
|
let res;
|
|
392
410
|
while (!(res = it.next()).done) {
|
|
393
411
|
const value = res.value;
|
|
394
|
-
if (!predicate(value))
|
|
395
|
-
return;
|
|
412
|
+
if (!predicate(value, index++)) return;
|
|
396
413
|
yield value;
|
|
397
414
|
}
|
|
398
415
|
}
|
|
399
416
|
function zipLongest2(xs, ys, filler) {
|
|
400
417
|
return Array.from(izipLongest2(xs, ys, filler));
|
|
401
418
|
}
|
|
419
|
+
function zipLongest3(xs, ys, zs, filler) {
|
|
420
|
+
return Array.from(izipLongest3(xs, ys, zs, filler));
|
|
421
|
+
}
|
|
402
422
|
var izipLongest = izipLongest2;
|
|
403
423
|
var zipLongest = zipLongest2;
|
|
404
424
|
function zipMany(...iters) {
|
|
@@ -406,33 +426,36 @@ function zipMany(...iters) {
|
|
|
406
426
|
}
|
|
407
427
|
|
|
408
428
|
// src/builtins.ts
|
|
409
|
-
function find(iterable,
|
|
429
|
+
function find(iterable, predicate) {
|
|
410
430
|
const it = iter(iterable);
|
|
411
|
-
if (
|
|
431
|
+
if (predicate === void 0) {
|
|
412
432
|
const value = it.next();
|
|
413
|
-
return value.done ?
|
|
433
|
+
return value.done ? void 0 : value.value;
|
|
414
434
|
} else {
|
|
415
435
|
let res;
|
|
436
|
+
let i = 0;
|
|
416
437
|
while (!(res = it.next()).done) {
|
|
417
438
|
const value = res.value;
|
|
418
|
-
if (
|
|
439
|
+
if (predicate(value, i++)) {
|
|
419
440
|
return value;
|
|
420
441
|
}
|
|
421
442
|
}
|
|
422
443
|
return void 0;
|
|
423
444
|
}
|
|
424
445
|
}
|
|
425
|
-
function every(iterable,
|
|
446
|
+
function every(iterable, predicate = identityPredicate) {
|
|
447
|
+
let index = 0;
|
|
426
448
|
for (const item of iterable) {
|
|
427
|
-
if (!
|
|
449
|
+
if (!predicate(item, index++)) {
|
|
428
450
|
return false;
|
|
429
451
|
}
|
|
430
452
|
}
|
|
431
453
|
return true;
|
|
432
454
|
}
|
|
433
|
-
function some(iterable,
|
|
455
|
+
function some(iterable, predicate = identityPredicate) {
|
|
456
|
+
let index = 0;
|
|
434
457
|
for (const item of iterable) {
|
|
435
|
-
if (
|
|
458
|
+
if (predicate(item, index++)) {
|
|
436
459
|
return true;
|
|
437
460
|
}
|
|
438
461
|
}
|
|
@@ -573,6 +596,7 @@ export {
|
|
|
573
596
|
icompress,
|
|
574
597
|
ifilter,
|
|
575
598
|
imap,
|
|
599
|
+
intersperse,
|
|
576
600
|
islice,
|
|
577
601
|
itake,
|
|
578
602
|
iter,
|
|
@@ -580,6 +604,7 @@ export {
|
|
|
580
604
|
izip2,
|
|
581
605
|
izip3,
|
|
582
606
|
izipLongest,
|
|
607
|
+
izipLongest3,
|
|
583
608
|
izipMany,
|
|
584
609
|
map,
|
|
585
610
|
max,
|
|
@@ -601,6 +626,7 @@ export {
|
|
|
601
626
|
zip,
|
|
602
627
|
zip3,
|
|
603
628
|
zipLongest,
|
|
629
|
+
zipLongest3,
|
|
604
630
|
zipMany
|
|
605
631
|
};
|
|
606
632
|
// istanbul ignore else -- @preserve
|