//#region src/constants.d.ts declare const DELIMITERS: { readonly comma: ","; readonly tab: "\t"; readonly pipe: "|"; }; type DelimiterKey = keyof typeof DELIMITERS; type Delimiter = typeof DELIMITERS[DelimiterKey]; declare const DEFAULT_DELIMITER: Delimiter; //#endregion //#region src/types.d.ts type JsonPrimitive = string | number | boolean | null; type JsonObject = { [Key in string]: JsonValue; } & { [Key in string]?: JsonValue | undefined; }; type JsonArray = JsonValue[] | readonly JsonValue[]; type JsonValue = JsonPrimitive | JsonObject | JsonArray; /** * Transforms or filters values during encoding. * * Called for every value (root, object properties, array elements) during the encoding process. * Similar to `JSON.stringify`'s replacer, but with path tracking. * * @param key The property key or array index as a string, empty at the root * @param value The normalized `JsonValue` at this location * @param path Array representing the path from root to this value * * @returns The replacement value (will be normalized again), or `undefined` to omit – * at the root, `undefined` means "no change" rather than an omission * * @example * ```ts * // Remove password fields * const replacer = (key, value) => { * if (key === 'password') return undefined * return value * } * * // Add timestamps * const replacer = (key, value, path) => { * if (path.length === 0 && typeof value === 'object' && value !== null) { * return { ...value, _timestamp: Date.now() } * } * return value * } * ``` */ type EncodeReplacer = (key: string, value: JsonValue, path: readonly (string | number)[]) => unknown; interface EncodeOptions { /** * Number of spaces per indentation level. * @default 2 */ indentSize?: number; /** * @deprecated Use `indentSize` instead. */ indent?: number; /** * Delimiter to use for tabular array rows and inline primitive arrays. * @default DELIMITERS.comma */ delimiter?: Delimiter; /** * 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). * @default undefined */ replacer?: EncodeReplacer; } type ResolvedEncodeOptions = Readonly>> & Pick; interface DecodeOptions { /** * Number of spaces per indentation level. * @default 2 */ indentSize?: number; /** * @deprecated Use `indentSize` instead. */ indent?: number; /** * When true, enforce strict validation of array lengths and tabular row counts. * @default true */ strict?: boolean; } type ResolvedDecodeOptions = Readonly>>; type DecodeStreamOptions = DecodeOptions; type JsonStreamEvent = { type: "startObject"; } | { type: "endObject"; } | { type: "startArray"; length: number; } | { type: "endArray"; } | { type: "key"; key: string; } | { type: "primitive"; value: JsonPrimitive; }; //#endregion //#region src/decode/errors.d.ts /** * Error thrown by the TOON decoder when input cannot be parsed. * * Extends `SyntaxError` so existing `instanceof SyntaxError` checks keep working. * Adds structured location fields for programmatic consumers and richer CLI output. */ declare class ToonDecodeError extends SyntaxError { /** 1-based line number where the error was detected, if known. */ readonly line?: number; /** Raw source line (including indentation) where the error was detected, if known. */ readonly source?: string; constructor(message: string, context?: { line?: number; source?: string; cause?: unknown; }); } //#endregion //#region src/encode/raw-string.d.ts /** * Pre-formatted string that the encoder emits verbatim at a primitive value * position, bypassing quoting, escaping, and number/keyword detection. * * Returned from a replacer for an object or array value, it is ignored and * the container is encoded normally. */ declare class RawString { readonly value: string; constructor(value: string); } /** * Wraps a pre-formatted string for verbatim emission, typically returned from * an encode `replacer`. Compose with `escapeString` to control quoting yourself. * * @param value The exact text to emit at the value position * @returns A `RawString` marker honored at primitive value positions * * @example * ```ts * encode({ name: 'Ada', age: 30 }, { * replacer: (key, value) => rawString(`"${escapeString(String(value))}"`) * }) * // name: "Ada" * // age: "30" * ``` */ declare function rawString(value: string): RawString; //#endregion //#region src/shared/string-utils.d.ts /** * Escapes special characters in a string for encoding. * * @remarks * Control characters outside `\n`, `\r`, `\t`, `\\`, and `"` are emitted as `\uXXXX`. */ declare function escapeString(value: string): string; //#endregion //#region src/index.d.ts /** * Encodes a JavaScript value into TOON format string. * * @param input Any JavaScript value (objects, arrays, primitives) * @param options Optional encoding configuration * @returns TOON formatted string * * @example * ```ts * encode({ name: 'Ada', age: 30 }) * // name: Ada * // age: 30 * * encode({ users: [{ id: 1 }, { id: 2 }] }) * // users[2]{id}: * // 1 * // 2 * * encode({ tags: [] }) * // tags: [] * * encode(data, { indentSize: 4 }) * ``` */ declare function encode(input: unknown, options?: EncodeOptions): string; /** * Decodes a TOON format string into a JavaScript value. * * @param input TOON formatted string * @param options Optional decoding configuration * @returns Parsed JavaScript value (object, array, or primitive) * * @example * ```ts * decode('name: Ada\nage: 30') * // { name: 'Ada', age: 30 } * * decode('users[2]:\n - id: 1\n - id: 2') * // { users: [{ id: 1 }, { id: 2 }] } * * decode('tags: []') * // { tags: [] } * * decode(toonString, { strict: false }) * ``` */ declare function decode(input: string, options?: DecodeOptions): JsonValue; /** * 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. * * @param input Any JavaScript value (objects, arrays, primitives) * @param options Optional encoding configuration * @returns Iterable of TOON lines (without trailing newlines) * * @example * ```ts * // Stream to stdout * for (const line of encodeLines({ name: 'Ada', age: 30 })) { * console.log(line) * } * * // Collect to array * const lines = Array.from(encodeLines(data)) * * // Equivalent to encode() * const toonString = Array.from(encodeLines(data, options)).join('\n') * ``` */ declare function encodeLines(input: unknown, options?: EncodeOptions): Iterable; /** * Decodes TOON format from pre-split lines into a JavaScript value. * * Convenience wrapper around the streaming decoder that builds the full * value in memory. * * @param lines Iterable of TOON lines (without newlines) * @param options Optional decoding configuration * @returns Parsed JavaScript value (object, array, or primitive) * * @example * ```ts * const lines = ['name: Ada', 'age: 30'] * decodeFromLines(lines) * // { name: 'Ada', age: 30 } * ``` */ declare function decodeFromLines(lines: Iterable, options?: DecodeOptions): JsonValue; /** * Synchronously decodes TOON lines into a stream of JSON events. * * Yields structured events (startObject, endObject, startArray, endArray, key, * primitive) that represent the JSON data model without building the full value tree. * * @param lines Iterable of TOON lines (without newlines) * @param options Optional decoding configuration * @returns Iterable of JSON stream events * * @example * ```ts * const lines = ['name: Ada', 'age: 30'] * for (const event of decodeStreamSync(lines)) { * console.log(event) * // { type: 'startObject' } * // { type: 'key', key: 'name' } * // { type: 'primitive', value: 'Ada' } * // ... * } * ``` */ declare function decodeStreamSync(lines: Iterable, options?: DecodeStreamOptions): Iterable; /** * Asynchronously decodes TOON lines into a stream of JSON events. * * Yields structured events (startObject, endObject, startArray, endArray, key, * primitive) that represent the JSON data model without building the full value tree. * Supports both sync and async iterables. * * @param source Async or sync iterable of TOON lines (without newlines) * @param options Optional decoding configuration * @returns Async iterable of JSON stream events * * @example * ```ts * const fileStream = createReadStream('data.toon', 'utf-8') * const lines = splitLines(fileStream) // Async iterable of lines * * for await (const event of decodeStream(lines)) { * console.log(event) * // { type: 'startObject' } * // { type: 'key', key: 'name' } * // { type: 'primitive', value: 'Ada' } * // ... * } * ``` */ declare function decodeStream(source: AsyncIterable | Iterable, options?: DecodeStreamOptions): AsyncIterable; //#endregion export { DEFAULT_DELIMITER, DELIMITERS, type DecodeOptions, type DecodeStreamOptions, type Delimiter, type DelimiterKey, type EncodeOptions, type EncodeReplacer, type JsonArray, type JsonObject, type JsonPrimitive, type JsonStreamEvent, type JsonValue, type RawString, type ResolvedDecodeOptions, type ResolvedEncodeOptions, ToonDecodeError, decode, decodeFromLines, decodeStream, decodeStreamSync, encode, encodeLines, escapeString, rawString };