MCPcopy Create free account
hub / github.com/CashScript/cashscript / formatObject

Function formatObject

packages/utils/src/artifact.ts:132–154  ·  view source on GitHub ↗
(
  obj: any,
  format: 'json' | 'ts',
  indentationLevel: number,
  isArrayElement: boolean,
)

Source from the content-addressed store, hash-verified

130}
131
132function 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
157function canInline(container: any): boolean {

Callers 1

stringifyFunction · 0.85

Calls 4

formatKeyFunction · 0.85
stringifyFunction · 0.85
canInlineFunction · 0.85
indentFunction · 0.85

Tested by

no test coverage detected