Examples
Example 1
Example 1
import * as ini from "@std/ini"; const iniFile = `# Example configuration file Global Key=Some data here [Section #1] Section Value=42 Section Date=1977-05-25 `; const parsed = ini.parse(iniFile, { reviver: (key, value, section) => { if (section === "Section #1") { if (key === "Section Value") return Number(value); if (key === "Section Date") return new Date(value); } return value; }, }); console.log(parsed); // => // { // "Global Key": "Some data here", // "Section #1": { "Section Value": 42, "Section Date": 1977-05-25T00:00:00.000Z } // } const text = ini.stringify(parsed, { replacer: (key, value, section) => { if (section === "Section #1" && key === "Section Date") { return (value as Date).toISOString().split("T")[0]; } return value; }, }); console.log(text); // => // Global Key=Some data here // [Section #1] // Section Value=42 // Section Date=1977-05-25
Optionally, IniMap may be used for finer INI handling. Using this class will permit preserving
comments, accessing values like a map, iterating over key/value/section entries, and more.
Example 2
Example 2
import { IniMap } from "@std/ini"; const ini = new IniMap(); ini.set("section1", "keyA", 100) console.log(ini.toString()) // => // [section1] // keyA=100 ini.set('keyA', 25) console.log(ini.toObject()) // => // { // keyA: 25, // section1: { // keyA: 100 // } // }
The reviver and replacer APIs can be used to extend the behavior of IniMap, such as adding support for duplicate keys as if they were arrays of values.
Example 3
Example 3
import { IniMap } from "@std/ini"; const iniFile = `# Example of key/value arrays [section1] key1=This key key1=is non-standard key1=but can be captured! `; const ini = new IniMap({ assignment: "=", deduplicate: true }); ini.parse(iniFile, (key, value, section) => { if (section) { if (ini.has(section, key)) { const exists = ini.get(section, key); if (Array.isArray(exists)) { exists.push(value); return exists; } else { return [exists, value]; } } } return value; }); console.log(ini.get("section1", "key1")); // => [ "This key", "is non-standard", "but can be captured!" ] const result = ini.toString((key, value) => { if (Array.isArray(value)) { return value.join( `${ini.formatting.lineBreak}${key}${ini.formatting.assignment}`, ); } return value; }); console.log(result === iniFile); // => true
Classes
Class implementation for fine control of INI data structures.
- clear(sectionName?: string): void
Clear a single section or the entire INI.
- comments(): Comments
Manage comments in the INI.
- delete(key: string): boolean
Delete a global key in the INI.
- entries(): Generator<[string, unknown, string | undefined]>
Iterate over each entry in the INI to retrieve key, value, and section.
- formatting(): FormattingNo documentation available
- from(): IniMapinput: string,options?: ParseOptions & FormattingOptions
Create an
IniMapfrom an INI string. - get(key: string): unknown
Get a value from a global key in the INI.
- has(key: string): boolean
Check if a global key exists in the INI.
- parse(): thistext: string,reviver?: ReviverFunction
Parse an INI string in this
IniMap. - set(): thiskey: string,value: any
Set the value of a global key in the INI.
- size(): number
Get the count of key/value pairs.
- toJSON(): Record<string, unknown | Record<string, unknown>>
Convenience method for
JSON.stringify. - toObject(): Record<string, unknown | Record<string, unknown>>
Convert this
IniMapto a plain object. - toString(replacer?: ReplacerFunction): string
Convert this
IniMapto an INI string.
Functions
Parse an INI config string into an object. Provide formatting options to override the default assignment operator.
Compile an object into an INI config string. Provide formatting options to modify the output.
Interfaces
- clear(): void
Clear all comments in the INI.
- deleteAtKey(key: string): boolean
Delete a comment before a global key in the INI.
- deleteAtLine(line: number): boolean
Delete a comment at a specific line in the INI.
- deleteAtSection(section: string): boolean
Delete a comment before a section line in the INI.
- getAtKey(key: string): string | undefined
Get a comment before a global key in the INI.
- getAtLine(line: number): string | undefined
Get a comment at a specific line in the INI.
- getAtSection(section: string): string | undefined
Get a comment before a section line in the INI.
- setAtKey(): Commentskey: string,text: string
Set a comment before a global key in the INI.
- setAtLine(): Commentsline: number,text: string
Set a comment at a specific line in the INI.
- setAtSection(): Commentssection: string,text: string
Set a comment before a section line in the INI.
Options for providing formatting marks.
- assignment: string
The character used to assign a value to a key; defaults to '='.
- commentChar: "#" | ";" | "//"
Mark to use for setting comments; expects '#', ';', '//', defaults to '#' unless another mark is found.
- deduplicate: boolean
Filter duplicate keys from INI string output; defaults to false to preserve data parity.
- lineBreak: "\n" | "\r\n"
Character(s) used to break lines in the config file; defaults to '\n'. Ignored on parse.
- pretty: boolean
Use a plain assignment char or pad with spaces; defaults to false. Ignored on parse.
Options for parsing INI strings.
- assignment: FormattingOptions["assignment"]
The character used to assign a value to a key; defaults to '='.
- reviver: ReviverFunction
Provide custom parsing of the value in a key/value pair.
Options for constructing INI strings.
- replacer: ReplacerFunction
Provide custom string conversion for the value in a key/value pair.
Type Aliases
Function for replacing JavaScript values with INI string values.
Function for replacing INI values with JavaScript values.