@@ -159,7 +159,7 @@ To reset formatting and apply a new style:
159159``` ts
160160node .reset ();
161161
162- console .log (node .toString ({ indentationLevel: 2 }));
162+ console .log (node .toString ({/* formatting options */ }));
163163```
164164
165165Output:
@@ -179,6 +179,8 @@ Output:
179179}
180180```
181181
182+ Refer to the [ Formatting] ( #formatting ) section for details on available options.
183+
182184To update the document while preserving formatting, use the ` update ` method:
183185
184186``` ts
@@ -238,6 +240,130 @@ Output:
238240}
239241```
240242
243+ Here’s a drop-in README section that documents those options clearly, with concise tables and runnable examples.
244+
245+ ### Formatting
246+
247+ When you call ` toString(options) ` you can control how the output is formatted.
248+ If an option is omitted, the formatter learns from the current document and keeps its style. If no style can be inferred,
249+ the formatter falls back to a compact style with no extra spaces or indentation.
250+
251+ ### Options
252+
253+ | Option | Type/Values | Description |
254+ | --------------------------------| ------------------------| --------------------------------------------------------|
255+ | ** indentationLevel** | ` number ` | Base indentation level for the document. |
256+ | ** indentationCharacter** | ` 'space' \| 'tab' ` | Character used for indentation. |
257+ | ** string.quote** | ` 'single' \| 'double' ` | Quotation style for string values. |
258+ | ** property.quote** | ` 'single' \| 'double' ` | Quotation style for property keys. |
259+ | ** property.unquoted** | ` boolean ` | Allow unquoted property keys when valid. |
260+ | ** array.indentationSize** | ` number ` | Indentation size for array entries. |
261+ | ** array.trailingIndentation** | ` boolean ` | Indent closing bracket on a new line. |
262+ | ** array.leadingIndentation** | ` boolean ` | Indent opening bracket on a new line. |
263+ | ** array.entryIndentation** | ` boolean ` | Indent each array entry. |
264+ | ** array.trailingComma** | ` boolean ` | Append a trailing comma after the last entry. |
265+ | ** array.commaSpacing** | ` boolean ` | Add space after commas in arrays. |
266+ | ** array.colonSpacing** | ` boolean ` | Add space after colons in arrays (for objects inline). |
267+ | ** object.indentationSize** | ` number ` | Indentation size for object properties. |
268+ | ** object.trailingIndentation** | ` boolean ` | Indent closing brace on a new line. |
269+ | ** object.leadingIndentation** | ` boolean ` | Indent opening brace on a new line. |
270+ | ** object.entryIndentation** | ` boolean ` | Indent each object property. |
271+ | ** object.trailingComma** | ` boolean ` | Append a trailing comma after the last property. |
272+ | ** object.commaSpacing** | ` boolean ` | Add space after commas in objects. |
273+ | ** object.colonSpacing** | ` boolean ` | Add space after colons in objects. |
274+
275+ ### Quick recipes
276+
277+ Here are some common formatting styles you can achieve by combining different options.
278+
279+ #### 1) Compact, single-line arrays; pretty multi-line objects
280+
281+ ``` ts
282+ node .toString ({
283+ array: {
284+ leadingIndentation: false , // keep arrays inline when short
285+ trailingComma: false ,
286+ commaSpacing: true ,
287+ },
288+ object: {
289+ leadingIndentation: true , // multi-line objects
290+ entryIndentation: true ,
291+ trailingIndentation: true ,
292+ indentationSize: 2 ,
293+ trailingComma: true ,
294+ commaSpacing: true ,
295+ colonSpacing: true ,
296+ },
297+ });
298+ ```
299+
300+ Output style:
301+
302+ ``` json5
303+ {
304+ " name" : " My Project" ,
305+ " keywords" : [" json5" , " parser" ], // array kept inline
306+ " deps" : {
307+ " typescript" : " ^5.6.0" , // multi-line object
308+ " vite" : " ^5.4.0" ,
309+ }
310+ }
311+ ```
312+
313+ #### 2) Tabs everywhere, single quotes, unquoted props where valid
314+
315+ ``` ts
316+ node .toString ({
317+ indentationCharacter: ' tab' ,
318+ string: {quote: ' single' },
319+ property: {quote: ' single' , unquoted: true },
320+ object: {
321+ leadingIndentation: true ,
322+ entryIndentation: true ,
323+ trailingIndentation: true ,
324+ trailingComma: false ,
325+ commaSpacing: true ,
326+ colonSpacing: true ,
327+ },
328+ });
329+ ```
330+
331+ Output style:
332+
333+ ``` json5
334+ {
335+ foo: ' bar' ,
336+ items: [
337+ ' a' ,
338+ ' b'
339+ ]
340+ }
341+ ```
342+
343+ #### 3) Strict compact style (no spaces after commas/colons)
344+
345+ ``` ts
346+ node .toString ({
347+ array: {
348+ leadingIndentation: false ,
349+ trailingComma: false ,
350+ commaSpacing: false ,
351+ },
352+ object: {
353+ leadingIndentation: false ,
354+ trailingComma: false ,
355+ commaSpacing: false ,
356+ colonSpacing: false ,
357+ },
358+ });
359+ ```
360+
361+ Output style:
362+
363+ ``` json5
364+ {" a" : 1 ," b" : [1 ,2 ,3 ]}
365+ ```
366+
241367## Contributing
242368
243369Contributions are welcome!
0 commit comments