Skip to content

Instantly share code, notes, and snippets.

@mseeley
Created September 10, 2010 21:43
Show Gist options
  • Save mseeley/574418 to your computer and use it in GitHub Desktop.
Save mseeley/574418 to your computer and use it in GitHub Desktop.
Encapsulated array walking
function Set (/* varargs */) {
var i = 0,
items = [].slice.call(arguments, 0);
this.item = function (idx) {
var item = items[i];
if (idx != null) {
if (idx in items) {
i = idx;
}
item = items[idx];
}
return item;
};
this.next = function (wrap) {
var len = items.length;
i = i >= len && wrap ? 0 : i + 1;
return i < len;
};
this.prev = function (wrap) {
i = (wrap && i <= 0 ? items.length : i) - 1;
return i >= 0;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment