class IniSectionEncoderStream

A transform stream that flattens INI sections back into individual lines.

This is the reverse of IniSectionDecoderStream. It takes a stream of IniSection objects and emits their lines in order: section header (if not global) followed by all entries.

Examples

Flatten sections to INI lines

import { IniSectionEncoderStream, type IniSection } from "@hertzg/wg-ini/sections";
import { assertEquals } from "@std/assert";

const sections: IniSection[] = [
  {
    section: null,
    entries: [
      { $comment: " comment outside of section" },
      { $assign: ["global_key", "global_value "], $comment: " with comment" },
      { $trailer: "" },
    ],
  },
  {
    section: { $section: "mysection" },
    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: "" },
    ],
  },
];

const stream = ReadableStream.from(sections).pipeThrough(
  new IniSectionEncoderStream()
);
const lines = await Array.fromAsync(stream);

assertEquals(lines, [
  { $comment: " comment outside of section" },
  { $assign: ["global_key", "global_value "], $comment: " with comment" },
  { $trailer: "" },
  { $section: "mysection" },
  { $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: "" },
]);

Constructors

IniSectionEncoderStream()

Create a new INI section encoder stream.

Usage

import { IniSectionEncoderStream } from "sections.ts";