@@ -7,29 +7,29 @@ type Writable<T> = {
77} ;
88
99type EncodeOptions = Readonly < {
10- output ?: Array < number > ;
11- maxDepth ?: number ;
12-
10+ output : Array < number > ;
11+ maxDepth : number ;
1312 extensionCodec : ExtensionCodecType ;
1413} > ;
1514
16- const DefaultOptions = {
17- maxDepth : 100 ,
18- extensionCodec : ExtensionCodec . defaultCodec ,
19- } ;
15+ const DEFAULT_MAX_DEPTH = 100 ;
2016
21- export function encode ( value : unknown , options : EncodeOptions = DefaultOptions ) : Array < number > {
22- const result = options . output || [ ] ;
17+ export function encode ( value : unknown , options : Partial < EncodeOptions > = { } ) : Array < number > {
18+ const output = options . output || [ ] ;
19+ const maxDepth = options . maxDepth || DEFAULT_MAX_DEPTH ;
20+ const extensionCodec = options . extensionCodec || ExtensionCodec . defaultCodec ;
2321
24- _encode ( result , 1 , value , options ) ;
22+ _encode ( value , 1 , { output , maxDepth , extensionCodec , } ) ;
2523
26- return result ;
24+ return output ;
2725}
28- function _encode ( rv : Writable < number > , depth : number , object : unknown , options : EncodeOptions ) {
26+ function _encode ( object : unknown , depth : number , options : EncodeOptions ) {
2927 if ( depth > options . maxDepth ! ) {
3028 throw new Error ( `Too deep objects in depth ${ depth } ` ) ;
3129 }
3230
31+ const rv : Writable < number > = options . output ;
32+
3333 if ( object == null ) {
3434 rv . push ( 0xc0 ) ;
3535 } else if ( object === false ) {
@@ -208,7 +208,7 @@ function _encode(rv: Writable<number>, depth: number, object: unknown, options:
208208 throw new Error ( `Too large array: ${ size } ` ) ;
209209 }
210210 for ( const item of object ) {
211- _encode ( rv , depth + 1 , item , options ) ;
211+ _encode ( item , depth + 1 , options ) ;
212212 }
213213 } else if ( isObject ( object ) ) {
214214 const keys = Object . keys ( object ) ;
@@ -227,8 +227,8 @@ function _encode(rv: Writable<number>, depth: number, object: unknown, options:
227227 }
228228
229229 for ( const key of keys ) {
230- _encode ( rv , depth + 1 , key , options ) ;
231- _encode ( rv , depth + 1 , object [ key ] , options ) ;
230+ _encode ( key , depth + 1 , options ) ;
231+ _encode ( object [ key ] , depth + 1 , options ) ;
232232 }
233233 } else {
234234 // not encodable unless ExtensionCodec handles it,
0 commit comments