From 51fdbb7fc2f8b1f6a176528691335aa6ef8a96cf Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Tue, 1 Aug 2017 00:09:19 -0300 Subject: [PATCH] chore: improve new add_command and add documentation --- README.md | 6 ++++++ changelog.md | 4 +++- lib/commands.js | 8 +++---- test/commands/addCommand.spec.js | 36 -------------------------------- test/node_redis.spec.js | 18 ++++++++++++++++ 5 files changed, 31 insertions(+), 41 deletions(-) delete mode 100644 test/commands/addCommand.spec.js diff --git a/README.md b/README.md index 2f01ed8a42..64972bfeb5 100644 --- a/README.md +++ b/README.md @@ -708,6 +708,12 @@ you can use `send_command()` to send arbitrary commands to Redis. All commands are sent as multi-bulk commands. `args` can either be an Array of arguments, or omitted / set to undefined. +## client.add_command(command_name) + +Calling add_command will add a new command to the prototype. The exact command +name will be used when calling using this new command. Using arbitrary arguments +is possible as with any other command. + ## client.connected Boolean tracking the state of the connection to the Redis server. diff --git a/changelog.md b/changelog.md index 3de9fff992..d650f9ba39 100644 --- a/changelog.md +++ b/changelog.md @@ -1,15 +1,17 @@ # Changelog -## v.2.8.0 - 20 Jul, 2017 +## v.2.8.0 - 31 Jul, 2017 Features - Accept UPPER_CASE commands in send_command +- Add arbitrary commands to the prototype by using `Redis.addCommand(name)` Bugfixes - Fixed not always copying subscribe unsubscribe arguments - Fixed emitting internal errors while reconnecting with auth +- Fixed crashing with invalid url option ## v.2.7.1 - 14 Mar, 2017 diff --git a/lib/commands.js b/lib/commands.js index 0800ea0b8b..6275ec8bf6 100644 --- a/lib/commands.js +++ b/lib/commands.js @@ -57,12 +57,12 @@ var addCommand = function (command) { } return this.internal_send_command(new Command(command, arr, callback)); }; - //alias commands with illegal function names (e.g. NR.RUN becomes NR_RUN and nr_run) + // Alias special function names (e.g. NR.RUN becomes NR_RUN and nr_run) if (commandName !== command) { RedisClient.prototype[commandName.toUpperCase()] = RedisClient.prototype[commandName] = RedisClient.prototype[command]; } if (changeFunctionName) { - Object.defineProperty(RedisClient.prototype[commandName], 'name', { + Object.defineProperty(RedisClient.prototype[command], 'name', { value: commandName }); } @@ -104,12 +104,12 @@ var addCommand = function (command) { this.queue.push(new Command(command, arr, callback)); return this; }; - //alias commands with illegal function names (e.g. NR.RUN becomes NR_RUN and nr_run) + // Alias special function names (e.g. NR.RUN becomes NR_RUN and nr_run) if (commandName !== command) { Multi.prototype[commandName.toUpperCase()] = Multi.prototype[commandName] = Multi.prototype[command]; } if (changeFunctionName) { - Object.defineProperty(Multi.prototype[commandName], 'name', { + Object.defineProperty(Multi.prototype[command], 'name', { value: commandName }); } diff --git a/test/commands/addCommand.spec.js b/test/commands/addCommand.spec.js deleted file mode 100644 index 7a0ce4df58..0000000000 --- a/test/commands/addCommand.spec.js +++ /dev/null @@ -1,36 +0,0 @@ -'use strict'; - -var config = require('../lib/config'); -var redis = config.redis; -var assert = require('assert'); - -describe("The 'addCommand/add_command' method", function () { - var client = redis.createClient(); - var testCommands = { - newcommand : 'newcommand', - nonJsSafe : 'really-new.command', - jsSafe : 'really_new_command' - }; - - it('camel case version exists', function () { - assert.strictEqual(typeof redis.addCommand, 'function'); - }); - it('snake version exists', function () { - assert.strictEqual(typeof redis.add_command, 'function'); - }); - it('does not already have the test standard command', function () { - assert.strictEqual(client[testCommands.newcommand], undefined); - }); - it('generates a new method for an added command', function () { - redis.addCommand(testCommands.newcommand); - assert.strictEqual(typeof client[testCommands.newcommand], 'function'); - }); - it('does not already have the test non-JS-safe command', function () { - assert.strictEqual(client[testCommands.nonJsSafe], undefined); - }); - it('converts illegal command names to JS-safe functions', function () { - redis.addCommand(testCommands.nonJsSafe); - assert.strictEqual(typeof client[testCommands.jsSafe], 'function'); - }); - client.quit(); -}); diff --git a/test/node_redis.spec.js b/test/node_redis.spec.js index d9fbc09728..758e5c89d9 100644 --- a/test/node_redis.spec.js +++ b/test/node_redis.spec.js @@ -12,6 +12,24 @@ var client; describe('The node_redis client', function () { + describe.only("The 'add_command' method", function () { + + it('camel case and snakeCase version exists', function () { + assert.strictEqual(typeof redis.addCommand, 'function'); + assert.strictEqual(typeof redis.add_command, 'function'); + }); + + it('converts special characters in functions names to lowercase', function () { + var command = 'really-new.command'; + assert.strictEqual(redis.prototype[command], undefined); + redis.addCommand(command); + assert.strictEqual(redis.prototype[command].name, 'really_new_command'); + assert.strictEqual(redis.prototype[command.toUpperCase()].name, 'really_new_command'); + assert.strictEqual(redis.prototype.really_new_command.name, 'really_new_command'); + assert.strictEqual(redis.prototype.REALLY_NEW_COMMAND.name, 'really_new_command'); + }); + }); + it('individual commands sanity check', function (done) { // All commands should work the same in multi context or without // Therefor individual commands always have to be handled in both cases