Skip to content

Commit

Permalink
enabled adding abritary commands
Browse files Browse the repository at this point in the history
added test and add_command alias

original and special char commands

tweaked code spacing
  • Loading branch information
stockholmux authored and BridgeAR committed Aug 1, 2017
1 parent 4f7f1ad commit 0437aa4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
4 changes: 3 additions & 1 deletion index.js
Expand Up @@ -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');
24 changes: 16 additions & 8 deletions lib/commands.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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
});
}
Expand Down Expand Up @@ -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;
36 changes: 36 additions & 0 deletions 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();
});

0 comments on commit 0437aa4

Please sign in to comment.