Skip to content

Commit

Permalink
fix(toJSON): handle null properties
Browse files Browse the repository at this point in the history
  • Loading branch information
christianalfoni committed Aug 3, 2016
1 parent c48392f commit 0a485a7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions helpers/toJSON.js
Expand Up @@ -3,9 +3,9 @@ function toJSON (form) {
return Object.keys(object).reduce(function (newObject, key) {
if (Array.isArray(object[key])) {
newObject[key] = extractArray(object[key])
} else if ('value' in object[key]) {
} else if (object[key] && 'value' in object[key]) {
newObject[key] = object[key].value
} else {
} else if (object[key] && typeof object[key] === 'object') {
newObject[key] = extractObject(object[key])
}
return newObject
Expand Down
27 changes: 27 additions & 0 deletions tests/toJSON.js
@@ -0,0 +1,27 @@
var toJSON = require('../helpers/toJSON')
var Form = require('../Form')

module.exports = {
setUp: function (callback) {
this.form = Form({
name: {
value: 'foo'
},
name2: {
value: 'bar'
}
})
callback()
},
tearDown: function (callback) {
callback()
},
testToJSON: function (test) {
this.form.name3 = null;
test.deepEqual(toJSON(this.form), {
name: 'foo',
name2: 'bar'
})
test.done()
}
}

0 comments on commit 0a485a7

Please sign in to comment.