Skip to content

Commit

Permalink
chore: improve new add_command and add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
BridgeAR committed Aug 1, 2017
1 parent 0437aa4 commit 51fdbb7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 41 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion 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

Expand Down
8 changes: 4 additions & 4 deletions lib/commands.js
Expand Up @@ -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
});
}
Expand Down Expand Up @@ -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
});
}
Expand Down
36 changes: 0 additions & 36 deletions test/commands/addCommand.spec.js

This file was deleted.

18 changes: 18 additions & 0 deletions test/node_redis.spec.js
Expand Up @@ -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
Expand Down

0 comments on commit 51fdbb7

Please sign in to comment.