Implemented array query
parent
d0431bbd99
commit
1ad9b82631
|
@ -68,7 +68,7 @@ test("Test parser", () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test("Test performing the queries", () => {
|
test("Test applyQuery", () => {
|
||||||
let data: any[] = [
|
let data: any[] = [
|
||||||
{ name: "interview/My Interview", lastModified: 1 },
|
{ name: "interview/My Interview", lastModified: 1 },
|
||||||
{ name: "interview/My Interview 2", lastModified: 2 },
|
{ name: "interview/My Interview 2", lastModified: 2 },
|
||||||
|
@ -116,3 +116,25 @@ test("Test performing the queries", () => {
|
||||||
applyQuery(parseQuery(`page where name in ["Pete"] select name`), data)
|
applyQuery(parseQuery(`page where name in ["Pete"] select name`), data)
|
||||||
).toStrictEqual([{ name: "Pete" }]);
|
).toStrictEqual([{ name: "Pete" }]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("Test applyQuery with multi value", () => {
|
||||||
|
let data: any[] = [
|
||||||
|
{ name: "Pete", children: ["John", "Angie"] },
|
||||||
|
{ name: "Angie", children: ["Angie"] },
|
||||||
|
{ name: "Steve" },
|
||||||
|
];
|
||||||
|
|
||||||
|
expect(
|
||||||
|
applyQuery(parseQuery(`page where children = "Angie"`), data)
|
||||||
|
).toStrictEqual([
|
||||||
|
{ name: "Pete", children: ["John", "Angie"] },
|
||||||
|
{ name: "Angie", children: ["Angie"] },
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
applyQuery(parseQuery(`page where children = ["Angie", "John"]`), data)
|
||||||
|
).toStrictEqual([
|
||||||
|
{ name: "Pete", children: ["John", "Angie"] },
|
||||||
|
{ name: "Angie", children: ["Angie"] },
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
|
@ -135,7 +135,19 @@ export function applyQuery<T>(parsedQuery: ParsedQuery, records: T[]): T[] {
|
||||||
for (let { op, prop, value } of parsedQuery.filter) {
|
for (let { op, prop, value } of parsedQuery.filter) {
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case "=":
|
case "=":
|
||||||
if (!(recordAny[prop] == value)) {
|
const recordPropVal = recordAny[prop];
|
||||||
|
if (Array.isArray(recordPropVal) && !Array.isArray(value)) {
|
||||||
|
// Record property is an array, and value is a scalar: find the value in the array
|
||||||
|
if (!recordPropVal.includes(value)) {
|
||||||
|
continue recordLoop;
|
||||||
|
}
|
||||||
|
} else if (Array.isArray(recordPropVal) && Array.isArray(value)) {
|
||||||
|
// Record property is an array, and value is an array: find the value in the array
|
||||||
|
if (!recordPropVal.some((v) => value.includes(v))) {
|
||||||
|
continue recordLoop;
|
||||||
|
}
|
||||||
|
} else if (!(recordPropVal == value)) {
|
||||||
|
// Both are scalars: exact value
|
||||||
continue recordLoop;
|
continue recordLoop;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in New Issue