Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including stupid-iterator with all npm packages installed. Try it out:

var stupidIterator = require("stupid-iterator")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

stupid-iterator v1.2.0

A simple collection helper

Stupid Iterator

A simple collection helper.

Usage

Get next item

var iterator = require('stupid-iterator');

var collection = [{name:'one'}, {name:'two'}, {name:'three'}, {name:'four'}];
var current = collection[0];
current = iterator.next(current, collection); // current is collection[1]
current = iterator.next(collection[3], collection); // current is collection[0]

Get previous item

var collection = [{name:'one'}, {name:'two'}, {name:'three'}, {name:'four'}];
var current = collection[1];
current = iterator.prev(current, collection); // current is collection[0]
current = iterator.prev(current, collection); // current is collection[3]

API


iterator.nextOrFalse(current, collection);
iterator.prevOrFalse(current, collection);
iterator.isFirst(current, collection);
iterator.isPrev(current, collection);
iterator.add(object, collection);
iterator.remove(object, collection);
var ite = iterator.create(current, collection);

Using iterator.create() Current and collection is stored in the object, so the syntax becomes easier to write and read. (See tests for more functionality, but you can basically use the same api).

var collection = [{name:'one'}, {name:'two'}, {name:'three'}, {name:'four'}];
var current = collection[0];
var ite = iterator.create(current, collection);

var obj = ite.next(); // equals collection[1];
obj = ite.next(); // equals collection[2];
obj = ite.prev(); // equals collection[1];
ite.set(collection[3]);
obj = ite.get() // equals collection[3];

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free