Skip to content

Commit 8a20d66

Browse files
uki00akeroxp
authored andcommitted
fix: improve signature of spop (#31)
* fix: improve signature of spop * fix: add tests for spop
1 parent 986461e commit 8a20d66

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

redis.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ export type Redis = {
232232
sismember(key: string, member: string): Promise<number>;
233233
smembers(key: string): Promise<string[]>;
234234
smove(source: string, destination: string, member: string): Promise<number>;
235-
spop(key: string, count?: number): Promise<string>;
235+
spop(key: string): Promise<string>;
236+
spop(key: string, count: number): Promise<string[]>;
236237
srandmember(key: string, count?: number): Promise<string>;
237238
srem(key: string, ...members: string[]): Promise<number>;
238239
sunion(...keys: string[]): Promise<string[]>;
@@ -1266,7 +1267,9 @@ class RedisImpl implements Redis {
12661267
}
12671268
}
12681269

1269-
spop(...args) {
1270+
spop(key: string): Promise<string>;
1271+
spop(key: string, count: number): Promise<string[]>;
1272+
spop(...args): Promise<string | string[]> {
12701273
return this.execStatusReply("SPOP", ...args);
12711274
}
12721275

redis_test.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
} from "./vendor/https/deno.land/std/testing/mod.ts";
77
import {
88
assertEquals,
9-
assertThrowsAsync
9+
assertThrowsAsync,
10+
assertArrayContains
1011
} from "./vendor/https/deno.land/std/testing/asserts.ts";
1112
// can be substituted with env variable
1213
const addr = {
@@ -25,7 +26,9 @@ test(async function beforeAll() {
2526
"get",
2627
"getset",
2728
"del1",
28-
"del2"
29+
"del2",
30+
"spop",
31+
"spopWithCount"
2932
);
3033
});
3134

@@ -97,6 +100,18 @@ test(async function testDecrby() {
97100
assertEquals(await redis.get("decryby"), "-101");
98101
});
99102

103+
test(async function testSpop() {
104+
await redis.sadd("spop", "a");
105+
const v = await redis.spop("spop");
106+
assertEquals(v, "a");
107+
});
108+
109+
test(async function testSpopWithCount() {
110+
await redis.sadd("spopWithCount", "a", "b");
111+
const v = await redis.spop("spopWithCount", 2);
112+
assertArrayContains(v, ["a", "b"]);
113+
});
114+
100115
test(async function testConcurrent() {
101116
let promises: Promise<any>[] = [];
102117
for (const key of ["a", "b", "c"]) {

0 commit comments

Comments
 (0)