Skip to content

Commit

Permalink
rewrite indent parsing, added function type support
Browse files Browse the repository at this point in the history
  • Loading branch information
Andcool-Systems committed Dec 21, 2024
1 parent 784fde4 commit 5f16af4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const config: ConfigType = {
case 'object': return '#569cd6';
case 'undefined': return '#569cd6';
case 'boolean': return '#569cd6';
case 'function': return '#dcdcaa';
default: return '#ce9178';
}
},
Expand Down
35 changes: 17 additions & 18 deletions src/json_parser/parser.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@ export class ParserService {
if (typeof obj !== "object" || obj === null || obj === undefined) {
const quotes = config.typeQuotes(typeof obj);
const color = config.typeColor(typeof obj);
return `<tspan style="fill: ${color};">${quotes}${obj}${quotes}</tspan>`;
const value = typeof obj !== 'function' ? obj : '&lt;function&gt;';
return `<tspan style="fill: ${color};">${quotes}${value}${quotes}</tspan>`;
}

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

return `<tspan x="${nextIndent}" dy="19">` +
return (
`<tspan x="${nextIndent}" dy="19">` +
`<tspan style="fill: ${config.colors.keys};">"${key}"</tspan>: ${formattedValue}${comma}` +
`</tspan>`;
`</tspan>`
);
})
.join(`\n`);

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

const result: ObjectStructureInfo[] = [];
const keys = Object.keys(obj);

for (const key of keys) {
const startLine = lineIndex.current++;

if (typeof obj[key] === 'object' && obj[key] !== null) {
result.push({ key, startLine, endLine: 0, depth });
const children = this.parseObjectStructure(obj[key], lineIndex, depth + 1);
result.push(...children);
const endLine = lineIndex.current++;
result.find(item => item.key === key && item.startLine === startLine)!.endLine = endLine;
}
}
Object.entries(obj)
.forEach(([key, value]) => {
const startLine = lineIndex.current++;

if (typeof value === 'object' && value !== null) {
result.push({ key, startLine, endLine: 0, depth });
result.push(...this.parseObjectStructure(value, lineIndex, depth + 1));
const endLinde = lineIndex.current++;
result.find(item => item.key === key && item.startLine === startLine)!.endLine = endLinde;
}
});
return result;
}
}

0 comments on commit 5f16af4

Please sign in to comment.