extra-iterator 0.2.1 → 0.3.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/dist/index.d.ts CHANGED
@@ -2,13 +2,20 @@ interface ArrayIsh<T> {
2
2
  [index: number]: T;
3
3
  length: number;
4
4
  }
5
+ export type ExtraIteratorSource<T> = Iterator<T, any, any> | Iterable<T, any, any> | ArrayIsh<T>;
5
6
  export declare class ExtraIterator<T> extends Iterator<T, any, any> {
6
- static from<T>(source: Iterator<T, any, any> | Iterable<T, any, any> | ArrayIsh<T>): ExtraIterator<T>;
7
- static fromKeys<T extends {}>(subject: T): ExtraIterator<keyof T>;
8
- static fromValues<T extends {}>(subject: T): ExtraIterator<T[keyof T]>;
9
- static fromEntries<T extends {}>(subject: T): ExtraIterator<[keyof T, T[keyof T]]>;
7
+ static from<T>(source: ExtraIteratorSource<T>): ExtraIterator<T>;
8
+ static zip<A, B>(a: ExtraIteratorSource<A>, b: ExtraIteratorSource<B>): ExtraIterator<[A, B]>;
9
+ static zip<A, B, C>(a: ExtraIteratorSource<A>, b: ExtraIteratorSource<B>, c: ExtraIteratorSource<C>): ExtraIterator<[A, B, C]>;
10
+ static zip<A, B, C, D>(a: ExtraIteratorSource<A>, b: ExtraIteratorSource<B>, c: ExtraIteratorSource<C>, d: ExtraIteratorSource<D>): ExtraIterator<[A, B, C, D]>;
11
+ static zip<A, B, C, D, E>(a: ExtraIteratorSource<A>, b: ExtraIteratorSource<B>, c: ExtraIteratorSource<C>, d: ExtraIteratorSource<D>, e: ExtraIteratorSource<E>): ExtraIterator<[A, B, C, D, E]>;
12
+ static zip<A, B, C, D, E, F>(a: ExtraIteratorSource<A>, b: ExtraIteratorSource<B>, c: ExtraIteratorSource<C>, d: ExtraIteratorSource<D>, e: ExtraIteratorSource<E>, f: ExtraIteratorSource<F>): ExtraIterator<[A, B, C, D, E, F]>;
13
+ static zip<A, B, C, D, E, F, G>(a: ExtraIteratorSource<A>, b: ExtraIteratorSource<B>, c: ExtraIteratorSource<C>, d: ExtraIteratorSource<D>, e: ExtraIteratorSource<E>, f: ExtraIteratorSource<F>, g: ExtraIteratorSource<G>): ExtraIterator<[A, B, C, D, E, F, G]>;
14
+ static zip<A, B, C, D, E, F, G, H>(a: ExtraIteratorSource<A>, b: ExtraIteratorSource<B>, c: ExtraIteratorSource<C>, d: ExtraIteratorSource<D>, e: ExtraIteratorSource<E>, f: ExtraIteratorSource<F>, g: ExtraIteratorSource<G>, h: ExtraIteratorSource<H>): ExtraIterator<[A, B, C, D, E, F, G, H]>;
15
+ static zip<T>(...iterables: ExtraIteratorSource<T>[]): ExtraIterator<T[]>;
10
16
  static empty<T = any>(): ExtraIterator<T>;
11
- static count(max: number): ExtraIterator<number>;
17
+ static count(): ExtraIterator<number>;
18
+ static count(end: number): ExtraIterator<number>;
12
19
  static count(start: number, end: number): ExtraIterator<number>;
13
20
  static count(start: number, end: number, interval: number): ExtraIterator<number>;
14
21
  static repeat<T>(count: number, value: T): ExtraIterator<T>;
@@ -30,26 +37,20 @@ export declare class ExtraIterator<T> extends Iterator<T, any, any> {
30
37
  first(): T | undefined;
31
38
  last(): T | undefined;
32
39
  at(index: number): T | undefined;
33
- appendMany(items: Iterable<T>): ExtraIterator<T>;
34
- appendOne(item: T): ExtraIterator<T>;
40
+ concat(items: Iterable<T>): ExtraIterator<T>;
41
+ append(item: T): ExtraIterator<T>;
35
42
  prependMany(items: Iterable<T>): ExtraIterator<T>;
36
- prependOne(item: T): ExtraIterator<T>;
43
+ prepend(item: T): ExtraIterator<T>;
37
44
  takeWhile(predicate: (value: T, index: number) => boolean): ExtraIterator<T>;
38
45
  dropWhile(predicate: (value: T, index: number) => boolean): ExtraIterator<T>;
39
46
  chunk(size: number): ExtraIterator<T[]>;
40
- /**
41
- * Pairs with another iterable to form an iterator of pairs.
42
- * // TODO Expand to support more than 2 iterables
43
- */
44
47
  zip<U>(other: Iterable<U>): ExtraIterator<[T, U]>;
45
- /**
46
- * Transforms from an iterator of pairs into a pair of iterators.
47
- * // TODO Expand to support more than 2 iterables
48
- */
49
- unzip(): T extends [infer U, infer V] ? [ExtraIterator<U>, ExtraIterator<V>] : never;
48
+ interpose<U>(separator: U): ExtraIterator<T | U>;
49
+ interleave<U>(other: ExtraIteratorSource<U>): ExtraIterator<T | U>;
50
50
  splice(startIndex: number, deleteCount: number, ...newItems: T[]): ExtraIterator<T>;
51
- with(index: number, value: T): ExtraIterator<T>;
51
+ defaultIfEmpty(provider: () => T): ExtraIterator<T>;
52
52
  collect<U>(collectfn: ((iter: Iterable<T>) => U)): U;
53
53
  toSortedBy(...keys: (keyof T)[]): T[];
54
+ count(): number;
54
55
  }
55
56
  export {};
package/dist/index.js CHANGED
@@ -10,24 +10,21 @@ export class ExtraIterator extends Iterator {
10
10
  }
11
11
  return new ExtraIterator(source);
12
12
  }
13
- static fromKeys(subject) {
14
- return new ExtraIterator(Object.keys(subject));
15
- }
16
- static fromValues(subject) {
17
- return new ExtraIterator(Object.values(subject));
18
- }
19
- static fromEntries(subject) {
20
- return new ExtraIterator(Object.entries(subject));
13
+ static zip(...iterables) {
14
+ return new ExtraIterator(function* () {
15
+ for (let iterators = iterables.map(iterable => ExtraIterator.from(iterable)), results; results = iterators.map(iterator => iterator.next()),
16
+ results.every(value => !value.done);) {
17
+ yield results.map(value => value.value);
18
+ }
19
+ }().toArray());
21
20
  }
22
21
  static empty() {
23
22
  return new ExtraIterator([]);
24
23
  }
25
- static count(start, end, interval) {
26
- if (typeof end === 'undefined') {
27
- end = start;
28
- start = 0;
29
- }
30
- interval ??= 1;
24
+ static count(...args) {
25
+ const [start, end, interval] = args.length === 0 ? [0, Infinity, 1]
26
+ : args.length === 1 ? [0, args[0], 1]
27
+ : [args[0], args[1], args[2] ?? 1];
31
28
  return new ExtraIterator(function* () {
32
29
  for (let counter = start; counter < end; counter += interval) {
33
30
  yield counter;
@@ -140,13 +137,13 @@ export class ExtraIterator extends Iterator {
140
137
  : index < 0 ? this.take(index).at(0)
141
138
  : this.drop(index).first();
142
139
  }
143
- appendMany(items) {
140
+ concat(items) {
144
141
  return ExtraIterator.from(function* () {
145
142
  yield* this;
146
143
  yield* items;
147
144
  }.call(this));
148
145
  }
149
- appendOne(item) {
146
+ append(item) {
150
147
  return ExtraIterator.from(function* () {
151
148
  yield* this;
152
149
  yield item;
@@ -158,7 +155,7 @@ export class ExtraIterator extends Iterator {
158
155
  yield* this;
159
156
  }.call(this));
160
157
  }
161
- prependOne(item) {
158
+ prepend(item) {
162
159
  return ExtraIterator.from(function* () {
163
160
  yield item;
164
161
  yield* this;
@@ -192,10 +189,6 @@ export class ExtraIterator extends Iterator {
192
189
  }
193
190
  }.call(this));
194
191
  }
195
- /**
196
- * Pairs with another iterable to form an iterator of pairs.
197
- * // TODO Expand to support more than 2 iterables
198
- */
199
192
  zip(other) {
200
193
  return ExtraIterator.from(function* () {
201
194
  const otherIterator = Iterator.from(other);
@@ -204,15 +197,31 @@ export class ExtraIterator extends Iterator {
204
197
  }
205
198
  }.call(this));
206
199
  }
207
- /**
208
- * Transforms from an iterator of pairs into a pair of iterators.
209
- * // TODO Expand to support more than 2 iterables
210
- */
211
- unzip() {
212
- return [
213
- this.map(value => value[0]),
214
- this.map(value => value[1]),
215
- ];
200
+ interpose(separator) {
201
+ return ExtraIterator.from(function* () {
202
+ for (let next = this.next(); !next.done;) {
203
+ yield next.value;
204
+ next = this.next();
205
+ if (!next.done) {
206
+ yield separator;
207
+ }
208
+ }
209
+ }.call(this));
210
+ }
211
+ interleave(other) {
212
+ return ExtraIterator.from(function* () {
213
+ const otherIterator = ExtraIterator.from(other);
214
+ for (let next, otherNext; next = this.next(),
215
+ otherNext = otherIterator.next(),
216
+ !next.done || !otherNext.done;) {
217
+ if (!next.done) {
218
+ yield next.value;
219
+ }
220
+ if (!otherNext.done) {
221
+ yield otherNext.value;
222
+ }
223
+ }
224
+ }.call(this));
216
225
  }
217
226
  splice(startIndex, deleteCount, ...newItems) {
218
227
  if (startIndex < 0) {
@@ -230,8 +239,17 @@ export class ExtraIterator extends Iterator {
230
239
  }
231
240
  }.call(this));
232
241
  }
233
- with(index, value) {
234
- return this.splice(index, 1, value);
242
+ defaultIfEmpty(provider) {
243
+ return ExtraIterator.from(function* () {
244
+ const result = this.next();
245
+ if (result.done) {
246
+ yield provider();
247
+ }
248
+ else {
249
+ yield result.value;
250
+ yield* this;
251
+ }
252
+ }.call(this));
235
253
  }
236
254
  collect(collectfn) {
237
255
  return collectfn(this);
@@ -248,4 +266,11 @@ export class ExtraIterator extends Iterator {
248
266
  return 0;
249
267
  });
250
268
  }
269
+ count() {
270
+ let count = 0;
271
+ for (let next; next = this.next(), !next.done;) {
272
+ count++;
273
+ }
274
+ return count;
275
+ }
251
276
  }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "An extension of the Iterator class with additional utility helper functions.",
4
4
  "author": "Leonardo Raele <leonardoraele@gmail.com>",
5
5
  "license": "MIT",
6
- "version": "0.2.1",
6
+ "version": "0.3.0",
7
7
  "type": "module",
8
8
  "exports": {
9
9
  ".": "./dist/index.js",