A transform stream that groups INI lines into sections.
Takes a stream of IniLine objects (from IniLineDecoderStream) and groups them into IniSection objects. Each section contains a header and all entries until the next section header.
The global section (lines before any [section] header) uses null for the
section property. It is only emitted if it contains entries.
Group INI lines into sections
Group INI lines into sections
import { IniSectionDecoderStream } from "@hertzg/wg-ini/sections"; import { assertEquals } from "@std/assert"; import type { IniLine } from "@hertzg/wg-ini/lines"; const lines: IniLine[] = [ { $comment: " comment outside of section" }, { $assign: ["global_key", "global_value "], $comment: " with comment" }, { $trailer: "" }, { $section: "mysection", $trailer: "" }, { $assign: ["key1", "1"], $comment: null }, { $assign: ["key2", "2"], $comment: null }, { $trailer: "" }, { $section: "mysection", $trailer: " # and a comment" }, { $assign: ["key1", "3"], $comment: null }, { $assign: ["key2", "4"], $comment: null }, { $trailer: "" }, ]; const stream = ReadableStream.from(lines).pipeThrough( new IniSectionDecoderStream() ); const sections = await Array.fromAsync(stream); assertEquals(sections, [ { section: null, entries: [ { $comment: " comment outside of section" }, { $assign: ["global_key", "global_value "], $comment: " with comment" }, { $trailer: "" }, ], }, { section: { $section: "mysection", $trailer: "" }, entries: [ { $assign: ["key1", "1"], $comment: null }, { $assign: ["key2", "2"], $comment: null }, { $trailer: "" }, ], }, { section: { $section: "mysection", $trailer: " # and a comment" }, entries: [ { $assign: ["key1", "3"], $comment: null }, { $assign: ["key2", "4"], $comment: null }, { $trailer: "" }, ], }, ]);