diff --git a/command.ts b/command.ts index b8bfe372..2e669dfb 100644 --- a/command.ts +++ b/command.ts @@ -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; } @@ -1205,6 +1211,21 @@ XRANGE somestream - + zlexcount(key: string, min: string, max: string): Promise; zpopmax(key: string, count?: number): Promise; zpopmin(key: string, count?: number): Promise; + zrandmember(key: string): Promise; + zrandmember( + key: string, + count: number, + opts?: ZRandmemberOpts, + ): Promise>; + /** + * @see {@link https://redis.io/docs/latest/commands/ZRANDMEMBER/ | ZRANDMEMBER} + * @since Redis 6.2.0 + */ + zrandmember( + key: string, + count?: number, + opts?: ZRandmemberOpts, + ): Promise>; zrange( key: string, start: number, diff --git a/redis.ts b/redis.ts index 919bec7d..0c168028 100644 --- a/redis.ts +++ b/redis.ts @@ -40,6 +40,7 @@ import type { ZAddReply, ZInterOpts, ZInterstoreOpts, + ZRandmemberOpts, ZRangeByLexOpts, ZRangeByScoreOpts, ZRangeOpts, @@ -2472,6 +2473,29 @@ class RedisImpl implements Redis { return this.execArrayReply("ZPOPMIN", key); } + zrandmember(key: string): Promise; + zrandmember( + key: string, + count: number, + opts?: ZRandmemberOpts, + ): Promise>; + zrandmember( + key: string, + count?: number, + opts?: ZRandmemberOpts, + ): Promise> { + 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, diff --git a/tests/commands/sorted_set.ts b/tests/commands/sorted_set.ts index 1d5aa385..9265144f 100644 --- a/tests/commands/sorted_set.ts +++ b/tests/commands/sorted_set.ts @@ -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, @@ -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 = { 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"]);