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
24 changes: 24 additions & 0 deletions command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ export type ClusterSetSlotSubcommand =
| "NODE"
| "STABLE";

/** Optional arguments for `HRANDFIELD` command */
export interface HRandfieldOpts {
/** If `true`, `WITHVALUES` argument is set. */
withValues?: boolean;
}

export interface MigrateOpts {
copy?: boolean;
replace?: boolean;
Expand Down Expand Up @@ -609,6 +615,24 @@ export interface RedisCommands {
key: string,
field_values: Record<string, RedisValue>,
): Promise<SimpleString>;

hrandfield(key: string): Promise<Bulk>;
hrandfield(key: string, count: number): Promise<Array<BulkString>>;
hrandfield(
key: string,
count: number,
opts: Omit<HRandfieldOpts, "withValues"> & { withValues: true },
): Promise<Array<RedisValue>>;
/**
* @see {@link https://redis.io/docs/latest/commands/hrandfield/ | HRANDFIELD}
* @since Redis 6.2.0
*/
hrandfield(
key: string,
count?: number,
opts?: HRandfieldOpts,
): Promise<Bulk | Array<BulkString> | Array<RedisValue>>;

hscan(
key: string,
cursor: number,
Expand Down
25 changes: 25 additions & 0 deletions redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
GeoRadiusOpts,
GeoUnit,
HelloOpts,
HRandfieldOpts,
HScanOpts,
LcsOpts,
LInsertLocation,
Expand Down Expand Up @@ -2597,6 +2598,30 @@ class RedisImpl implements Redis {
>;
}

hrandfield(key: string): Promise<Bulk>;
hrandfield(key: string, count: number): Promise<Array<BulkString>>;
hrandfield(
key: string,
count: number,
opts: Omit<HRandfieldOpts, "withValues"> & { withValues: true },
): Promise<Array<RedisValue>>;
hrandfield(
key: string,
count?: number,
opts?: HRandfieldOpts,
): Promise<Bulk | Array<BulkString> | Array<RedisValue>> {
const args: Array<RedisValue> = [];
if (count != null) {
args.push(count);
}
if (opts?.withValues) {
args.push("WITHVALUES");
}
return args.length === 0
? this.execBulkReply("HRANDFIELD", key)
: this.execArrayReply("HRANDFIELD", key, ...args);
}

hscan(
key: string,
cursor: number,
Expand Down
75 changes: 73 additions & 2 deletions tests/commands/hash.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { assertEquals } from "../../deps/std/assert.ts";
import { afterAll, beforeAll, beforeEach, it } from "../../deps/std/testing.ts";
import {
assert,
assertArrayIncludes,
assertEquals,
assertExists,
} from "../../deps/std/assert.ts";
import {
afterAll,
beforeAll,
beforeEach,
describe,
it,
} from "../../deps/std/testing.ts";
import type { Connector, TestServer } from "../test_util.ts";
import type { Redis } from "../../mod.ts";

Expand Down Expand Up @@ -80,6 +91,66 @@ export function hashTests(
assertEquals(await client.hmset("key", ["f4", "4"], ["f5", "5"]), "OK");
});

describe("hrandfield", () => {
it("returns a random field from the hash", async () => {
const key = "some-key";
const field1 = "foo";
const field2 = "bar";
await client.hset(key, field1, "1");
await client.hset(key, field2, "2");

const reply = await client.hrandfield(key);

assertExists(reply);
assert([field1, field2].includes(reply));
});

it("supports `count` argument", async () => {
const key = "some-key";
const field1 = "foo";
const field2 = "bar";
const field3 = "baz";
await client.hset(key, field1, "1");
await client.hset(key, field2, "2");
await client.hset(key, field3, "3");

const count = 2;
const reply = await client.hrandfield(key, count);

assertEquals(reply.length, count);
assertArrayIncludes([field1, field2, field3], reply);
});

it("supports `WITHVALUES` argument", async () => {
const key = "some-key";
const fields: Record<string, string> = {
"foo": "1",
"bar": "2",
"baz": "3",
};
for (const [field, value] of Object.entries(fields)) {
await client.hset(key, field, value);
}

const count = 2;
const reply = await client.hrandfield(key, count, {
withValues: true,
});

const expected = Object.keys(fields).reduce(
(expected: Array<string>, field) => {
if (reply.includes(field)) {
expected.push(field, fields[field]);
}
return expected;
},
[],
);
assertEquals(reply.length, count * 2);
assertArrayIncludes(reply, expected);
});
});

it("hset", async () => {
assertEquals(await client.hset("key", "f1", "1"), 1);
assertEquals(await client.hset("key", { f2: "2", f3: "3" }), 2);
Expand Down