Function parse

  • 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.

    Type Parameters

    • T extends object

      The type of the value

    Parameters

    • text: string

      The text to parse

    • options: ParseOptions = {}

      The options to use

    Returns T

    The parsed object

    If the INI string is invalid or if it contains multi-line values.

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

    const parsed = parse(`
    key = value

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

    assertEquals(parsed, { key: "value", "section 1": { foo: "Hello", baz: "World" } })
    import { parse } from "@std/ini/parse";
    import { assertEquals } from "@std/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(value);
    } else if (key === "amount") {
    return +value;
    }
    }
    return value;
    }
    });

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