Skip to content

Commit

Permalink
Set constructor can now take an initial array of keys
Browse files Browse the repository at this point in the history
  • Loading branch information
cpettitt committed Aug 21, 2013
1 parent e796b4e commit 97e21ad
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/data/Set.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
module.exports = Set;

function Set() {
function Set(initialKeys) {
this._size = 0;
this._keys = {};

if (initialKeys) {
initialKeys.forEach(function(key) {
this.add(key);
}, this);
}
}

Set.prototype.size = function() { return this._size; };
Expand Down
8 changes: 8 additions & 0 deletions test/data/Set-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ describe("data/Set", function() {
set.add("c");
});

describe("constructor with initial keys", function() {
it("adds the initial keys to the set", function() {
set = new Set(["foo", "bar", "baz", "foo"]);
assert.equal(set.size(), 3);
assert.deepEqual(set.keys().sort(), ["foo", "bar", "baz"].sort());
});
});

describe("size", function() {
it("returns the size of the set", function() {
assert.equal(set.size(), 3);
Expand Down

0 comments on commit 97e21ad

Please sign in to comment.