-
Notifications
You must be signed in to change notification settings - Fork 4
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
Add json query support for index #53
Conversation
Hello, I'm Franklin Bot and I will run some test suites that validate the page speed.
|
|
Looks useful to me. I wonder if there is an existing lightweight JS library that can maybe perform the query on the JSON? Although I must admit I looked around briefly but couldn't find anything as lightweight as your json-query.js 😄 |
Yes, I tried searching around and couldn't find as specific as what I was looking for. |
@@ -0,0 +1,55 @@ | |||
/* eslint-disable max-len */ | |||
export default function query(select, from, where, orderBy, rowCount) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be good if we can include some test for this
/* eslint-disable max-len */ | ||
export default function query(select, from, where, orderBy, rowCount) { | ||
// Validate input parameters | ||
if (!Array.isArray(select) || !Array.isArray(from) || !Array.isArray(where) || where.length !== 3) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we replace this with !([select, from, where].every(Array.isArray) && where.length === 3)
@@ -332,6 +334,10 @@ export async function fetchIndex(indexFile, sheet, pageSize = 500) { | |||
return newIndex; | |||
} | |||
|
|||
export function queryIndex(index, select, where, orderBy, rowCount) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why wrapper function is required here ?
from.sort((a, b) => { | ||
const aValue = a[orderByField]; | ||
const bValue = b[orderByField]; | ||
if (sortOrder === 'asc') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Conditions can be simplified by using Math.sign
if (sortOrder === 'asc') { | |
if (sortOrder === 'asc') { | |
Math.sign(aValue - bValue) | |
} else if (sortOrder === 'desc') { | |
Math.sign(bValue - aValue) | |
} |
}); | ||
|
||
// Limit the number of rows based on the rowCount parameter | ||
const selectedData = filteredData.slice(0, rowCount); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add pagination support also in this ?
Thanks for reviewing. I found a library (jslinq) which seems to do what we want, providing a richer api and is quite lightweight. Hence closing this PR and opening a new one #63 for it. |
Please always provide the GitHub issue(s) your PR is for, as well as test URLs where your change can be observed (before and after):
Fix #9
Seeking early feedback on this (currently DRAFT) PR for adding a generic querying mechanism for index.
Sunstar site uses article feeds heavily at many places wherein the feeds have filtering criteria (like related, featured, recent, category etc.) where this may be useful.
Test URLs:
Before: https://main--sunstar--hlxsites.hlx.page/
After: https://i-9--sunstar--hlxsites.hlx.page/
If you are adding a new block or a variation to an existing block, has it been added in the block-library ?