Skip to content

Commit de30977

Browse files
committed
Merge pull request #1 from NodeRedis/feature/exists
feat: add .exists() to check if a command exists
2 parents ae56f81 + 9ad83b6 commit de30977

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

README.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,20 @@ commands.list.forEach(function (command) {
2424
});
2525
```
2626

27-
`.hasFlag` is used to check if the command has the flag:
27+
`.exists()` is used to check if the command exists:
28+
29+
```javascript
30+
commands.exists('set') // true
31+
commands.exists('other-command') // false
32+
```
33+
34+
`.hasFlag()` is used to check if the command has the flag:
2835

2936
```javascript
3037
commands.hasFlag('set', 'readonly') // false
3138
```
3239

33-
`.getKeyIndexes` is used to get the indexes of keys in the command arguments:
40+
`.getKeyIndexes()` is used to get the indexes of keys in the command arguments:
3441

3542
```javascript
3643
commands.getKeyIndexes('set', ['key', 'value']) // [0]

index.js

+11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ var commands = require('./commands');
1212
*/
1313
exports.list = Object.keys(commands);
1414

15+
/**
16+
* Check if the command exists
17+
*
18+
* @param {string} commandName - the command name
19+
* @return {boolean} result
20+
* @public
21+
*/
22+
exports.exists = function (commandName) {
23+
return Boolean(commands[commandName]);
24+
};
25+
1526
/**
1627
* Check if the command has the flag
1728
*

test/index.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,23 @@ describe('redis-commands', function () {
2626
});
2727
});
2828

29-
describe('.hasFlag', function () {
29+
describe('.exists()', function () {
30+
it('should return true for existing commands', function () {
31+
expect(commands.exists('set')).to.eql(true);
32+
expect(commands.exists('get')).to.eql(true);
33+
expect(commands.exists('cluster')).to.eql(true);
34+
expect(commands.exists('quit')).to.eql(true);
35+
expect(commands.exists('config')).to.eql(true);
36+
});
37+
38+
it('should return false for non-existing commands', function () {
39+
expect(commands.exists('SET')).to.eql(false);
40+
expect(commands.exists('set get')).to.eql(false);
41+
expect(commands.exists('other-command')).to.eql(false);
42+
});
43+
});
44+
45+
describe('.hasFlag()', function () {
3046
it('should return true if the command has the flag', function () {
3147
expect(commands.hasFlag('set', 'write')).to.eql(true);
3248
expect(commands.hasFlag('set', 'denyoom')).to.eql(true);
@@ -41,7 +57,7 @@ describe('redis-commands', function () {
4157
});
4258
});
4359

44-
describe('.getKeyIndexes', function () {
60+
describe('.getKeyIndexes()', function () {
4561
var index = commands.getKeyIndexes;
4662

4763
it('should return key indexes', function () {

0 commit comments

Comments
 (0)