Skip to content

Commit 8e8db4f

Browse files
uki00akeroxp
authored andcommitted
fix: zrange with 'withScore' option (#40)
1 parent 01a6f98 commit 8e8db4f

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

redis.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1449,7 +1449,7 @@ class RedisImpl implements Redis {
14491449
private pushZrangeOpts(args, opts?) {
14501450
if (opts) {
14511451
if (opts.withScore) {
1452-
args.push("WITHSCORE");
1452+
args.push("WITHSCORES");
14531453
}
14541454
if (opts.offset !== void 0 && opts.count !== void 0) {
14551455
args.push("LIMIT", opts.offset, opts.count);

redis_test.ts

+77-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,15 @@ test(async function beforeAll() {
2828
"del1",
2929
"del2",
3030
"spop",
31-
"spopWithCount"
31+
"spopWithCount",
32+
"zrange",
33+
"zrangeWithScores",
34+
"zrevrange",
35+
"zrevrangeWithScores",
36+
"zrangebyscore",
37+
"zrangebyscoreWithScores",
38+
"zrevrangebyscore",
39+
"zrevrangebyscoreWithScores"
3240
);
3341
});
3442

@@ -112,6 +120,74 @@ test(async function testSpopWithCount() {
112120
assertArrayContains(v, ["a", "b"]);
113121
});
114122

123+
test(async function testZrange() {
124+
redis.zadd("zrange", 1, "one");
125+
redis.zadd("zrange", 2, "two");
126+
redis.zadd("zrange", 3, "three");
127+
const v = await redis.zrange("zrange", 0, 1);
128+
assertEquals(v, ["one", "two"]);
129+
});
130+
131+
test(async function testZrangeWithScores() {
132+
redis.zadd("zrangeWithScores", 1, "one");
133+
redis.zadd("zrangeWithScores", 2, "two");
134+
redis.zadd("zrangeWithScores", 3, "three");
135+
const v = await redis.zrange("zrangeWithScores", 0, 1, { withScore: true });
136+
assertEquals(v, ["one", "1", "two", "2"]);
137+
});
138+
139+
test(async function testZrevrange() {
140+
redis.zadd("zrevrange", 1, "one");
141+
redis.zadd("zrevrange", 2, "two");
142+
redis.zadd("zrevrange", 3, "three");
143+
const v = await redis.zrevrange("zrevrange", 0, 1)
144+
assertEquals(v, ["three", "two"]);
145+
});
146+
147+
test(async function testZrevrangeWithScores() {
148+
redis.zadd("zrevrangeWithScores", 1, "one");
149+
redis.zadd("zrevrangeWithScores", 2, "two");
150+
redis.zadd("zrevrangeWithScores", 3, "three");
151+
const v = await redis.zrevrange("zrevrangeWithScores", 0, 1, { withScore: true });
152+
assertEquals(v, ["three", "3", "two", "2"]);
153+
});
154+
155+
test(async function testZrangebyscore() {
156+
redis.zadd("zrangebyscore", 2, "m1");
157+
redis.zadd("zrangebyscore", 5, "m2");
158+
redis.zadd("zrangebyscore", 8, "m3");
159+
redis.zadd("zrangebyscore", 10, "m4");
160+
const v = await redis.zrangebyscore("zrangebyscore", 3, 9);
161+
assertEquals(v, ["m2", "m3"]);
162+
});
163+
164+
test(async function testZrangebyscoreWithScores() {
165+
redis.zadd("zrangebyscoreWithScores", 2, "m1");
166+
redis.zadd("zrangebyscoreWithScores", 5, "m2");
167+
redis.zadd("zrangebyscoreWithScores", 8, "m3");
168+
redis.zadd("zrangebyscoreWithScores", 10, "m4");
169+
const v = await redis.zrangebyscore("zrangebyscoreWithScores", 3, 9, { withScore: true });
170+
assertEquals(v, ["m2", "5", "m3", "8"]);
171+
});
172+
173+
test(async function testZrevrangebyscore() {
174+
redis.zadd("zrevrangebyscore", 2, "m1");
175+
redis.zadd("zrevrangebyscore", 5, "m2");
176+
redis.zadd("zrevrangebyscore", 8, "m3");
177+
redis.zadd("zrevrangebyscore", 10, "m4");
178+
const v = await redis.zrevrangebyscore("zrevrangebyscore", 9, 4);
179+
assertEquals(v, ["m3", "m2"]);
180+
});
181+
182+
test(async function testZrevrangebyscore() {
183+
redis.zadd("zrevrangebyscoreWithScores", 2, "m1");
184+
redis.zadd("zrevrangebyscoreWithScores", 5, "m2");
185+
redis.zadd("zrevrangebyscoreWithScores", 8, "m3");
186+
redis.zadd("zrevrangebyscoreWithScores", 10, "m4");
187+
const v = await redis.zrevrangebyscore("zrevrangebyscoreWithScores", 9, 4, { withScore: true });
188+
assertEquals(v, ["m3", "8", "m2", "5"]);
189+
});
190+
115191
test(async function testConcurrent() {
116192
let promises: Promise<any>[] = [];
117193
for (const key of ["a", "b", "c"]) {

0 commit comments

Comments
 (0)