parse

import { parse } from "jsr:@ende/ini";

function

function parse<T extends object>(text: string, options: ParseOptions = [UNSUPPORTED]): T

Parse an INI config string into an object.

Values are parsed as strings by default to preserve data parity from the original. To parse values as other types besides strings, use ParseOptions.reviver.

Nested sections, repeated key names within a section, and key/value arrays are not supported. White space padding and lines starting with #, ;, or // will be treated as comments.

text: string

The text to parse

options: ParseOptions = [UNSUPPORTED]

The options to use

Returns

The parsed object

Example: Usage

import { parse } from "@ende/ini/parse";
import { assertEquals } from "@ende/assert";

const parsed = parse(`
key = value

[section 1]
foo = Hello
baz = World
`);

assertEquals(parsed, { key: "value", "section 1": { foo: "Hello", baz: "World" } })

Example: Using custom reviver

import { parse } from "@ende/ini/parse";
import { assertEquals } from "@ende/assert";

const parsed = parse(`
[section Foo]
date = 2012-10-10
amount = "12345"
`, {
  reviver(key, value, section) {
    if (section === "section Foo") {
      if (key === "date") {
        return new Date(String(value));
      } else if (key === "amount") {
        return Number(value);
      }
    }
    return value;
  }
});

assertEquals(parsed, {
  "section Foo": {
    date: new Date("2012-10-10"),
    amount: 12345,
  }
})

Source