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

Commit

Permalink
feat: resolver.tree and resolver.multicodec
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Oct 26, 2016
1 parent fcc2ab5 commit 21ddefc
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ipld-dag-cbor",
"version": "0.6.0",
"description": "JavaScript implementation of the IPLD (InterpPlanetary Linked Data)",
"main": "lib/index.js",
"main": "src/index.js",
"jsnext:main": "src/index.js",
"scripts": {
"test": "aegir-test",
Expand Down
52 changes: 51 additions & 1 deletion src/resolver.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
'use strict'

const util = require('./util')

exports = module.exports

exports.multicodec = 'dag-cbor'

/*
* resolve: receives a path and a block and returns the value on path,
* throw if not possible. `block` is an IPFS Block instance (contains data+key)
* throw if not possible. `block` is an IPFS Block instance (contains data + key)
*/
exports.resolve = (block, path) => {

}

/*
Expand All @@ -19,10 +22,57 @@ exports.tree = (block, options) => {
if (!options) {
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
}

// TODO recheck this API
/*
* patch: modifies or adds value on path, yields a new block with that change
*/
exports.patch = (block, path, value) => {}

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

let toReturn = {}
let flatObject
for (let i in obj) {
if (!obj.hasOwnProperty(i)) {
continue
}

if (Array.isArray(obj[i])) {
continue
}

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
}
83 changes: 83 additions & 0 deletions test/resolver.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const dagCBOR = require('../src')
const resolver = dagCBOR.resolver
const Block = require('ipfs-block')

describe('IPLD format resolver (local)', () => {
let emptyNodeBlock
let nodeBlock

before(() => {
const emptyNode = {}
const node = {
name: 'I am a node',
someLink: { '/': 'LINK' },
nest: {
foo: {
bar: 'baz'
}
},
array: [
{ a: 'b' },
2
]

}

emptyNodeBlock = new Block(dagCBOR.util.serialize(emptyNode))
nodeBlock = new Block(dagCBOR.util.serialize(node))
})

it('multicodec is dag-cbor', () => {
expect(resolver.multicodec).to.equal('dag-cbor')
})

describe('empty node', () => {
describe('resolver.resolve', () => {
it.skip('path', () => {
})
})

it('resolver.tree', () => {
const paths = resolver.tree(emptyNodeBlock)
expect(paths).to.eql([])
})

it.skip('resolver.patch', (done) => {})
})

describe('node', () => {
describe.skip('resolver.resolve', () => {
it('path', () => {
})
})

it('resolver.tree', () => {
const paths = resolver.tree(nodeBlock)
expect(paths).to.eql([{
path: 'name',
value: 'I am a node'
}, {
// 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'
} */])
})

it.skip('resolver.patch', () => {})
})
})

0 comments on commit 21ddefc

Please sign in to comment.