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). This 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: E | undefined
So for example, an inline implementation would look something like:
;
Once you've done that, you can freely use the iterator as such:
; for of counter console.logcounterVal; // ...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.
StringCharacterIterator
StringCharacterIterator iterates over the characters of a provided input string.
; for of iter console.logcharacter; // Prints 'H', 'e', 'l', 'l', 'o', '!'.
StringLineIterator
StringLineIterator iterates over the lines of a provided input string.
; for of iter console.logline; // Prints 'Hello', 'there,', 'world!'.
ObjectIterator
ObjectIterator iterates over the key-value pairs of a provided input object, returning IOKeyValuePairs from #next()
.
; for of iter console.logkvPair; // 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.
; for of iter console.logkvPair; // 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.
; console.logiter.hasNext; // Prints 'false'.
License
iter-over is made available under the GNU General Public License v3.
Copyright (C) 2019 Trevor Sears