Skip to content

Commit

Permalink
Replace the like query where clause with the RegExp
Browse files Browse the repository at this point in the history
  • Loading branch information
FSou1 committed Apr 21, 2021
1 parent 21116ab commit 74abfa5
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
7 changes: 7 additions & 0 deletions like.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function getLikeRegExp(input: string): RegExp {
const pattern = input
.replaceAll(".", "\\.")
.replaceAll("%", ".*?");

return new RegExp("^" + pattern + "$");
}
91 changes: 91 additions & 0 deletions like_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { assert, assertEquals } from "./deps.ts";
import { getLikeRegExp } from "./like.ts";

Deno.test("where name like 'abc.txt'", () => {
const regexp = getLikeRegExp("abc.txt");

/* Match */
assert("abc.txt".match(regexp));

/* Does not match */
assertEquals("a.txt".match(regexp), null);
assertEquals("abc_d.txt".match(regexp), null);
assertEquals("abc.tx".match(regexp), null);
});

Deno.test("where name like '%.txt'", () => {
const regexp = getLikeRegExp("%.txt");

/* Match */
assert("abc.txt".match(regexp));
assert("c.txt".match(regexp));
assert("ab_c.txt".match(regexp));
assert("ab_-\/*+ddc.txt".match(regexp));

/* Does not match */
assertEquals("abc.bin".match(regexp), null);
assertEquals(".txn".match(regexp), null);
assertEquals("abc_txt".match(regexp), null);
assertEquals("abc.!txt".match(regexp), null);
assertEquals("abc.txt_bin".match(regexp), null);
});

Deno.test("where name like '%.txt%'", () => {
const regexp = getLikeRegExp("%.txt%");

/* Match */
assert("abc.txt".match(regexp));
assert("c.txt".match(regexp));
assert("ab_c.txt".match(regexp));
assert("ab_-\/*+ddc.txt".match(regexp));

/* Does not match */
assertEquals("abc.bin".match(regexp), null);
assertEquals(".txn".match(regexp), null);
assertEquals("abc_txt".match(regexp), null);
});

Deno.test("where name like 'ab%txt'", () => {
const regexp = getLikeRegExp("ab%txt");

/* Match */
assert("abc.txt".match(regexp));
assert("abdef_.txt".match(regexp));
assert("ab_c.txt".match(regexp));
assert("ab_-\/*+ddc.txt".match(regexp));

/* Does not match */
assertEquals("abc.bin".match(regexp), null);
assertEquals("abc".match(regexp), null);
assertEquals("d_abc.txt".match(regexp), null);
assertEquals("abc.txt.".match(regexp), null);
assertEquals("abc.txt!".match(regexp), null);
});

Deno.test("where name like 'abc.%'", () => {
const regexp = getLikeRegExp("abc.%");

/* Match */
assert("abc.txt".match(regexp));
assert("abc.bin".match(regexp));
assert("abc.".match(regexp));

/* Does not match */
assertEquals("abc".match(regexp), null);
assertEquals("d".match(regexp), null);
assertEquals("abc?.".match(regexp), null);
});

Deno.test("where name like '%.%'", () => {
const regexp = getLikeRegExp("%.%");

/* Match */
assert("abc.txt".match(regexp));
assert("abc.bin".match(regexp));
assert("abc.".match(regexp));
assert(".".match(regexp));
assert(".txt".match(regexp));

/* Does not match */
assertEquals("abc".match(regexp), null);
});
2 changes: 1 addition & 1 deletion mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { fsselect } from "./mod.ts";

Deno.test("if 'select * from .' works", async () => {
const result = await fsselect("select * from .");
assert(result.length === 18);
assert(result.length === 20);
});

Deno.test("if 'select * from root' works", async () => {
Expand Down

0 comments on commit 74abfa5

Please sign in to comment.