Skip to content

Commit 2463256

Browse files
authored
Merge pull request #33 from jg-rp/export-options
Update docs and export `SerializationOptions`
2 parents c688308 + 72ec2ba commit 2463256

File tree

7 files changed

+748
-914
lines changed

7 files changed

+748
-914
lines changed

docs/docs/quick-start.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,26 @@ console.log(nodes.values()); // [ 'John', 'Sally', 'Jane' ]
147147

148148
`compile()` is also re-exported to JSON P3's top-level namespace.
149149

150+
### Query serialization
151+
152+
A [compiled `JSONPathQuery`](#compilation) can be serialized back to a string using its [`toString()`](./api/namespaces/jsonpath/classes/JSONPathQuery.md#tostring) method. As of version 2.1.0, the default string representation uses shorthand notation where possible and double quotes for names and string literals rather than single quotes.
153+
154+
```javascript
155+
import { jsonpath } from "json-p3";
156+
157+
const path = jsonpath.compile("$['users'][?@.score < 100]['name']");
158+
console.log(path.toString()); // $.users[?@.score < 100].name
159+
```
160+
161+
To serialize a `JSONPathQuery` using the canonical bracket notation and single quotes, pass a [`SerializationOptions`](./api/namespaces/jsonpath/type-aliases/SerializationOptions.md) object as an argument to `toString()`.
162+
163+
```javascript
164+
import { jsonpath } from "json-p3";
165+
166+
const path = jsonpath.compile("$.users[?@.score < 100].name");
167+
console.log(path.toString({ form: "canonical" })); $['users'][?@['score'] < 100]['name']
168+
```
169+
150170
## JSON Pointer
151171
152172
Resolve a JSON Pointer ([RFC 6901](https://datatracker.ietf.org/doc/html/rfc6901)) against some data using `jsonpointer.resolve()`.

docs/package-lock.json

Lines changed: 695 additions & 890 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,28 @@
1414
"write-heading-ids": "docusaurus write-heading-ids"
1515
},
1616
"dependencies": {
17-
"@docusaurus/core": "^3.6.3",
18-
"@docusaurus/preset-classic": "^3.6.3",
17+
"@docusaurus/core": "^3.7.0",
18+
"@docusaurus/preset-classic": "^3.7.0",
1919
"@mdx-js/react": "^3.1.0",
2020
"@monaco-editor/react": "^4.6.0",
2121
"@tsconfig/docusaurus": "^2.0.3",
2222
"allotment": "^1.20.2",
2323
"autoprefixer": "^10.4.20",
2424
"clsx": "^2.1.1",
25-
"docusaurus-plugin-typedoc": "^1.1.1",
25+
"docusaurus-plugin-typedoc": "^1.2.2",
2626
"monaco-editor": "^0.52.0",
2727
"monaco-themes": "^0.4.4",
2828
"postcss": "^8.4.49",
2929
"prism-react-renderer": "^2.4.0",
3030
"react": "^18.3.1",
3131
"react-dom": "^18.3.1",
3232
"tailwindcss": "^3.4.16",
33-
"typedoc": "^0.27.4",
34-
"typedoc-plugin-markdown": "^4.3.2",
33+
"typedoc": "^0.27.6",
34+
"typedoc-plugin-markdown": "^4.4.1",
3535
"typescript": "^5.6.2"
3636
},
3737
"devDependencies": {
38-
"@docusaurus/module-type-aliases": "^3.3.2",
38+
"@docusaurus/module-type-aliases": "^3.7.0",
3939
"prettier": "^3.4.2",
4040
"prettier-plugin-tailwindcss": "^0.6.9"
4141
},

docs/sidebars.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ const sidebars = {
3535
],
3636
API: [
3737
{
38-
type: "autogenerated",
39-
dirName: "api",
38+
type: "category",
39+
label: "Typedoc API",
40+
link: {
41+
type: "doc",
42+
id: "api/index",
43+
},
44+
items: require("./docs/api/typedoc-sidebar.cjs"),
4045
},
4146
],
4247
};

src/path/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ export {
2929
} from "./errors";
3030

3131
export { Nothing, KEY_MARK } from "./types";
32-
export type { JSONPathValue, FilterContext } from "./types";
32+
export type {
33+
JSONPathValue,
34+
FilterContext,
35+
SerializationOptions,
36+
} from "./types";
3337

3438
export const DEFAULT_ENVIRONMENT = new JSONPathEnvironment();
3539

src/path/path.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,18 @@ import { JSONPathSegment, DescendantSegment } from "./segments";
66
import { SerializationOptions } from "./types";
77

88
/**
9-
*
9+
* A compiled JSONPath query ready to be applied to different data repeatedly.
1010
*/
1111
export class JSONPathQuery {
12-
/**
13-
*
14-
* @param environment -
15-
* @param segments -
16-
*/
1712
constructor(
1813
readonly environment: JSONPathEnvironment,
1914
readonly segments: JSONPathSegment[],
2015
) {}
2116

2217
/**
23-
*
24-
* @param value -
25-
* @returns
18+
* Apply this JSONPath query to _value_.
19+
* @param value - A JSON-like object to apply this query to.
20+
* @returns Nodes matched by applying this query to _value_.
2621
*/
2722
public query(value: JSONValue): JSONPathNodeList {
2823
let nodes = [new JSONPathNode(value, [], value)];
@@ -33,9 +28,9 @@ export class JSONPathQuery {
3328
}
3429

3530
/**
36-
*
37-
* @param value -
38-
* @returns
31+
* Apply this JSONPath query to _value_.
32+
* @param value - A JSON-like object to apply this query to.
33+
* @returns An iterator over nodes matched by applying this query to _value_.
3934
*/
4035
public lazyQuery(value: JSONValue): IterableIterator<JSONPathNode> {
4136
let nodes: IterableIterator<JSONPathNode> = [
@@ -66,10 +61,12 @@ export class JSONPathQuery {
6661
* Return a string representation of this query.
6762
*/
6863
public toString(options?: SerializationOptions): string {
69-
// return this.prettyPath();
7064
return `$${this.segments.map((s) => s.toString(options)).join("")}`;
7165
}
7266

67+
/**
68+
* Return `true` if this query is a _singular query_, or `false` otherwise.
69+
*/
7370
public singularQuery(): boolean {
7471
for (const segment of this.segments) {
7572
if (segment instanceof DescendantSegment) return false;

src/path/types.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const Nothing = Symbol.for("jsonpath.nothing");
99
export type JSONPathValue = JSONValue | typeof Nothing;
1010

1111
/**
12-
* Object passed to FilterExpression.evaluate().
12+
* Object passed to `FilterExpression.evaluate()`.
1313
*/
1414
export type FilterContext = {
1515
environment: JSONPathEnvironment;
@@ -31,7 +31,9 @@ export function hasStringKey(
3131

3232
export const KEY_MARK = "\x02";
3333

34-
/** Options for serializing paths */
34+
/**
35+
* Options for serializing paths.
36+
*/
3537
export type SerializationOptions = {
3638
/**
3739
* `pretty` paths always use:
@@ -50,6 +52,7 @@ export type SerializationOptions = {
5052
*/
5153
form: "pretty" | "canonical";
5254
};
55+
5356
export const defaultSerializationOptions: SerializationOptions = {
5457
form: "pretty",
5558
};

0 commit comments

Comments
 (0)