diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a393f2..d7e2a0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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** diff --git a/package.json b/package.json index bd00473..e5a8e0d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/path/expression.ts b/src/path/expression.ts index 46f106f..e9cc163 100644 --- a/src/path/expression.ts +++ b/src/path/expression.ts @@ -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 = "&&"; @@ -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);