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 |
|
---|
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 [spec](https://tc39.es/ecma262/#sec-additional-properties-of-the-string.prototype-object).
|
---|
13 |
|
---|
14 | Because the `Iterator.prototype` methods depend on a receiver (the `this` value), the main export in each subdirectory takes the string to operate on as the first argument.
|
---|
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)
|
---|
22 | - [`Iterator.from`](https://tc39.es/proposal-iterator-helpers/#sec-iterator.from)
|
---|
23 | - [`Iterator.prototype.constructor`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.constructor)
|
---|
24 | - [`Iterator.prototype.drop`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.drop)
|
---|
25 | - [`Iterator.prototype.every`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.every)
|
---|
26 | - [`Iterator.prototype.filter`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.filter)
|
---|
27 | - [`Iterator.prototype.find`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.find)
|
---|
28 | - [`Iterator.prototype.flatMap`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.flatmap)
|
---|
29 | - [`Iterator.prototype.forEach`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.foreach)
|
---|
30 | - [`Iterator.prototype.map`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.map)
|
---|
31 | - [`Iterator.prototype.reduce`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.reduce)
|
---|
32 | - [`Iterator.prototype.some`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.some)
|
---|
33 | - [`Iterator.prototype.take`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.take)
|
---|
34 | - [`Iterator.prototype.toArray`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.toarray)
|
---|
35 |
|
---|
36 | ## Environments where this is needed
|
---|
37 |
|
---|
38 | - node v22, Chrome >= v122: has a [bug](https://issues.chromium.org/issues/336839115)
|
---|
39 | - node < v22, Chrome < v122, Safari <= v17.1, Firefox <= v125: not implemented
|
---|
40 |
|
---|
41 | ## Getting started
|
---|
42 |
|
---|
43 | ```sh
|
---|
44 | npm install --save es-iterator-helpers
|
---|
45 | ```
|
---|
46 |
|
---|
47 | ## Usage/Examples
|
---|
48 |
|
---|
49 | ```js
|
---|
50 | const map = require('es-iterator-helpers/Iterator.prototype.map');
|
---|
51 | const toArray = require('es-iterator-helpers/Iterator.prototype.toArray');
|
---|
52 | const assert = require('assert');
|
---|
53 |
|
---|
54 | const iterator = [1, 2, 3].values();
|
---|
55 |
|
---|
56 | const mapped = map(iterator, (x) => x + 10);
|
---|
57 | assert.deepEqual(
|
---|
58 | mapped.next(),
|
---|
59 | {
|
---|
60 | done: false,
|
---|
61 | value: 11,
|
---|
62 | }
|
---|
63 | );
|
---|
64 | assert.deepEqual(
|
---|
65 | toArray(mapped),
|
---|
66 | [12, 13]
|
---|
67 | );
|
---|
68 | ```
|
---|
69 |
|
---|
70 | ```js
|
---|
71 | require('./auto'); // shim all of the methods
|
---|
72 |
|
---|
73 | require('./Iterator.prototype.map/auto'); // shim the “map” method
|
---|
74 | ```
|
---|
75 |
|
---|
76 | ## Tests
|
---|
77 | Simply clone the repo, `npm install`, and run `npm test`
|
---|
78 |
|
---|
79 | [package-url]: https://npmjs.org/package/es-iterator-helpers
|
---|
80 | [npm-version-svg]: https://versionbadg.es/es-shims/iterator-helpers.svg
|
---|
81 | [deps-svg]: https://david-dm.org/es-shims/iterator-helpers.svg
|
---|
82 | [deps-url]: https://david-dm.org/es-shims/iterator-helpers
|
---|
83 | [dev-deps-svg]: https://david-dm.org/es-shims/iterator-helpers/dev-status.svg
|
---|
84 | [dev-deps-url]: https://david-dm.org/es-shims/iterator-helpers#info=devDependencies
|
---|
85 | [npm-badge-png]: https://nodei.co/npm/es-iterator-helpers.png?downloads=true&stars=true
|
---|
86 | [license-image]: https://img.shields.io/npm/l/es-iterator-helpers.svg
|
---|
87 | [license-url]: LICENSE
|
---|
88 | [downloads-image]: https://img.shields.io/npm/dm/es-iterator-helpers.svg
|
---|
89 | [downloads-url]: https://npm-stat.com/charts.html?package=es-iterator-helpers
|
---|
90 | [codecov-image]: https://codecov.io/gh/es-shims/iterator-helpers/branch/main/graphs/badge.svg
|
---|
91 | [codecov-url]: https://app.codecov.io/gh/es-shims/iterator-helpers/
|
---|
92 | [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/es-shims/iterator-helpers
|
---|
93 | [actions-url]: https://github.com/es-shims/iterator-helpers/actions
|
---|