Implemented array query
This commit is contained in:
parent
d0431bbd99
commit
1ad9b82631
@ -68,7 +68,7 @@ test("Test parser", () => {
|
||||
});
|
||||
});
|
||||
|
||||
test("Test performing the queries", () => {
|
||||
test("Test applyQuery", () => {
|
||||
let data: any[] = [
|
||||
{ name: "interview/My Interview", lastModified: 1 },
|
||||
{ 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)
|
||||
).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) {
|
||||
switch (op) {
|
||||
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;
|
||||
}
|
||||
break;
|
||||
|
Loading…
Reference in New Issue
Block a user