Skip to content
Sugary iteration utilities and interfaces.
Branch: master
Clone or download
T99 Fixed issue caused by ae7928 with `[Symbol.iterator]`.
Also began adding test files with Jest for obvious reasons.
Latest commit 9443489 Jun 16, 2019

readme.md

iter-over

iter-over is an iteration toolset for JavaScript/TypeScript that provides interfaces as well as utility classes for iteration using the native JavaScript Symbol.iterator method.

Find iter-over on NPM.

Installation

Install from NPM with

$ npm install --save iter-over

Basic Usage

For most use-cases you'll want to extend AbstractIterator (the iter-over abstract iterator class). The abstract class implements such methods as #forEachRemaining(callback) and automagically implements the [Symbol.iterator] method so that you don't have to! The only methods you have to implement are:

public hasNext(): boolean { ... }
public next(): T | undefined { ... }

Once you've done that, you can freely use the iterator as such:

import { AbstractIterator } from "iter-over";

class MyCounter extends AbstractIterator<number> {

	private val: number = 0;
	
	public hasNext(): boolean {
		
		return (this.val < 10);
		
	}
	
	public next(): number {
		
		return this.val++;
		
	}
	
}

let counter: MyCounter = new MyCounter();

for (let counterVal of counter) console.log(counterVal);

// ...console logs 0 through 9.

Utility Classes

There are a handful of utility classes provided with iter-over that are all ready for you to use.

StringIterator

StringIterator iterates over the characters of a provided input string.

let iter: StringIterator = new StringIterator("Hello!");

for (let character of iter) console.log(character);

// Prints 'H', 'e', 'l', 'l', 'o', '!'.

ObjectIterator

ObjectIterator iterates over the key-value pairs of a provided input object, returning IOKeyValuePairs from #next().

let iter: ObjectIterator = new ObjectIterator({
	key1: "value 1",
	key2: 2,
	key3: false,
	key4: {
		innerKey: "innerVal"
	}
});

for (let kvPair of iter) console.log(kvPair);

// Prints...
//	'{ key: "key1", value: "value 1" }',
//	'{ key: "key2", value: 2 }',
//	'{ key: "key3", value: false }',
//	'{ key: "key4", value: { innerKey: "innerVal" } }'

If you have more strictly-typed objects you can also pass a type to ObjectIterator.

let iter: ObjectIterator<number> = new ObjectIterator<number>({
	key1: 0,
	key2: 11,
	key3: 42
});

for (let kvPair of iter) console.log(kvPair);

// Prints...
//	'{ key: "key1", value: 0 }',
//	'{ key: "key2", value: 11 }',
//	'{ key: "key3", value: 42 }'

EmptyIterator

Sometimes it is semantically useful to have an empty iterator that follows the iterator pattern but will never have content.

let iter: EmptyIterator = new EmptyIterator();

console.log(iter.hasNext());

// Prints 'false'.
You can’t perform that action at this time.