Skip to content

Commit 941f230

Browse files
committed
Implement like statement
1 parent 74abfa5 commit 941f230

3 files changed

Lines changed: 80 additions & 9 deletions

File tree

README.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export interface IDirEntry {
6767

6868
### Operators
6969

70-
The list of supported operators: `>`, `<`, `=`, `<>`.
70+
The list of supported operators: `>`, `<`, `=`, `<>`, `like`.
7171

7272
### Supported
7373

@@ -85,10 +85,4 @@ The list of supported operators: `>`, `<`, `=`, `<>`.
8585

8686
`select * from root where name = 'root.txt'`
8787

88-
### TODO
89-
90-
`select * from root where name like '%txt'`
91-
92-
`select * from root where name like '%txt%'`
93-
94-
`select * from root where name like 'txt%'`
88+
`select * from root where name like '%.txt'`

mod_test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,76 @@ Deno.test("if select * from root where name <> 'root.txt' works", async () => {
147147
"test_folder_with_folder",
148148
];
149149
assertArrayIncludes<string>(names, expectedNames);
150+
});
151+
152+
Deno.test("if select * from root where name like 'test_%' works", async () => {
153+
const result = await fsselect("select * from root where name like 'test_%'");
154+
assert(result.length === 3);
155+
156+
const names = result.map((i) => i.name as string);
157+
const expectedNames = [
158+
"test_folder_with_file",
159+
"test_folder_with_files",
160+
"test_folder_with_folder",
161+
];
162+
assertArrayIncludes<string>(names, expectedNames);
163+
});
164+
165+
Deno.test("if select * from root where name like '%folder%' works", async () => {
166+
const result = await fsselect("select * from root where name like '%folder%'");
167+
assert(result.length === 3);
168+
169+
const names = result.map((i) => i.name as string);
170+
const expectedNames = [
171+
"test_folder_with_file",
172+
"test_folder_with_files",
173+
"test_folder_with_folder",
174+
];
175+
assertArrayIncludes<string>(names, expectedNames);
176+
});
177+
178+
Deno.test("if select * from root where name like '%.txt' works", async () => {
179+
const result = await fsselect("select * from root where name like '%.txt'");
180+
assert(result.length === 1);
181+
182+
const names = result.map((i) => i.name as string);
183+
const expectedNames = [
184+
"root.txt"
185+
];
186+
assertArrayIncludes<string>(names, expectedNames);
187+
});
188+
189+
Deno.test("if select * from root where name like '%with_file%' works", async () => {
190+
const result = await fsselect("select * from root where name like '%with_file%'");
191+
assert(result.length === 2);
192+
193+
const names = result.map((i) => i.name as string);
194+
const expectedNames = [
195+
"test_folder_with_file",
196+
"test_folder_with_files",
197+
];
198+
assertArrayIncludes<string>(names, expectedNames);
199+
});
200+
201+
Deno.test("if select * from root where name like 'some' does not return entries", async () => {
202+
const result = await fsselect("select * from root where name like 'some'");
203+
assert(result.length === 0);
204+
205+
const names = result.map((i) => i.name as string);
206+
const expectedNames: string[] = [];
207+
assertArrayIncludes<string>(names, expectedNames);
208+
});
209+
210+
Deno.test("if select * from root where name like '%%' works", async () => {
211+
const result = await fsselect("select * from root where name like '%%'");
212+
assert(result.length === 4);
213+
214+
const names = result.map((i) => i.name as string);
215+
const expectedNames = [
216+
"test_folder_with_file",
217+
"test_folder_with_files",
218+
"test_folder_with_folder",
219+
"root.txt"
220+
];
221+
assertArrayIncludes<string>(names, expectedNames);
150222
});

where.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getLikeRegExp } from "./like.ts";
12
import { IDirEntry, IWhereClause, IWhereCondition } from "./types.ts";
23

34
export function where(entry: IDirEntry, where: IWhereClause | null): boolean {
@@ -75,7 +76,7 @@ function meet(entry: IDirEntry, condition: IWhereCondition): boolean {
7576
"LessThan": null,
7677
"Equal": equalString,
7778
"Different": differentString,
78-
"Like": null,
79+
"Like": likeString,
7980
};
8081

8182
const operation = operations[op];
@@ -125,3 +126,7 @@ function equalString(left: string | undefined, right: string): boolean {
125126
function differentString(left: string | undefined, right: string): boolean {
126127
return typeof left !== "undefined" && left !== right;
127128
}
129+
130+
function likeString(left: string | undefined, right: string): boolean {
131+
return typeof left !== "undefined" && left.match( getLikeRegExp(right) ) != null;
132+
}

0 commit comments

Comments
 (0)