From 74abfa517bd6d8b199094a98f36269d90f5ef3ee Mon Sep 17 00:00:00 2001 From: Maxim Zhukov Date: Wed, 21 Apr 2021 02:09:52 -0500 Subject: [PATCH] Replace the like query where clause with the RegExp --- like.ts | 7 ++++ like_test.ts | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++ mod_test.ts | 2 +- 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 like.ts create mode 100644 like_test.ts diff --git a/like.ts b/like.ts new file mode 100644 index 0000000..119d6d3 --- /dev/null +++ b/like.ts @@ -0,0 +1,7 @@ +export function getLikeRegExp(input: string): RegExp { + const pattern = input + .replaceAll(".", "\\.") + .replaceAll("%", ".*?"); + + return new RegExp("^" + pattern + "$"); +} diff --git a/like_test.ts b/like_test.ts new file mode 100644 index 0000000..4a5d956 --- /dev/null +++ b/like_test.ts @@ -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); +}); diff --git a/mod_test.ts b/mod_test.ts index be424c9..9214a3b 100644 --- a/mod_test.ts +++ b/mod_test.ts @@ -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 () => {