Skip to content

Commit

Permalink
Add non-standard keys selector and current key identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
jg-rp committed Mar 25, 2024
1 parent a36642a commit 0d21ccf
Show file tree
Hide file tree
Showing 13 changed files with 1,321 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ benchmark/*.txt
tests/dev.test.ts
dev.js
dev.mjs

# system
.DS_Store
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# JSON P3 Change Log

## Version 1.1.2 (unreleased)
## Version 1.2.0 (unreleased)

**Fixes**

- Fixed the error and error message arising from JSONPath queries with filter expressions and a missing closing bracket for the segment. Previously we would get a `JSONPathLexerError`, stating we "can't backup beyond start", which is meant to be an internal error. We now get a `JSONPathSyntaxError` with the message "unclosed bracketed selection".

**Features**

- Added a non-standard _keys_ selector (`~`). The keys selector selects property names from an object or indexes from and array. It is only enabled when setting the `strict` option to `false` when constructing a `JSONPathEnvironment`.
- Added a non-standard _current key_ identifier (`#`). `#` will be the key or index corresponding to `@` in a filter expression. The current key identifier is only enabled when setting the `strict` option to `false` when constructing a `JSONPathEnvironment`.

## Version 1.1.1

**Fixes**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-p3",
"version": "1.1.1",
"version": "1.2.0",
"author": "James Prior",
"license": "MIT",
"description": "JSONPath, JSON Pointer and JSON Patch",
Expand Down
20 changes: 17 additions & 3 deletions src/path/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ import { Match as MatchFilterFunction } from "./functions/match";
import { Search as SearchFilterFunction } from "./functions/search";
import { Value as ValueFilterFunction } from "./functions/value";
import { tokenize } from "./lex";
import { tokenize as non_standard_tokenize } from "./extra";

Check failure on line 16 in src/path/environment.ts

View workflow job for this annotation

GitHub Actions / lint

'/home/runner/work/json-p3/json-p3/src/path/extra/index.ts' imported multiple times

Check failure on line 16 in src/path/environment.ts

View workflow job for this annotation

GitHub Actions / lint

'/home/runner/work/json-p3/json-p3/src/path/extra/index.ts' imported multiple times
import { JSONPathNode, JSONPathNodeList } from "./node";
import { Parser } from "./parse";
import { Parser as NonStandardParser } from "./extra";

Check failure on line 19 in src/path/environment.ts

View workflow job for this annotation

GitHub Actions / lint

'/home/runner/work/json-p3/json-p3/src/path/extra/index.ts' imported multiple times

Check failure on line 19 in src/path/environment.ts

View workflow job for this annotation

GitHub Actions / lint

'/home/runner/work/json-p3/json-p3/src/path/extra/index.ts' imported multiple times
import { JSONPath } from "./path";
import { Token, TokenStream } from "./token";
import { JSONValue } from "../types";
import { CurrentKey } from "./extra/expression";

/**
* JSONPath environment options. The defaults are in compliance with JSONPath
Expand Down Expand Up @@ -104,7 +107,9 @@ export class JSONPathEnvironment {
*/
public functionRegister: Map<string, FilterFunction> = new Map();

private parser: Parser;
// TODO: have non-standard parser inherit from Parser?
private parser: Parser | NonStandardParser;
private tokenize: (path: string) => Token[];

/**
* @param options - Environment configuration options.
Expand All @@ -115,7 +120,15 @@ export class JSONPathEnvironment {
this.minIntIndex = options.maxIntIndex ?? -Math.pow(2, 53) - 1;
this.maxRecursionDepth = options.maxRecursionDepth ?? 50;
this.nondeterministic = options.nondeterministic ?? false;
this.parser = new Parser(this);

if (this.strict) {
this.parser = new Parser(this);
this.tokenize = tokenize;
} else {
this.parser = new NonStandardParser(this);
this.tokenize = non_standard_tokenize;
}

this.setupFilterFunctions();
}

Expand All @@ -126,7 +139,7 @@ export class JSONPathEnvironment {
public compile(path: string): JSONPath {
return new JSONPath(
this,
this.parser.parse(new TokenStream(tokenize(path))),
this.parser.parse(new TokenStream(this.tokenize(path))),
);
}

Expand Down Expand Up @@ -232,6 +245,7 @@ export class JSONPathEnvironment {
if (
!(
arg instanceof FilterExpressionLiteral ||
arg instanceof CurrentKey ||
(arg instanceof JSONPathQuery && arg.path.singularQuery()) ||
(arg instanceof FunctionExtension &&
this.functionRegister.get(arg.name)?.returnType ===
Expand Down
12 changes: 12 additions & 0 deletions src/path/extra/expression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { FilterExpression } from "../expression";
import { FilterContext, Nothing } from "../types";

export class CurrentKey extends FilterExpression {
public evaluate(context: FilterContext): string | number | typeof Nothing {
return context.currentKey ?? Nothing;
}

public toString(): string {
return "#";
}
}
2 changes: 2 additions & 0 deletions src/path/extra/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { tokenize } from "./lex";
export { Parser } from "./parse";
Loading

0 comments on commit 0d21ccf

Please sign in to comment.