Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(commands): add new command LINSERT #1053

Merged
merged 2 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export * from './llen';
export * from './lpop';
export * from './lpopBuffer';
export * from './lpush';
export * from './linsert';
export * from './lpushx';
export * from './lrange';
export * from './lrem';
Expand Down
25 changes: 25 additions & 0 deletions src/commands/linsert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export function linsert(key, position, pivot, element) {
if (this.data.has(key) && !(this.data.get(key) instanceof Array)) {
throw new Error(`Key ${key} does not contain a list`);
}
const list = this.data.get(key) || [];
const pivotIndex = list.indexOf(pivot);
if (pivotIndex < 0) return -1;
let elementIndex = pivotIndex;
switch (position) {
case `BEFORE`:
elementIndex = pivotIndex;
break;
case `AFTER`:
elementIndex = pivotIndex + 1;
break;
default:
throw new Error(
`The position of the new element must be BEFORE the pivot or AFTER the pivot`
);
}
list.splice(elementIndex, 0, element);
const {length} = list;
this.data.set(key, list);
return length;
}
62 changes: 62 additions & 0 deletions test/commands/linsert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import MockRedis from '../../src';

describe('linsert', () => {
it('should add the value to the list at the correct position', () => {
const redis = new MockRedis({
data: {
foo: ['1'],
},
});

return redis
.linsert('foo', `BEFORE`, 1, 0)
.then(() => expect(redis.data.get('foo')).toEqual(['0', '1']))
.then(() => redis.linsert('foo', `AFTER`, 1, 2))
.then(() => expect(redis.data.get('foo')).toEqual(['0', '1', '2']));
});

it('should return the new length of the list', () => {
let redis = new MockRedis({
data: {},
});

return redis
.linsert('foo', `BEFORE`, 1, 0)
.then((length) => expect(length).toBe(-1))
.then(() => {
redis = new MockRedis({
data: { foo: ['1'] },
});
})
.then(() => redis.linsert('foo', `BEFORE`, 1, 0))
.then((length) => expect(length).toBe(2));
});

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
.linsert('foo', `BEFORE`, 1, 0)
.catch((err) =>
expect(err.message).toBe('Key foo does not contain a list')
);
});

it('should throw an exception if the position is not allowed', () => {
const redis = new MockRedis({
data: {},
});

return redis
.linsert(`foo`, `POSITION_UNKNOWN`, 1, 0)
.catch((err) =>
expect(err.message).toBe(
`The position of the new element must be BEFORE the pivot or AFTER the pivot`
)
);
});
});