Skip to content

Commit

Permalink
fix: address JS & TS linting complaints
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Oct 12, 2022
1 parent e6c9957 commit c12db2a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 64 deletions.
111 changes: 65 additions & 46 deletions src/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,70 @@ import { bytes as binary, CID } from './index.js'
// eslint-disable-next-line
import * as API from './interface.js'

const readonly = ({ enumerable = true, configurable = false } = {}) => ({
enumerable,
configurable,
writable: false
})
function readonly ({ enumerable = true, configurable = false } = {}) {
return { enumerable, configurable, writable: false }
}

/**
* @param {[string|number, string]} path
* @param {any} value
* @returns {Iterable<[string, CID]>}
*/
function * linksWithin (path, value) {
if (value != null && typeof value === 'object') {
if (Array.isArray(value)) {
for (const [index, element] of value.entries()) {
const elementPath = [...path, index]
const cid = CID.asCID(element)
if (cid) {
yield [elementPath.join('/'), cid]
} else if (typeof element === 'object') {
yield * links(element, elementPath)
}
}
} else {
const cid = CID.asCID(value)
if (cid) {
yield [path.join('/'), cid]
} else {
yield * links(value, path)
}
}
}
}

/**
* @template T
* @param {T} source
* @param {Array<string|number>} base
* @returns {Iterable<[string, CID]>}
*/
const links = function * (source, base) {
if (source == null) return
if (source instanceof Uint8Array) return
function * links (source, base) {
if (source == null || source instanceof Uint8Array) {
return
}
for (const [key, value] of Object.entries(source)) {
const path = [...base, key]
if (value != null && typeof value === 'object') {
if (Array.isArray(value)) {
for (const [index, element] of value.entries()) {
const elementPath = [...path, index]
const cid = CID.asCID(element)
if (cid) {
yield [elementPath.join('/'), cid]
} else if (typeof element === 'object') {
yield * links(element, elementPath)
}
}
} else {
const cid = CID.asCID(value)
if (cid) {
yield [path.join('/'), cid]
} else {
yield * links(value, path)
}
const path = /** @type {[string|number, string]} */ ([...base, key])
yield * linksWithin(path, value)
}
}

/**
* @param {[string|number, string]} path
* @param {any} value
* @returns {Iterable<string>}
*/
function * treeWithin (path, value) {
if (Array.isArray(value)) {
for (const [index, element] of value.entries()) {
const elementPath = [...path, index]
yield elementPath.join('/')
if (typeof element === 'object' && !CID.asCID(element)) {
yield * tree(element, elementPath)
}
}
} else {
yield * tree(value, path)
}
}

Expand All @@ -49,23 +76,15 @@ const links = function * (source, base) {
* @param {Array<string|number>} base
* @returns {Iterable<string>}
*/
const tree = function * (source, base) {
if (source == null) return
function * tree (source, base) {
if (source == null || typeof source !== 'object') {
return
}
for (const [key, value] of Object.entries(source)) {
const path = [...base, key]
const path = /** @type {[string|number, string]} */ ([...base, key])
yield path.join('/')
if (value != null && !(value instanceof Uint8Array) && typeof value === 'object' && !CID.asCID(value)) {
if (Array.isArray(value)) {
for (const [index, element] of value.entries()) {
const elementPath = [...path, index]
yield elementPath.join('/')
if (typeof element === 'object' && !CID.asCID(element)) {
yield * tree(element, elementPath)
}
}
} else {
yield * tree(value, path)
}
yield * treeWithin(path, value)
}
}
}
Expand All @@ -77,7 +96,7 @@ const tree = function * (source, base) {
* @param {string[]} path
* @return {API.BlockCursorView<unknown>}
*/
const get = (source, path) => {
function get (source, path) {
let node = /** @type {Record<string, any>} */(source)
for (const [index, key] of path.entries()) {
node = node[key]
Expand Down Expand Up @@ -151,7 +170,7 @@ class Block {
* @param {API.MultihashHasher<Alg>} options.hasher
* @returns {Promise<API.BlockView<T, Code, Alg>>}
*/
const encode = async ({ value, codec, hasher }) => {
async function encode ({ value, codec, hasher }) {
if (typeof value === 'undefined') throw new Error('Missing required argument "value"')
if (!codec || !hasher) throw new Error('Missing required argument: codec or hasher')

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

Expand All @@ -202,7 +221,7 @@ const decode = async ({ bytes, codec, hasher }) => {
* @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
* @returns {API.BlockView<T, Code, Alg, V>}
*/
const createUnsafe = ({ bytes, cid, value: maybeValue, codec }) => {
function createUnsafe ({ bytes, cid, value: maybeValue, codec }) {
const value = maybeValue !== undefined
? maybeValue
: (codec && codec.decode(bytes))
Expand All @@ -229,7 +248,7 @@ const createUnsafe = ({ bytes, cid, value: maybeValue, codec }) => {
* @param {API.MultihashHasher<Alg>} options.hasher
* @returns {Promise<API.BlockView<T, Code, Alg, V>>}
*/
const create = async ({ bytes, cid, hasher, codec }) => {
async function create ({ bytes, cid, hasher, codec }) {
if (!bytes) throw new Error('Missing required argument "bytes"')
if (!hasher) throw new Error('Missing required argument "hasher"')
const value = codec.decode(bytes)
Expand Down
17 changes: 0 additions & 17 deletions test/test-cid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,23 +217,6 @@ describe('CID', () => {
)
})

/* TODO: after i have a keccak hash for the new interface
it('handles multibyte varint encoded codec codes', () => {
const ethBlockHash = textEncoder.encode('8a8e84c797605fbe75d5b5af107d4220a2db0ad35fd66d9be3d38d87c472b26d', 'hex')
const hash = keccak256.digest(ethBlockHash)
const cid1 = CID.create(1, 0x90, hash)
const cid2 = CID.parse(cid1.toString())
assert.deepStrictEqual(cid1.code, 0x90)
assert.deepStrictEqual(cid1.version, 1)
assert.deepStrictEqual(cid1.multihash, hash)
assert.deepStrictEqual(cid2.code, 0x90)
assert.deepStrictEqual(cid2.version, 1)
assert.deepStrictEqual(cid2.multihash, hash)
})
*/

it('.bytes', async () => {
const hash = await sha256.digest(textEncoder.encode('abc'))
const code = 0x71
Expand Down
11 changes: 10 additions & 1 deletion test/ts-use/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,14 @@
},
"scripts": {
"test": "npm install && npm_config_yes=true npx -p typescript tsc --noEmit"
},
"eslintConfig": {
"extends": "ipfs",
"parserOptions": {
"sourceType": "module",
"project": [
"./test/ts-use/tsconfig.json"
]
}
}
}
}
1 change: 1 addition & 0 deletions test/ts-use/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const main = async () => {
codec: json
})

/* eslint-disable no-console */
console.log(block)
}

Expand Down

0 comments on commit c12db2a

Please sign in to comment.