js-ini-parser
Version:
A simple ini parser that save comments and spaces
66 lines (63 loc) • 2.37 kB
TypeScript
type Block = {
type: 'comment';
text: string;
} | {
type: 'data';
key: string;
value: string;
comment?: Block;
};
type ParserOptions = {
allowGlobalSection?: boolean;
globalSectionName?: string;
allowEmptyValue?: boolean;
debug?: boolean;
};
type Section = {
section: string;
blocks: Block[];
};
type IniObject = Section[];
type AddSectionOptions = {
override?: boolean;
};
type AddCommentOptions = {
attachToKey?: string;
};
declare class Line {
text: string;
number: number;
constructor(text: string, number: number);
isComment(): boolean;
isSection(): boolean;
isEmptyKeyValuePair(): boolean;
isKeyValuePair(): boolean;
}
/**
* Parse an ini file and return an object
* @param text The ini file content to parse as a string (required)
* @param {Object} options - The parser options (optional)
* @param {Boolean} options.allowGlobalSection - allow a section named global (default: false)
* @param {String} options.globalSectionName - the name of the global section (default: '')
* @param {Boolean} options.allowEmptyValue - allow empty values (default: false) When false, empty values will be ignored
* @returns The parsed object
*
*/
declare function parseIni(text: string, options: ParserOptions): IniObject;
/**
* Stringify a parsed object to an ini string
* @param sections {IniObject} - the parsed object to stringify to ini
* @param options {ParserOptions} - the options to use for the parser (default: {})
*/
declare function stringifyIni(sections: IniObject, options: ParserOptions): string;
declare function addSection(obj: IniObject, sectionName: string): Section;
declare function addKeyValue(obj: IniObject, section: string, key: string, value: string, options: AddSectionOptions): Block;
declare function addComment(obj: IniObject, section: string, comment: string, options: AddCommentOptions): Block;
declare function getSection(sections: Section[], sectionName: string): Section | undefined;
declare function updateKeyValue(obj: IniObject, section: string, key: string, value: string): {
type: "data";
key: string;
value: string;
comment?: Block;
};
export { AddCommentOptions, AddSectionOptions, Block, IniObject, Line, ParserOptions, Section, addComment, addKeyValue, addSection, getSection, parseIni, stringifyIni, updateKeyValue };