Skip to content

Commit

Permalink
Lazy filter queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
jg-rp committed Oct 15, 2023
1 parent 0b89902 commit eea0f80
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/path/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/path/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/path/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type FilterContext = {
environment: JSONPathEnvironment;
currentValue: JSONValue;
rootValue: JSONValue;
lazy?: boolean;
};

/**
Expand Down

0 comments on commit eea0f80

Please sign in to comment.