Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved table data display #6

Merged
merged 9 commits into from
Jan 17, 2025
21 changes: 18 additions & 3 deletions src/functions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export function parseQuery(query) {
const parts = query.split('.');
return parts.map(part => {
const match = part.match(/(\w+)\[(.*)\]/);
const parsed = parts.map(part => {
const match = part.match(/(\w*)\[(.*)\]/);
if (match) {
return {
type: 'filter',
Expand All @@ -14,6 +14,17 @@ export function parseQuery(query) {
name: part
};
});
let first = true;
for (const q of parsed) {
if (first) {
first = false;
} else {
if (q.type === "filter" && !q.field) {
throw Error(`Filter without field: ${query}`);
}
}
}
return parsed;
}

function parseCondition(condition) {
Expand Down Expand Up @@ -60,7 +71,11 @@ export function executeQuery(json, parsedQuery) {
result = result[part.name];
} else if (part.type === 'filter') {
const { conditions, operators } = parseCondition(part.condition);
const filteredResult = result[part.field].filter(item => {
let arr = result;
if (part.field) {
arr = result[part.field];
}
const filteredResult = arr.filter(item => {
return evaluateConditions(item, conditions, operators);
});
// Store the filtered result in finalResult
Expand Down
Loading