Open
Description
Motivation
Performance
Current behavior
Each time I pass a path it needs to be parsed and the queried for all possible matching items
Desired behavior
Be able to create and object for a path that stores the parsed query and allows me to execute it many times, skipping the overhead of path parsing. Additionally it would be good to specify the search limit.
Example:
const bookTitlePath = new JSONPath<string>('$..book[*].title');
const bookPricePath = new JSONPath<number>('$..book[*].price');
function getFirstBook(storeData: object): string {
return bookTitlePath.value(storeData); // Returns first match
}
// Consider books are in desc order of sales
function getAveragePriceOfBestSellingBooks(storeData: object): number {
const prices: number[] = bookPricePath.query(storeData, 10); // Get up to 10 elements
// The only viable usage of reduce 😬
return prices.reduce((total, price) => total + price, 0) / prices.length;
}
Alternatives considered
Could not find any.