Permalink
Browse files

Set pop and shift

  • Loading branch information...
1 parent a0c0d15 commit a8af825065db3efd3433e881792ed8778b1cc5e2 @kriskowal committed Dec 29, 2012
Showing with 25 additions and 0 deletions.
  1. +16 −0 set.js
  2. +9 −0 spec/set-spec.js
View
16 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;
View
@@ -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.