包详细信息

es-get-iterator

ljharb49.9mMIT1.1.3

Get an iterator for any JS language value. Works robustly across all environments, all versions.

iterator, Symbol.iterator, iterable, collection

自述文件

es-get-iterator Version Badge

dependency status dev dependency status License Downloads

npm badge

Get an iterator for any JS language value. Works robustly across all environments, all versions.

In modern engines, value[Symbol.iterator]() is sufficient to produce an iterator (an object with a .next method) for that object. However, older engines:

  • may lack Symbol 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)
  • 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 attempts to provide an abstraction over all that complexity!

In node v13+, exports is used to provide a lean implementation that lacks all the complexity described above, in combination with the browser field so that bundlers will pick up the proper implementation.

Targeting browsers with Symbol support

If you are targeting browsers that definitely all have Symbol support, then you can configure your bundler to replace require('has-symbols')() with a literal true, which should allow dead code elimination to reduce the size of the bundled code.

With @rollup/plugin-replace

// rollup.config.js

import replace from '@rollup/plugin-replace';

export default {
    ...
    plugins: [
        replace({
            "require('has-symbols')()": 'true',
            delimiters: ['', '']
        })
    ]
};

Example

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

var iterator = getIterator('a 💩');
assert.deepEqual(
    [iterator.next(), iterator.next(), iterator.next(), iterator.next()],
    [{ done: false, value: 'a' }, { done: false, value: ' ' }, { done: false, value: '💩' }, { done: true, value: undefined }]
);

var iterator = getIterator([1, 2]);
assert.deepEqual(
    [iterator.next(), iterator.next(), iterator.next()],
    [{ done: false, value: 1 }, { done: false, value: 2 }, { done: true, value: undefined }]
);

var iterator = getIterator(new Set([1, 2]));
assert.deepEqual(
    [iterator.next(), iterator.next(), iterator.next()],
    [{ done: false, value: 1 }, { done: false, value: 2 }, { done: true, value: undefined }]
);

var iterator = getIterator(new Map([[1, 2], [3, 4]]));
assert.deepEqual(
    [iterator.next(), iterator.next(), iterator.next()],
    [{ done: false, value: [1, 2] }, { done: false, value: [3, 4] }, { done: true, value: undefined }]
);

Tests

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

更新日志

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.1.3 - 2023-01-12

Commits

  • [actions] reuse common workflows c97cb76
  • [actions] use node/install instead of node/run; use codecov action 6d09911
  • [meta] use npmignore to autogenerate an npmignore file c7e0e85
  • [Dev Deps] update eslint, @ljharb/eslint-config, aud, auto-changelog, es5-shim, object-inspect, tape 1353190
  • [Refactor] extract logic to stop-iteration-iterator ab19956
  • [Dev Deps] update eslint, @ljharb/eslint-config, aud, auto-changelog, es5-shim, object-inspect, safe-publish-latest, tape de2ae73
  • [Tests] start testing more variants e059f33
  • [actions] update codecov uploader c8ffcec
  • [Dev Deps] update eslint, @ljharb/eslint-config, aud, es5-shim, has-bigints, object-inspect, tape 8cd2e87
  • [Dev Deps] update eslint, @ljharb/eslint-config, aud, object-inspect, tape 7676030
  • [actions] update checkout action bdbe6c9
  • [Dev Deps] update @ljharb/eslint-config, aud, es6-shim 67cddd6
  • [Tests] fix debug output issues in FF 24 a726fdc
  • [Deps] update has-symbols, is-arguments, is-string 044907b
  • [Deps] update get-intrinsic, has-symbols e492f8f
  • [meta] use prepublishOnly script for npm 7+ eccda6b
  • [Dev Deps] update object-inspect c24dfa5
  • [Deps] update get-intrinsic 1bd68ce

v1.1.2 - 2021-01-26

Commits

  • [meta] npmignore github action workflows 0cd2f21
  • [readme] remove travis badge 357065b
  • [Dev Deps] update eslint, @ljharb/eslint-config, has-bigints, object-inspect, tape 13a77f2
  • [Deps] update get-intrinsic, is-arguments, is-map, is-set 5f8d7f1
  • [meta] update actions, dotfiles 5ea3e50
  • [Tests] fix ESM test matrix 9ab614c
  • [Dev Deps] update eslint, @ljharb/eslint-config, es5-shim e843ad9
  • [Deps] update call-bind, get-intrinsic 4301b3e
  • [meta] avoid upcoming deprecation warning in node; add "browser" field 57297b1
  • [Tests] skip npm ls in node 0.x tests 1409196
  • [Dev Deps] update eslint e4dcea4

v1.1.1 - 2020-11-06

Commits

  • [Tests] migrate tests to Github Actions e10fd31
  • [Fix] Support iterators defined by es6-shim when loaded after es-get-iterator f2ef7e1
  • [Dev Deps] update eslint, @ljharb/eslint-config, aud, auto-changelog, es6-shim, object-inspect, tape 1ee86fc
  • [actions] add "Allow Edits" workflow c785c69
  • [Refactor] use get-intrinsic and call-bind instead of es-abstract 65f4ef5
  • [Dev Deps] update auto-changelog, es5-shim, tape; add aud 91301ed
  • [Dev Deps] update aud afc91d9
  • [actions] switch Automatic Rebase workflow to pull_request_target event 1962743
  • [Deps] update es-abstract d2b57c8

v1.1.0 - 2020-01-25

Commits

  • [New] add native ESM variant via conditional exports 325629d
  • [Tests] fix test matrix 01c20cf
  • [Docs] Add modern browser example for Rollup ab9f17d
  • [Deps] update is-map, is-set, es-abstract, is-string a1b9645
  • [Fix] node.js only runs where "exports" is supported, and arguments is iterable there ccc7646
  • [Dev Deps] update eslint, @ljharb/eslint-config, tape 4281453
  • [Dev Deps] update @ljharb/eslint-config f4fc99c
  • [Deps] update es-abstract 70b0423
  • [Tests] add string coverage for a lone surrogate not followed by a proper surrogate ending 796e497

v1.0.2 - 2019-12-16

Commits

v1.0.1 - 2019-11-27

Commits

  • [Fix] fix bugs in pre-Symbol environments 592f78a

v1.0.0 - 2019-11-25

Commits

  • Initial tests. 71f5fdd
  • Initial implementation d7e0480
  • Initial commit eb5372c
  • readme 8d6ad14
  • npm init 9b84446
  • [meta] add auto-changelog e2d2e4f
  • [meta] add funding field; create FUNDING.yml 5a31c77
  • [actions] add automatic rebasing / merge commit blocking 644429e
  • [Tests] add npm run lint f22172f
  • Only apps should have lockfiles fcf8441
  • [meta] add safe-publish-latest 946befa
  • [Tests] only test on majors, since travis has a 200 build limit aeb5f09