From c4509f0bc5ea8ba46e6f3d1315c79c95438e5f0c Mon Sep 17 00:00:00 2001 From: Brad Dunbar Date: Mon, 31 Aug 2020 11:15:02 -0400 Subject: [PATCH] Add STRALGO command (#141) --- command.ts | 12 ++++++++++++ redis.ts | 29 +++++++++++++++++++++++++++++ tests/string_test.ts | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) diff --git a/command.ts b/command.ts index 18a8fa84..8d095e85 100644 --- a/command.ts +++ b/command.ts @@ -184,6 +184,18 @@ export type RedisCommands = { setex(key: string, seconds: number, value: string): Promise; setnx(key: string, value: string): Promise; setrange(key: string, offset: number, value: string): Promise; + stralgo( + algorithm: "LCS", + target: "KEYS" | "STRINGS", + a: string, + b: string, + opts?: { + idx?: boolean; + len?: boolean; + minmatchlen?: number; + withmatchlen?: boolean; + }, + ): Promise; strlen(key: string): Promise; // Geo diff --git a/redis.ts b/redis.ts index b98914f1..395a32f4 100644 --- a/redis.ts +++ b/redis.ts @@ -1289,6 +1289,35 @@ export class RedisImpl implements Redis { return this.execIntegerReply("SREM", key, ...members); } + stralgo( + algorithm: "LCS", + target: "KEYS" | "STRINGS", + a: string, + b: string, + opts?: { + idx?: boolean; + len?: boolean; + minmatchlen?: number; + withmatchlen?: boolean; + }, + ) { + const args: (number | string)[] = []; + if (opts?.idx) { + args.push("IDX"); + } + if (opts?.len) { + args.push("LEN"); + } + if (opts?.withmatchlen) { + args.push("WITHMATCHLEN"); + } + if (opts?.minmatchlen) { + args.push("MINMATCHLEN"); + args.push(opts.minmatchlen); + } + return this.execBulkReply("STRALGO", "LCS", target, a, b, ...args); + } + strlen(key: string) { return this.execIntegerReply("STRLEN", key); } diff --git a/tests/string_test.ts b/tests/string_test.ts index c76492de..7ed2d643 100644 --- a/tests/string_test.ts +++ b/tests/string_test.ts @@ -202,6 +202,44 @@ suite.test("setrange", async () => { assertEquals(v, "Hello, Redis!"); }); +suite.test("stralgo", async () => { + await client.set("a", "Hello"); + await client.set("b", "Deno!"); + const matches = [[[4, 4], [3, 3]], [[1, 1], [1, 1]]]; + const matchesWithLen = [[[4, 4], [3, 3], 1], [[1, 1], [1, 1], 1]]; + assertEquals(await client.stralgo("LCS", "KEYS", "a", "b"), "eo"); + assertEquals(await client.stralgo("LCS", "KEYS", "a", "b", { len: true }), 2); + assertEquals( + await client.stralgo("LCS", "KEYS", "a", "b", { idx: true }), + ["matches", matches, "len", 2], + ); + assertEquals( + await client.stralgo( + "LCS", + "KEYS", + "a", + "b", + { idx: true, withmatchlen: true }, + ), + ["matches", matchesWithLen, "len", 2], + ); + assertEquals( + await client.stralgo( + "LCS", + "KEYS", + "a", + "b", + { idx: true, minmatchlen: 2 }, + ), + ["matches", [], "len", 2], + ); + assertEquals(await client.stralgo("LCS", "STRINGS", "Hello", "Deno!"), "eo"); + assertEquals( + await client.stralgo("LCS", "STRINGS", "Hello", "Deno!", { len: true }), + 2, + ); +}); + suite.test("strlen", async () => { await client.set("key", "foobar"); const v = await client.strlen("key");