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

Commit

Permalink
Feat/tree+is cid (#45)
Browse files Browse the repository at this point in the history
* chore: update deps

* fix+feat: fix .tree and add isLink + more tests
  • Loading branch information
daviddias committed Mar 13, 2017
1 parent 71e03eb commit 60f6a2f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 27 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@
},
"homepage": "https://github.com/ipld/js-ipld-dag-cbor",
"dependencies": {
"async": "^2.1.4",
"async": "^2.1.5",
"borc": "^2.0.2",
"bs58": "^4.0.0",
"cids": "~0.4.1",
"is-circular": "^1.0.1",
"multihashes": "~0.3.3",
"multihashing-async": "~0.4.2",
"multihashes": "~0.4.3",
"multihashing-async": "~0.4.3",
"traverse": "^0.6.6"
},
"devDependencies": {
"aegir": "^10.0.0",
"chai": "^3.5.0",
"deep-freeze": "0.0.1",
"garbage": "0.0.0",
"ipfs-block": "~0.5.4",
"ipfs-block": "~0.5.5",
"pre-commit": "^1.2.2"
},
"contributors": [
Expand Down
53 changes: 34 additions & 19 deletions src/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ exports.resolve = (block, path, callback) => {
})
}

function flattenObject (obj, delimiter) {
delimiter = delimiter || '/'

if (Object.keys(obj).length === 0) {
return []
}

return traverse(obj).reduce(function (acc, x) {
if (typeof x === 'object' && x['/']) {
this.update(undefined)
}
const path = this.path.join(delimiter)

if (path !== '') {
acc.push({ path: path, value: x })
}
return acc
}, [])
}

/*
* tree: returns a flattened array with paths: values of the project. options
* are option (i.e. nestness)
Expand All @@ -82,9 +102,7 @@ exports.tree = (block, options, callback) => {
options = undefined
}

if (!options) {
options = {}
}
options = options || {}

util.deserialize(block.data, (err, node) => {
if (err) {
Expand All @@ -97,23 +115,20 @@ exports.tree = (block, options, callback) => {
})
}

function flattenObject (obj, delimiter) {
if (!delimiter) {
delimiter = '/'
}

if (Object.keys(obj).length === 0) {
return []
}
exports.isLink = (block, path, callback) => {
exports.resolve(block, path, (err, result) => {
if (err) {
return callback(err)
}

return traverse(obj).reduce(function (acc, x) {
if (this.isLeaf) {
acc.push({
path: this.path.join(delimiter),
value: x
})
if (result.remainderPath.length > 0) {
return callback(new Error('path out of scope'))
}

return acc
}, [])
if (typeof result.value === 'object' && result.value['/']) {
callback(null, result.value)
} else {
callback(null, false)
}
})
}
26 changes: 22 additions & 4 deletions test/resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const dagCBOR = require('../src')
const resolver = dagCBOR.resolver
const Block = require('ipfs-block')
const series = require('async/series')
const CID = require('cids')

describe('IPLD format resolver (local)', () => {
let emptyNodeBlock
Expand Down Expand Up @@ -77,19 +78,36 @@ describe('IPLD format resolver (local)', () => {

expect(paths).to.eql([
'name',
'nest',
'nest/foo',
'nest/foo/bar',
'array',
'array/0',
'array/0/a',
'array/1',
'someLink///0',
'someLink///1',
'someLink///2',
'someLink///3'
'someLink'
])

done()
})
})

it('resolver.isLink with valid Link', (done) => {
resolver.isLink(nodeBlock, '', (err, link) => {
expect(err).to.not.exist
expect(CID.isCID(new CID(link['/']))).to.be.true
done()
})
})

it('resolver.isLink with invalid Link', (done) => {
resolver.isLink(nodeBlock, '', (err, link) => {
expect(err).to.not.exist
expect(link).to.be.false
done()
})
})

describe('resolver.resolve', () => {
it('path within scope', (done) => {
resolver.resolve(nodeBlock, 'name', (err, result) => {
Expand Down

0 comments on commit 60f6a2f

Please sign in to comment.