Skip to content

Commit

Permalink
feat: Implement ZREMRANGEBYSCORE (stipsan#539)
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-codaio authored and stipsan committed Aug 25, 2018
1 parent e9a263c commit dc124d5
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compat.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
| [zrem](http://redis.io/commands/ZREM) | :white_check_mark: | :white_check_mark: |
| [zremrangebylex](http://redis.io/commands/ZREMRANGEBYLEX) | :white_check_mark: | :x: |
| [zremrangebyrank](http://redis.io/commands/ZREMRANGEBYRANK) | :white_check_mark: | :white_check_mark: |
| [zremrangebyscore](http://redis.io/commands/ZREMRANGEBYSCORE) | :white_check_mark: | :x: |
| [zremrangebyscore](http://redis.io/commands/ZREMRANGEBYSCORE) | :white_check_mark: | :white_check_mark: |
| [zrevrange](http://redis.io/commands/ZREVRANGE) | :white_check_mark: | :white_check_mark: |
| [zrevrangebylex](http://redis.io/commands/ZREVRANGEBYLEX) | :white_check_mark: | :x: |
| [zrevrangebyscore](http://redis.io/commands/ZREVRANGEBYSCORE) | :white_check_mark: | :white_check_mark: |
Expand Down
1 change: 1 addition & 0 deletions src/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export * from './zrange';
export * from './zrangebyscore';
export * from './zrem';
export * from './zremrangebyrank';
export * from './zremrangebyscore';
export * from './zrevrange';
export * from './zrevrangebyscore';
export * from './zscan';
13 changes: 13 additions & 0 deletions src/commands/zremrangebyscore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { zrevrangebyscore } from './index';

export function zremrangebyscore(key, inputMax, inputMin) {
const vals = zrevrangebyscore.call(this, key, inputMax, inputMin);

const map = this.data.get(key);
vals.forEach(val => {
map.delete(val);
});
this.data.set(key, map);

return vals.length;
}
89 changes: 89 additions & 0 deletions test/commands/zremrangebyscore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import Map from 'es6-map';
import expect from 'expect';

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

describe('zremrangebyscore', () => {
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' }],
]),
};

it('should remove using not strict compare', () => {
const redis = new MockRedis({ data });

return redis
.zremrangebyscore('foo', 3, 1)
.then(res => expect(res).toBe(3))
.then(() => {
expect(redis.data.get('foo').has('first')).toBe(false);
expect(redis.data.get('foo').has('second')).toBe(false);
expect(redis.data.get('foo').has('third')).toBe(false);
expect(redis.data.get('foo').has('fourth')).toBe(true);
expect(redis.data.get('foo').has('fifth')).toBe(true);
});
});

it('should return using strict compare', () => {
const redis = new MockRedis({ data });

return redis
.zremrangebyscore('foo', 5, '(3')
.then(res => expect(res).toEqual(2))
.then(() => {
expect(redis.data.get('foo').has('first')).toBe(true);
expect(redis.data.get('foo').has('second')).toBe(true);
expect(redis.data.get('foo').has('third')).toBe(true);
expect(redis.data.get('foo').has('fourth')).toBe(false);
expect(redis.data.get('foo').has('fifth')).toBe(false);
});
});

it('should accept infinity string', () => {
const redis = new MockRedis({ data });

return redis
.zremrangebyscore('foo', '+inf', '-inf')
.then(res => expect(res).toEqual(5))
.then(() => {
expect(redis.data.get('foo').has('first')).toBe(false);
expect(redis.data.get('foo').has('second')).toBe(false);
expect(redis.data.get('foo').has('third')).toBe(false);
expect(redis.data.get('foo').has('fourth')).toBe(false);
expect(redis.data.get('foo').has('fifth')).toBe(false);
});
});

it('should return zero if out-of-range', () => {
const redis = new MockRedis({ data });

return redis
.zremrangebyscore('foo', 100, 10)
.then(res => expect(res).toEqual(0));
});

it('should return zero if key not found', () => {
const redis = new MockRedis({ data });

return redis
.zremrangebyscore('boo', 100, 10)
.then(res => expect(res).toEqual(0));
});

it('should return zero if the key contains something other than a list', () => {
const redis = new MockRedis({
data: {
foo: 'not a list',
},
});

return redis
.zremrangebyscore('foo', 2, 1)
.then(res => expect(res).toEqual(0));
});
});

0 comments on commit dc124d5

Please sign in to comment.