Skip to content

Commit b43e476

Browse files
authored
Merge pull request jdorn#728 from tohosaku/Fix_remove_empty_properties
Fix jdorn#725 remove_empty_properties
2 parents a136e18 + 51f46e2 commit b43e476

File tree

3 files changed

+88
-9
lines changed

3 files changed

+88
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- AbstractEditor, AbstractTheme, AbstractIconlib had been removed, re-expose them
66
- fix static property #724 #723
7+
- fix option remove_empty_properties #725 #728
78

89
### 2.2.0
910

src/editors/object.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,20 +1049,19 @@ export class ObjectEditor extends AbstractEditor {
10491049
return undefined
10501050
}
10511051
const result = super.getValue()
1052-
const isEmpty = i => typeof result[i] === 'undefined' || result[i] === '' ||
1052+
const isEmpty = obj => typeof obj === 'undefined' || obj === '' ||
10531053
(
1054-
result[i] === Object(result[i]) &&
1055-
Object.keys(result[i]).length === 0 &&
1056-
result[i].constructor === Object
1054+
obj === Object(obj) &&
1055+
Object.keys(obj).length === 0 &&
1056+
obj.constructor === Object
10571057
)
1058-
if (this.jsoneditor.options.remove_empty_properties || this.options.remove_empty_properties) {
1059-
Object.keys(result).forEach(i => {
1060-
if (isEmpty(i)) {
1061-
delete result[i]
1058+
if (result && (this.jsoneditor.options.remove_empty_properties || this.options.remove_empty_properties)) {
1059+
Object.keys(result).forEach(key => {
1060+
if (isEmpty(result[key])) {
1061+
delete result[key]
10621062
}
10631063
})
10641064
}
1065-
10661065
return result
10671066
}
10681067

tests/unit/editors/object.spec.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { JSONEditor } from '../../../src/core'
2+
3+
const fixture = [
4+
{
5+
title: 'Object Editor Test',
6+
schema: {
7+
type: 'object',
8+
required: [
9+
'name'
10+
],
11+
properties: {
12+
name: {
13+
type: 'string',
14+
default: 'Jeremy Dorn'
15+
}
16+
}
17+
},
18+
value: {
19+
name: 'Jeremy Dorn'
20+
}
21+
},
22+
{
23+
title: 'remove_empty_properties test',
24+
schema: {
25+
type: 'object',
26+
required: [
27+
'name'
28+
],
29+
properties: {
30+
name: {
31+
type: 'string',
32+
default: 'Jeremy Dorn'
33+
},
34+
location: {
35+
type: 'object',
36+
properties: {
37+
city: {
38+
type: 'string'
39+
}
40+
},
41+
options: {
42+
remove_empty_properties: true
43+
}
44+
}
45+
},
46+
options: {
47+
remove_empty_properties: true
48+
}
49+
},
50+
value: {
51+
name: 'Jeremy Dorn'
52+
}
53+
}
54+
]
55+
56+
describe('Object Editor', () => {
57+
let element
58+
let editor
59+
60+
beforeEach(() => {
61+
document.body.insertAdjacentHTML(
62+
'afterbegin',
63+
'<div id="fixture"></div>')
64+
element = document.getElementById('fixture')
65+
})
66+
67+
afterEach(() => {
68+
editor.destroy()
69+
})
70+
71+
fixture.forEach(spec => {
72+
it(spec.title, () => {
73+
editor = new JSONEditor(element, {
74+
schema: spec.schema
75+
})
76+
expect(editor.getValue()).toEqual(spec.value)
77+
})
78+
})
79+
})

0 commit comments

Comments
 (0)