Skip to content

Commit 17890ec

Browse files
fix
1 parent ec1590a commit 17890ec

1 file changed

Lines changed: 20 additions & 22 deletions

File tree

src/formatters/HeapSnapshotFormatter.ts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ export interface FormattedSnapshotEntry {
1616
retainedSize: number;
1717
}
1818

19-
export interface FormattedNodeEntry {
20-
id: number;
21-
name: string;
22-
type: string;
23-
distance: number;
24-
selfSize: number;
25-
retainedSize: number;
19+
function isNodeLike(
20+
item: unknown,
21+
): item is DevTools.HeapSnapshotModel.HeapSnapshotModel.Node {
22+
return (
23+
typeof item === 'object' && item !== null && 'id' in item && 'name' in item
24+
);
2625
}
2726

2827
export class HeapSnapshotFormatter {
@@ -32,24 +31,23 @@ export class HeapSnapshotFormatter {
3231
this.#aggregates = aggregates;
3332
}
3433

35-
static formatNodes(items: readonly unknown[]): string {
34+
static formatNodes(
35+
items: ReadonlyArray<
36+
| DevTools.HeapSnapshotModel.HeapSnapshotModel.Node
37+
| DevTools.HeapSnapshotModel.HeapSnapshotModel.Edge
38+
>,
39+
): string {
3640
const lines: string[] = [];
37-
lines.push('id,name,type,distance,selfSize,retainedSize');
41+
42+
if (items.length > 0 && isNodeLike(items[0])) {
43+
lines.push('id,name,type,distance,selfSize,retainedSize');
44+
}
3845

3946
for (const item of items) {
40-
if (typeof item === 'object' && item !== null) {
41-
if (
42-
'id' in item &&
43-
'name' in item &&
44-
'type' in item &&
45-
'distance' in item &&
46-
'selfSize' in item &&
47-
'retainedSize' in item
48-
) {
49-
lines.push(
50-
`${item.id},"${item.name}",${item.type},${item.distance},${item.selfSize},${item.retainedSize}`,
51-
);
52-
}
47+
if (isNodeLike(item)) {
48+
lines.push(
49+
`${item.id},"${item.name}",${item.type},${item.distance},${item.selfSize},${item.retainedSize}`,
50+
);
5351
}
5452
}
5553

0 commit comments

Comments
 (0)