From 0437aa49851365bdf82711b7ff6ac7dc369ec7bb Mon Sep 17 00:00:00 2001 From: Kyle Davis Date: Thu, 17 Nov 2016 15:13:57 -0500 Subject: [PATCH] enabled adding abritary commands added test and add_command alias original and special char commands tweaked code spacing --- index.js | 4 +++- lib/commands.js | 24 ++++++++++++++------- test/commands/addCommand.spec.js | 36 ++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 test/commands/addCommand.spec.js diff --git a/index.js b/index.js index bf73b5c9e4..58fcf84c6c 100644 --- a/index.js +++ b/index.js @@ -1100,4 +1100,6 @@ exports.AggregateError = errorClasses.AggregateError; // Add all redis commands / node_redis api to the client require('./lib/individualCommands'); require('./lib/extendedApi'); -require('./lib/commands'); + +//enables adding new commands (for modules and new commands) +exports.addCommand = exports.add_command = require('./lib/commands'); \ No newline at end of file diff --git a/lib/commands.js b/lib/commands.js index 6ca01df2c7..0800ea0b8b 100644 --- a/lib/commands.js +++ b/lib/commands.js @@ -17,11 +17,7 @@ var changeFunctionName = (function () { } }()); -// TODO: Rewrite this including the invidual commands into a Commands class -// that provided a functionality to add new commands to the client - -commands.list.forEach(function (command) { - +var addCommand = function (command) { // Some rare Redis commands use special characters in their command name // Convert those to a underscore to prevent using invalid function names var commandName = command.replace(/(?:^([0-9])|[^a-zA-Z0-9_$])/g, '_$1'); @@ -61,8 +57,12 @@ commands.list.forEach(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) + if (commandName !== command) { + RedisClient.prototype[commandName.toUpperCase()] = RedisClient.prototype[commandName] = RedisClient.prototype[command]; + } if (changeFunctionName) { - Object.defineProperty(RedisClient.prototype[command], 'name', { + Object.defineProperty(RedisClient.prototype[commandName], 'name', { value: commandName }); } @@ -104,10 +104,18 @@ commands.list.forEach(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) + if (commandName !== command) { + Multi.prototype[commandName.toUpperCase()] = Multi.prototype[commandName] = Multi.prototype[command]; + } if (changeFunctionName) { - Object.defineProperty(Multi.prototype[command], 'name', { + Object.defineProperty(Multi.prototype[commandName], 'name', { value: commandName }); } } -}); +}; + +commands.list.forEach(addCommand); + +module.exports = addCommand; diff --git a/test/commands/addCommand.spec.js b/test/commands/addCommand.spec.js new file mode 100644 index 0000000000..7a0ce4df58 --- /dev/null +++ b/test/commands/addCommand.spec.js @@ -0,0 +1,36 @@ +'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(); +});