Skip to main content

Function Signature

Encodes a JavaScript value into TOON format as a sequence of lines. This function yields TOON lines one at a time without building the full string, making it suitable for streaming large outputs to files, HTTP responses, or process stdout.

Parameters

unknown
required
Any JavaScript value to encode (objects, arrays, primitives). The value will be normalized to JSON-compatible types.
EncodeOptions
Optional encoding configuration.
number
default:"2"
Number of spaces per indentation level.
Delimiter
default:"DELIMITERS.comma"
Delimiter to use for tabular array rows and inline primitive arrays.
'off' | 'safe'
default:"'off'"
Enable key folding to collapse single-key wrapper chains. When set to 'safe', nested objects with single keys are collapsed into dotted paths (e.g., data.metadata.items instead of nested indentation).
number
default:"Infinity"
Maximum number of segments to fold when keyFolding is enabled. Controls how deep the folding can go in single-key chains. Values 0 or 1 have no practical effect (treated as effectively disabled).
EncodeReplacer
A function to transform or filter values during encoding. Called for the root value and every nested property/element. Return undefined to omit properties/elements (root cannot be omitted).

Returns

Iterable<string>
Iterable of TOON lines (without trailing newlines). Each line is a complete TOON line ready to be written or printed.

Behavior

  • Lines are yielded one at a time without building the full string in memory
  • Each line does NOT include a trailing newline character
  • Empty objects yield zero lines
  • Lines do not have trailing whitespace
  • The iterable can be consumed with for-of loops, Array.from(), or spread syntax

Examples

Use Cases

Memory Efficiency

For large data structures, encodeLines avoids building the entire TOON string in memory:

HTTP Streaming