Skip to content

Commit

Permalink
Add STRALGO command (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
braddunbar authored Aug 31, 2020
1 parent 659c0aa commit c4509f0
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
12 changes: 12 additions & 0 deletions command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@ export type RedisCommands = {
setex(key: string, seconds: number, value: string): Promise<Status>;
setnx(key: string, value: string): Promise<Integer>;
setrange(key: string, offset: number, value: string): Promise<Integer>;
stralgo(
algorithm: "LCS",
target: "KEYS" | "STRINGS",
a: string,
b: string,
opts?: {
idx?: boolean;
len?: boolean;
minmatchlen?: number;
withmatchlen?: boolean;
},
): Promise<Bulk>;
strlen(key: string): Promise<Integer>;

// Geo
Expand Down
29 changes: 29 additions & 0 deletions redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
38 changes: 38 additions & 0 deletions tests/string_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit c4509f0

Please sign in to comment.