Skip to content

Commit

Permalink
Refactor and update change log
Browse files Browse the repository at this point in the history
  • Loading branch information
jg-rp committed Jan 23, 2025
1 parent 25841b6 commit d6d44e1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# JSON P3 Change Log

## Version 2.1.0 (unreleased)

**Changes**

- Fixed `JSONPathQuery` serialization. `JSONPathQuery.toString()` was not handling name selectors containing `'` or `\`, and was a bit vague about the format serialized paths would use. `JSONPathQuery.toString()` now accepts an options object with a single `form` option. `form` can be one of `"pretty"` (the default) or `"canonical"`. The canonical format uses bracket notation and single quotes, whereas the pretty format uses shorthand notation where possible and double quotes. See [issue #30](https://github.com/jg-rp/json-p3/issues/30) and [PR #32](https://github.com/jg-rp/json-p3/pull/32).
- Added `JSONPathNode.getPath(options?)`, which returns a string representation of the node's location. As above, the `form` option can be one of `"pretty"` (the default) or `"canonical"`.
- Deprecated `JSONPathNode.path` in favour of `JSONPathNode.getPath(options?)`.
- Changed the string representation of _filter selectors_. Both canonical and pretty formats now only include parentheses where necessary.

## Version 2.0.0

**Breaking changes**
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": "2.0.0",
"version": "2.1.0",
"author": "James Prior",
"license": "MIT",
"description": "JSONPath, JSON Pointer and JSON Patch",
Expand Down
22 changes: 12 additions & 10 deletions src/path/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@ export class LogicalExpression extends FilterExpression {
expression: FilterExpression,
parentPrecedence: number,
): string {
let precedence: number;
let op: string;
let left: string;
let right: string;

if (expression instanceof InfixExpression) {
let precedence: number;
let op: string;
let left: string;
let right: string;

if (expression.operator === "&&") {
precedence = PRECEDENCE_LOGICAL_AND;
op = "&&";
Expand All @@ -213,16 +213,18 @@ export class LogicalExpression extends FilterExpression {
} else {
return expression.toString(options);
}
} else if (expression instanceof PrefixExpression) {

const expr = `${left} ${op} ${right}`;
return precedence < parentPrecedence ? `(${expr})` : expr;
}

if (expression instanceof PrefixExpression) {
const operand = _toString(expression.right, PRECEDENCE_PREFIX);
const expr = `!${operand}`;
return parentPrecedence > PRECEDENCE_PREFIX ? `(${expr})` : expr;
} else {
return expression.toString(options);
}

const expr = `${left} ${op} ${right}`;
return precedence < parentPrecedence ? `(${expr})` : expr;
return expression.toString(options);
}

return _toString(this.expression, 0);
Expand Down

0 comments on commit d6d44e1

Please sign in to comment.