regex-to-strings icon indicating copy to clipboard operation
regex-to-strings copied to clipboard

Implement `Symbol.iterator` on `Expansion` class

Open homomorphist opened this issue 2 years ago • 0 comments
trafficstars

Currently to iterate over an expander you must call getIterator:

const digitExpander = expand(/\d/);

for (const digit of digitExpander.getIterator()) {
	// ...
}

By implementing Symbol.iterator, you would be able to do the following:

const digitExpander = expand(/\d/);

for (const digit of digitExpander) {
	// ...
}

This can be done by changing the Expansion like so:

export default class Expansion {
	// ... (omitted) ....

	/**
	 * Alias for `getIterator`
	 */
	public [Symbol.iterator]: () => IterableIterator<string>;

	// ... (omitted) ....

	public constructor(getIterator: IterableSource, count: Expansion['count']) {
		this.getIterator = toIterable(getIterator);
		this[Symbol.iterator] = this.getIterator;
		this.count = count;
	}
}

I'd contribute it myself, but I'm not sure how exactly you would like it styled, and would probably end up messing something up, haha. :)

homomorphist avatar Mar 03 '23 19:03 homomorphist