1- /// <reference path="./types.d.ts" />
1+ /* eslint-disable import-x/unambiguous -- Demo */
22/* globals JSONPath, LZString -- Test UMD */
3- /* eslint-disable import/unambiguous -- Demo */
3+ /// <reference path="./types.d.ts" />
44
55// Todo: Extract testing example paths/contents and use for a
66// pulldown that can populate examples
1212// Todo: Could add JSON/JS syntax highlighting in sample and result,
1313// ideally with a jsonpath-plus parser highlighter as well
1414
15- const $ = ( s ) => document . querySelector ( s ) ;
15+ /**
16+ * @param {string } s
17+ * @returns {HTMLElement }
18+ */
19+ const $ = ( s ) => /** @type {HTMLElement } */ ( document . querySelector ( s ) ) ;
1620
17- const jsonpathEl = $ ( '#jsonpath' ) ;
18- const jsonSample = $ ( '#jsonSample' ) ;
21+ /**
22+ * @param {string } s
23+ * @returns {HTMLInputElement }
24+ */
25+ const $i = ( s ) => /** @type {HTMLInputElement } */ ( document . querySelector ( s ) ) ;
26+
27+ const jsonpathEl = $i ( '#jsonpath' ) ;
28+ const jsonSample = $i ( '#jsonSample' ) ;
1929
2030const updateUrl = ( ) => {
2131 const path = jsonpathEl . value ;
2232 const jsonText = LZString . compressToEncodedURIComponent ( jsonSample . value ) ;
2333 const url = new URL ( location . href ) ;
2434 url . searchParams . set ( 'path' , path ) ;
2535 url . searchParams . set ( 'json' , jsonText ) ;
26- url . searchParams . set ( 'eval' , $ ( '#eval' ) . value ) ;
27- url . searchParams . set ( 'ignoreEvalErrors' , $ ( '#ignoreEvalErrors' ) . value ) ;
28- history . replaceState ( null , '' , url . toString ( ) ) ;
36+ url . searchParams . set ( 'eval' , $i ( '#eval' ) . value ) ;
37+ url . searchParams . set ( 'ignoreEvalErrors' , $i ( '#ignoreEvalErrors' ) . value ) ;
38+ history . replaceState ( null , '' , url . href ) ;
2939} ;
3040
3141const loadUrl = ( ) => {
3242 const url = new URL ( location . href ) ;
3343 if ( url . searchParams . has ( 'path' ) ) {
34- jsonpathEl . value = url . searchParams . get ( 'path' ) ;
44+ jsonpathEl . value = /** @type { string } */ ( url . searchParams . get ( 'path' ) ) ;
3545 }
3646 if ( url . searchParams . has ( 'json' ) ) {
3747 jsonSample . value = LZString . decompressFromEncodedURIComponent (
38- url . searchParams . get ( 'json' )
48+ /** @type { string } */ ( url . searchParams . get ( 'json' ) )
3949 ) ;
4050 }
4151 if ( url . searchParams . has ( 'eval' ) ) {
42- $ ( '#eval' ) . value = url . searchParams . get ( 'eval' ) ;
52+ $i ( '#eval' ) . value = /** @type {string } */ (
53+ url . searchParams . get ( 'eval' )
54+ ) ;
4355 }
4456 if ( url . searchParams . has ( 'ignoreEvalErrors' ) ) {
45- $ ( '#ignoreEvalErrors' ) . value = url . searchParams . get ( 'ignoreEvalErrors' ) ;
57+ $i ( '#ignoreEvalErrors' ) . value = /** @type {string } */ (
58+ url . searchParams . get ( 'ignoreEvalErrors' )
59+ ) ;
4660 }
4761} ;
4862
@@ -52,7 +66,7 @@ const updateResults = () => {
5266 setTimeout ( ( ) => {
5367 jsonSample . reportValidity ( ) ;
5468 jsonpathEl . reportValidity ( ) ;
55- } ) ;
69+ } , 0 ) ;
5670 } ;
5771 let json ;
5872 jsonSample . setCustomValidity ( '' ) ;
@@ -61,24 +75,29 @@ const updateResults = () => {
6175 try {
6276 json = JSON . parse ( jsonSample . value ) ;
6377 } catch ( err ) {
64- jsonSample . setCustomValidity ( 'Error parsing JSON: ' + err . toString ( ) ) ;
78+ jsonSample . setCustomValidity (
79+ 'Error parsing JSON: ' +
80+ /** @type {Error } */ ( err ) . toString ( )
81+ ) ;
6582 reportValidity ( ) ;
6683 return ;
6784 }
6885 try {
6986 const result = new JSONPath . JSONPath ( {
7087 path : jsonpathEl . value ,
7188 json,
72- eval : $ ( '#eval' ) . value === 'false' ? false : $ ( '#eval' ) . value ,
73- ignoreEvalErrors : $ ( '#ignoreEvalErrors' ) . value === 'true'
89+ eval : $i ( '#eval' ) . value === 'false' ? false : $i ( '#eval' ) . value ,
90+ ignoreEvalErrors : $i ( '#ignoreEvalErrors' ) . value === 'true'
7491 } ) ;
75- $ ( '#results' ) . value = JSON . stringify ( result , null , 2 ) ;
92+ $i ( '#results' ) . value = JSON . stringify ( result , null , 2 ) ;
7693 } catch ( err ) {
7794 jsonpathEl . setCustomValidity (
78- 'Error executing JSONPath: ' + err . toString ( )
95+ 'Error executing JSONPath: ' +
96+ /** @type {Error } */
97+ ( err ) . toString ( )
7998 ) ;
8099 reportValidity ( ) ;
81- $ ( '#results' ) . value = '' ;
100+ $i ( '#results' ) . value = '' ;
82101 }
83102} ;
84103
0 commit comments