Skip to content

Commit 51fdbb7

Browse files
committed
chore: improve new add_command and add documentation
1 parent 0437aa4 commit 51fdbb7

File tree

5 files changed

+31
-41
lines changed

5 files changed

+31
-41
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,12 @@ you can use `send_command()` to send arbitrary commands to Redis.
708708

709709
All commands are sent as multi-bulk commands. `args` can either be an Array of arguments, or omitted / set to undefined.
710710

711+
## client.add_command(command_name)
712+
713+
Calling add_command will add a new command to the prototype. The exact command
714+
name will be used when calling using this new command. Using arbitrary arguments
715+
is possible as with any other command.
716+
711717
## client.connected
712718

713719
Boolean tracking the state of the connection to the Redis server.

changelog.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
# Changelog
22

3-
## v.2.8.0 - 20 Jul, 2017
3+
## v.2.8.0 - 31 Jul, 2017
44

55
Features
66

77
- Accept UPPER_CASE commands in send_command
8+
- Add arbitrary commands to the prototype by using `Redis.addCommand(name)`
89

910
Bugfixes
1011

1112
- Fixed not always copying subscribe unsubscribe arguments
1213
- Fixed emitting internal errors while reconnecting with auth
14+
- Fixed crashing with invalid url option
1315

1416
## v.2.7.1 - 14 Mar, 2017
1517

lib/commands.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ var addCommand = function (command) {
5757
}
5858
return this.internal_send_command(new Command(command, arr, callback));
5959
};
60-
//alias commands with illegal function names (e.g. NR.RUN becomes NR_RUN and nr_run)
60+
// Alias special function names (e.g. NR.RUN becomes NR_RUN and nr_run)
6161
if (commandName !== command) {
6262
RedisClient.prototype[commandName.toUpperCase()] = RedisClient.prototype[commandName] = RedisClient.prototype[command];
6363
}
6464
if (changeFunctionName) {
65-
Object.defineProperty(RedisClient.prototype[commandName], 'name', {
65+
Object.defineProperty(RedisClient.prototype[command], 'name', {
6666
value: commandName
6767
});
6868
}
@@ -104,12 +104,12 @@ var addCommand = 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)
107+
// Alias special function names (e.g. NR.RUN becomes NR_RUN and nr_run)
108108
if (commandName !== command) {
109109
Multi.prototype[commandName.toUpperCase()] = Multi.prototype[commandName] = Multi.prototype[command];
110110
}
111111
if (changeFunctionName) {
112-
Object.defineProperty(Multi.prototype[commandName], 'name', {
112+
Object.defineProperty(Multi.prototype[command], 'name', {
113113
value: commandName
114114
});
115115
}

test/commands/addCommand.spec.js

Lines changed: 0 additions & 36 deletions
This file was deleted.

test/node_redis.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@ var client;
1212

1313
describe('The node_redis client', function () {
1414

15+
describe.only("The 'add_command' method", function () {
16+
17+
it('camel case and snakeCase version exists', function () {
18+
assert.strictEqual(typeof redis.addCommand, 'function');
19+
assert.strictEqual(typeof redis.add_command, 'function');
20+
});
21+
22+
it('converts special characters in functions names to lowercase', function () {
23+
var command = 'really-new.command';
24+
assert.strictEqual(redis.prototype[command], undefined);
25+
redis.addCommand(command);
26+
assert.strictEqual(redis.prototype[command].name, 'really_new_command');
27+
assert.strictEqual(redis.prototype[command.toUpperCase()].name, 'really_new_command');
28+
assert.strictEqual(redis.prototype.really_new_command.name, 'really_new_command');
29+
assert.strictEqual(redis.prototype.REALLY_NEW_COMMAND.name, 'really_new_command');
30+
});
31+
});
32+
1533
it('individual commands sanity check', function (done) {
1634
// All commands should work the same in multi context or without
1735
// Therefor individual commands always have to be handled in both cases

0 commit comments

Comments
 (0)