Skip to content

Commit

Permalink
fix(merkling): Fix saving of objects with null properties
Browse files Browse the repository at this point in the history
fix #1
  • Loading branch information
kanej committed Mar 1, 2018
1 parent 06f871b commit 0514126
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/ipld.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ class IpldProxy {
}

isIpld (obj) {
return this.allowedStatuses.includes(obj[statusSymbol])
return obj && this.allowedStatuses.includes(obj[statusSymbol])
}

isPersisted (obj) {
return obj[statusSymbol] === this.UNLOADED || obj[statusSymbol] === this.SAVED
return obj && (obj[statusSymbol] === this.UNLOADED || obj[statusSymbol] === this.SAVED)
}

create (cid, status, obj) {
Expand Down
20 changes: 13 additions & 7 deletions src/merkling.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@ const Merkling = function (options) {
return resolve(elem)
}

const subpersists = Object.keys(elem).map(key => {
return typeof elem[key] === 'object'
? this._persist(elem[key])
: null
}).filter(Boolean)

return Promise.all(subpersists).then((values) => {
const subpersists = Object.keys(elem)
.filter(key => elem[key])
.map(key => {
return typeof elem[key] === 'object'
? this._persist(elem[key])
: null
}).filter(Boolean)

return Promise.all(subpersists).then(() => {
if (!this.ipldProxy.isIpld(elem)) {
return resolve(elem)
}
Expand All @@ -102,6 +104,10 @@ const Merkling = function (options) {
}

this._substituteMerkleLinks = (elem) => {
if (!elem) {
return elem
}

const dagNode = this.ipldProxy.extract(elem)

Object.keys(dagNode).forEach(key => {
Expand Down
12 changes: 12 additions & 0 deletions test/merkling.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ describe('api tests', () => {
done()
})

test('Save an object with a nested null', async (done) => {
const simple = { name: 'Example', next: null }
const node = merkle.create(simple)
const savedNode = await merkle.save(node)
expect(savedNode).not.toBeNull()
expect(savedNode.name).toBe('Example')
expect(savedNode._cid).not.toBeNull()
expect(savedNode._cid.toBaseEncodedString()).toBe('zdpuAm1ZHnidGxaUbTExkEeEYZRG6jxNPsgeZNe53L6hB5Ny6')
expect(savedNode.next).toBeNull()
done()
})

test('Saving null throws', () => {
expect(() => { merkle.save(null) }).toThrow('Argument exception, trying to save null or undefined')
})
Expand Down

0 comments on commit 0514126

Please sign in to comment.