Skip to content

Commit

Permalink
Merge pull request #1 from NodeRedis/feature/exists
Browse files Browse the repository at this point in the history
feat: add .exists() to check if a command exists
  • Loading branch information
BridgeAR committed Feb 9, 2016
2 parents ae56f81 + 9ad83b6 commit de30977
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,20 @@ commands.list.forEach(function (command) {
});
```

`.hasFlag` is used to check if the command has the flag:
`.exists()` is used to check if the command exists:

```javascript
commands.exists('set') // true
commands.exists('other-command') // false
```

`.hasFlag()` is used to check if the command has the flag:

```javascript
commands.hasFlag('set', 'readonly') // false
```

`.getKeyIndexes` is used to get the indexes of keys in the command arguments:
`.getKeyIndexes()` is used to get the indexes of keys in the command arguments:

```javascript
commands.getKeyIndexes('set', ['key', 'value']) // [0]
Expand Down
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ var commands = require('./commands');
*/
exports.list = Object.keys(commands);

/**
* Check if the command exists
*
* @param {string} commandName - the command name
* @return {boolean} result
* @public
*/
exports.exists = function (commandName) {
return Boolean(commands[commandName]);
};

/**
* Check if the command has the flag
*
Expand Down
20 changes: 18 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,23 @@ describe('redis-commands', function () {
});
});

describe('.hasFlag', function () {
describe('.exists()', function () {
it('should return true for existing commands', function () {
expect(commands.exists('set')).to.eql(true);
expect(commands.exists('get')).to.eql(true);
expect(commands.exists('cluster')).to.eql(true);
expect(commands.exists('quit')).to.eql(true);
expect(commands.exists('config')).to.eql(true);
});

it('should return false for non-existing commands', function () {
expect(commands.exists('SET')).to.eql(false);
expect(commands.exists('set get')).to.eql(false);
expect(commands.exists('other-command')).to.eql(false);
});
});

describe('.hasFlag()', function () {
it('should return true if the command has the flag', function () {
expect(commands.hasFlag('set', 'write')).to.eql(true);
expect(commands.hasFlag('set', 'denyoom')).to.eql(true);
Expand All @@ -41,7 +57,7 @@ describe('redis-commands', function () {
});
});

describe('.getKeyIndexes', function () {
describe('.getKeyIndexes()', function () {
var index = commands.getKeyIndexes;

it('should return key indexes', function () {
Expand Down

0 comments on commit de30977

Please sign in to comment.