11import type { Depth , JsonArray , JsonObject , JsonPrimitive , JsonValue , ResolvedEncodeOptions } from '../types'
22import { DOT , LIST_ITEM_MARKER } from '../constants'
33import { tryFoldKeyChain } from './folding'
4- import { isArrayOfArrays , isArrayOfObjects , isArrayOfPrimitives , isJsonArray , isJsonObject , isJsonPrimitive } from './normalize'
4+ import { isArrayOfArrays , isArrayOfObjects , isArrayOfPrimitives , isEmptyObject , isJsonArray , isJsonObject , isJsonPrimitive } from './normalize'
55import { encodeAndJoinPrimitives , encodeKey , encodePrimitive , formatHeader } from './primitives'
66import { LineWriter } from './writer'
77
@@ -38,8 +38,8 @@ export function encodeObject(value: JsonObject, writer: LineWriter, depth: Depth
3838
3939 const effectiveFlattenDepth = remainingDepth ?? options . flattenDepth
4040
41- for ( const key of keys ) {
42- encodeKeyValuePair ( key , value [ key ] ! , writer , depth , options , keys , rootLiteralKeys , pathPrefix , effectiveFlattenDepth )
41+ for ( const [ key , val ] of Object . entries ( value ) ) {
42+ encodeKeyValuePair ( key , val , writer , depth , options , keys , rootLiteralKeys , pathPrefix , effectiveFlattenDepth )
4343 }
4444}
4545
@@ -66,7 +66,7 @@ export function encodeKeyValuePair(key: string, value: JsonValue, writer: LineWr
6666 encodeArray ( foldedKey , leafValue , writer , depth , options )
6767 return
6868 }
69- else if ( isJsonObject ( leafValue ) && Object . keys ( leafValue ) . length === 0 ) {
69+ else if ( isJsonObject ( leafValue ) && isEmptyObject ( leafValue ) ) {
7070 writer . push ( depth , `${ encodedFoldedKey } :` )
7171 return
7272 }
@@ -94,13 +94,8 @@ export function encodeKeyValuePair(key: string, value: JsonValue, writer: LineWr
9494 encodeArray ( key , value , writer , depth , options )
9595 }
9696 else if ( isJsonObject ( value ) ) {
97- const nestedKeys = Object . keys ( value )
98- if ( nestedKeys . length === 0 ) {
99- // Empty object
100- writer . push ( depth , `${ encodedKey } :` )
101- }
102- else {
103- writer . push ( depth , `${ encodedKey } :` )
97+ writer . push ( depth , `${ encodedKey } :` )
98+ if ( ! isEmptyObject ( value ) ) {
10499 encodeObject ( value , writer , depth + 1 , options , rootLiteralKeys , currentPath , effectiveFlattenDepth )
105100 }
106101 }
@@ -279,16 +274,14 @@ export function encodeMixedArrayAsListItems(
279274}
280275
281276export function encodeObjectAsListItem ( obj : JsonObject , writer : LineWriter , depth : Depth , options : ResolvedEncodeOptions ) : void {
282- const keys = Object . keys ( obj )
283- if ( keys . length === 0 ) {
277+ if ( isEmptyObject ( obj ) ) {
284278 writer . push ( depth , LIST_ITEM_MARKER )
285279 return
286280 }
287281
288- // First key-value on the same line as "- "
289- const firstKey = keys [ 0 ] !
282+ const entries = Object . entries ( obj )
283+ const [ firstKey , firstValue ] = entries [ 0 ] !
290284 const encodedKey = encodeKey ( firstKey )
291- const firstValue = obj [ firstKey ] !
292285
293286 if ( isJsonPrimitive ( firstValue ) ) {
294287 writer . pushListItem ( depth , `${ encodedKey } : ${ encodePrimitive ( firstValue , options . delimiter ) } ` )
@@ -327,20 +320,16 @@ export function encodeObjectAsListItem(obj: JsonObject, writer: LineWriter, dept
327320 }
328321 }
329322 else if ( isJsonObject ( firstValue ) ) {
330- const nestedKeys = Object . keys ( firstValue )
331- if ( nestedKeys . length === 0 ) {
332- writer . pushListItem ( depth , `${ encodedKey } :` )
333- }
334- else {
335- writer . pushListItem ( depth , `${ encodedKey } :` )
323+ writer . pushListItem ( depth , `${ encodedKey } :` )
324+ if ( ! isEmptyObject ( firstValue ) ) {
336325 encodeObject ( firstValue , writer , depth + 2 , options )
337326 }
338327 }
339328
340- // Remaining keys on indented lines
341- for ( let i = 1 ; i < keys . length ; i ++ ) {
342- const key = keys [ i ] !
343- encodeKeyValuePair ( key , obj [ key ] ! , writer , depth + 1 , options )
329+ // Remaining entries on indented lines
330+ for ( let i = 1 ; i < entries . length ; i ++ ) {
331+ const [ key , value ] = entries [ i ] !
332+ encodeKeyValuePair ( key , value , writer , depth + 1 , options )
344333 }
345334}
346335
0 commit comments