-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhere.ts
132 lines (109 loc) · 3.47 KB
/
where.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { getLikeRegExp } from "./like.ts";
import { IDirEntry, IWhereClause, IWhereCondition } from "./types.ts";
export function where(entry: IDirEntry, where: IWhereClause | null): boolean {
const conditions = where?.conditions;
if (!conditions?.length) {
return true;
}
for (const condition of conditions) {
if (!meet(entry, condition)) {
return false;
}
}
return true;
}
function meet(entry: IDirEntry, condition: IWhereCondition): boolean {
const { left, op, right } = condition;
if (left === "size") {
const operations = {
"GreaterThan": greaterThan,
"LessThan": lessThan,
"Equal": equal,
"Different": different,
"Like": null,
};
const operation = operations[op];
if (!operation) {
throw new Error(
`The operation '${op}' is not supported for the '${left}' property`,
);
}
return operation(entry.size, <number> right);
} else if (left === "isDirectory") {
const operations = {
"GreaterThan": null,
"LessThan": null,
"Equal": equalBoolean,
"Different": differentBoolean,
"Like": null,
};
const operation = operations[op];
if (!operation) {
throw new Error(
`The operation '${op}' is not supported for the '${left}' property`,
);
}
return operation(entry.isDirectory, JSON.parse(right as string));
} else if (left === "isFile") {
const operations = {
"GreaterThan": null,
"LessThan": null,
"Equal": equalBoolean,
"Different": differentBoolean,
"Like": null,
};
const operation = operations[op];
if (!operation) {
throw new Error(
`The operation '${op}' is not supported for the '${left}' property`,
);
}
return operation(entry.isFile, JSON.parse(right as string));
} else if (left === "name") {
const operations = {
"GreaterThan": null,
"LessThan": null,
"Equal": equalString,
"Different": differentString,
"Like": likeString,
};
const operation = operations[op];
if (!operation) {
throw new Error(
`The operation '${op}' is not supported for the '${left}' property`,
);
}
return operation(entry.name, right as string);
}
return false;
}
/* Number */
function greaterThan(left: number | undefined, right: number): boolean {
return typeof left !== "undefined" && left > right;
}
function lessThan(left: number | undefined, right: number): boolean {
return typeof left !== "undefined" && left < right;
}
function equal(left: number | undefined, right: number): boolean {
return typeof left !== "undefined" && left === right;
}
function different(left: number | undefined, right: number): boolean {
return typeof left !== "undefined" && left !== right;
}
/* Boolean */
function equalBoolean(left: boolean | undefined, right: boolean): boolean {
return typeof left !== "undefined" && left === right;
}
function differentBoolean(left: boolean | undefined, right: boolean): boolean {
return typeof left !== "undefined" && left !== right;
}
/* String */
function equalString(left: string | undefined, right: string): boolean {
return typeof left !== "undefined" && left === right;
}
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;
}