Skip to content
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
21 changes: 21 additions & 0 deletions command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ interface ZStoreOpts {
export type ZInterstoreOpts = ZStoreOpts;
export type ZUnionstoreOpts = ZStoreOpts;

/** Optional arguments for `ZRANDMEMBER` command */
export interface ZRandmemberOpts {
/** If `true`, `WITHSCORES` argument is set. */
withScores?: boolean;
}

export interface ZRangeOpts {
withScore?: boolean;
}
Expand Down Expand Up @@ -1205,6 +1211,21 @@ XRANGE somestream - +
zlexcount(key: string, min: string, max: string): Promise<Integer>;
zpopmax(key: string, count?: number): Promise<BulkString[]>;
zpopmin(key: string, count?: number): Promise<BulkString[]>;
zrandmember(key: string): Promise<Bulk>;
zrandmember(
key: string,
count: number,
opts?: ZRandmemberOpts,
): Promise<Array<BulkString>>;
/**
* @see {@link https://redis.io/docs/latest/commands/ZRANDMEMBER/ | ZRANDMEMBER}
* @since Redis 6.2.0
*/
zrandmember(
key: string,
count?: number,
opts?: ZRandmemberOpts,
): Promise<Bulk | Array<BulkString>>;
zrange(
key: string,
start: number,
Expand Down
24 changes: 24 additions & 0 deletions redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import type {
ZAddReply,
ZInterOpts,
ZInterstoreOpts,
ZRandmemberOpts,
ZRangeByLexOpts,
ZRangeByScoreOpts,
ZRangeOpts,
Expand Down Expand Up @@ -2472,6 +2473,29 @@ class RedisImpl implements Redis {
return this.execArrayReply<BulkString>("ZPOPMIN", key);
}

zrandmember(key: string): Promise<Bulk>;
zrandmember(
key: string,
count: number,
opts?: ZRandmemberOpts,
): Promise<Array<BulkString>>;
zrandmember(
key: string,
count?: number,
opts?: ZRandmemberOpts,
): Promise<Bulk | Array<BulkString>> {
const args = [];
if (count != null) {
args.push(count);
}
if (opts?.withScores) {
args.push("WITHSCORES");
}
return args.length === 0
? this.execBulkReply("ZRANDMEMBER", key)
: this.execArrayReply("ZRANDMEMBER", key, ...args);
}

zrange(
key: string,
start: number,
Expand Down
48 changes: 47 additions & 1 deletion tests/commands/sorted_set.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { assert, assertEquals } from "../../deps/std/assert.ts";
import {
assert,
assertArrayIncludes,
assertEquals,
} from "../../deps/std/assert.ts";
import type { IsExact } from "../../deps/std/testing.ts";
import {
afterAll,
Expand Down Expand Up @@ -175,6 +179,48 @@ export function zsetTests(
assertEquals(await client.zpopmax("key", 1), ["two", "2"]);
});

describe("zrandmember", () => {
it("return a random member from the sorted set", async () => {
const members = { foo: 1, bar: 2, baz: 3 };
const key = "sorted-set";
await client.zadd(key, members);

const actual = await client.zrandmember(key);

assertArrayIncludes(Object.keys(members), [actual]);
});

it("supports `count` argument", async () => {
const members = { a: 1, b: 2, c: 3 };
const key = "sorted-set";
await client.zadd(key, members);

const count = 2;
const actual = await client.zrandmember(key, count);

assertArrayIncludes(Object.keys(members), actual);
assertEquals(actual.length, count);
});

it("supports `WITHSCORES` argument", async () => {
const members: Record<string, number> = { a: 1, b: 2, c: 3 };
const key = "sorted-set";
await client.zadd(key, members);

const count = 2;
const actual = await client.zrandmember(key, count, { withScores: true });

assertArrayIncludes(
Object.entries(members).flatMap(([x, score]) => [x, String(score)]),
actual,
);
assertEquals(actual.length, count * 2);

const scores = actual.map((x) => members[x]).filter(Boolean);
assertArrayIncludes(Object.values(members), scores);
});
});

it("zrange", async () => {
await client.zadd("key", { one: 1, two: 2 });
assertEquals(await client.zrange("key", 1, 2), ["two"]);
Expand Down