diff --git a/demo/index.html b/demo/index.html index a23c294..40a9dff 100644 --- a/demo/index.html +++ b/demo/index.html @@ -76,6 +76,7 @@

JSONPath Demo (To demo on Node instead, see the diff --git a/demo/index.js b/demo/index.js index 64e7c2f..b9db8bd 100644 --- a/demo/index.js +++ b/demo/index.js @@ -1,4 +1,5 @@ -/* globals JSONPath -- Test UMD */ +/// +/* globals JSONPath, LZString -- Test UMD */ /* eslint-disable import/unambiguous -- Demo */ // Todo: Extract testing example paths/contents and use for a @@ -14,8 +15,38 @@ const $ = (s) => document.querySelector(s); const jsonpathEl = $('#jsonpath'); +const jsonSample = $('#jsonSample'); + +const updateUrl = () => { + const path = jsonpathEl.value; + const jsonText = LZString.compressToEncodedURIComponent(jsonSample.value); + const url = new URL(location.href); + url.searchParams.set('path', path); + url.searchParams.set('json', jsonText); + url.searchParams.set('eval', $('#eval').value); + url.searchParams.set('ignoreEvalErrors', $('#ignoreEvalErrors').value); + history.replaceState(null, '', url.toString()); +}; + +const loadUrl = () => { + const url = new URL(location.href); + if (url.searchParams.has('path')) { + jsonpathEl.value = url.searchParams.get('path'); + } + if (url.searchParams.has('json')) { + jsonSample.value = LZString.decompressFromEncodedURIComponent( + url.searchParams.get('json') + ); + } + if (url.searchParams.has('eval')) { + $('#eval').value = url.searchParams.get('eval'); + } + if (url.searchParams.has('ignoreEvalErrors')) { + $('#ignoreEvalErrors').value = url.searchParams.get('ignoreEvalErrors'); + } +}; + const updateResults = () => { - const jsonSample = $('#jsonSample'); const reportValidity = () => { // Doesn't work without a timeout setTimeout(() => { @@ -52,19 +83,26 @@ const updateResults = () => { }; $('#jsonpath').addEventListener('input', () => { + updateUrl(); updateResults(); }); $('#jsonSample').addEventListener('input', () => { + updateUrl(); updateResults(); }); $('#eval').addEventListener('change', () => { + updateUrl(); updateResults(); }); $('#ignoreEvalErrors').addEventListener('change', () => { + updateUrl(); updateResults(); }); -window.addEventListener('load', updateResults); +window.addEventListener('load', () => { + loadUrl(); + updateResults(); +}); diff --git a/demo/types.d.ts b/demo/types.d.ts new file mode 100644 index 0000000..cf4aac6 --- /dev/null +++ b/demo/types.d.ts @@ -0,0 +1,12 @@ +import '../src/jsonpath.d.ts' +import type { JSONPathType } from 'jsonpath-plus'; + +declare global { + var LZString: { + decompressFromEncodedURIComponent: (value: string) => string; + compressToEncodedURIComponent: (value: string) => string; + }; + var JSONPath: { + JSONPath: JSONPathType + } +}