Skip to content

Commit 5f16af4

Browse files
rewrite indent parsing, added function type support
1 parent 784fde4 commit 5f16af4

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const config: ConfigType = {
2424
case 'object': return '#569cd6';
2525
case 'undefined': return '#569cd6';
2626
case 'boolean': return '#569cd6';
27+
case 'function': return '#dcdcaa';
2728
default: return '#ce9178';
2829
}
2930
},

src/json_parser/parser.service.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,20 @@ export class ParserService {
2323
if (typeof obj !== "object" || obj === null || obj === undefined) {
2424
const quotes = config.typeQuotes(typeof obj);
2525
const color = config.typeColor(typeof obj);
26-
return `<tspan style="fill: ${color};">${quotes}${obj}${quotes}</tspan>`;
26+
const value = typeof obj !== 'function' ? obj : '&lt;function&gt;';
27+
return `<tspan style="fill: ${color};">${quotes}${value}${quotes}</tspan>`;
2728
}
2829

2930
const entries = Object.entries(obj)
3031
.map(([key, value], index, array) => {
3132
const formattedValue = this.parse(value, indent, depth + 1);
3233
const comma = index < array.length - 1 ? ',' : '';
3334

34-
return `<tspan x="${nextIndent}" dy="19">` +
35+
return (
36+
`<tspan x="${nextIndent}" dy="19">` +
3537
`<tspan style="fill: ${config.colors.keys};">"${key}"</tspan>: ${formattedValue}${comma}` +
36-
`</tspan>`;
38+
`</tspan>`
39+
);
3740
})
3841
.join(`\n`);
3942

@@ -59,25 +62,21 @@ export class ParserService {
5962
lineIndex: { current: number } = { current: 2 },
6063
depth: number = 0
6164
): ObjectStructureInfo[] {
62-
if (typeof obj !== 'object' || obj === null) {
63-
return [];
64-
}
65+
if (typeof obj !== 'object' || obj === null) return [];
6566

6667
const result: ObjectStructureInfo[] = [];
67-
const keys = Object.keys(obj);
68-
69-
for (const key of keys) {
70-
const startLine = lineIndex.current++;
7168

72-
if (typeof obj[key] === 'object' && obj[key] !== null) {
73-
result.push({ key, startLine, endLine: 0, depth });
74-
const children = this.parseObjectStructure(obj[key], lineIndex, depth + 1);
75-
result.push(...children);
76-
const endLine = lineIndex.current++;
77-
result.find(item => item.key === key && item.startLine === startLine)!.endLine = endLine;
78-
}
79-
}
69+
Object.entries(obj)
70+
.forEach(([key, value]) => {
71+
const startLine = lineIndex.current++;
8072

73+
if (typeof value === 'object' && value !== null) {
74+
result.push({ key, startLine, endLine: 0, depth });
75+
result.push(...this.parseObjectStructure(value, lineIndex, depth + 1));
76+
const endLinde = lineIndex.current++;
77+
result.find(item => item.key === key && item.startLine === startLine)!.endLine = endLinde;
78+
}
79+
});
8180
return result;
8281
}
8382
}

0 commit comments

Comments
 (0)