UNPKG

1.93 kBMarkdownView Raw
1An ini format parser and serializer for node.
2
3Sections are treated as nested objects. Items before the first heading
4are saved on the object directly.
5
6## Usage
7
8Consider an ini-file `config.ini` that looks like this:
9
10 ; this comment is being ignored
11 scope = global
12
13 [database]
14 user = dbuser
15 password = dbpassword
16 database = use_this_database
17
18 [paths.default]
19 datadir = /var/lib/data
20
21You can read, manipulate and write the ini-file like so:
22
23 var fs = require('fs')
24 , ini = require('ini')
25
26 var config = ini.parse(fs.readFileSync('./config.ini', 'utf-8'))
27
28 config.scope = 'local'
29 config.database.database = 'use_another_database'
30 config.paths.default.tmpdir = '/tmp'
31 delete config.paths.default.datadir
32
33 fs.writeFileSync('./config_modified.ini', ini.stringify(config, 'section'))
34
35This will result in a file called `config_modified.ini` being written to the filesystem with the following content:
36
37 [section]
38 scope = local
39 [section.database]
40 user = dbuser
41 password = dbpassword
42 database = use_another_database
43 [section.paths.default]
44 tmpdir = /tmp
45
46## API
47
48### decode(inistring)
49Decode the ini-style formatted `inistring` into a nested object.
50
51### parse(inistring)
52Alias for `decode(inistring)`
53
54### encode(object, [section])
55Encode the object `object` into an ini-style formatted string. If the optional parameter `section` is given, then all top-level properties of the object are put into this section and the `section`-string is prepended to all sub-sections, see the usage example above.
56
57### stringify(object, [section])
58Alias for `encode(object, [section])`
59
60### safe(val)
61Escapes the string `val` such that it is safe to be used as a key or value in an ini-file. Basically escapes quotes. For example
62
63 ini.safe('"unsafe string"')
64
65would result in
66
67 "\"unsafe string\""
68
69### unsafe(val)
70Unescapes the string `val`
71