Last active
August 29, 2015 14:02
-
-
Save trevordixon/b2cf95faef6e25ed1552 to your computer and use it in GitHub Desktop.
Very simple javascript array-backed set
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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