Skip to content

Commit

Permalink
feat: implement zcard command stipsan#702 (stipsan#732)
Browse files Browse the repository at this point in the history
  • Loading branch information
ljht authored and stipsan committed May 13, 2019
1 parent 2abdf23 commit 3b36191
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compat.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
| [xreadgroup](http://redis.io/commands/XREADGROUP) | :x: | :x: |
| [xrevrange](http://redis.io/commands/XREVRANGE) | :x: | :x: |
| [zadd](http://redis.io/commands/ZADD) | :white_check_mark: | :white_check_mark: |
| [zcard](http://redis.io/commands/ZCARD) | :white_check_mark: | :x: |
| [zcard](http://redis.io/commands/ZCARD) | :white_check_mark: | :white_check_mark: |
| [zcount](http://redis.io/commands/ZCOUNT) | :white_check_mark: | :x: |
| [zincrby](http://redis.io/commands/ZINCRBY) | :white_check_mark: | :white_check_mark: |
| [zinterstore](http://redis.io/commands/ZINTERSTORE) | :white_check_mark: | :x: |
Expand Down
1 change: 1 addition & 0 deletions src/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export * from './xrange';
export * from './xread';
export * from './xrevrange';
export * from './zadd';
export * from './zcard';
export * from './zcount';
export * from './zincrby';
export * from './zrange';
Expand Down
12 changes: 12 additions & 0 deletions src/commands/zcard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Map from 'es6-map';

export function zcard(key) {
const map = this.data.get(key);
if (!map) {
return 0;
}
if (!(map instanceof Map)) {
throw new Error(`Key ${key} does not contain a sorted set`);
}
return this.data.get(key).size;
}
36 changes: 36 additions & 0 deletions test/commands/zcard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import expect from 'expect';
import Map from 'es6-map';

import MockRedis from '../../src';

describe('zcard', () => {
it('should return the number of items in the sorted set', () => {
const redis = new MockRedis({
data: {
foo: new Map([[1, 'one'], [3, 'three'], [4, 'four']]),
},
});

return redis.zcard('foo').then(length => expect(length).toBe(3));
});

it('should return 0 if the sorted set does not exist', () => {
const redis = new MockRedis();

return redis.zcard('foo').then(length => expect(length).toBe(0));
});

it('should throw an exception if the key contains something other than a sorted set', () => {
const redis = new MockRedis({
data: {
foo: 'not a sorted set',
},
});

return redis
.zcard('foo')
.catch(err =>
expect(err.message).toBe('Key foo does not contain a sorted set')
);
});
});

0 comments on commit 3b36191

Please sign in to comment.