diff --git a/package.json b/package.json index 85faf6b..13a6198 100644 --- a/package.json +++ b/package.json @@ -39,9 +39,9 @@ "dependencies": { "babel-runtime": "^6.6.1", "bs58": "^3.0.0", - "cbor": "^1.0.4", + "cbor": "^2.0.1", "cbor-sync": "^1.0.2", - "cids": "^0.1.1", + "cids": "^0.2.0", "lodash.clonedeep": "^4.3.2", "lodash.defaults": "^4.0.1", "lodash.includes": "^4.3.0", @@ -49,7 +49,8 @@ "multiaddr": "^2.0.0", "multihashes": "^0.2.2", "multihashing": "^0.2.1", - "nofilter": "0.0.2" + "nofilter": "0.0.3", + "traverse": "^0.6.6" }, "devDependencies": { "aegir": "8.1.2", diff --git a/src/resolver.js b/src/resolver.js index ed7fba5..d4b90f1 100644 --- a/src/resolver.js +++ b/src/resolver.js @@ -2,6 +2,7 @@ const util = require('./util') const _times = require('lodash.times') +const traverse = require('traverse') exports = module.exports @@ -82,15 +83,7 @@ exports.tree = (block, options) => { } const node = util.deserialize(block.data) - const flatObj = flattenObject(node) - const paths = Object.keys(flatObj) - .map((key) => { - return { - path: key, - value: flatObj[key] - } - }) - return paths + return flattenObject(node) } // TODO recheck this API @@ -104,33 +97,18 @@ function flattenObject (obj, delimiter) { delimiter = '/' } - let toReturn = {} - let flatObject - for (let i in obj) { - if (!obj.hasOwnProperty(i)) { - continue - } + if (Object.keys(obj).length === 0) { + return [] + } - if (Array.isArray(obj[i])) { - continue + return traverse(obj).reduce(function (acc, x) { + if (this.isLeaf) { + acc.push({ + path: this.path.join(delimiter), + value: x + }) } - if ((typeof obj[i]) === 'object') { - flatObject = flattenObject(obj[i]) - for (let x in flatObject) { - if (!flatObject.hasOwnProperty(x)) { - continue - } - - if (flatObject[x] && Array === flatObject.constructor) { - continue - } - - toReturn[i + (isNaN(x) ? delimiter + x : '')] = flatObject[x] - } - } else { - toReturn[i] = obj[i] - } - } - return toReturn + return acc + }, []) } diff --git a/test/resolver.spec.js b/test/resolver.spec.js index 495dc14..a05b78c 100644 --- a/test/resolver.spec.js +++ b/test/resolver.spec.js @@ -74,21 +74,19 @@ describe('IPLD format resolver (local)', () => { path: 'name', value: 'I am a node' }, { - // TODO confirm how to represent links in tree + // TODO: confirm how to represent links in tree path: 'someLink//', value: 'LINK' }, { path: 'nest/foo/bar', value: 'baz' - } - // TODO fix array in .tree - /*, { + }, { path: 'array/0/a', value: 'b' }, { path: 'array/1', - value: '2' - } */]) + value: 2 + }]) }) it.skip('resolver.patch', () => {}) diff --git a/test/util.spec.js b/test/util.spec.js index 8d201c6..904bb40 100644 --- a/test/util.spec.js +++ b/test/util.spec.js @@ -6,8 +6,8 @@ const dagCBOR = require('../src') describe('util', () => { const obj = { - 'someKey': 'someValue', - 'link': { '/': 'aaaaa' } + someKey: 'someValue', + link: { '/': 'aaaaa' } } it('.serialize and .deserialize', () => {