Skip to content

Commit

Permalink
Set pop and shift
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Dec 29, 2012
1 parent a0c0d15 commit a8af825
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
16 changes: 16 additions & 0 deletions set.js
Expand Up @@ -101,6 +101,22 @@ Set.prototype["delete"] = function (value) {
return false;
};

Set.prototype.pop = function () {
if (this.length) {
var result = this.order.head.prev.value;
this["delete"](result);
return result;
}
};

Set.prototype.shift = function () {
if (this.length) {
var result = this.order.head.next.value;
this["delete"](result);
return result;
}
};

Set.prototype.one = function () {
if (this.length > 0) {
return this.store.one().value;
Expand Down
9 changes: 9 additions & 0 deletions spec/set-spec.js
Expand Up @@ -19,5 +19,14 @@ describe("Set", function () {
return Set(values, Object.is);
}, [{}, {}, {}, {}], true);

it("should pop and shift", function () {
var a = {i: 2};
var b = {i: 1};
var c = {i: 0};
var set = Set([a, b, c], Object.is);
expect(set.pop()).toBe(c);
expect(set.shift()).toBe(a);
});

});

0 comments on commit a8af825

Please sign in to comment.