From eea0f8024e28a41b74b23eced41f235b8a1d11cf Mon Sep 17 00:00:00 2001 From: James Prior Date: Sun, 15 Oct 2023 18:00:52 +0100 Subject: [PATCH] Lazy filter queries. --- src/path/expression.ts | 10 ++++++++-- src/path/selectors.ts | 2 ++ src/path/types.ts | 1 + 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/path/expression.ts b/src/path/expression.ts index f3593c4..ec6eda7 100644 --- a/src/path/expression.ts +++ b/src/path/expression.ts @@ -190,7 +190,11 @@ export abstract class JSONPathQuery extends FilterExpression { export class RelativeQuery extends JSONPathQuery { public evaluate(context: FilterContext): JSONPathNodeList { - return this.path.query(context.currentValue); // TODO: lazy query? + return context.lazy + ? new JSONPathNodeList( + Array.from(this.path.lazyQuery(context.currentValue)), + ) + : this.path.query(context.currentValue); } public toString(): string { @@ -200,7 +204,9 @@ export class RelativeQuery extends JSONPathQuery { export class RootQuery extends JSONPathQuery { public evaluate(context: FilterContext): JSONPathNodeList { - return this.path.query(context.rootValue); // TODO: lazy query? + return context.lazy + ? new JSONPathNodeList(Array.from(this.path.lazyQuery(context.rootValue))) + : this.path.query(context.rootValue); } public toString(): string { diff --git a/src/path/selectors.ts b/src/path/selectors.ts index f8ecbfc..6eac694 100644 --- a/src/path/selectors.ts +++ b/src/path/selectors.ts @@ -495,6 +495,7 @@ export class FilterSelector extends JSONPathSelector { environment: this.environment, currentValue: value, rootValue: node.root, + lazy: true, }; if (this.expression.evaluate(filterContext)) { yield new JSONPathNode(value, node.location.concat(i), node.root); @@ -506,6 +507,7 @@ export class FilterSelector extends JSONPathSelector { environment: this.environment, currentValue: value, rootValue: node.root, + lazy: true, }; if (this.expression.evaluate(filterContext)) { yield new JSONPathNode(value, node.location.concat(key), node.root); diff --git a/src/path/types.ts b/src/path/types.ts index a28062e..c2b4160 100644 --- a/src/path/types.ts +++ b/src/path/types.ts @@ -15,6 +15,7 @@ export type FilterContext = { environment: JSONPathEnvironment; currentValue: JSONValue; rootValue: JSONValue; + lazy?: boolean; }; /**