@@ -3,43 +3,70 @@ import { bytes as binary, CID } from './index.js'
33// eslint-disable-next-line
44import * 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 )
0 commit comments