Package detail

iterate-iterator

ljharb9.2mMIT1.0.2

Iterate any JS iterator. Works robustly in all environments, all versions.

iterate, iterator, iterable, es2015

readme

iterate-iterator Version Badge

github actions coverage dependency status dev dependency status License Downloads

npm badge

Iterate any iterable JS iterator. Works robustly in all environments, all versions.

In modern engines, [...value] or Array.from(value) or for (const item of value) { } are sufficient to iterate an iterable value (an object with a Symbol.iterator method), which includes all builtin iterators. However, older engines:

  • may lack Symbol, array spread, or for..of support altogether
  • may have Symbol.iterator but not implement it on everything it should, like arguments objects
  • may have Map and Set, but a non-standard name for the iterator-producing method (.iterator or ['@@iterator'], eg) and no syntax to support it
  • may be old versions of Firefox that produce values until they throw a StopIteration exception, rather than having iteration result objects
  • may be polyfilled/shimmed/shammed, with es6-shim or core-js or similar

This library simplifies iterating an iterator object, so no loops are required.

If called with a single iterator, it will return an array of the yielded values. If also called with a callback function, it will instead call that callback once for each yielded value.

Example

var iterate = require('iterate-iterator');
var getIterator = require('es-get-iterator');
var assert = require('assert');

assert.deepEqual(iterate(getIterator('a 💩')), ['a', ' ', '💩']);
assert.deepEqual(iterate(getIterator([1, 2])), [1, 2]);
assert.deepEqual(iterate(getIterator(new Set([1, 2]))), [1, 2]);
assert.deepEqual(iterate(getIterator(new Map([[1, 2], [3, 4]]))), [[1, 2], [3, 4]]);

function assertWithCallback(iterable, expected) {
    var values = [];
    var callback = function (x) { values.push(x); };
    iterate(iterable, callback);
    assert.deepEqual(values, expected);
}
assertWithCallback(getIterator('a 💩'), ['a', ' ', '💩']);
assertWithCallback(getIterator([1, 2]), [1, 2]);
assertWithCallback(getIterator(new Set([1, 2])), [1, 2]);
assertWithCallback(getIterator(new Map([[1, 2], [3, 4]])), [[1, 2], [3, 4]]);

Tests

Simply clone the repo, npm install, and run npm test

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

Generated by auto-changelog.

v1.0.2 - 2021-10-01

Commits

  • [Tests] migrate tests to Github Actions 13a9d00
  • [meta] do not publish Github Actions workflows 15d4556
  • [actions] use node/install instead of node/run; use codecov action a23decd
  • [Tests] run nyc on all tests; use tape runner 86adda1
  • [Tests] fix tests in IE 8 9c44f28
  • [Dev Deps] update eslint, @ljharb/eslint-config, aud, auto-changelog, es-get-iterator, object-inspect, tape 7887af7
  • [Dev Deps] update eslint, @ljharb/eslint-config, auto-changelog, object-inspect, tape 21975ca
  • [Dev Deps] update eslint, @ljharb/eslint-config, aud, es-get-iterator, object-inspect, tape a42d8cd
  • [actions] add "Allow Edits" workflow a23d108
  • [readme] remove travis badge dc38174
  • [Dev Deps] update @ljharb/eslint-config, es-get-iterator 970e212
  • [readme] add actions and codecov badges 1a41785
  • [actions] switch Automatic Rebase workflow to pull_request_target event 9e5bcbf
  • [Dev Deps] update aud, auto-changelog 63f0b43
  • [Dev Deps] update auto-changelog, tape 2186bf5
  • [meta] add package.json to "exports" 97a1ac2
  • [meta] use prepublishOnly, for npm 7+ b35c088
  • [Tests] only audit prod deps 15f9f7c
  • [meta] add funding field ab52815
  • [Dev Deps] update tape d044397

v1.0.1 - 2020-01-18

Commits

v1.0.0 - 2020-01-18

Commits