Skip to content

Commit c12db2a

Browse files
committed
fix: address JS & TS linting complaints
1 parent e6c9957 commit c12db2a

File tree

4 files changed

+76
-64
lines changed

4 files changed

+76
-64
lines changed

src/block.js

Lines changed: 65 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,70 @@ import { bytes as binary, CID } from './index.js'
33
// eslint-disable-next-line
44
import * as API from './interface.js'
55

6-
const readonly = ({ enumerable = true, configurable = false } = {}) => ({
7-
enumerable,
8-
configurable,
9-
writable: false
10-
})
6+
function readonly ({ enumerable = true, configurable = false } = {}) {
7+
return { enumerable, configurable, writable: false }
8+
}
9+
10+
/**
11+
* @param {[string|number, string]} path
12+
* @param {any} value
13+
* @returns {Iterable<[string, CID]>}
14+
*/
15+
function * linksWithin (path, value) {
16+
if (value != null && typeof value === 'object') {
17+
if (Array.isArray(value)) {
18+
for (const [index, element] of value.entries()) {
19+
const elementPath = [...path, index]
20+
const cid = CID.asCID(element)
21+
if (cid) {
22+
yield [elementPath.join('/'), cid]
23+
} else if (typeof element === 'object') {
24+
yield * links(element, elementPath)
25+
}
26+
}
27+
} else {
28+
const cid = CID.asCID(value)
29+
if (cid) {
30+
yield [path.join('/'), cid]
31+
} else {
32+
yield * links(value, path)
33+
}
34+
}
35+
}
36+
}
1137

1238
/**
1339
* @template T
1440
* @param {T} source
1541
* @param {Array<string|number>} base
1642
* @returns {Iterable<[string, CID]>}
1743
*/
18-
const links = function * (source, base) {
19-
if (source == null) return
20-
if (source instanceof Uint8Array) return
44+
function * links (source, base) {
45+
if (source == null || source instanceof Uint8Array) {
46+
return
47+
}
2148
for (const [key, value] of Object.entries(source)) {
22-
const path = [...base, key]
23-
if (value != null && typeof value === 'object') {
24-
if (Array.isArray(value)) {
25-
for (const [index, element] of value.entries()) {
26-
const elementPath = [...path, index]
27-
const cid = CID.asCID(element)
28-
if (cid) {
29-
yield [elementPath.join('/'), cid]
30-
} else if (typeof element === 'object') {
31-
yield * links(element, elementPath)
32-
}
33-
}
34-
} else {
35-
const cid = CID.asCID(value)
36-
if (cid) {
37-
yield [path.join('/'), cid]
38-
} else {
39-
yield * links(value, path)
40-
}
49+
const path = /** @type {[string|number, string]} */ ([...base, key])
50+
yield * linksWithin(path, value)
51+
}
52+
}
53+
54+
/**
55+
* @param {[string|number, string]} path
56+
* @param {any} value
57+
* @returns {Iterable<string>}
58+
*/
59+
function * treeWithin (path, value) {
60+
if (Array.isArray(value)) {
61+
for (const [index, element] of value.entries()) {
62+
const elementPath = [...path, index]
63+
yield elementPath.join('/')
64+
if (typeof element === 'object' && !CID.asCID(element)) {
65+
yield * tree(element, elementPath)
4166
}
4267
}
68+
} else {
69+
yield * tree(value, path)
4370
}
4471
}
4572

@@ -49,23 +76,15 @@ const links = function * (source, base) {
4976
* @param {Array<string|number>} base
5077
* @returns {Iterable<string>}
5178
*/
52-
const tree = function * (source, base) {
53-
if (source == null) return
79+
function * tree (source, base) {
80+
if (source == null || typeof source !== 'object') {
81+
return
82+
}
5483
for (const [key, value] of Object.entries(source)) {
55-
const path = [...base, key]
84+
const path = /** @type {[string|number, string]} */ ([...base, key])
5685
yield path.join('/')
5786
if (value != null && !(value instanceof Uint8Array) && typeof value === 'object' && !CID.asCID(value)) {
58-
if (Array.isArray(value)) {
59-
for (const [index, element] of value.entries()) {
60-
const elementPath = [...path, index]
61-
yield elementPath.join('/')
62-
if (typeof element === 'object' && !CID.asCID(element)) {
63-
yield * tree(element, elementPath)
64-
}
65-
}
66-
} else {
67-
yield * tree(value, path)
68-
}
87+
yield * treeWithin(path, value)
6988
}
7089
}
7190
}
@@ -77,7 +96,7 @@ const tree = function * (source, base) {
7796
* @param {string[]} path
7897
* @return {API.BlockCursorView<unknown>}
7998
*/
80-
const get = (source, path) => {
99+
function get (source, path) {
81100
let node = /** @type {Record<string, any>} */(source)
82101
for (const [index, key] of path.entries()) {
83102
node = node[key]
@@ -151,7 +170,7 @@ class Block {
151170
* @param {API.MultihashHasher<Alg>} options.hasher
152171
* @returns {Promise<API.BlockView<T, Code, Alg>>}
153172
*/
154-
const encode = async ({ value, codec, hasher }) => {
173+
async function encode ({ value, codec, hasher }) {
155174
if (typeof value === 'undefined') throw new Error('Missing required argument "value"')
156175
if (!codec || !hasher) throw new Error('Missing required argument: codec or hasher')
157176

@@ -177,7 +196,7 @@ const encode = async ({ value, codec, hasher }) => {
177196
* @param {API.MultihashHasher<Alg>} options.hasher
178197
* @returns {Promise<API.BlockView<T, Code, Alg>>}
179198
*/
180-
const decode = async ({ bytes, codec, hasher }) => {
199+
async function decode ({ bytes, codec, hasher }) {
181200
if (!bytes) throw new Error('Missing required argument "bytes"')
182201
if (!codec || !hasher) throw new Error('Missing required argument: codec or hasher')
183202

@@ -202,7 +221,7 @@ const decode = async ({ bytes, codec, hasher }) => {
202221
* @param {{ cid: API.Link<T, Code, Alg, V>, value:T, codec?: API.BlockDecoder<Code, T>, bytes: API.ByteView<T> }|{cid:API.Link<T, Code, Alg, V>, bytes:API.ByteView<T>, value?:void, codec:API.BlockDecoder<Code, T>}} options
203222
* @returns {API.BlockView<T, Code, Alg, V>}
204223
*/
205-
const createUnsafe = ({ bytes, cid, value: maybeValue, codec }) => {
224+
function createUnsafe ({ bytes, cid, value: maybeValue, codec }) {
206225
const value = maybeValue !== undefined
207226
? maybeValue
208227
: (codec && codec.decode(bytes))
@@ -229,7 +248,7 @@ const createUnsafe = ({ bytes, cid, value: maybeValue, codec }) => {
229248
* @param {API.MultihashHasher<Alg>} options.hasher
230249
* @returns {Promise<API.BlockView<T, Code, Alg, V>>}
231250
*/
232-
const create = async ({ bytes, cid, hasher, codec }) => {
251+
async function create ({ bytes, cid, hasher, codec }) {
233252
if (!bytes) throw new Error('Missing required argument "bytes"')
234253
if (!hasher) throw new Error('Missing required argument "hasher"')
235254
const value = codec.decode(bytes)

test/test-cid.spec.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -217,23 +217,6 @@ describe('CID', () => {
217217
)
218218
})
219219

220-
/* TODO: after i have a keccak hash for the new interface
221-
it('handles multibyte varint encoded codec codes', () => {
222-
const ethBlockHash = textEncoder.encode('8a8e84c797605fbe75d5b5af107d4220a2db0ad35fd66d9be3d38d87c472b26d', 'hex')
223-
const hash = keccak256.digest(ethBlockHash)
224-
const cid1 = CID.create(1, 0x90, hash)
225-
const cid2 = CID.parse(cid1.toString())
226-
227-
assert.deepStrictEqual(cid1.code, 0x90)
228-
assert.deepStrictEqual(cid1.version, 1)
229-
assert.deepStrictEqual(cid1.multihash, hash)
230-
231-
assert.deepStrictEqual(cid2.code, 0x90)
232-
assert.deepStrictEqual(cid2.version, 1)
233-
assert.deepStrictEqual(cid2.multihash, hash)
234-
})
235-
*/
236-
237220
it('.bytes', async () => {
238221
const hash = await sha256.digest(textEncoder.encode('abc'))
239222
const code = 0x71

test/ts-use/package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,14 @@
66
},
77
"scripts": {
88
"test": "npm install && npm_config_yes=true npx -p typescript tsc --noEmit"
9+
},
10+
"eslintConfig": {
11+
"extends": "ipfs",
12+
"parserOptions": {
13+
"sourceType": "module",
14+
"project": [
15+
"./test/ts-use/tsconfig.json"
16+
]
17+
}
918
}
10-
}
19+
}

test/ts-use/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const main = async () => {
99
codec: json
1010
})
1111

12+
/* eslint-disable no-console */
1213
console.log(block)
1314
}
1415

0 commit comments

Comments
 (0)