@rauschma/iterator-helpers-polyfill

0.7.0 • Public • Published

Iterator helpers polyfill

A polyfill for the ECMAScript proposal “Iterator Helpers” (documentation for this proposal).

Warning

Don’t trust this code:

  • It’s mostly untested.
  • I cut corners to make the TypeScript types work.

Caveats:

  • The focus is on simple code, not on spec compliance.
  • I use textual search-and-replace to convert the asynchronous code to synchronous code.
    • Performed via: npm run syncify

Functionality:

  • This polyfill implements all constructs specified in the proposal.
  • This polyfill deliberately does not provide any additional functionality.

Installation

npm install @rauschma/iterator-helpers-polyfill

Examples

Polyfill

import assert from 'node:assert/strict';
import '@rauschma/iterator-helpers-polyfill/install'; // install polyfill globally

assert.deepEqual(
  new Set(['a', 'b', 'c']).values()
  .map(x => x + x).toArray(),
  ['aa', 'bb', 'cc']
);

assert.deepEqual(
  new Set(['a', 'b', 'c']).values()
  .filter(x => x <= 'b').toArray(),
  ['a', 'b']
);

assert.deepEqual(
  new Set(['a', 'b', 'c']).values()
  .take(1).toArray(),
  ['a']
);

Library (doesn’t change JavaScript’s globals)

import assert from 'node:assert/strict';
import {XIterator} from '@rauschma/iterator-helpers-polyfill';

assert.deepEqual(
  XIterator.from(new Set(['a', 'b', 'c']))
  .map(x => x + x).toArray(),
  ['aa', 'bb', 'cc']
);

assert.deepEqual(
  XIterator.from(new Set(['a', 'b', 'c']))
  .filter(x => x <= 'b').toArray(),
  ['a', 'b']
);

assert.deepEqual(
  XIterator.from(new Set(['a', 'b', 'c']))
  .take(1).toArray(),
  ['a']
);

More examples

Material on iterator helpers

Package Sidebar

Install

npm i @rauschma/iterator-helpers-polyfill

Weekly Downloads

0

Version

0.7.0

License

MIT

Unpacked Size

117 kB

Total Files

53

Last publish

Collaborators

  • rauschma