Skip to content

Commit

Permalink
Node: add SMISMEMBER command (valkey-io#1955)
Browse files Browse the repository at this point in the history
* Node: add SMISMEMBER command

Signed-off-by: aaron-congo <[email protected]>
  • Loading branch information
aaron-congo authored Jul 17, 2024
1 parent 432932f commit 6baf4af
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Node: Added SINTERCARD command ([#1956](https:/valkey-io/valkey-glide/pull/1956))
* Node: Added SINTERSTORE command ([#1929](https:/valkey-io/valkey-glide/pull/1929))
* Node: Added SUNION command ([#1919](https:/valkey-io/valkey-glide/pull/1919))
* Node: Added SMISMEMBER command ([#1955](https:/valkey-io/valkey-glide/pull/1955))
* Node: Added SDIFF command ([#1924](https:/valkey-io/valkey-glide/pull/1924))
* Node: Added LOLWUT command ([#1934](https:/valkey-io/valkey-glide/pull/1934))

Expand Down
23 changes: 23 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import {
createSInterStore,
createSIsMember,
createSMembers,
createSMIsMember,
createSMove,
createSPop,
createSRem,
Expand Down Expand Up @@ -1552,6 +1553,28 @@ export class BaseClient {
return this.createWritePromise(createSIsMember(key, member));
}

/**
* Checks whether each member is contained in the members of the set stored at `key`.
*
* See https://valkey.io/commands/smismember/ for more details.
*
* @param key - The key of the set to check.
* @param members - A list of members to check for existence in the set.
* @returns An `array` of `boolean` values, each indicating if the respective member exists in the set.
*
* since Valkey version 6.2.0.
*
* @example
* ```typescript
* await client.sadd("set1", ["a", "b", "c"]);
* const result = await client.smismember("set1", ["b", "c", "d"]);
* console.log(result); // Output: [true, true, false] - "b" and "c" are members of "set1", but "d" is not.
* ```
*/
public smismember(key: string, members: string[]): Promise<boolean[]> {
return this.createWritePromise(createSMIsMember(key, members));
}

/** Removes and returns one random member from the set value store at `key`.
* See https://valkey.io/commands/spop/ for details.
* To pop multiple members, see `spopCount`.
Expand Down
10 changes: 10 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,16 @@ export function createSIsMember(
return createCommand(RequestType.SIsMember, [key, member]);
}

/**
* @internal
*/
export function createSMIsMember(
key: string,
members: string[],
): command_request.Command {
return createCommand(RequestType.SMIsMember, [key].concat(members));
}

/**
* @internal
*/
Expand Down
17 changes: 17 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import {
createSInterStore,
createSIsMember,
createSMembers,
createSMIsMember,
createSMove,
createSPop,
createSRem,
Expand Down Expand Up @@ -889,6 +890,22 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createSIsMember(key, member));
}

/**
* Checks whether each member is contained in the members of the set stored at `key`.
*
* See https://valkey.io/commands/smismember/ for more details.
*
* @param key - The key of the set to check.
* @param members - A list of members to check for existence in the set.
*
* Command Response - An `array` of `boolean` values, each indicating if the respective member exists in the set.
*
* since Valkey version 6.2.0.
*/
public smismember(key: string, members: string[]): T {
return this.addAndReturn(createSMIsMember(key, members));
}

/** Removes and returns one random member from the set value store at `key`.
* See https://valkey.io/commands/spop/ for details.
* To pop multiple members, see `spopCount`.
Expand Down
37 changes: 37 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,43 @@ export function runBaseTests<Context>(config: {
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`smismember test_%p`,
async (protocol) => {
await runTest(async (client: BaseClient) => {
if (await checkIfServerVersionLessThan("6.2.0")) {
return;
}

const key = uuidv4();
const stringKey = uuidv4();
const nonExistingKey = uuidv4();

expect(await client.sadd(key, ["a", "b"])).toEqual(2);
expect(await client.smismember(key, ["b", "c"])).toEqual([
true,
false,
]);

expect(await client.smismember(nonExistingKey, ["b"])).toEqual([
false,
]);

// invalid argument - member list must not be empty
await expect(client.smismember(key, [])).rejects.toThrow(
RequestError,
);

// key exists, but it is not a set
checkSimple(await client.set(stringKey, "foo")).toEqual("OK");
await expect(
client.smismember(stringKey, ["a"]),
).rejects.toThrow(RequestError);
}, protocol);
},
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`spop and spopCount test_%p`,
async (protocol) => {
Expand Down
6 changes: 6 additions & 0 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,12 @@ export async function transactionTest(
args.push(1);
baseTransaction.sismember(key7, "bar");
args.push(true);

if (!(await checkIfServerVersionLessThan("6.2.0"))) {
baseTransaction.smismember(key7, ["bar", "foo", "baz"]);
args.push([true, true, false]);
}

baseTransaction.smembers(key7);
args.push(new Set(["bar"]));
baseTransaction.spop(key7);
Expand Down

0 comments on commit 6baf4af

Please sign in to comment.