Skip to content

Commit

Permalink
fix(#284): zadd with opts argument does not work (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
uki00a authored Jan 3, 2022
1 parent 00fa4fd commit 6ecd32b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
12 changes: 10 additions & 2 deletions redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1980,23 +1980,31 @@ class RedisImpl implements Redis {
) {
const args: (string | number)[] = [key];
if (Array.isArray(param1)) {
this.pushZAddOpts(args, param2 as ZAddOpts);
args.push(...param1.flatMap((e) => e));
opts = param2 as ZAddOpts;
} else if (typeof param1 === "object") {
this.pushZAddOpts(args, param2 as ZAddOpts);
for (const [member, score] of Object.entries(param1)) {
args.push(score as number, member);
}
opts = param2 as ZAddOpts;
} else {
this.pushZAddOpts(args, opts);
args.push(param1, param2 as string);
}
return this.execIntegerReply("ZADD", ...args);
}

private pushZAddOpts(
args: (string | number)[],
opts?: ZAddOpts,
): void {
if (opts?.mode) {
args.push(opts.mode);
}
if (opts?.ch) {
args.push("CH");
}
return this.execIntegerReply("ZADD", ...args);
}

zaddIncr(
Expand Down
17 changes: 17 additions & 0 deletions tests/commands/sorted_set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ export async function zsetTests(
);
});

await run("zaddWithMode", async () => {
assertEquals(await client.zadd("key", 1, "1", { mode: "NX" }), 1);
assertEquals(await client.zadd("key", { "1": 1 }, { mode: "XX" }), 0);
assertEquals(
await client.zadd("key", [[1, "1"], [2, "2"]], { mode: "NX" }),
1,
);
});

await run("zaddWithCH", async () => {
assertEquals(await client.zadd("key", [[1, "foo"], [2, "bar"]]), 2);
assertEquals(
await client.zadd("key", { "foo": 1, "bar": 3, "baz": 4 }, { ch: true }),
2,
);
});

await run("zcount", async () => {
await client.zadd("key", { "1": 1, "2": 2 });
assertEquals(await client.zcount("key", 0, 1), 1);
Expand Down

0 comments on commit 6ecd32b

Please sign in to comment.