seqit v1.0.1
seqit
Sequential Iterator
Sometimes the JavaScript forEach loop is not dynamic enough, especially when you have to break out of the loop early, visit elements in a different order, or modify the object you are iterating over in-place.
seqit is a general-purpose iterator module compatible with both Node.js and the browser, designed to provide a simple method of looping over an array or object using for or while, inspired by C++ iterators. You can wrap any array or object with the seqit function and iterate over its elements or keys. You can also edit the values you're iterating over in-place. seqit does not copy any data; it simply binds to an existing structure.
Examples
var seqit = require('seqit');
var array = [5, 4, 3, 5, 6, 7, 8, 9, 10];
for (var it = seqit(array); it !== it.end; it.next) {
console.log(it());
}Equivalently, in a while loop,
var it = seqit(array)
while (it.hasNext) {
console.log(it.next);
}Looping over an object:
var obj = { a: 5, b: 'woop' };
for (var it = seqit(obj); it !== it.end; it.next) {
console.log(it().key + ': ' + it().value);
}Editing an object in place:
var obj = { a: 'woop', b: 'doop' }
for (var it = seqit(obj); it !== it.end; it.next) {
it().key += 'a';
it().value += 'a';
}
console.log(obj);
// { aa: 'woopa', ba: 'doopa' }Editing an array in place:
var array = [5, 4, 3, 5, 6, 7, 8, 9, 10];
for (var it = seqit(array); it !== it.end; it.next) {
it(11);
}
console.log(array);
// [11, 11, 11, 11, 11, 11, 11, 11, 11]Using the iteration index:
var array = [5, 4, 3, 5, 6, 7, 8, 9, 10];
for (var it = seqit(array); it !== it.end; it.next) {
console.log(it.index);
}
// 0, 1, 2, 3 ...You can also set it.index if you want to manually change where you are in the loop.
More examples to come...
Documentation
seqit(*val, config)Function that returns anIteratorinstance. If the first arguments are numbers,seqitwill return anIteratorthat iterates over a range of numbers. The first number is inclusive and the second is exclusive. You can also pass a single number to start from 0 implicitly, and you can pass a third number to indicate the step size. For example,seqit(7)will iterate over values 0 through 6,seqit(1, 7)will iterate over values 1 through 6, andseqit(1, 7, 2)will iterate over values 1, 3, and 5. If you pass an object toseqitit will return an iterator that iterates over the key value pairs of an object. If you pass an array toseqittheIteratorwill iterate over each element. The last argument toseqitcan be a configuration object, where you can pass{ reversed: true, step: number }to specify that you want to iterate in reverse order or set the step size.Iterator(val)If you call the iterator with no argument, it will return the current element being iterated over. If you pass a value in, it will set the current element to that value.Iterator.sizeReturns the amount of elements in the collection being iterated over.Iterator.size = valManually set the size of collection being iterated over.Iterator.endA function that returns the last element in the collection, or the first if you are in reverse order. If you have reached the end of the iteration this function will be equal to theIteratoritself.Iterator.end = valSets the last element of the array toval, or the first if you are in reverse order.Iterator.beginA function that returns the first element in the collection, or the last element if you are in reverse order. If you are at the beginning of the iteration this function will be equal to theIteratoritself.Iterator.begin = valSets the first element of the array toval, or the last if you are in reverse order.Iterator.reversedEqual totrueif you are in reverse order, false otherwise.Iterator.reversed = true/falseSet the order of the iteration.Iterator.stepAccessing this property will advance the iteration by one step.Iterator.step = nThis will set the step size of the iteration ton.Iterator.step(n)This will advance the iteration bynstep sizes.Iterator.backAccessing this property will advance the iteration by one step in the direction opposite to the iteration order.Iterator.back(n)This will advance the iteration bynstep sizes in the direction opposite to the iteration order.Iterator.indexEqual to the current index in the array being iterated over.Iterator.index = valSet the current index of the iteration.Iterator.nextAcceessing this property returns the current element then advances the iteration forward by one step.Iterator.prevAccessing this property returns the current element then advances the iteration backward by one step.Iterator.hasNextWill betrueif it is possible to callIterator.next,falseotherwise.Iterator.hasPrevWill betrueif it is possible to callIterator.prev,falseotherwise.Iterator.resetAccessing this property will reset the iteration to the state it was in when theIteratorwas constructed.Iterator.find(function or value)This will advance the iteration to the next element where the function you pass in returnstrue. If you are iterating over an array the function receives as its arguments: the current element, the current index, and the whole array, in that order. If you are iterating over an object the function is passed four arguments: the current value, the current key, the current index, and the whole object. If you passfinda value instead of a function, it will find the next element where the value is equal to the supplied value. If you pass an object as the value,findwill iterate until it encounters an object that contains at least those keys and associated values.findwill returntrueif it found a match, andfalseif it reached the end of the iteration without finding a match.Iterator.select(function or value)This function takes the same type of parameter asfind, but it changes the behavior of the iteration so that it only visits matching elements. If you accessIterator.resetor callIterator.select()with no argument it resets this behavior.selectreturns the iterator itself.
Examples of select and find:
var it = seqit([5, 1, 4, 2, 3]);
while (it.find(function (v) { return v > 3; })) {
console.log(it());
}
// console: '5' '4'or equivalently:
for (var it = seqit([5, 1, 4, 2, 3]).select(function (v) { return v > 3; }); it !== it.end; it.next) {
console.log(it());
}License
MIT