Skip to content

Commit 20166fb

Browse files
rework geo json and text data parsing (#94)
1 parent c1194c0 commit 20166fb

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

src/renderer/outputLoader.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@ import {csvParse} from 'd3-dsv';
33
const aq = require('arquero');
44
const xmlParser = require('fast-xml-parser');
55

6+
/**
7+
* OutputLoader loads data from notebook cell output item.
8+
*/
69
export class OutputLoader {
710

11+
/**
12+
* Creates new OutputLoader instance.
13+
* @param outputData Notebook cell output item.
14+
* @param mimeType Notebook cell output mime type.
15+
*/
816
constructor (private outputData: OutputItem, private mimeType: string) {
9-
1017
}
1118

1219

@@ -17,24 +24,29 @@ export class OutputLoader {
1724
// try getting JSON data first
1825
const objectData = this.getJsonData(this.outputData);
1926
if (objectData !== undefined) {
27+
if (objectData.features) {
28+
console.log('data.table:format: GeoJSON');
29+
return this.flattenGeoData(objectData);
30+
}
2031
return objectData;
2132
}
2233

2334
// try parsing text data
2435
let textData: string = this.outputData.text();
2536
if (textData.length > 0) {
26-
if (textData.startsWith("'") && textData.endsWith("'")) {
27-
// strip out start/end single quotes from notebook cell output
28-
textData = textData.substr(1, textData.length-2);
29-
}
3037
console.log('data.table:text:', textData.substring(0, Math.min(300, textData.length)), '...');
3138

3239
// see if text data is in json data format
3340
const jsonData = this.getJsonData(textData);
3441
if (jsonData !== undefined) {
42+
if (jsonData.features) {
43+
console.log('data.table:format: GeoJSON');
44+
return this.flattenGeoData(jsonData);
45+
}
3546
return jsonData;
3647
}
3748
else if (textData.startsWith('<?xml version="1.0"')) {
49+
// parse XML data
3850
return this.xmlParse(textData);
3951
}
4052
else if (this.isCsv(textData)) {
@@ -75,6 +87,10 @@ export class OutputLoader {
7587
console.log('data.table:format: JSON array');
7688
return objectData;
7789
}
90+
else {
91+
console.log('data.table:format: JSON');
92+
return objectData;
93+
}
7894
}
7995

8096
// try getting json data object
@@ -85,11 +101,6 @@ export class OutputLoader {
85101
jsonData = jsonData.data;
86102
}
87103

88-
if (jsonData.features) {
89-
console.log('data.table:format: GeoJSON');
90-
jsonData = this.flattenGeoData(jsonData);
91-
}
92-
93104
if (Array.isArray(jsonData)) {
94105
console.log('data.table:format: JSON array');
95106
return jsonData;
@@ -102,11 +113,12 @@ export class OutputLoader {
102113
return csvParse(jsonData);
103114
}
104115
else if (jsonData.startsWith('<?xml version="1.0"')) {
116+
// try to parse XML data as the last resort
105117
return this.xmlParse(jsonData);
106118
}
107119
}
108120
}
109-
catch (error) {
121+
catch (error: any) {
110122
console.log('data.table: JSON.parse error:\n', error.message);
111123
}
112124
return undefined;
@@ -129,6 +141,10 @@ export class OutputLoader {
129141
textData = textData.replace(objectEndRegEx, '}');
130142
textData = textData.replace(xRegEx, ' ');
131143
textData = textData.replace(newLineRegEx, '');
144+
if (textData.startsWith("'") && textData.endsWith("'")) {
145+
// strip out start/end single quotes from notebook cell output
146+
textData = textData.substr(1, textData.length-2);
147+
}
132148
// console.log('data.table:text:', textData.substring(0, Math.min(500, textData.length)), '...');
133149
return textData;
134150
}
@@ -233,7 +249,7 @@ export class OutputLoader {
233249
}
234250
}
235251
}
236-
catch(error) {
252+
catch(error: any) {
237253
console.log('data.table: XML parse error:\n', error.message);
238254
}
239255
return jsonData;

0 commit comments

Comments
 (0)