@@ -3,43 +3,70 @@ import { bytes as binary, CID } from './index.js'
3
3
// eslint-disable-next-line
4
4
import * as API from './interface.js'
5
5
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
+ }
11
37
12
38
/**
13
39
* @template T
14
40
* @param {T } source
15
41
* @param {Array<string|number> } base
16
42
* @returns {Iterable<[string, CID]> }
17
43
*/
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
+ }
21
48
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 )
41
66
}
42
67
}
68
+ } else {
69
+ yield * tree ( value , path )
43
70
}
44
71
}
45
72
@@ -49,23 +76,15 @@ const links = function * (source, base) {
49
76
* @param {Array<string|number> } base
50
77
* @returns {Iterable<string> }
51
78
*/
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
+ }
54
83
for ( const [ key , value ] of Object . entries ( source ) ) {
55
- const path = [ ...base , key ]
84
+ const path = /** @type { [string|number, string] } */ ( [ ...base , key ] )
56
85
yield path . join ( '/' )
57
86
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 )
69
88
}
70
89
}
71
90
}
@@ -77,7 +96,7 @@ const tree = function * (source, base) {
77
96
* @param {string[] } path
78
97
* @return {API.BlockCursorView<unknown> }
79
98
*/
80
- const get = ( source , path ) => {
99
+ function get ( source , path ) {
81
100
let node = /** @type {Record<string, any> } */ ( source )
82
101
for ( const [ index , key ] of path . entries ( ) ) {
83
102
node = node [ key ]
@@ -151,7 +170,7 @@ class Block {
151
170
* @param {API.MultihashHasher<Alg> } options.hasher
152
171
* @returns {Promise<API.BlockView<T, Code, Alg>> }
153
172
*/
154
- const encode = async ( { value, codec, hasher } ) => {
173
+ async function encode ( { value, codec, hasher } ) {
155
174
if ( typeof value === 'undefined' ) throw new Error ( 'Missing required argument "value"' )
156
175
if ( ! codec || ! hasher ) throw new Error ( 'Missing required argument: codec or hasher' )
157
176
@@ -177,7 +196,7 @@ const encode = async ({ value, codec, hasher }) => {
177
196
* @param {API.MultihashHasher<Alg> } options.hasher
178
197
* @returns {Promise<API.BlockView<T, Code, Alg>> }
179
198
*/
180
- const decode = async ( { bytes, codec, hasher } ) => {
199
+ async function decode ( { bytes, codec, hasher } ) {
181
200
if ( ! bytes ) throw new Error ( 'Missing required argument "bytes"' )
182
201
if ( ! codec || ! hasher ) throw new Error ( 'Missing required argument: codec or hasher' )
183
202
@@ -202,7 +221,7 @@ const decode = async ({ bytes, codec, hasher }) => {
202
221
* @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
203
222
* @returns {API.BlockView<T, Code, Alg, V> }
204
223
*/
205
- const createUnsafe = ( { bytes, cid, value : maybeValue , codec } ) => {
224
+ function createUnsafe ( { bytes, cid, value : maybeValue , codec } ) {
206
225
const value = maybeValue !== undefined
207
226
? maybeValue
208
227
: ( codec && codec . decode ( bytes ) )
@@ -229,7 +248,7 @@ const createUnsafe = ({ bytes, cid, value: maybeValue, codec }) => {
229
248
* @param {API.MultihashHasher<Alg> } options.hasher
230
249
* @returns {Promise<API.BlockView<T, Code, Alg, V>> }
231
250
*/
232
- const create = async ( { bytes, cid, hasher, codec } ) => {
251
+ async function create ( { bytes, cid, hasher, codec } ) {
233
252
if ( ! bytes ) throw new Error ( 'Missing required argument "bytes"' )
234
253
if ( ! hasher ) throw new Error ( 'Missing required argument "hasher"' )
235
254
const value = codec . decode ( bytes )
0 commit comments