From 50d9f8d45e1829aa35cc77ffb04bff345f997079 Mon Sep 17 00:00:00 2001 From: Matt Ranney Date: Sat, 30 Jul 2011 17:21:25 -0700 Subject: [PATCH] Fix and test for [GH-123] Passing an Array as as the last argument should expand as users expect. The old behavior was to coerce the arguments into Strings, which did surprising things with Arrays. --- index.js | 10 ++++++++++ package.json | 2 +- test.js | 16 +++++++++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 5931e5724c..c3eb0c6d8d 100644 --- a/index.js +++ b/index.js @@ -457,6 +457,7 @@ RedisClient.prototype.return_reply = function (reply) { } else if (this.monitoring) { len = reply.indexOf(" "); timestamp = reply.slice(0, len); + // TODO - this de-quoting doesn't work correctly if you put JSON strings in your values. args = reply.slice(len + 1).match(/"[^"]+"/g).map(function (elem) { return elem.replace(/"/g, ""); }); @@ -508,6 +509,15 @@ RedisClient.prototype.send_command = function (command, args, callback) { return; } + // if the last argument is an array, expand it out. This allows commands like this: + // client.command(arg1, [arg2, arg3, arg4], cb); + // and converts to: + // client.command(arg1, arg2, arg3, arg4, cb); + // which is convenient for some things like sadd + if (Array.isArray(args[args.length - 1])) { + args = args.slice(0, -1).concat(args[args.length - 1]); + } + command_obj = new Command(command, args, false, callback); if ((!this.ready && !this.send_anyway) || !stream.writable) { diff --git a/package.json b/package.json index 50f3251eaa..6ad55ca23d 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { "name" : "redis", - "version" : "0.6.5", + "version" : "0.6.7", "description" : "Redis client library", "author": "Matt Ranney ", "contributors": [ diff --git a/test.js b/test.js index 627f9c2123..7489e0c652 100644 --- a/test.js +++ b/test.js @@ -389,7 +389,7 @@ tests.HMSET_BUFFER_AND_ARRAY = function () { field1 = "buffer", value1 = new Buffer("abcdefghij"), field2 = "array", - value2 = [], + value2 = ["array contents"], name = "HSET"; client.HMSET(key, field1, value1, field2, value2, last(name, require_string("OK", name))); @@ -695,6 +695,20 @@ tests.SADD = function () { client.sadd('set0', 'member0', last(name, require_number(0, name))); }; +tests.SADD2 = function () { + var name = "SADD2"; + + client.del("set0"); + client.sadd("set0", ["member0", "member1", "member2"], require_number(3, name)); + client.smembers("set0", function (err, res) { + assert.strictEqual(res.length, 3); + assert.strictEqual(res[0], "member0"); + assert.strictEqual(res[1], "member1"); + assert.strictEqual(res[2], "member2"); + next(name); + }); +}; + tests.SISMEMBER = function () { var name = "SISMEMBER";