Skip to content

Commit

Permalink
feat: implement ltrim command (stipsan#709)
Browse files Browse the repository at this point in the history
  • Loading branch information
DarrenOne authored and stipsan committed Apr 1, 2019
1 parent 0790e7c commit cdc4774
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compat.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
| [lrange](http://redis.io/commands/LRANGE) | :white_check_mark: | :white_check_mark: |
| [lrem](http://redis.io/commands/LREM) | :white_check_mark: | :white_check_mark: |
| [lset](http://redis.io/commands/LSET) | :white_check_mark: | :white_check_mark: |
| [ltrim](http://redis.io/commands/LTRIM) | :white_check_mark: | :x: |
| [ltrim](http://redis.io/commands/LTRIM) | :white_check_mark: | :white_check_mark: |
| [memory](http://redis.io/commands/MEMORY) | :white_check_mark: | :x: |
| [mget](http://redis.io/commands/MGET) | :white_check_mark: | :white_check_mark: |
| [migrate](http://redis.io/commands/MIGRATE) | :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 @@ -51,6 +51,7 @@ export * from './lpushx';
export * from './lrange';
export * from './lrem';
export * from './lset';
export * from './ltrim';
export * from './mget';
export * from './mset';
export * from './msetnx';
Expand Down
19 changes: 19 additions & 0 deletions src/commands/ltrim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function ltrim(key, s, e) {
if (this.data.has(key) && !(this.data.get(key) instanceof Array)) {
throw new Error(`Key ${key} does not contain a list`);
}
let start = parseInt(s, 10);
let end = parseInt(e, 10);

const list = this.data.get(key) || [];

if (start < 0) {
start = list.length + start;
}
if (end < 0) {
end = list.length + end;
}

this.data.set(key, list.slice(start, end + 1));
return 'OK';
}
77 changes: 77 additions & 0 deletions test/commands/ltrim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import expect from 'expect';

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

describe('ltrim', () => {
it('should return first 3 items', () => {
const redis = new MockRedis({
data: {
foo: ['1', '2', '3', '4', '5'],
},
});

return Promise.all([
redis.ltrim('foo', 0, 2).then(res => expect(res).toEqual('OK')),
redis
.lrange('foo', 0, -1)
.then(res => expect(res).toEqual(['1', '2', '3'])),
]);
});

it('should return last 3 items', () => {
const redis = new MockRedis({
data: {
foo: ['1', '2', '3', '4', '5'],
},
});

return Promise.all([
redis.ltrim('foo', -3, -1).then(res => expect(res).toEqual('OK')),
redis
.lrange('foo', 0, -1)
.then(res => expect(res).toEqual(['3', '4', '5'])),
]);
});

it('should return last all items on larger numbers', () => {
const redis = new MockRedis({
data: {
foo: ['1', '2', '3', '4', '5'],
},
});

return Promise.all([
redis.ltrim('foo', 0, 100).then(res => expect(res).toEqual('OK')),
redis
.lrange('foo', 0, -1)
.then(res => expect(res).toEqual(['1', '2', '3', '4', '5'])),
]);
});

it('should return empty array if out-of-range', () => {
const redis = new MockRedis({
data: {
foo: ['1', '2', '3', '4', '5'],
},
});

return Promise.all([
redis.ltrim('foo', 10, 100).then(res => expect(res).toEqual('OK')),
redis.lrange('foo', 0, -1).then(res => expect(res).toEqual([])),
]);
});

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

return redis
.ltrim('foo', 0, 2)
.catch(err =>
expect(err.message).toBe('Key foo does not contain a list')
);
});
});

0 comments on commit cdc4774

Please sign in to comment.