Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class JsonProcessor implements Processor {
let value = resMap[key];
const isBool = typeof value === "boolean";
const isNumber = this.isNumber(value);
value = value.toString();
const isNull = value === null;
value = (isNull) ? "" : value.toString();

const id: string = idGenerator.generateId(value, {}, ctx);
const segment: Segment = {
Expand Down Expand Up @@ -48,7 +49,9 @@ class JsonProcessor implements Processor {
? {isBool: true}
: isNumber
? {isNumber: true}
: {},
: isNull
? {isNull: true}
: {},
children: [
{
"type": "segment",
Expand Down Expand Up @@ -89,15 +92,23 @@ class JsonProcessor implements Processor {
segmentsMap[segment.id] = segment;
});

const rows: { key: string, value: string | boolean | number }[] = [];
const rows: { key: string, value: string | boolean | number | null }[] = [];

visitParents(data.layout, {tagName: "tr"}, (row: any) => {
const key = row.children[0].children[0].value
const isBool = row.children[1]?.properties?.isBool;
const isNumber = row.children[1]?.properties?.isNumber;

const value = segmentsMap[row.children[1].children[0].id].text;
rows.push({key, value: isBool ? (value === "true") : isNumber ? Number(value) : value});
const isNull = row.children[1]?.properties?.isNull;

const item = segmentsMap[row.children[1].children[0].id].text;
const value = isBool
? (item === "true")
: isNumber
? Number(item)
: isNull
? null
: item;
rows.push({key, value});
});

return JSON.stringify(this._convertMapToJson(rows), null, 2);
Expand Down Expand Up @@ -132,7 +143,7 @@ class JsonProcessor implements Processor {
return result;
}

protected _convertMapToJson(rows: { key: string, value: string | boolean | number }[]) {
protected _convertMapToJson(rows: { key: string, value: string | boolean | number | null }[]) {
const result = {};

rows.forEach(row => {
Expand All @@ -155,7 +166,7 @@ class JsonProcessor implements Processor {
}

const lastKey: string = keys[keys.length - 1];
const lastValue: string | boolean | number = value;
const lastValue: string | boolean | number | null = value;

if (Number.isInteger(Number(lastKey)) && typeof lastValue === "string") {
currentObj.push({[lastValue?.split(':')[0].trim()]: lastValue.split(':')[1].trim()});
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/06.09.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"name": "Kinematics",
"complexity": "average",
"interest": "great",
"bool": true
"bool": true,
"null": null



Expand Down