Skip to content

Commit

Permalink
Implement like statement
Browse files Browse the repository at this point in the history
  • Loading branch information
FSou1 committed Apr 22, 2021
1 parent 74abfa5 commit 941f230
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 9 deletions.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export interface IDirEntry {

### Operators

The list of supported operators: `>`, `<`, `=`, `<>`.
The list of supported operators: `>`, `<`, `=`, `<>`, `like`.

### Supported

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

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

### TODO

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

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

`select * from root where name like 'txt%'`
`select * from root where name like '%.txt'`
72 changes: 72 additions & 0 deletions mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,76 @@ Deno.test("if select * from root where name <> 'root.txt' works", async () => {
"test_folder_with_folder",
];
assertArrayIncludes<string>(names, expectedNames);
});

Deno.test("if select * from root where name like 'test_%' works", async () => {
const result = await fsselect("select * from root where name like 'test_%'");
assert(result.length === 3);

const names = result.map((i) => i.name as string);
const expectedNames = [
"test_folder_with_file",
"test_folder_with_files",
"test_folder_with_folder",
];
assertArrayIncludes<string>(names, expectedNames);
});

Deno.test("if select * from root where name like '%folder%' works", async () => {
const result = await fsselect("select * from root where name like '%folder%'");
assert(result.length === 3);

const names = result.map((i) => i.name as string);
const expectedNames = [
"test_folder_with_file",
"test_folder_with_files",
"test_folder_with_folder",
];
assertArrayIncludes<string>(names, expectedNames);
});

Deno.test("if select * from root where name like '%.txt' works", async () => {
const result = await fsselect("select * from root where name like '%.txt'");
assert(result.length === 1);

const names = result.map((i) => i.name as string);
const expectedNames = [
"root.txt"
];
assertArrayIncludes<string>(names, expectedNames);
});

Deno.test("if select * from root where name like '%with_file%' works", async () => {
const result = await fsselect("select * from root where name like '%with_file%'");
assert(result.length === 2);

const names = result.map((i) => i.name as string);
const expectedNames = [
"test_folder_with_file",
"test_folder_with_files",
];
assertArrayIncludes<string>(names, expectedNames);
});

Deno.test("if select * from root where name like 'some' does not return entries", async () => {
const result = await fsselect("select * from root where name like 'some'");
assert(result.length === 0);

const names = result.map((i) => i.name as string);
const expectedNames: string[] = [];
assertArrayIncludes<string>(names, expectedNames);
});

Deno.test("if select * from root where name like '%%' works", async () => {
const result = await fsselect("select * from root where name like '%%'");
assert(result.length === 4);

const names = result.map((i) => i.name as string);
const expectedNames = [
"test_folder_with_file",
"test_folder_with_files",
"test_folder_with_folder",
"root.txt"
];
assertArrayIncludes<string>(names, expectedNames);
});
7 changes: 6 additions & 1 deletion where.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getLikeRegExp } from "./like.ts";
import { IDirEntry, IWhereClause, IWhereCondition } from "./types.ts";

export function where(entry: IDirEntry, where: IWhereClause | null): boolean {
Expand Down Expand Up @@ -75,7 +76,7 @@ function meet(entry: IDirEntry, condition: IWhereCondition): boolean {
"LessThan": null,
"Equal": equalString,
"Different": differentString,
"Like": null,
"Like": likeString,
};

const operation = operations[op];
Expand Down Expand Up @@ -125,3 +126,7 @@ function equalString(left: string | undefined, right: string): boolean {
function differentString(left: string | undefined, right: string): boolean {
return typeof left !== "undefined" && left !== right;
}

function likeString(left: string | undefined, right: string): boolean {
return typeof left !== "undefined" && left.match( getLikeRegExp(right) ) != null;
}

0 comments on commit 941f230

Please sign in to comment.