Skip to content

Commit

Permalink
Merge pull request jdorn#728 from tohosaku/Fix_remove_empty_properties
Browse files Browse the repository at this point in the history
Fix jdorn#725 remove_empty_properties
  • Loading branch information
schmunk42 authored Apr 27, 2020
2 parents a136e18 + 51f46e2 commit b43e476
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

### 2.2.0

Expand Down
17 changes: 8 additions & 9 deletions src/editors/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -1049,20 +1049,19 @@ export class ObjectEditor extends AbstractEditor {
return undefined
}
const result = super.getValue()
const isEmpty = i => typeof result[i] === 'undefined' || result[i] === '' ||
const isEmpty = obj => typeof obj === 'undefined' || obj === '' ||
(
result[i] === Object(result[i]) &&
Object.keys(result[i]).length === 0 &&
result[i].constructor === Object
obj === Object(obj) &&
Object.keys(obj).length === 0 &&
obj.constructor === Object
)
if (this.jsoneditor.options.remove_empty_properties || this.options.remove_empty_properties) {
Object.keys(result).forEach(i => {
if (isEmpty(i)) {
delete result[i]
if (result && (this.jsoneditor.options.remove_empty_properties || this.options.remove_empty_properties)) {
Object.keys(result).forEach(key => {
if (isEmpty(result[key])) {
delete result[key]
}
})
}

return result
}

Expand Down
79 changes: 79 additions & 0 deletions tests/unit/editors/object.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { JSONEditor } from '../../../src/core'

const fixture = [
{
title: 'Object Editor Test',
schema: {
type: 'object',
required: [
'name'
],
properties: {
name: {
type: 'string',
default: 'Jeremy Dorn'
}
}
},
value: {
name: 'Jeremy Dorn'
}
},
{
title: 'remove_empty_properties test',
schema: {
type: 'object',
required: [
'name'
],
properties: {
name: {
type: 'string',
default: 'Jeremy Dorn'
},
location: {
type: 'object',
properties: {
city: {
type: 'string'
}
},
options: {
remove_empty_properties: true
}
}
},
options: {
remove_empty_properties: true
}
},
value: {
name: 'Jeremy Dorn'
}
}
]

describe('Object Editor', () => {
let element
let editor

beforeEach(() => {
document.body.insertAdjacentHTML(
'afterbegin',
'<div id="fixture"></div>')
element = document.getElementById('fixture')
})

afterEach(() => {
editor.destroy()
})

fixture.forEach(spec => {
it(spec.title, () => {
editor = new JSONEditor(element, {
schema: spec.schema
})
expect(editor.getValue()).toEqual(spec.value)
})
})
})

0 comments on commit b43e476

Please sign in to comment.