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

Commit 21ddefc

Browse files
committed
feat: resolver.tree and resolver.multicodec
1 parent fcc2ab5 commit 21ddefc

3 files changed

Lines changed: 135 additions & 2 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "ipld-dag-cbor",
33
"version": "0.6.0",
44
"description": "JavaScript implementation of the IPLD (InterpPlanetary Linked Data)",
5-
"main": "lib/index.js",
5+
"main": "src/index.js",
66
"jsnext:main": "src/index.js",
77
"scripts": {
88
"test": "aegir-test",

src/resolver.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
'use strict'
22

3+
const util = require('./util')
4+
35
exports = module.exports
46

57
exports.multicodec = 'dag-cbor'
68

79
/*
810
* resolve: receives a path and a block and returns the value on path,
9-
* throw if not possible. `block` is an IPFS Block instance (contains data+key)
11+
* throw if not possible. `block` is an IPFS Block instance (contains data + key)
1012
*/
1113
exports.resolve = (block, path) => {
14+
1215
}
1316

1417
/*
@@ -19,10 +22,57 @@ exports.tree = (block, options) => {
1922
if (!options) {
2023
options = {}
2124
}
25+
26+
const node = util.deserialize(block.data)
27+
const flatObj = flattenObject(node)
28+
const paths = Object.keys(flatObj)
29+
.map((key) => {
30+
return {
31+
path: key,
32+
value: flatObj[key]
33+
}
34+
})
35+
return paths
2236
}
2337

2438
// TODO recheck this API
2539
/*
2640
* patch: modifies or adds value on path, yields a new block with that change
2741
*/
2842
exports.patch = (block, path, value) => {}
43+
44+
function flattenObject (obj, delimiter) {
45+
if (!delimiter) {
46+
delimiter = '/'
47+
}
48+
49+
let toReturn = {}
50+
let flatObject
51+
for (let i in obj) {
52+
if (!obj.hasOwnProperty(i)) {
53+
continue
54+
}
55+
56+
if (Array.isArray(obj[i])) {
57+
continue
58+
}
59+
60+
if ((typeof obj[i]) === 'object') {
61+
flatObject = flattenObject(obj[i])
62+
for (let x in flatObject) {
63+
if (!flatObject.hasOwnProperty(x)) {
64+
continue
65+
}
66+
67+
if (flatObject[x] && Array === flatObject.constructor) {
68+
continue
69+
}
70+
71+
toReturn[i + (isNaN(x) ? delimiter + x : '')] = flatObject[x]
72+
}
73+
} else {
74+
toReturn[i] = obj[i]
75+
}
76+
}
77+
return toReturn
78+
}

test/resolver.spec.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const expect = require('chai').expect
5+
const dagCBOR = require('../src')
6+
const resolver = dagCBOR.resolver
7+
const Block = require('ipfs-block')
8+
9+
describe('IPLD format resolver (local)', () => {
10+
let emptyNodeBlock
11+
let nodeBlock
12+
13+
before(() => {
14+
const emptyNode = {}
15+
const node = {
16+
name: 'I am a node',
17+
someLink: { '/': 'LINK' },
18+
nest: {
19+
foo: {
20+
bar: 'baz'
21+
}
22+
},
23+
array: [
24+
{ a: 'b' },
25+
2
26+
]
27+
28+
}
29+
30+
emptyNodeBlock = new Block(dagCBOR.util.serialize(emptyNode))
31+
nodeBlock = new Block(dagCBOR.util.serialize(node))
32+
})
33+
34+
it('multicodec is dag-cbor', () => {
35+
expect(resolver.multicodec).to.equal('dag-cbor')
36+
})
37+
38+
describe('empty node', () => {
39+
describe('resolver.resolve', () => {
40+
it.skip('path', () => {
41+
})
42+
})
43+
44+
it('resolver.tree', () => {
45+
const paths = resolver.tree(emptyNodeBlock)
46+
expect(paths).to.eql([])
47+
})
48+
49+
it.skip('resolver.patch', (done) => {})
50+
})
51+
52+
describe('node', () => {
53+
describe.skip('resolver.resolve', () => {
54+
it('path', () => {
55+
})
56+
})
57+
58+
it('resolver.tree', () => {
59+
const paths = resolver.tree(nodeBlock)
60+
expect(paths).to.eql([{
61+
path: 'name',
62+
value: 'I am a node'
63+
}, {
64+
// TODO confirm how to represent links in tree
65+
path: 'someLink//',
66+
value: 'LINK'
67+
}, {
68+
path: 'nest/foo/bar',
69+
value: 'baz'
70+
}
71+
// TODO fix array in .tree
72+
/*, {
73+
path: 'array/0/a',
74+
value: 'b'
75+
}, {
76+
path: 'array/1',
77+
value: '2'
78+
} */])
79+
})
80+
81+
it.skip('resolver.patch', () => {})
82+
})
83+
})

0 commit comments

Comments
 (0)