Created
September 20, 2016 15:48
-
-
Save igormukhin/7ec738764141d4443e76415178075a5a to your computer and use it in GitHub Desktop.
Mimics Java Set in JavaScript (DRAFT, NOT TESTED)
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 Set() { | |
this.data = {}; | |
this.add.apply(this, arguments); | |
} | |
Set.prototype = { | |
add: function (key) { | |
this.data[key] = true; | |
}, | |
remove: function (key) { | |
delete this.data[key]; | |
}, | |
clear: function () { | |
this.data = {}; | |
return this; | |
}, | |
contains: function (key) { | |
return this.data.hasOwnProperty(key); | |
}, | |
isEmpty: function () { | |
for (var key in this.data) { | |
if (this.has(key)) { | |
return false; | |
} | |
} | |
return true; | |
}, | |
size: function () { | |
}, | |
keys: function () { | |
var results = []; | |
$.each(this.data, function (idx, key) { | |
results.push(key); | |
}); | |
return results; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment