Skip to content

Repository files navigation

JSDSL - Iterator

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

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:

publicabstracthasNext(): boolean;publicabstractnext(): 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:

classMyIteratorextendsAbstractIterator<any>{ ... }for(letelementofnewMyIterator()){ ... }

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:

publicabstractiterator(): 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:

classMyIterableextendsAbstractIterable<any>{ ... }for(letelementofnewMyIterable()){ ... }

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

Releases

Sponsor this project

Contributors

Languages

Generated from T99/ts-module-template