Skip to content

Commit ed94c99

Browse files
committed
Fix search and add tests
1 parent 3b6cf8d commit ed94c99

File tree

7 files changed

+38
-9
lines changed

7 files changed

+38
-9
lines changed
Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/demo.82d29495.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/demo.e80c5b10.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8"> <title>Big JSON Viewer Demo</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <link rel="stylesheet" href="default.f39b0572.css"> <style>body{font-family:Helvetica,serif}</style> </head> <body> <div class="p-3" style="position:fixed;top:0;right:0;background:#fff;border:1px solid #ccc"> <input type="text" placeholder="Search" id="search"> <span id="searchInfo"></span> </div> <div class="container"> <h1 class="mt-3">Big JSON Viewer Demo</h1> <p> Watch large JSON structures in the Browser.<br> <a href="https://github.com/dhcode/big-json-viewer">On GitHub</a> </p> <h4 class="mt-3">JSON text</h4> <div> <a href="javascript:" data-load="simpleData">Simple test data</a> | <a href="javascript:" data-load="largeData">Large data</a> </div> <div> <textarea id="code" style="min-height:15em;width:100%;font-family:monospace,serif;font-size:.8em"></textarea> </div> <h4 class="mt-3">Big JSON Viewer output</h4> <div id="viewer"> </div> <h5 class="mt-3">Open paths</h5> <div> <textarea id="paths" style="min-height:6em;width:100%;font-family:monospace,serif;font-size:.8em"></textarea> </div> <h5 class="mt-3">Copied paths</h5> <div> <input type="text" id="copied" style="width:100%;font-family:monospace,serif;font-size:.8em"> </div> <footer class="mt-5"> Created by <a href="https://github.com/dhcode">Dominik Herbst</a> </footer> </div> <script src="demo.e80c5b10.js"></script> </body> </html>
1+
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8"> <title>Big JSON Viewer Demo</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <link rel="stylesheet" href="default.f39b0572.css"> <style>body{font-family:Helvetica,serif}</style> </head> <body> <div class="p-3" style="position:fixed;top:0;right:0;background:#fff;border:1px solid #ccc"> <input type="text" placeholder="Search" id="search"> <span id="searchInfo"></span> </div> <div class="container"> <h1 class="mt-3">Big JSON Viewer Demo</h1> <p> Watch large JSON structures in the Browser.<br> <a href="https://github.com/dhcode/big-json-viewer">On GitHub</a> </p> <h4 class="mt-3">JSON text</h4> <div> <a href="javascript:" data-load="simpleData">Simple test data</a> | <a href="javascript:" data-load="largeData">Large data</a> </div> <div> <textarea id="code" style="min-height:15em;width:100%;font-family:monospace,serif;font-size:.8em"></textarea> </div> <h4 class="mt-3">Big JSON Viewer output</h4> <div id="viewer"> </div> <h5 class="mt-3">Open paths</h5> <div> <textarea id="paths" style="min-height:6em;width:100%;font-family:monospace,serif;font-size:.8em"></textarea> </div> <h5 class="mt-3">Copied paths</h5> <div> <input type="text" id="copied" style="width:100%;font-family:monospace,serif;font-size:.8em"> </div> <footer class="mt-5"> Created by <a href="https://github.com/dhcode">Dominik Herbst</a> </footer> </div> <script src="demo.82d29495.js"></script> </body> </html>

src/big-json-viewer-dom.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ describe('Big JSON Viewer', function() {
137137
expect(cursor2.matches.length).toEqual(1);
138138
expect(root.getOpenPaths()).toEqual([['test']]);
139139

140+
const cursor3 = await viewer.openBySearch(/notExisting/);
141+
expect(cursor3).toBeTruthy();
142+
expect(cursor3.matches.length).toEqual(0);
143+
140144
viewer.destroy();
141145
});
142146

src/big-json-viewer-dom.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,10 +359,14 @@ export class BigJsonViewerDom {
359359
async navigateTo(index: number) {
360360
this.index = index;
361361
const match = this.matches[index];
362-
const openedElement = await viewer.openSearchMatch(
363-
nodeElement,
364-
this.matches[index]
365-
);
362+
if (!match) {
363+
console.warn(
364+
'searchIndex does not exist on ' + this.matches.length,
365+
index
366+
);
367+
return;
368+
}
369+
const openedElement = await viewer.openSearchMatch(nodeElement, match);
366370
if (openedElement) {
367371
if (openedElement.scrollIntoView) {
368372
openedElement.scrollIntoView({ block: 'center' });
@@ -397,7 +401,9 @@ export class BigJsonViewerDom {
397401

398402
this.dispatchNodeEvent('openedNodes', nodeElement);
399403

400-
await cursor.navigateTo(0);
404+
if (matches.length) {
405+
await cursor.navigateTo(0);
406+
}
401407

402408
return cursor;
403409
}

src/parser/js-parser.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ describe('JS JSON Parser', function() {
77
expect(info).toBeNull();
88
});
99

10+
it('should handle symbol with error', function() {
11+
const symbol = Symbol('a');
12+
const instance = new JsParser(symbol);
13+
expect(() => {
14+
const info = instance.getRootNodeInfo();
15+
}).toThrow();
16+
});
17+
18+
it('should handle function with error', function() {
19+
const func = function() {};
20+
const instance = new JsParser(func);
21+
expect(() => {
22+
const info = instance.getRootNodeInfo();
23+
}).toThrow();
24+
});
25+
1026
it('should handle empty object', function() {
1127
const instance = new JsParser({});
1228
const info = instance.getRootNodeInfo();
@@ -213,6 +229,9 @@ describe('JS JSON Parser', function() {
213229
expect(nodes.length).toEqual(2);
214230
expectStringNode(nodes[0], 'd', ['3']);
215231
expectStringNode(nodes[1], 'e', ['4']);
232+
233+
const node = info.getByIndex(1);
234+
expectStringNode(node, 'b', ['1']);
216235
});
217236

218237
it('should handle object pagination', function() {

0 commit comments

Comments
 (0)