Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add SMOVE

  • Loading branch information...
commit a18178a5cee834e500927b4fde7b0ecb29ab6ff2 1 parent 4813102
@elcuervo authored
Showing with 44 additions and 2 deletions.
  1. +1 −1  README.md
  2. +10 −0 index.html
  3. +11 −0 lodis.coffee
  4. +22 −1 lodis.js
View
2  README.md
@@ -121,7 +121,7 @@
* SLAVEOF host port
* SLOWLOG subcommand [argument]
* SMEMBERS key ✓
- * SMOVE source destination member
+ * SMOVE source destination member ✓
* SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination]
* SPOP key
* SRANDMEMBER key
View
10 index.html
@@ -517,6 +517,16 @@
assert(l.sismember("foo", "one"));
assert(!l.sismember("foo", "two"));
+ },
+
+ "SMOVE should move a member of a set to another set": function(){
+ l.sadd("foo", "one");
+ l.sadd("foo", "two");
+ l.sadd("bar", "three");
+ l.smove("foo", "bar", "two");
+
+ assert_equal(l.smembers("foo"), ["one"]);
+ assert_equal(l.smembers("bar"), ["three", "two"]);
}
});
View
11 lodis.coffee
@@ -246,6 +246,7 @@ class @Lodis
set = this._get_set(key)
value = set[1..-1]
this._set_packed(key, value)
+ value
lpushx: (key, value) ->
if this.exists(key)
@@ -377,3 +378,13 @@ class @Lodis
set = this._get_set(key)
set.indexOf(value) > -1
+ smove: (source, destination, member) ->
+ if this.exists(source)
+ if member in this.smembers(source)
+ result = []
+ result.push item for item in this._get_set(source) when item != member
+ this._set_packed(source, result)
+ this.rpush(destination, member)
+ true
+ else
+ false
View
23 lodis.js
@@ -379,7 +379,8 @@
if (this.exists(key)) {
set = this._get_set(key);
value = set.slice(1);
- return this._set_packed(key, value);
+ this._set_packed(key, value);
+ return value;
}
};
Lodis.prototype.lpushx = function(key, value) {
@@ -592,6 +593,26 @@
return set.indexOf(value) > -1;
}
};
+ Lodis.prototype.smove = function(source, destination, member) {
+ var item, result, _i, _len, _ref;
+ if (this.exists(source)) {
+ if (__indexOf.call(this.smembers(source), member) >= 0) {
+ result = [];
+ _ref = this._get_set(source);
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ item = _ref[_i];
+ if (item !== member) {
+ result.push(item);
+ }
+ }
+ this._set_packed(source, result);
+ this.rpush(destination, member);
+ return true;
+ } else {
+ return false;
+ }
+ }
+ };
return Lodis;
})();
}).call(this);
Please sign in to comment.
Something went wrong with that request. Please try again.