Thanks to visit codestin.com
Credit goes to github.com

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
34 changes: 34 additions & 0 deletions pkg/commands/zdiffstore.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { keygen, newHttpClient } from "../test-utils.ts";
import { afterAll } from "https://deno.land/[email protected]/testing/bdd.ts";
import { ZRangeCommand } from "./zrange.ts";
import { assertEquals } from "https://deno.land/[email protected]/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);
});
13 changes: 13 additions & 0 deletions pkg/commands/zdiffstore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Command, CommandOptions } from "./command.ts";

/**
* @see https://redis.io/commands/zdiffstore
*/
export class ZDiffStoreCommand extends Command<number, number> {
constructor(
cmd: [destination: string, numkeys: number, ...keys: string[]],
opts?: CommandOptions<number, number>,
) {
super(["zdiffstore", ...cmd], opts);
}
}
7 changes: 7 additions & 0 deletions pkg/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -270,6 +271,12 @@ export class Pipeline {
bitpos = (...args: CommandArgs<typeof BitPosCommand>) =>
this.chain(new BitPosCommand(args, this.commandOptions));

/**
* @see https://redis.io/commands/zdiffstore
*/
zdiffstore = (...args: CommandArgs<typeof ZDiffStoreCommand>) =>
this.chain(new ZDiffStoreCommand(args, this.commandOptions));

/**
* @see https://redis.io/commands/dbsize
*/
Expand Down
12 changes: 11 additions & 1 deletion pkg/redis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,17 @@ Deno.test("special data", async (t) => {
await redis.set(key, encodeURIComponent(value));
const res = await redis.get<string>(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<string>(key);

assertEquals(res!, value);
});
await t.step("emojis", async () => {
const key = newKey();
Expand Down
7 changes: 7 additions & 0 deletions pkg/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
/**
Expand Down Expand Up @@ -880,6 +881,12 @@ export class Redis {
zcount = (...args: CommandArgs<typeof ZCountCommand>) =>
new ZCountCommand(args, this.opts).exec(this.client);

/**
* @see https://redis.io/commands/zdiffstore
*/
zdiffstore = (...args: CommandArgs<typeof ZDiffStoreCommand>) =>
new ZDiffStoreCommand(args, this.opts).exec(this.client);

/**
* @see https://redis.io/commands/zincrby
*/
Expand Down