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
186 changes: 98 additions & 88 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,99 +3,109 @@ import {visitParents} from "unist-util-visit-parents";
import {SegmentsMap} from "./types";

class JsonProcessor implements Processor {
parse(res: string, ctx?: Context): Document {
const idGenerator: IdGenerator = new IdGenerator();
const segments: Segment[] = []
const resJson = JSON.parse(res)
const resMap = this._convertJsonToMap(resJson)
const resKeys = Object.keys(resMap)

const element: any = resKeys.map((key) => {

let value = resMap[key]
const isBool = typeof value === "boolean"
value = value.toString();

const id: string = idGenerator.generateId(value, {}, ctx)
const segment: Segment = {
id,
text: value || "",
};

segments.push(segment);

return {
type: "element",
tagName: "tr",
properties: {},
children: [
{
type: "element",
tagName: "td",
properties: {},
children: [
{
type: "text",
value: key
}
]
},
{
type: "element",
tagName: "td",
properties: isBool ? {isBool: true} : {},
children: [
{
"type": "segment",
"id": id
}
]
}
]
}
})

const layout: LayoutRoot = {
type: "root",
parse(res: string, ctx?: Context): Document {
const idGenerator: IdGenerator = new IdGenerator();
const segments: Segment[] = [];
const resJson = JSON.parse(res);
const resMap = this._convertJsonToMap(resJson);
const resKeys = Object.keys(resMap);

const element: any = resKeys.map((key) => {

let value = resMap[key];
const isBool = typeof value === "boolean";
const isNumber = this.isNumber(value);
value = value.toString();

const id: string = idGenerator.generateId(value, {}, ctx);
const segment: Segment = {
id,
text: value || "",
};

segments.push(segment);

return {
type: "element",
tagName: "tr",
properties: {},
children: [
{
type: "element",
tagName: "td",
properties: {},
children: [
{
type: "element",
tagName: "table",
children: [
{
type: "element",
tagName: "tbody",
properties: {},
children: element
}
],
properties: {}
}
{
type: "text",
value: key
}
]
},
{
type: "element",
tagName: "td",
properties: isBool
? {isBool: true}
: isNumber
? {isNumber: true}
: {},
children: [
{
"type": "segment",
"id": id
}
]
}
]
};
});

const layout: LayoutRoot = {
type: "root",
children: [
{
type: "element",
tagName: "table",
children: [
{
type: "element",
tagName: "tbody",
properties: {},
children: element
}
],
properties: {}
}
]
};

return {segments, layout}
}
return {segments, layout};
}

stringify(data: Document, ctx?: Context): string {
const segmentsMap: SegmentsMap = {};
stringify(data: Document, ctx?: Context): string {
const segmentsMap: SegmentsMap = {};

data.segments.forEach((segment: Segment): void => {
segmentsMap[segment.id] = segment;
});
data.segments.forEach((segment: Segment): void => {
segmentsMap[segment.id] = segment;
});

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

const rows: {key: string, value: string | boolean}[] = []
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;

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

const value = segmentsMap[row.children[1].children[0].id].text
rows.push({key, value: isBool ? (value === "true") : value})
})
return JSON.stringify(this._convertMapToJson(rows), null, 2);
}

return JSON.stringify(this._convertMapToJson(rows), null, 2)
}
protected isNumber(value: any) {
return typeof value === 'number' && !isNaN(value) && Number.isFinite(value);
}

protected _convertJsonToMap(jsonObj: any, parentKey: string = ''): Record<string, any> {
let result: Record<string, any> = {};
Expand All @@ -122,11 +132,11 @@ class JsonProcessor implements Processor {
return result;
}

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

rows.forEach(row => {
const {key, value} = row
const {key, value} = row;
const keys = key.split('.').map(str => str.trim());
let currentObj: any = result;

Expand All @@ -145,10 +155,10 @@ class JsonProcessor implements Processor {
}

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

if (Number.isInteger(Number(lastKey)) && typeof lastValue !== "boolean") {
currentObj.push({ [lastValue?.split(':')[0].trim()]: lastValue.split(':')[1].trim() });
if (Number.isInteger(Number(lastKey)) && typeof lastValue === "string") {
currentObj.push({[lastValue?.split(':')[0].trim()]: lastValue.split(':')[1].trim()});
} else {
currentObj[lastKey] = lastValue;
}
Expand Down
16 changes: 8 additions & 8 deletions test/fixtures/06.09.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"physics":[
{
"name": "Mechanics",
"name": "Mechanics",
"sections":[
{
"name": "Kinematics",
Expand All @@ -10,26 +10,26 @@
"bool": true



},
{
"name": "Hydrodynamics",
"complexity": "4",
"interest": "3",
"interest": 3,
"bool": false

}
]
},
{
"note": "addition",
"useful links":
"useful links":
[
{ "url" :"https://ru.wikipedia.org/wiki/%D0%A4%D0%B8%D0%B7%D0%B8%D0%BA%D0%B0"
{ "url" :"https://ru.wikipedia.org/wiki/%D0%A4%D0%B8%D0%B7%D0%B8%D0%BA%D0%B0"
}
]


},
{
"name": "Electricity",
Expand All @@ -41,6 +41,6 @@
}
]
}

]
}