From 28c9636faf6d639bca826566a384588403e8e765 Mon Sep 17 00:00:00 2001 From: Andreas Thomas Date: Mon, 2 Jan 2023 08:52:56 +0100 Subject: [PATCH 1/2] feat: zdiffstore --- pkg/commands/zdiffstore.test.ts | 34 +++++++++++++++++++++++++++++++++ pkg/commands/zdiffstore.ts | 13 +++++++++++++ pkg/pipeline.ts | 7 +++++++ pkg/redis.test.ts | 12 +++++++++++- pkg/redis.ts | 7 +++++++ 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 pkg/commands/zdiffstore.test.ts create mode 100644 pkg/commands/zdiffstore.ts diff --git a/pkg/commands/zdiffstore.test.ts b/pkg/commands/zdiffstore.test.ts new file mode 100644 index 00000000..8341fae6 --- /dev/null +++ b/pkg/commands/zdiffstore.test.ts @@ -0,0 +1,34 @@ +import { keygen, newHttpClient, randomID } from "../test-utils.ts"; +import { afterAll } from "https://deno.land/std@0.152.0/testing/bdd.ts"; +import { ZRangeCommand } from "./zrange.ts"; +import { assertEquals } from "https://deno.land/std@0.152.0/testing/asserts.ts"; + +import { ZAddCommand } from "./zadd.ts"; +import { ZDiffStoreCommand } from "./zdiffstore.ts"; + +const client = newHttpClient(); + +const { newKey, cleanup } = keygen(); +afterAll(cleanup); +Deno.test("stors the diff", async () => { + const key1 = newKey(); + const key2 = newKey(); + const out = newKey(); + + await new ZAddCommand([key1, { score: 1, member: "one" }, { + score: 2, + member: "two", + }, { score: 3, member: "three" }]).exec(client); + await new ZAddCommand([key2, { score: 1, member: "one" }, { + score: 2, + member: "two", + }]).exec(client); + const res = await new ZDiffStoreCommand([out, 2, key1, key2]).exec(client); + + assertEquals(res, 1); + + const zset3 = await new ZRangeCommand([out, 0, -1, { withScores: true }]) + .exec(client); + assertEquals(zset3[0], "three"); + assertEquals(zset3[1], 3); +}); diff --git a/pkg/commands/zdiffstore.ts b/pkg/commands/zdiffstore.ts new file mode 100644 index 00000000..4136e3d4 --- /dev/null +++ b/pkg/commands/zdiffstore.ts @@ -0,0 +1,13 @@ +import { Command, CommandOptions } from "./command.ts"; + +/** + * @see https://redis.io/commands/zdiffstore + */ +export class ZDiffStoreCommand extends Command { + constructor( + cmd: [destination: string, numkeys: number, ...keys: string[]], + opts?: CommandOptions, + ) { + super(["zdiffstore", ...cmd], opts); + } +} diff --git a/pkg/pipeline.ts b/pkg/pipeline.ts index 2b5581f6..be370ca7 100644 --- a/pkg/pipeline.ts +++ b/pkg/pipeline.ts @@ -128,6 +128,7 @@ import { UpstashResponse } from "./http.ts"; import { CommandArgs } from "./types.ts"; import { ZMScoreCommand } from "./commands/zmscore.ts"; import { HRandFieldCommand } from "./commands/hrandfield.ts"; +import { ZDiffStoreCommand } from "./commands/zdiffstore.ts"; /** * Upstash REST API supports command pipelining to send multiple commands in @@ -270,6 +271,12 @@ export class Pipeline { bitpos = (...args: CommandArgs) => this.chain(new BitPosCommand(args, this.commandOptions)); + /** + * @see https://redis.io/commands/zdiffstore + */ + zdiffstore = (...args: CommandArgs) => + this.chain(new ZDiffStoreCommand(args, this.commandOptions)); + /** * @see https://redis.io/commands/dbsize */ diff --git a/pkg/redis.test.ts b/pkg/redis.test.ts index 0aa318df..773c3bc7 100644 --- a/pkg/redis.test.ts +++ b/pkg/redis.test.ts @@ -135,7 +135,17 @@ Deno.test("special data", async (t) => { await redis.set(key, encodeURIComponent(value)); const res = await redis.get(key); - assertEquals(res!, decodeURIComponent(value)); + assertEquals(decodeURIComponent(res!), value); + }); + + await t.step("without encodeURIComponent", async () => { + const key = newKey(); + const value = "😀"; + const redis = new Redis(client); + await redis.set(key, value); + const res = await redis.get(key); + + assertEquals(res!, value); }); await t.step("emojis", async () => { const key = newKey(); diff --git a/pkg/redis.ts b/pkg/redis.ts index 1cf2349c..6b485c32 100644 --- a/pkg/redis.ts +++ b/pkg/redis.ts @@ -128,6 +128,7 @@ import { Pipeline } from "./pipeline.ts"; import type { CommandArgs } from "./types.ts"; import { Script } from "./script.ts"; import { ZMScoreCommand } from "./commands/zmscore.ts"; +import { ZDiffStoreCommand } from "./commands/zdiffstore.ts"; export type RedisOptions = { /** @@ -880,6 +881,12 @@ export class Redis { zcount = (...args: CommandArgs) => new ZCountCommand(args, this.opts).exec(this.client); + /** + * @see https://redis.io/commands/zdiffstore + */ + zdiffstore = (...args: CommandArgs) => + new ZDiffStoreCommand(args, this.opts).exec(this.client); + /** * @see https://redis.io/commands/zincrby */ From 6f7fc366b9bd738f03effb5412c6abd0a0788ee4 Mon Sep 17 00:00:00 2001 From: Andreas Thomas Date: Mon, 2 Jan 2023 09:09:49 +0100 Subject: [PATCH 2/2] feat: zdiffstore --- pkg/commands/zdiffstore.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/commands/zdiffstore.test.ts b/pkg/commands/zdiffstore.test.ts index 8341fae6..302e382c 100644 --- a/pkg/commands/zdiffstore.test.ts +++ b/pkg/commands/zdiffstore.test.ts @@ -1,4 +1,4 @@ -import { keygen, newHttpClient, randomID } from "../test-utils.ts"; +import { keygen, newHttpClient } from "../test-utils.ts"; import { afterAll } from "https://deno.land/std@0.152.0/testing/bdd.ts"; import { ZRangeCommand } from "./zrange.ts"; import { assertEquals } from "https://deno.land/std@0.152.0/testing/asserts.ts";