@kingjs/create-enumerable-type
Create an enumerable type from a MoveNext()
factory.
Usage
Create an enumerable type for iterating Array
using a moveNext()
factory like this:
var createEnumerableType = require('@kingjs/create-enumerable-type');
var moveNextFactory = function(array) {
var index = -1;
return function moveNext() {
if (index + 1 == array.length)
return false;
this.current_ = array[++index];
return true;
}
}
var ArrayEnumerable = createEnumerableType(moveNextFactory);
Voila! Now, test the ArrayEnumerable
like this:
var enumerable = new ArrayEnumerable([0, 1, 2]);
var enumerator = enumerable.getEnumerator();
var actual = [];
while (enumerator.moveNext())
actual.push(enumerator.current);
actual;
outputs:
[0, 1, 2];
API
declare function createEnumerableType(
moveNextFactory: {
(...args: any[]): {
moveNext: { (): Boolean }
}
}): typeof Enumerable;
declare class Enumerable {
constructor(...args: any[]);
getEnumerator: {
(): {
moveNext: { (): Boolean },
current: any
}
}
}
Parameters
moveNextFactory
: a function that returns amoveNext()
function closed over the enumerable activation arguments.
Return Value
An enumerable type whose activation arguments are saved and passed to the specified
moveNextFactory
whengetEnumerator()
is called.
Install
With npm installed, run
$ npm install @kingjs/create-enumerable
Acknowledgments
create-enumerable
was inspired by the boilerplate generated by C# yield
syntax. Though we cannot generate the state machine, we can generate the type scaffolding.
See Also
License
MIT