[d565449] | 1 | # es-iterator-helpers <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
|
---|
| 2 |
|
---|
| 3 | [![github actions][actions-image]][actions-url]
|
---|
| 4 | [![coverage][codecov-image]][codecov-url]
|
---|
| 5 | [![License][license-image]][license-url]
|
---|
| 6 | [![Downloads][downloads-image]][downloads-url]
|
---|
| 7 |
|
---|
| 8 | [![npm badge][npm-badge-png]][package-url]
|
---|
| 9 |
|
---|
| 10 | An ESnext spec-compliant sync iterator helpers shim/polyfill/replacement that works as far down as ES3.
|
---|
| 11 |
|
---|
[0c6b92a] | 12 | This package implements the [es-shim API](https://github.com/es-shims/api) “multi” interface. It works in an ES3-supported environment and complies with the [iterator helpers spec](https://tc39.es/proposal-iterator-helpers/) and the [iterator sequencing spec](https://tc39.es/proposal-iterator-sequencing/).
|
---|
[d565449] | 13 |
|
---|
[0c6b92a] | 14 | Because the `Iterator.prototype` methods depend on a receiver (the `this` value), the main export in each subdirectory takes the iterator to operate on as the first argument.
|
---|
[d565449] | 15 |
|
---|
| 16 | The main export of the package itself is simply an array of the available directory names. It’s sole intended use is for build tooling and testing.
|
---|
| 17 |
|
---|
| 18 | ## Supported things
|
---|
| 19 |
|
---|
| 20 | - [`Iterator` constructor](https://tc39.es/proposal-iterator-helpers/#sec-iterator-constructor)
|
---|
| 21 | - [`Iterator.prototype`](https://tc39.es/proposal-iterator-helpers/#sec-iterator.prototype)
|
---|
[0c6b92a] | 22 | - [`Iterator.concat`](https://tc39.es/proposal-iterator-sequencing/)
|
---|
[d565449] | 23 | - [`Iterator.from`](https://tc39.es/proposal-iterator-helpers/#sec-iterator.from)
|
---|
| 24 | - [`Iterator.prototype.constructor`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.constructor)
|
---|
| 25 | - [`Iterator.prototype.drop`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.drop)
|
---|
| 26 | - [`Iterator.prototype.every`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.every)
|
---|
| 27 | - [`Iterator.prototype.filter`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.filter)
|
---|
| 28 | - [`Iterator.prototype.find`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.find)
|
---|
| 29 | - [`Iterator.prototype.flatMap`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.flatmap)
|
---|
| 30 | - [`Iterator.prototype.forEach`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.foreach)
|
---|
| 31 | - [`Iterator.prototype.map`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.map)
|
---|
| 32 | - [`Iterator.prototype.reduce`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.reduce)
|
---|
| 33 | - [`Iterator.prototype.some`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.some)
|
---|
| 34 | - [`Iterator.prototype.take`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.take)
|
---|
| 35 | - [`Iterator.prototype.toArray`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.toarray)
|
---|
| 36 |
|
---|
| 37 | ## Environments where this is needed
|
---|
| 38 |
|
---|
| 39 | - node v22, Chrome >= v122: has a [bug](https://issues.chromium.org/issues/336839115)
|
---|
| 40 | - node < v22, Chrome < v122, Safari <= v17.1, Firefox <= v125: not implemented
|
---|
[0c6b92a] | 41 | - all environments lack Iterator.concat
|
---|
[d565449] | 42 |
|
---|
| 43 | ## Getting started
|
---|
| 44 |
|
---|
| 45 | ```sh
|
---|
| 46 | npm install --save es-iterator-helpers
|
---|
| 47 | ```
|
---|
| 48 |
|
---|
| 49 | ## Usage/Examples
|
---|
| 50 |
|
---|
[0c6b92a] | 51 | Using explicit imports:
|
---|
| 52 |
|
---|
[d565449] | 53 | ```js
|
---|
| 54 | const map = require('es-iterator-helpers/Iterator.prototype.map');
|
---|
| 55 | const toArray = require('es-iterator-helpers/Iterator.prototype.toArray');
|
---|
| 56 | const assert = require('assert');
|
---|
| 57 |
|
---|
| 58 | const iterator = [1, 2, 3].values();
|
---|
| 59 |
|
---|
| 60 | const mapped = map(iterator, (x) => x + 10);
|
---|
| 61 | assert.deepEqual(
|
---|
| 62 | mapped.next(),
|
---|
| 63 | {
|
---|
| 64 | done: false,
|
---|
| 65 | value: 11,
|
---|
| 66 | }
|
---|
| 67 | );
|
---|
| 68 | assert.deepEqual(
|
---|
| 69 | toArray(mapped),
|
---|
| 70 | [12, 13]
|
---|
| 71 | );
|
---|
| 72 | ```
|
---|
| 73 |
|
---|
[0c6b92a] | 74 | Shim using `require`:
|
---|
| 75 |
|
---|
| 76 | ```js
|
---|
| 77 | require('es-iterator-helpers/auto'); // shim all of the methods
|
---|
| 78 |
|
---|
| 79 | require('es-iterator-helpers/Iterator.prototype.map/auto'); // shim the “map” method
|
---|
| 80 | ```
|
---|
| 81 |
|
---|
| 82 | Shim using `import` syntax:
|
---|
| 83 |
|
---|
| 84 | [](#preventEval)
|
---|
[d565449] | 85 | ```js
|
---|
[0c6b92a] | 86 | import 'es-iterator-helpers/auto'; // shim all of the methods
|
---|
[d565449] | 87 |
|
---|
[0c6b92a] | 88 | import 'es-iterator-helpers/Iterator.prototype.map/auto'; // shim the “map” method
|
---|
[d565449] | 89 | ```
|
---|
| 90 |
|
---|
| 91 | ## Tests
|
---|
| 92 | Simply clone the repo, `npm install`, and run `npm test`
|
---|
| 93 |
|
---|
| 94 | [package-url]: https://npmjs.org/package/es-iterator-helpers
|
---|
| 95 | [npm-version-svg]: https://versionbadg.es/es-shims/iterator-helpers.svg
|
---|
| 96 | [deps-svg]: https://david-dm.org/es-shims/iterator-helpers.svg
|
---|
| 97 | [deps-url]: https://david-dm.org/es-shims/iterator-helpers
|
---|
| 98 | [dev-deps-svg]: https://david-dm.org/es-shims/iterator-helpers/dev-status.svg
|
---|
| 99 | [dev-deps-url]: https://david-dm.org/es-shims/iterator-helpers#info=devDependencies
|
---|
| 100 | [npm-badge-png]: https://nodei.co/npm/es-iterator-helpers.png?downloads=true&stars=true
|
---|
| 101 | [license-image]: https://img.shields.io/npm/l/es-iterator-helpers.svg
|
---|
| 102 | [license-url]: LICENSE
|
---|
| 103 | [downloads-image]: https://img.shields.io/npm/dm/es-iterator-helpers.svg
|
---|
| 104 | [downloads-url]: https://npm-stat.com/charts.html?package=es-iterator-helpers
|
---|
| 105 | [codecov-image]: https://codecov.io/gh/es-shims/iterator-helpers/branch/main/graphs/badge.svg
|
---|
| 106 | [codecov-url]: https://app.codecov.io/gh/es-shims/iterator-helpers/
|
---|
| 107 | [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/es-shims/iterator-helpers
|
---|
| 108 | [actions-url]: https://github.com/es-shims/iterator-helpers/actions
|
---|