@ipld/codec-interface
Advanced tools
Comparing version 1.0.7 to 1.0.8
39
index.js
@@ -6,12 +6,12 @@ 'use strict' | ||
const links = function * (decoded, path = []) { | ||
for (let key of Object.keys(decoded)) { | ||
let _path = path.slice() | ||
for (const key of Object.keys(decoded)) { | ||
const _path = path.slice() | ||
_path.push(key) | ||
let val = decoded[key] | ||
const val = decoded[key] | ||
if (val && typeof val === 'object') { | ||
if (Array.isArray(val)) { | ||
for (let i = 0; i < val.length; i++) { | ||
let __path = _path.slice() | ||
const __path = _path.slice() | ||
__path.push(i) | ||
let o = val[i] | ||
const o = val[i] | ||
if (CID.isCID(o)) { | ||
@@ -35,13 +35,13 @@ yield [__path.join('/'), o] | ||
const tree = function * (decoded, path = []) { | ||
for (let key of Object.keys(decoded)) { | ||
let _path = path.slice() | ||
for (const key of Object.keys(decoded)) { | ||
const _path = path.slice() | ||
_path.push(key) | ||
yield _path.join('/') | ||
let val = decoded[key] | ||
const val = decoded[key] | ||
if (val && typeof val === 'object' && !CID.isCID(val)) { | ||
if (Array.isArray(val)) { | ||
for (let i = 0; i < val.length; i++) { | ||
let __path = _path.slice() | ||
const __path = _path.slice() | ||
__path.push(i) | ||
let o = val[i] | ||
const o = val[i] | ||
yield __path.join('/') | ||
@@ -60,8 +60,14 @@ if (typeof o === 'object' && !CID.isCID(o)) { | ||
const readonly = () => { throw new Error('Read-only property') } | ||
const readonly = () => { | ||
throw new Error('Read-only property') | ||
} | ||
class Reader { | ||
constructor (decoded) { | ||
Object.defineProperty(this, 'decoded', { get: () => decoded, set: readonly }) | ||
Object.defineProperty(this, 'decoded', { | ||
get: () => decoded, | ||
set: readonly | ||
}) | ||
} | ||
get (path) { | ||
@@ -71,4 +77,4 @@ let node = this.decoded | ||
while (path.length) { | ||
let key = path.shift() | ||
if (node[key] === undefined) throw new Error(`Object has no property ${key}`) | ||
const key = path.shift() | ||
if (node[key] === undefined) { throw new Error(`Object has no property ${key}`) } | ||
node = node[key] | ||
@@ -79,5 +85,7 @@ if (CID.isCID(node)) return { value: node, remaining: path.join('/') } | ||
} | ||
links () { | ||
return links(this.decoded) | ||
} | ||
tree () { | ||
@@ -94,2 +102,3 @@ return tree(this.decoded) | ||
} | ||
reader (block) { | ||
@@ -99,3 +108,3 @@ // Skip a decoding if the source is available. | ||
// Full decoding is required for the standard Reader interface | ||
let decoded = block.decode() | ||
const decoded = block.decode() | ||
return new Reader(decoded) | ||
@@ -102,0 +111,0 @@ } |
{ | ||
"name": "@ipld/codec-interface", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "Codec interface for IPLD.", | ||
@@ -17,8 +17,8 @@ "main": "index.js", | ||
"devDependencies": { | ||
"aegir": "^18.2.2", | ||
"hundreds": "0.0.1", | ||
"aegir": "^20.5.1", | ||
"hundreds": "0.0.2", | ||
"tsame": "^2.0.1" | ||
}, | ||
"dependencies": { | ||
"cids": "~0.6.0" | ||
"cids": "~0.7.2" | ||
}, | ||
@@ -25,0 +25,0 @@ "repository": { |
@@ -13,3 +13,3 @@ 'use strict' | ||
const encode = obj => { | ||
for (let key of Object.keys(obj)) { | ||
for (const key of Object.keys(obj)) { | ||
if (key.startsWith('link:')) { | ||
@@ -19,8 +19,8 @@ obj[key] = obj[key].toBaseEncodedString() | ||
} | ||
let str = JSON.stringify(obj) | ||
const str = JSON.stringify(obj) | ||
return Buffer.from(str) | ||
} | ||
const decode = buffer => { | ||
let obj = JSON.parse(buffer.toString()) | ||
for (let key of Object.keys(obj)) { | ||
const obj = JSON.parse(buffer.toString()) | ||
for (const key of Object.keys(obj)) { | ||
if (key.startsWith('link:')) { | ||
@@ -35,15 +35,15 @@ obj[key] = new CID(obj[key]) | ||
test('test create', async () => { | ||
test('test create', () => { | ||
create() | ||
}) | ||
test('test encode/decode', async () => { | ||
let codec = create() | ||
let buffer = codec.encode({ hello: 'world' }) | ||
let obj = codec.decode(buffer) | ||
test('test encode/decode', () => { | ||
const codec = create() | ||
const buffer = codec.encode({ hello: 'world' }) | ||
const obj = codec.decode(buffer) | ||
same(obj, { hello: 'world' }) | ||
}) | ||
test('test codec property', async () => { | ||
let codec = create() | ||
test('test codec property', () => { | ||
const codec = create() | ||
same(codec.codec, 'terrible-dag') | ||
@@ -50,0 +50,0 @@ let threw = false |
@@ -27,7 +27,7 @@ 'use strict' | ||
test('get path', async () => { | ||
let reader = getReader() | ||
let one = reader.get('/a/1').value | ||
test('get path', () => { | ||
const reader = getReader() | ||
const one = reader.get('/a/1').value | ||
same(one, 1) | ||
let incomplete = reader.get('l/one/two') | ||
const incomplete = reader.get('l/one/two') | ||
same(incomplete.remaining, 'one/two') | ||
@@ -37,3 +37,3 @@ assert.ok(CID.isCID(incomplete.value)) | ||
test('source optimization', async () => { | ||
test('source optimization', () => { | ||
let reader = mock.reader({ source: () => fixture }) | ||
@@ -47,13 +47,13 @@ let one = reader.get('/a/1').value | ||
test('links', async () => { | ||
let reader = getReader() | ||
let links = Array.from(reader.links()) | ||
let keys = new Set(links.map(a => a[0])) | ||
same(keys, new Set([ 'a/2', 'a/4/l', 'l', 'o/l' ])) | ||
test('links', () => { | ||
const reader = getReader() | ||
const links = Array.from(reader.links()) | ||
const keys = new Set(links.map(a => a[0])) | ||
same(keys, new Set(['a/2', 'a/4/l', 'l', 'o/l'])) | ||
links.forEach(l => assert.ok(CID.isCID(l[1]))) | ||
}) | ||
test('tree', async () => { | ||
let reader = getReader() | ||
let tree = Array.from(reader.tree()) | ||
test('tree', () => { | ||
const reader = getReader() | ||
const tree = Array.from(reader.tree()) | ||
same(new Set(tree), new Set([ | ||
@@ -76,4 +76,4 @@ 'a', | ||
test('property not found', async () => { | ||
let reader = getReader() | ||
test('property not found', () => { | ||
const reader = getReader() | ||
let threw = false | ||
@@ -80,0 +80,0 @@ try { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8759
223
+ Addedcids@0.7.5(transitive)
+ Addedmulticodec@1.0.4(transitive)
- Removedcids@0.6.0(transitive)
- Removedmulticodec@0.5.7(transitive)
Updatedcids@~0.7.2