8000 Implement `Symbol.iterator` on `Expansion` class · Issue #84 · wimpyprogrammer/regex-to-strings · GitHub
[go: up one dir, main page]

Skip to content
Implement Symbol.iterator on Expansion class #84
Open
@homomorphist

Description

@homomorphist

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. :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0