Skip to content

Commit 57c8afb

Browse files
committed
fix)commands): add count argument to lpop and remove key when list is empty
1 parent 6a58955 commit 57c8afb

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

src/commands/lpop.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,40 @@
11
import { convertStringToBuffer } from '../commands-utils/convertStringToBuffer'
22

3-
export function lpop(key) {
3+
export function lpop(key, count) {
44
if (this.data.has(key) && !(this.data.get(key) instanceof Array)) {
55
throw new Error(`Key ${key} does not contain a list`)
66
}
77
const list = this.data.get(key) || []
88

9-
const item = list.length > 0 ? list.shift() : null
9+
if (list.length === 0) {
10+
return null
11+
}
12+
13+
if (count) {
14+
const items = list.slice(0, count)
15+
const remainingItems = list.slice(count)
1016

11-
this.data.set(key, list)
17+
if (remainingItems.length > 0) {
18+
this.data.set(key, remainingItems)
19+
} else {
20+
this.data.delete(key)
21+
}
22+
23+
return items
24+
}
25+
26+
const item = list.shift()
27+
28+
if (list.length > 0) {
29+
this.data.set(key, list)
30+
} else {
31+
this.data.delete(key)
32+
}
1233

1334
return item
1435
}
1536

16-
export function lpopBuffer(key) {
17-
const val = lpop.apply(this, [key])
37+
export function lpopBuffer(key, count) {
38+
const val = lpop.apply(this, [key, count])
1839
return convertStringToBuffer(val)
1940
}

test/integration/commands/lpop.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,56 @@ runTwinSuite('lpop', (command, equals) => {
2121
}
2222
)
2323

24+
// @TODO Rewrite test so it runs on a real Redis instance
25+
;(process.env.IS_E2E ? it.skip : it)(
26+
'should remove key and return first elements when it was last on the list',
27+
() => {
28+
const redis = new Redis({
29+
data: {
30+
foo: ['1'],
31+
},
32+
})
33+
34+
return redis[command]('foo')
35+
.then(result => expect(equals(result, '1')).toBe(true))
36+
.then(() => redis.exists('foo'))
37+
.then(status => expect(status).toBe(0))
38+
}
39+
)
40+
41+
// @TODO Rewrite test so it runs on a real Redis instance
42+
;(process.env.IS_E2E ? it.skip : it)(
43+
'should remove and return "count" elements if second argument is provided',
44+
() => {
45+
const redis = new Redis({
46+
data: {
47+
foo: ['5', '4', '3', '2', '1'],
48+
},
49+
})
50+
51+
return redis[command]('foo', 2)
52+
.then(result => expect(result.map(v => Buffer.isBuffer(v) ? v.toString() : v)).toEqual(['5', '4']))
53+
.then(() => expect(redis.data.get('foo')).toEqual(['3', '2', '1']))
54+
}
55+
)
56+
57+
// @TODO Rewrite test so it runs on a real Redis instance
58+
;(process.env.IS_E2E ? it.skip : it)(
59+
'should remove key and return all elements on larger number if second argument is provided',
60+
() => {
61+
const redis = new Redis({
62+
data: {
63+
foo: ['5', '4', '3', '2', '1'],
64+
},
65+
})
66+
67+
return redis[command]('foo', 7)
68+
.then(result => expect(result.map(v => Buffer.isBuffer(v) ? v.toString() : v)).toEqual(['5', '4', '3', '2', '1']))
69+
.then(() => redis.exists('foo'))
70+
.then(status => expect(status).toBe(0))
71+
}
72+
)
73+
2474
// @TODO Rewrite test so it runs on a real Redis instance
2575
;(process.env.IS_E2E ? it.skip : it)(
2676
'should return buffer values correctly as buffer',

0 commit comments

Comments
 (0)