Function parse
- parse<T>(text, options?): T
Type Parameters
Parameters
- text: string
The text to parse
- options: ParseOptions = {}
The options to use
Returns T
The parsed object
Example: Usage
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" } })Example: Using custom reviver
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,
}
})- text: string
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.