Skip to content

Commit

Permalink
feat: add zscore command (stipsan#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylewm authored and stipsan committed Oct 12, 2018
1 parent 54feb35 commit 0c69f9b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compat.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,5 @@
| [zrevrangebyscore](http://redis.io/commands/ZREVRANGEBYSCORE) | :white_check_mark: | :white_check_mark: |
| [zrevrank](http://redis.io/commands/ZREVRANK) | :white_check_mark: | :x: |
| [zscan](http://redis.io/commands/ZSCAN) | :white_check_mark: | :white_check_mark: |
| [zscore](http://redis.io/commands/ZSCORE) | :white_check_mark: | :x: |
| [zscore](http://redis.io/commands/ZSCORE) | :white_check_mark: | :white_check_mark: |
| [zunionstore](http://redis.io/commands/ZUNIONSTORE) | :white_check_mark: | :x: |
1 change: 1 addition & 0 deletions src/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,4 @@ export * from './zremrangebyscore';
export * from './zrevrange';
export * from './zrevrangebyscore';
export * from './zscan';
export * from './zscore';
18 changes: 18 additions & 0 deletions src/commands/zscore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Map from 'es6-map';

export function zscore(key, member) {
const map = this.data.get(key);

// @TODO investigate a more stable way to detect sorted lists
if (!map || !(map instanceof Map)) {
return null;
}

const entry = map.get(member);

if (!entry) {
return null;
}

return entry.score.toString();
}
41 changes: 41 additions & 0 deletions test/commands/zscore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Map from 'es6-map';
import expect from 'expect';

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

describe('zscore', () => {
const data = {
foo: new Map([
['first', { score: 1, value: 'first' }],
['second', { score: 2, value: 'second' }],
['third', { score: 3, value: 'third' }],
['fourth', { score: 4, value: 'fourth' }],
['fifth', { score: 5, value: 'fifth' }],
]),
bar: 'not a sorted set',
};

it('should return the score of an existing member as a string', () => {
const redis = new MockRedis({ data });

return redis.zscore('foo', 'third').then(res => expect(res).toBe('3'));
});

it('should return null when the member does not exist', () => {
const redis = new MockRedis({ data });

return redis.zscore('foo', 'sixth').then(res => expect(res).toNotExist());
});

it('should return null when the key is not a sorted set', () => {
const redis = new MockRedis({ data });

return redis.zscore('bar', 'first').then(res => expect(res).toNotExist());
});

it('should return null when the key does not exist', () => {
const redis = new MockRedis({ data });

return redis.zscore('baz', 'first').then(res => expect(res).toNotExist());
});
});

0 comments on commit 0c69f9b

Please sign in to comment.