mod.ts

High-level utilities for working with INI data.

Examples

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

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);

Functions

lines.ts

Transform streams for encoding and decoding individual lines of an INI file.

Examples

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);

Classes

c
IniLineDecoderStream(options?: IniLineDecoderStreamOptions)

A transform stream that decodes lines of an INI file into structured objects.

c
IniLineEncoderStream(options?: IniLineEncoderStreamOptions)

A transform stream that encodes INI line objects back into text lines.

Interfaces

I
IniLineDecoderStreamOptions

Configuration options for IniLineDecoderStream.

I
IniLineEncoderStreamOptions

Configuration options for IniLineEncoderStream.

Type Aliases

T
IniLineComment = { $comment: string; }

Represents an INI comment line (e.g., # comment).

T
IniLineTrailer = { $trailer: string; }

Represents an INI trailer line (empty lines or unrecognized content).

sections.ts

Transform streams for grouping INI lines into sections.

Examples

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);

Classes

c
IniSectionDecoderStream()

A transform stream that groups INI lines into sections.

c
IniSectionEncoderStream()

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

Type Aliases

T
IniSection = { section: IniLineSection | null; entries: (IniLineAssign | IniLineComment | IniLineTrailer)[]; }

Represents an INI section with its header and entries.