Parse .ini
files into JSON objects. Support lists.
$ npm install shmaml
const parse = require('shmaml');
const resultPromise = parse('../path/to/config.ini');
// or:
const result = parse.sync('../path/to/config.ini');
ย
Example config.ini
+ list:
username = me
pasword = 1234
[SectionA]
key=value
[SectionB]
key=value
list=[
item1,
item2,
item3
]
Becomes:
result = {
username: 'me',
pasword: 1234,
SectionA: {
key: 'value',
},
SectionB: {
key: 'value',
list: ['item1', 'item2', 'item3']
},
}
-
key-value pairs are noted with an equal sign.
key="value"
-
Quoting values is optional. Strings are used as default.
; These are all the same: key=value key='value' key="value"
-
Unquoted numbers are parsed as numbers. Use single/double quotes to make number values parsed as strings.
number = 55 string = '55' string = "55"
-
Unquoted booleans are parsed as booleans. Use single/double quotes to make boolean values parsed as strings. CaSe InSeNsITiVe.
bool = TRUE bool = false string = 'true' string = "false"
-
Sections are noted with wrapping square brackets.
[categoryA] key="value" [categoryB] key="value"
-
Lists are also noted with wrapping square brackets when come after a
key=
.[categoryA] key="value" list=[item1, item2, item3]
-
Multiline lists are also supported.
[categoryA] multilineList=[ item1, item2, item3 ] key="value"
NOTE: Nested lists are NOT supported.
list=[A, [B, [C, D]], E]
-
Comments are ignored, obviously. Use quotes to work with semicolons:
; line comment [category] key1=value1 ; inline comment key2='quoted ; semicolon' key3="double quoted ; semicolon"
-
Whitespace (including tabs) is trimmed:
;Same [sectionA] [ sectionA ]
;Same key= value key =value key = value
[sectionA] key=value ;Same as [sectionA] key=value
-
Preserve whitespace using quotes:
[' section '] key=" value "
{ " section ": { key: " value " } }
-
Duplicates.
Duplicated section names are merged to the same object.[sectionA] key1=value1 [sectionA] key2=value2
result = { SectionA: { key1: 'value1', key2: 'value2', }, }
Duplicated key names in the same section: the later overwrites the former.
sameKey=value1 sameKey=value2 sameKey=value3
result = { sameKey: 'value3' }