Skip to main content

Iterator helpers

About 2 minfeaturees-proposal

Iterator helpersopen in new window

Note

This is an ECMAScript proposal, please do not confuse it with the helper function provided by Core-JS

Modules

Types

interface Iterator<T> {
  drop(limit: number): Iterator<T>;
  every(callbackfn: (value: T) => boolean): boolean;
  filter(callbackfn: (value: T) => boolean): Iterator<T>;
  find(callbackfn: (value: T) => boolean): T;
  flatMap<U>(
    callbackfn: (value: T, couner: number) => Iterable<U> | Iterator<U>
  ): Iterator<U>;
  forEach(callbackfn: (value: T) => void): void;
  indexed(): Iterator<[number, T]>;
  map<U>(callbackfn: (value: T) => U): Iterator<U>;
  reduce<U>(callbackfn: (memo: U, value: T) => U, initialValue: U): U;
  some(callbackfn: (value: T) => boolean): boolean;
  take(limit: number): Iterator<T>;
  toArray(): Array<T>;
  [Symbol.toStringTag]: "Iterator";
}
interface IteratorConstructor {
  from<T>(iterable: Iterable<T> | Iterator<T>): Iterator<T>;
}

Entry points

core-js/proposals/iterator-helpers
core-js(-pure)/full/async-iterator
core-js(-pure)/full/async-iterator/drop
core-js(-pure)/full/async-iterator/every
core-js(-pure)/full/async-iterator/filter
core-js(-pure)/full/async-iterator/find
core-js(-pure)/full/async-iterator/flat-map
core-js(-pure)/full/async-iterator/for-each
core-js(-pure)/full/async-iterator/from
core-js(-pure)/full/async-iterator/indexed
core-js(-pure)/full/async-iterator/map
core-js(-pure)/full/async-iterator/reduce
core-js(-pure)/full/async-iterator/some
core-js(-pure)/full/async-iterator/take
core-js(-pure)/full/async-iterator/to-array
core-js(-pure)/full/iterator
core-js(-pure)/full/iterator/drop
core-js(-pure)/full/iterator/every
core-js(-pure)/full/iterator/filter
core-js(-pure)/full/iterator/find
core-js(-pure)/full/iterator/flat-map
core-js(-pure)/full/iterator/for-each
core-js(-pure)/full/iterator/from
core-js(-pure)/full/iterator/indexed
core-js(-pure)/full/iterator/map
core-js(-pure)/full/iterator/reduce
core-js(-pure)/full/iterator/some
core-js(-pure)/full/iterator/take
core-js(-pure)/full/iterator/to-array

Example

Exampleopen in new window:

[1, 2, 3, 4, 5, 6, 7]
  .values()
  .drop(1)
  .take(5)
  .filter((it) => it % 2)
  .map((it) => it ** 2)
  .toArray(); // => [9, 25]

Iterator.from({
  next: () => ({ done: Math.random() > 0.9, value: (Math.random() * 10) | 0 }),
}).toArray(); // => [7, 6, 3, 0, 2, 8]

await AsyncIterator.from([1, 2, 3, 4, 5, 6, 7])
  .drop(1)
  .take(5)
  .filter((it) => it % 2)
  .map((it) => it ** 2)
  .toArray(); // => [9, 25]

await [1, 2, 3]
  .values()
  .toAsync()
  .map(async (it) => it ** 2)
  .toArray(); // => [1, 4, 9]

Caveats

  • For preventing prototypes pollution, in the pure version, new %IteratorPrototype% methods are not added to the real %IteratorPrototype%, they available only on wrappers - instead of [].values().map(fn) use Iterator.from([]).map(fn).