Skip to content

Commit 66c961c

Browse files
committed
- Enhancement: Add demo
- Docs: Fix browser usage example
1 parent 4128b69 commit 66c961c

File tree

6 files changed

+102
-1
lines changed

6 files changed

+102
-1
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = {
66
"console",
77
"Date.now",
88
"document.head",
9+
"document.querySelector",
910
"JSON",
1011
"Object.keys",
1112
"XMLHttpRequest"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ magic is necessary:
6767

6868
<script src="node_modules/jsonpath-plus/dist/index-umd.js"></script>
6969
<script>
70-
const result = JSONPath({path: '...', json: ...});
70+
const result = JSONPath.JSONPath({path: '...', json: ...});
7171
</script>
7272
```
7373

demo/index.css

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#jsonpath {
2+
width: 90%;
3+
margin-bottom: 10px;
4+
}
5+
6+
.container {
7+
float: left;
8+
width: 48%;
9+
}
10+
11+
.container textarea {
12+
margin: 2%;
13+
width: 98%;
14+
height: 565px;
15+
}
16+
17+
#demoNode {
18+
font-size: small;
19+
}

demo/index.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<html lang="en" dir="ltr">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>JSONPath Demo</title>
6+
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon" />
7+
<link rel="stylesheet" href="index.css" />
8+
</head>
9+
<body>
10+
<h2>JSONPath Demo <i id="demoNode">(To demo on Node instead, see the <a href="https://npm.runkit.com/jsonpath-plus">library on Runkit</a>.)</i>
11+
</h2>
12+
<form>
13+
<div>
14+
<label><b>JSONPath:</b>
15+
<input id="jsonpath" placeholder="$.books" />
16+
</label>
17+
</div>
18+
<div id="jsonSampleContainer" class="container">
19+
<label><b>JSON sample:</b>
20+
<textarea id="jsonSample" placeholder="{&quot;books&quot;: []}"></textarea>
21+
</label>
22+
</div>
23+
<div id="resultContainer" class="container">
24+
<label><b>Results:</b>
25+
<textarea id="results"></textarea>
26+
</label>
27+
</div>
28+
</form>
29+
<script src="../dist/index-umd.js"></script>
30+
<script src="index.js"></script>
31+
</body>
32+
</html>

demo/index.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* globals JSONPath */
2+
/* eslint-disable import/unambiguous */
3+
4+
// Todo: Extract testing example paths/contents and use for a
5+
// pulldown that can populate examples
6+
7+
// Todo: Make configurable with other JSONPath options
8+
9+
// Todo: Allow source to be treated as an (evaled) JSON object
10+
11+
// Todo: Could add JSON/JS syntax highlighting in sample and result,
12+
// ideally with a jsonpath-plus parser highlighter as well
13+
14+
const $ = (s) => document.querySelector(s);
15+
16+
const updateResults = () => {
17+
const jsonSample = $('#jsonSample');
18+
const reportValidity = () => {
19+
// Doesn't work without a timeout
20+
setTimeout(() => {
21+
jsonSample.reportValidity();
22+
});
23+
};
24+
let json;
25+
try {
26+
json = JSON.parse(jsonSample.value);
27+
jsonSample.setCustomValidity('');
28+
reportValidity();
29+
} catch (err) {
30+
jsonSample.setCustomValidity('Error parsing JSON: ' + err.toString());
31+
reportValidity();
32+
return;
33+
}
34+
const result = JSONPath.JSONPath({
35+
path: $('#jsonpath').value,
36+
json
37+
});
38+
39+
$('#results').value = JSON.stringify(result, null, 2);
40+
};
41+
42+
$('#jsonpath').addEventListener('change', () => {
43+
updateResults();
44+
});
45+
46+
$('#jsonSample').addEventListener('change', () => {
47+
updateResults();
48+
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
"open-docs": "open-cli http://localhost:8084/docs/ts/ && npm start",
9595
"mocha": "mocha --require test-helpers/node-env.js test",
9696
"test": "npm run rollup && npm run mocha",
97+
"open": "open-cli http://localhost:8084/demo/ && npm start",
9798
"start": "static -p 8084",
9899
"rollup": "rollup -c",
99100
"eslint": "eslint --ext js,md,html .",

0 commit comments

Comments
 (0)