| 130 | } |
| 131 | |
| 132 | function formatObject( |
| 133 | obj: any, |
| 134 | format: 'json' | 'ts', |
| 135 | indentationLevel: number, |
| 136 | isArrayElement: boolean, |
| 137 | ): string { |
| 138 | const entries = Object.entries(obj).filter(([, value]) => value !== undefined); |
| 139 | |
| 140 | if (entries.length === 0) return '{}'; |
| 141 | |
| 142 | const formatKey = (key: string): string => (format === 'json' ? JSON.stringify(key) : key); |
| 143 | const formatted = entries.map( |
| 144 | ([key, value]) => `${formatKey(key)}: ${stringify(value, format, indentationLevel + 1)}`, |
| 145 | ); |
| 146 | |
| 147 | if (isArrayElement && entries.length <= MAX_INLINE_OBJECT_PROPERTIES && canInline(obj)) { |
| 148 | return `{ ${formatted.join(', ')} }`; |
| 149 | } |
| 150 | |
| 151 | const lines = formatted.map((entry) => `${indent(indentationLevel)}${entry}`).join(',\n'); |
| 152 | const trailingComma = format === 'ts' ? ',' : ''; |
| 153 | return `{\n${lines}${trailingComma}\n${indent(indentationLevel - 1)}}`; |
| 154 | } |
| 155 | |
| 156 | // A container can be inlined when none of its values are themselves a non-empty object or array |
| 157 | function canInline(container: any): boolean { |