Skip to content

Commit 0437aa4

Browse files
stockholmuxBridgeAR
authored andcommitted
enabled adding abritary commands
added test and add_command alias original and special char commands tweaked code spacing
1 parent 4f7f1ad commit 0437aa4

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,4 +1100,6 @@ exports.AggregateError = errorClasses.AggregateError;
11001100
// Add all redis commands / node_redis api to the client
11011101
require('./lib/individualCommands');
11021102
require('./lib/extendedApi');
1103-
require('./lib/commands');
1103+
1104+
//enables adding new commands (for modules and new commands)
1105+
exports.addCommand = exports.add_command = require('./lib/commands');

lib/commands.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ var changeFunctionName = (function () {
1717
}
1818
}());
1919

20-
// TODO: Rewrite this including the invidual commands into a Commands class
21-
// that provided a functionality to add new commands to the client
22-
23-
commands.list.forEach(function (command) {
24-
20+
var addCommand = function (command) {
2521
// Some rare Redis commands use special characters in their command name
2622
// Convert those to a underscore to prevent using invalid function names
2723
var commandName = command.replace(/(?:^([0-9])|[^a-zA-Z0-9_$])/g, '_$1');
@@ -61,8 +57,12 @@ commands.list.forEach(function (command) {
6157
}
6258
return this.internal_send_command(new Command(command, arr, callback));
6359
};
60+
//alias commands with illegal function names (e.g. NR.RUN becomes NR_RUN and nr_run)
61+
if (commandName !== command) {
62+
RedisClient.prototype[commandName.toUpperCase()] = RedisClient.prototype[commandName] = RedisClient.prototype[command];
63+
}
6464
if (changeFunctionName) {
65-
Object.defineProperty(RedisClient.prototype[command], 'name', {
65+
Object.defineProperty(RedisClient.prototype[commandName], 'name', {
6666
value: commandName
6767
});
6868
}
@@ -104,10 +104,18 @@ commands.list.forEach(function (command) {
104104
this.queue.push(new Command(command, arr, callback));
105105
return this;
106106
};
107+
//alias commands with illegal function names (e.g. NR.RUN becomes NR_RUN and nr_run)
108+
if (commandName !== command) {
109+
Multi.prototype[commandName.toUpperCase()] = Multi.prototype[commandName] = Multi.prototype[command];
110+
}
107111
if (changeFunctionName) {
108-
Object.defineProperty(Multi.prototype[command], 'name', {
112+
Object.defineProperty(Multi.prototype[commandName], 'name', {
109113
value: commandName
110114
});
111115
}
112116
}
113-
});
117+
};
118+
119+
commands.list.forEach(addCommand);
120+
121+
module.exports = addCommand;

test/commands/addCommand.spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
var config = require('../lib/config');
4+
var redis = config.redis;
5+
var assert = require('assert');
6+
7+
describe("The 'addCommand/add_command' method", function () {
8+
var client = redis.createClient();
9+
var testCommands = {
10+
newcommand : 'newcommand',
11+
nonJsSafe : 'really-new.command',
12+
jsSafe : 'really_new_command'
13+
};
14+
15+
it('camel case version exists', function () {
16+
assert.strictEqual(typeof redis.addCommand, 'function');
17+
});
18+
it('snake version exists', function () {
19+
assert.strictEqual(typeof redis.add_command, 'function');
20+
});
21+
it('does not already have the test standard command', function () {
22+
assert.strictEqual(client[testCommands.newcommand], undefined);
23+
});
24+
it('generates a new method for an added command', function () {
25+
redis.addCommand(testCommands.newcommand);
26+
assert.strictEqual(typeof client[testCommands.newcommand], 'function');
27+
});
28+
it('does not already have the test non-JS-safe command', function () {
29+
assert.strictEqual(client[testCommands.nonJsSafe], undefined);
30+
});
31+
it('converts illegal command names to JS-safe functions', function () {
32+
redis.addCommand(testCommands.nonJsSafe);
33+
assert.strictEqual(typeof client[testCommands.jsSafe], 'function');
34+
});
35+
client.quit();
36+
});

0 commit comments

Comments
 (0)