Skip to content

Commit 0b0cae4

Browse files
authored
properly deal with empty objects (#19)
Fixes #15
2 parents 8737fb7 + ccdbcdb commit 0b0cae4

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

projects/demo/src/app/app.component.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ <h3>Clickable Nodes</h3>
3434
}
3535
</div>
3636

37-
<h3>Primatives</h3>
37+
<h3>Simple Types</h3>
3838
<div class="json-container">
3939
<ngx-json-treeview [json]="13" />
4040
<ngx-json-treeview [json]="'hello, world!'" />
4141
<ngx-json-treeview [json]="true" />
4242
<ngx-json-treeview [json]="null" />
4343
<ngx-json-treeview [json]="{}" />
44+
<ngx-json-treeview [json]="[]" />
4445
</div>
4546

4647
<h3>Arrays</h3>

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
<section class="ngx-json-treeview">
22
@if (segments().length === 0) {
3-
<section [class]="'segment-type-' + rootType()">
3+
@let sectionClass =
4+
['object', 'array'].includes(rootType())
5+
? 'punctuation'
6+
: 'segment-type-' + rootType();
7+
<section [class]="sectionClass">
48
<span class="segment-primative">
59
{{ asString() }}
610
</span>
711
</section>
812
} @else {
913
@if (_currentDepth() === 0) {
10-
<span class="puctuation">{{ openingBrace() }}</span>
14+
<span class="punctuation">{{ openingBrace() }}</span>
1115
}
1216
@for (
1317
segment of segments();
@@ -17,6 +21,7 @@
1721
) {
1822
<section [class]="'segment segment-type-' + segment.type">
1923
@let expandable = isExpandable(segment);
24+
@let empty = isEmpty(segment);
2025
@let openingBrace = openingBraceForSegment(segment);
2126
@let closingBrace = closingBraceForSegment(segment);
2227
@let clickableValue = isClickable(segment);
@@ -32,8 +37,13 @@
3237
}
3338
<span class="segment-key">{{ `"${segment.key}"` }}</span>
3439
</span>
35-
<span class="puctuation">: </span>
36-
@if (!expandable || !segment.expanded) {
40+
<span class="punctuation">: </span>
41+
@if (empty) {
42+
<span
43+
class="punctuation"
44+
>{{ `${openingBrace}${closingBrace}${i < len - 1 ? ',' : ''}` }}</span
45+
>
46+
} @else if (!expandable || !segment.expanded) {
3747
<span
3848
[tabindex]="clickableValue ? 0 : -1"
3949
[class.segment-label]="expandable"
@@ -43,10 +53,10 @@
4353
(keydown.enter)="onValueClickHandler(segment)"
4454
>{{ segment.description }}</span
4555
>
46-
<span class="puctuation">{{ i < len - 1 ? ',' : '' }}</span>
56+
<span class="punctuation">{{ i < len - 1 ? ',' : '' }}</span>
4757
} @else {
4858
@if (openingBrace) {
49-
<span class="puctuation">
59+
<span class="punctuation">
5060
{{ openingBrace }}
5161
</span>
5262
}
@@ -64,7 +74,7 @@
6474
[_currentDepth]="_currentDepth() + 1"
6575
(onValueClick)="onValueClickHandler($event)" />
6676
@if (closingBrace) {
67-
<span class="puctuation"
77+
<span class="punctuation"
6878
>{{ closingBrace }}{{ i < len - 1 ? ',' : '' }}</span
6979
>
7080
}
@@ -73,7 +83,7 @@
7383
</section>
7484
}
7585
@if (_currentDepth() === 0) {
76-
<span class="puctuation">{{ closingBrace() }}</span>
86+
<span class="punctuation">{{ closingBrace() }}</span>
7787
}
7888
}
7989
</section>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ $type-colors: (
100100
}
101101
}
102102

103-
.puctuation {
103+
.punctuation {
104104
color: var(--ngx-json-punctuation, #000);
105105
}
106106

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ export class NgxJsonTreeviewComponent {
142142
return ']';
143143
} else return '}';
144144
});
145-
asString = computed<string>(() => JSON.stringify(this.json(), null, 2));
145+
asString = computed<string>(() =>
146+
JSON.stringify(this.json(), null, 2).trim()
147+
);
146148

147149
isExpandable(segment: Segment) {
148150
return (
@@ -151,6 +153,13 @@ export class NgxJsonTreeviewComponent {
151153
);
152154
}
153155

156+
isEmpty(segment: Segment) {
157+
return (
158+
(segment.type === 'object' && Object.keys(segment.value).length === 0) ||
159+
(segment.type === 'array' && segment.value.length === 0)
160+
);
161+
}
162+
154163
isClickable(segment: Segment) {
155164
return this.enableClickableValues() && this.isClickableValue()(segment);
156165
}

0 commit comments

Comments
 (0)