High-level utilities for working with INI data.
Parse and roundtrip via arrays
Parse and roundtrip via arrays
import { assertEquals } from "@std/assert"; import { parseArray, stringifyArray } from "@hertzg/wg-ini"; const text = [ "global_key=global_value", "", "[mysection]", "key1=1", "key2=2", "", ].join("\n"); const arr = await parseArray(text); assertEquals(arr, [ [null, [["global_key", "global_value"]]], ["mysection", [["key1", "1"], ["key2", "2"]]], ]); const roundtrip = await stringifyArray(arr); assertEquals(roundtrip, text);
Parse and stringify via objects
Parse and stringify via objects
import { assertEquals } from "@std/assert"; import { parseObject, stringifyObject } from "@hertzg/wg-ini"; const text = [ "global_key=global_value", "", "[mysection]", "key1=1", "key2=2", "", ].join("\n"); const obj = await parseObject(text); assertEquals(obj, { "": { global_key: "global_value" }, mysection: { key1: "1", key2: "2" }, }); const roundtrip = await stringifyObject(obj); assertEquals(roundtrip, text);
Decode a text stream into INI sections.
Encode a stream of INI sections into text lines.
Parse an INI string into an array of sections.
Parse an INI string into an object.
Convert an array of sections to an INI string.
Convert an object to an INI string.
Transform streams for encoding and decoding individual lines of an INI file.
Decode and encode INI lines
Decode and encode INI lines
import { assertEquals } from "@std/assert"; import { IniLineDecoderStream, IniLineEncoderStream } from "@hertzg/wg-ini/lines"; const lines = ["[section]", "key=value"]; const decoded = await Array.fromAsync( ReadableStream.from(lines).pipeThrough(new IniLineDecoderStream()) ); assertEquals(decoded, [ { $section: "section", $trailer: "" }, { $assign: ["key", "value"], $comment: null }, ]); const encoded = await Array.fromAsync( ReadableStream.from(decoded).pipeThrough(new IniLineEncoderStream()) ); assertEquals(encoded, lines);
A transform stream that decodes lines of an INI file into structured objects.
A transform stream that encodes INI line objects back into text lines.
Configuration options for IniLineDecoderStream.
-
commentDelimiter: string
Delimiter for comments (default:
"#"). -
keyValueDelimiter: string
Delimiter for key-value pairs (default:
"="). -
sectionEndDelimiter: string
Delimiter for section end (default:
"]"). -
sectionStartDelimiter: string
Delimiter for section start (default:
"[").
Configuration options for IniLineEncoderStream.
-
commentDelimiter: string
Delimiter for comments (default:
"#"). -
keyValueDelimiter: string
Delimiter for key-value pairs (default:
"="). -
sectionEndDelimiter: string
Delimiter for section end (default:
"]"). -
sectionStartDelimiter: string
Delimiter for section start (default:
"[").
| IniLineAssign
| IniLineComment
| IniLineTrailer
Union type for all INI line types.
Represents an INI key-value assignment line (e.g., key=value).
Represents an INI comment line (e.g., # comment).
Represents an INI section header line (e.g., [section]).
Represents an INI trailer line (empty lines or unrecognized content).
Transform streams for grouping INI lines into sections.
Group lines into sections
Group lines into sections
import { assertEquals } from "@std/assert"; import { IniSectionDecoderStream } from "@hertzg/wg-ini/sections"; import type { IniLine } from "@hertzg/wg-ini/lines"; const lines: IniLine[] = [ { $assign: ["global", "value"], $comment: null }, { $section: "mysection", $trailer: "" }, { $assign: ["key", "value"], $comment: null }, ]; const sections = await Array.fromAsync( ReadableStream.from(lines).pipeThrough(new IniSectionDecoderStream()) ); assertEquals(sections.length, 2); assertEquals(sections[0].section, null); assertEquals(sections[0].entries.length, 1); assertEquals(sections[1].section?.$section, "mysection"); assertEquals(sections[1].entries.length, 1);
A transform stream that groups INI lines into sections.
A transform stream that flattens INI sections back into individual lines.
Represents an INI section with its header and entries.
-
entries: (IniLineAssign | IniLineComment | IniLineTrailer)[]
Lines belonging to this section (assignments, comments, trailers).
-
section: IniLineSection | null
The section header, or
nullfor the global section.