1.1.2 • Published 3 years ago

@jsdsl/iterator v1.1.2

Weekly downloads
2
License
GPL-3.0-or-later
Repository
github
Last release
3 years ago

JSDSL - Iterator

A collection of classes that allow iteration over a predefined collection of elements.

Find @jsdsl/iterator on NPM.

Table of Contents

Installation

Install from NPM with

$ npm install --save @jsdsl/iterator

Basic Usage

The Iterator class has a handful of useful methods:

hasNext(): boolean;

next(): E | undefined;

forEach(callback: (element: E) => any): void;

remove?(): E | undefined;

reset?(): void;

Each of which help enable iteration over the elements underlying the Iterator.

Perhaps of more use is the AbstractIterator, for which only the first two of the methods mentioned above need to be implemented:

public abstract hasNext(): boolean;

public abstract next(): E | undefined;

Correctly implementing the above two methods will result in a class whose instances can be iterated over by a regular for...of loop:

class MyIterator extends AbstractIterator<any> { ... }

for (let element of new MyIterator()) { ... }

This package also includes the Iterable and AbstractIterable classes. The Iterable class represents some structure that can be iterated over, and exposes the following methods:

iterator(): Iterator<E>;

[Symbol.iterator](): IterableIterator<E>;

Meanwhile the AbstractIterable class does the small task of implementing the [Symbol.iterator] method, simply requiring the implementation of the iterator method:

public abstract iterator(): Iterator<E>;

Similar to the Iterator and AbstractIterator classes, correctly implementing the required methods will result in a class whose instances can also be iterated over by a regular for...of loop:

class MyIterable extends AbstractIterable<any> { ... }

for (let element of new MyIterable()) { ... }

Documentation

See the wiki for full documentation.

License

@jsdsl/iterator is made available under the GNU General Public License v3.

Copyright (C) 2021 Trevor Sears