Skip to content

Instantly share code, notes, and snippets.

@trevordixon
Last active August 29, 2015 14:02
Show Gist options
  • Save trevordixon/b2cf95faef6e25ed1552 to your computer and use it in GitHub Desktop.
Save trevordixon/b2cf95faef6e25ed1552 to your computer and use it in GitHub Desktop.
Very simple javascript array-backed set
function ArraySet() {
this._items = [];
}
ArraySet.prototype.add = function add(item) {
if (this._items.indexOf(item) > -1) {
return false;
}
this._items.push(item);
return true;
};
ArraySet.prototype.remove = function remove(item) {
var index = this._items.indexOf(item);
if (index === -1) {
return false;
}
this._items.splice(index, 1);
return true;
};
ArraySet.prototype.has = function has(item) {
return this._items.indexOf(item) > -1;
};
ArraySet.prototype.forEach = function forEach(cb) {
return this._items.forEach(cb);
};
ArraySet.prototype.empty = function empty() {
this._items.length = 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment