Skip to content

Commit 877919f

Browse files
committed
Handle null, undefined, Date and function
1 parent b7695b5 commit 877919f

2 files changed

Lines changed: 44 additions & 16 deletions

File tree

projects/demo/src/app/app.component.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,18 @@ export class AppComponent {
2525
...structuredClone(this.baseObj),
2626
nested: {
2727
...structuredClone(this.baseObj),
28+
function: () => {
29+
return 'foo';
30+
},
2831
deeplyNested: {
2932
...structuredClone(this.baseObj),
33+
function: () => {
34+
return 'bar';
35+
},
3036
},
3137
},
3238
function: () => {
33-
return 'foo';
39+
return 'baz';
3440
},
3541
};
3642
}

projects/ngx-json-treeview/src/lib/ngx-json-treeview.component.ts

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ export class NgxJsonTreeviewComponent {
8787
segment.description = 'null';
8888
} else if (Array.isArray(segment.value)) {
8989
segment.type = 'array';
90-
const len = segment.value.length;
9190
segment.description = this.previewString(segment.value);
9291
} else if (segment.value instanceof Date) {
9392
segment.type = 'date';
@@ -107,7 +106,11 @@ export class NgxJsonTreeviewComponent {
107106
private previewString(obj: any, limit = 200, stringsLimit = 10) {
108107
let result = '';
109108

110-
if (typeof obj === 'string') {
109+
if (obj === null) {
110+
result += 'null';
111+
} else if (obj === undefined) {
112+
result += 'undefined';
113+
} else if (typeof obj === 'string') {
111114
if (obj.length > stringsLimit) {
112115
result += `"${obj.substring(0, stringsLimit)}…"`;
113116
} else {
@@ -118,21 +121,40 @@ export class NgxJsonTreeviewComponent {
118121
} else if (typeof obj === 'number') {
119122
result += `${obj}`;
120123
} else if (typeof obj === 'object') {
121-
const isArray = Array.isArray(obj);
122-
result += isArray ? `Array[${obj.length}] [` : 'Object {';
123-
for (const key in obj) {
124-
const value = obj[key];
125-
result += isArray ? '' : `${key}: `;
126-
if (result.length >= limit) {
127-
return result.substring(0, limit);
124+
if (obj instanceof Date) {
125+
result += obj.toISOString();
126+
} else if (Array.isArray(obj)) {
127+
result += `Array[${obj.length}] [`;
128+
for (const key in obj) {
129+
if (result.length >= limit) {
130+
break;
131+
}
132+
result += this.previewString(obj[key], limit - result.length);
133+
result += ', ';
128134
}
129-
result += this.previewString(value, limit - result.length);
130-
result += `, `;
131-
}
132-
if (result.endsWith(', ')) {
133-
result = result.slice(0, -2);
135+
if (result.endsWith(', ')) {
136+
result = result.slice(0, -2);
137+
}
138+
result += ']';
139+
} else {
140+
result += 'Object {';
141+
for (const key in obj) {
142+
if (result.length >= limit) {
143+
break;
144+
}
145+
if (obj[key] !== undefined) {
146+
result += `${key}: `;
147+
result += this.previewString(obj[key], limit - result.length);
148+
result += ', ';
149+
}
150+
}
151+
if (result.endsWith(', ')) {
152+
result = result.slice(0, -2);
153+
}
154+
result += '}';
134155
}
135-
result += isArray ? ']' : '}';
156+
} else if (typeof obj === 'function') {
157+
result += 'Function';
136158
}
137159

138160
if (result.length >= limit) {

0 commit comments

Comments
 (0)