Skip to content

Commit 03b5561

Browse files
committed
#6 Implement the filter by name
1 parent 2da9257 commit 03b5561

4 files changed

Lines changed: 181 additions & 4 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ The list of supported operators: `>`, `<`, `=`, `<>`.
8383

8484
`select * from root where isFile <> false`
8585

86-
### TODO
87-
8886
`select * from root where name = 'root.txt'`
8987

88+
### TODO
89+
9090
`select * from root where name like '%txt'`
9191

9292
`select * from root where name like '%txt%'`

mod_test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,44 @@ Deno.test("if 'select * from root where isFile <> false' works", async () => {
107107
assertEquals(result[0].name, "root.txt");
108108
assertEquals(result[0].isDirectory, false);
109109
assertEquals(result[0].isFile, true);
110+
});
111+
112+
Deno.test("if select * from root where name = 'test_folder_with_file' works", async () => {
113+
const result = await fsselect("select * from root where name = 'test_folder_with_file'");
114+
assert(result.length === 1);
115+
assertEquals(result[0].name, "test_folder_with_file");
116+
assertEquals(result[0].isDirectory, true);
117+
});
118+
119+
Deno.test("if select * from root where name = 'root.txt' works", async () => {
120+
const result = await fsselect("select * from root where name = 'root.txt'");
121+
assert(result.length === 1);
122+
assertEquals(result[0].name, "root.txt");
123+
assertEquals(result[0].isFile, true);
124+
});
125+
126+
Deno.test("if select * from root where name <> 'test_folder_with_file' works", async () => {
127+
const result = await fsselect("select * from root where name <> 'test_folder_with_file'");
128+
assert(result.length === 3);
129+
130+
const names = result.map((i) => i.name as string);
131+
const expectedNames = [
132+
"root.txt",
133+
"test_folder_with_files",
134+
"test_folder_with_folder",
135+
];
136+
assertArrayIncludes<string>(names, expectedNames);
137+
});
138+
139+
Deno.test("if select * from root where name <> 'root.txt' works", async () => {
140+
const result = await fsselect("select * from root where name <> 'root.txt'");
141+
assert(result.length === 3);
142+
143+
const names = result.map((i) => i.name as string);
144+
const expectedNames = [
145+
"test_folder_with_file",
146+
"test_folder_with_files",
147+
"test_folder_with_folder",
148+
];
149+
assertArrayIncludes<string>(names, expectedNames);
110150
});

select_test.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,114 @@ Deno.test("if 3 folders are returned when the where clause has isFile <> true",
309309
];
310310
assertArrayIncludes<string>(names, expectedNames);
311311
});
312+
313+
Deno.test("if the 'root.txt' is returned when the where clause has name = 'root.txt'", async () => {
314+
const query: IQuery = {
315+
type: "select",
316+
fields: ["*"],
317+
from: "root",
318+
where: {
319+
conditions: [
320+
{
321+
left: "name",
322+
op: "Equal",
323+
right: "root.txt",
324+
},
325+
],
326+
},
327+
};
328+
329+
const result = await select(query);
330+
331+
assert(result.length === 1);
332+
assertEquals(result[0].name, "root.txt");
333+
assertEquals(result[0].isDirectory, false);
334+
assertEquals(result[0].isFile, true);
335+
});
336+
337+
Deno.test("if a folder is returned when the where clause has name = 'test_folder_with_folder'", async () => {
338+
const query: IQuery = {
339+
type: "select",
340+
fields: ["*"],
341+
from: "root",
342+
where: {
343+
conditions: [
344+
{
345+
left: "name",
346+
op: "Equal",
347+
right: "test_folder_with_folder",
348+
},
349+
],
350+
},
351+
};
352+
353+
const result = await select(query);
354+
355+
assert(result.length === 1);
356+
assertEquals(result[0].name, "test_folder_with_folder");
357+
assertEquals(result[0].isDirectory, true);
358+
assertEquals(result[0].isFile, false);
359+
});
360+
361+
362+
363+
364+
365+
366+
367+
368+
Deno.test("if the 'root.txt' is not returned when the where clause has name <> 'root.txt'", async () => {
369+
const query: IQuery = {
370+
type: "select",
371+
fields: ["*"],
372+
from: "root",
373+
where: {
374+
conditions: [
375+
{
376+
left: "name",
377+
op: "Different",
378+
right: "root.txt",
379+
},
380+
],
381+
},
382+
};
383+
384+
const result = await select(query);
385+
386+
assert(result.length === 3);
387+
const names = result.map((i) => i.name as string);
388+
const expectedNames = [
389+
"test_folder_with_file",
390+
"test_folder_with_files",
391+
"test_folder_with_folder",
392+
];
393+
assertArrayIncludes<string>(names, expectedNames);
394+
});
395+
396+
Deno.test("if a folder is returned when the where clause has name <> 'test_folder_with_folder'", async () => {
397+
const query: IQuery = {
398+
type: "select",
399+
fields: ["*"],
400+
from: "root",
401+
where: {
402+
conditions: [
403+
{
404+
left: "name",
405+
op: "Different",
406+
right: "test_folder_with_folder",
407+
},
408+
],
409+
},
410+
};
411+
412+
const result = await select(query);
413+
414+
assert(result.length === 3);
415+
const names = result.map((i) => i.name as string);
416+
const expectedNames = [
417+
"test_folder_with_file",
418+
"test_folder_with_files",
419+
"root.txt",
420+
];
421+
assertArrayIncludes<string>(names, expectedNames);
422+
});

where.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function meet(entry: IDirEntry, condition: IWhereCondition): boolean {
3434
);
3535
}
3636

37-
return operation(entry.size, <number>right);
37+
return operation(entry.size, <number> right);
3838
} else if (left === "isDirectory") {
3939
const operations = {
4040
"GreaterThan": null,
@@ -69,6 +69,23 @@ function meet(entry: IDirEntry, condition: IWhereCondition): boolean {
6969
}
7070

7171
return operation(entry.isFile, JSON.parse(right as string));
72+
} else if (left === "name") {
73+
const operations = {
74+
"GreaterThan": null,
75+
"LessThan": null,
76+
"Equal": equalString,
77+
"Different": differentString,
78+
"Like": null,
79+
};
80+
81+
const operation = operations[op];
82+
if (!operation) {
83+
throw new Error(
84+
`The operation '${op}' is not supported for the '${left}' property`,
85+
);
86+
}
87+
88+
return operation(entry.name, right as string);
7289
}
7390

7491
return false;
@@ -98,4 +115,13 @@ function equalBoolean(left: boolean | undefined, right: boolean): boolean {
98115

99116
function differentBoolean(left: boolean | undefined, right: boolean): boolean {
100117
return typeof left !== "undefined" && left !== right;
101-
}
118+
}
119+
120+
/* String */
121+
function equalString(left: string | undefined, right: string): boolean {
122+
return typeof left !== "undefined" && left === right;
123+
}
124+
125+
function differentString(left: string | undefined, right: string): boolean {
126+
return typeof left !== "undefined" && left !== right;
127+
}

0 commit comments

Comments
 (0)