Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
fix: add array handling to .tree
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire authored and daviddias committed Oct 26, 2016
1 parent bea41ea commit 656ad84
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 46 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,18 @@
"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",
"lodash.times": "^4.3.2",
"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",
Expand Down
48 changes: 13 additions & 35 deletions src/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const util = require('./util')
const _times = require('lodash.times')
const traverse = require('traverse')

exports = module.exports

Expand Down Expand Up @@ -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
Expand All @@ -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
}, [])
}
10 changes: 4 additions & 6 deletions test/resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {})
Expand Down
4 changes: 2 additions & 2 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const dagCBOR = require('../src')

describe('util', () => {
const obj = {
'someKey': 'someValue',
'link': { '/': 'aaaaa' }
someKey: 'someValue',
link: { '/': 'aaaaa' }
}

it('.serialize and .deserialize', () => {
Expand Down

0 comments on commit 656ad84

Please sign in to comment.