diff --git a/set.js b/set.js index 37a4a52..2b3e088 100644 --- a/set.js +++ b/set.js @@ -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; diff --git a/spec/set-spec.js b/spec/set-spec.js index c91003d..32377de 100644 --- a/spec/set-spec.js +++ b/spec/set-spec.js @@ -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); + }); + });