|
1 | | -An ini format parser and serializer for node. |
2 | 1 |
|
3 | | -Sections are treated as nested objects. Items before the first |
4 | | -heading are saved on the object directly. |
| 2 | +An INI format parser & serializer. |
| 3 | + |
| 4 | +## Note |
| 5 | + |
| 6 | +- Sections are treated as nested objects. |
| 7 | + |
| 8 | +- Section-less items are treated as globals. |
5 | 9 |
|
6 | 10 | ## Usage |
7 | 11 |
|
8 | | -Consider an ini-file `config.ini` that looks like this: |
| 12 | +Consider an INI file such as the following: |
| 13 | + |
9 | 14 | ```ini |
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 | | - array[] = first value |
21 | | - array[] = second value |
22 | | - array[] = third value |
| 15 | +; This comment is being ignored |
| 16 | +scope = global |
| 17 | + |
| 18 | +[database] |
| 19 | +user = dbuser |
| 20 | +password = dbpassword |
| 21 | +database = use_this_database |
| 22 | + |
| 23 | +[paths.default] |
| 24 | +datadir = /var/lib/data |
| 25 | +array[] = first value |
| 26 | +array[] = second value |
| 27 | +array[] = third value |
23 | 28 | ``` |
24 | 29 |
|
25 | | -You can read, manipulate and write the ini-file like so: |
| 30 | +You can **read**, **modify** and **write** it like so: |
26 | 31 |
|
27 | 32 | ```js |
28 | | - var fs = require('fs') |
29 | | - , ini = require('ini') |
| 33 | +import { writeFile , readFile } from 'node:fs/promises' |
| 34 | +import { stringify , parse } from 'ini' |
| 35 | + |
| 36 | +// Read INI file as text |
| 37 | + |
| 38 | +let text = await readFile(`./Original.ini`,{ |
| 39 | + encoding : 'utf-8' |
| 40 | +}) |
| 41 | + |
| 42 | +// Parse text data to object |
| 43 | + |
| 44 | +const config = parse(text) |
30 | 45 |
|
31 | | - var config = ini.parse(fs.readFileSync('./config.ini', 'utf-8')) |
| 46 | +// Modify data object |
32 | 47 |
|
33 | | - config.scope = 'local' |
34 | | - config.database.database = 'use_another_database' |
35 | | - config.paths.default.tmpdir = '/tmp' |
36 | | - delete config.paths.default.datadir |
37 | | - config.paths.default.array.push('fourth value') |
| 48 | +config.scope = 'local' |
| 49 | +config.database.database = 'use_another_database' |
| 50 | +config.paths.default.tmpdir = '/tmp' |
| 51 | +delete config.paths.default.datadir |
| 52 | +config.paths.default.array.push('fourth value') |
38 | 53 |
|
39 | | - fs.writeFileSync('./config_modified.ini', ini.stringify(config, { section: 'section' })) |
| 54 | +// Stringify data object |
| 55 | + |
| 56 | +text = stringify(config,{ |
| 57 | + section : 'section' |
| 58 | +}) |
| 59 | + |
| 60 | +// Write INI file as text |
| 61 | + |
| 62 | +await writeFile(`./Modified.ini`,text) |
40 | 63 | ``` |
41 | 64 |
|
42 | | -This will result in a file called `config_modified.ini` being written |
43 | | -to the filesystem with the following content: |
| 65 | +The written file will contain the following: |
44 | 66 |
|
45 | 67 | ```ini |
46 | | - [section] |
47 | | - scope=local |
48 | | - [section.database] |
49 | | - user=dbuser |
50 | | - password=dbpassword |
51 | | - database=use_another_database |
52 | | - [section.paths.default] |
53 | | - tmpdir=/tmp |
54 | | - array[]=first value |
55 | | - array[]=second value |
56 | | - array[]=third value |
57 | | - array[]=fourth value |
| 68 | +[section] |
| 69 | +scope=local |
| 70 | +[section.database] |
| 71 | +user=dbuser |
| 72 | +password=dbpassword |
| 73 | +database=use_another_database |
| 74 | +[section.paths.default] |
| 75 | +tmpdir=/tmp |
| 76 | +array[]=first value |
| 77 | +array[]=second value |
| 78 | +array[]=third value |
| 79 | +array[]=fourth value |
58 | 80 | ``` |
59 | 81 |
|
60 | 82 | ## API |
61 | 83 |
|
62 | | -### decode(inistring) |
| 84 | +### Parse |
63 | 85 |
|
64 | | -Decode the ini-style formatted `inistring` into a nested object. |
| 86 | +Attempts to turn the given INI string into a nested data object. |
65 | 87 |
|
66 | | -### parse(inistring) |
| 88 | +```js |
| 89 | +// You can also use `decode` |
| 90 | +const object = parse(`<INI Text>`) |
| 91 | +``` |
| 92 | + |
| 93 | +### Stringify |
67 | 94 |
|
68 | | -Alias for `decode(inistring)` |
| 95 | +Encodes the given data object as an INI formatted string. |
69 | 96 |
|
70 | | -### encode(object, [options]) |
| 97 | +```js |
| 98 | +// You can also use `encode` |
| 99 | +stringify(object,{ |
71 | 100 |
|
72 | | -Encode the object `object` into an ini-style formatted string. If the |
73 | | -optional parameter `section` is given, then all top-level properties |
74 | | -of the object are put into this section and the `section`-string is |
75 | | -prepended to all sub-sections, see the usage example above. |
| 101 | + /** |
| 102 | + * Whether to insert spaces before & after `=` |
| 103 | + * |
| 104 | + * Disabled by default to have better |
| 105 | + * compatibility with old picky parsers. |
| 106 | + */ |
76 | 107 |
|
77 | | -The `options` object may contain the following: |
| 108 | + whitespace : false , |
78 | 109 |
|
79 | | -* `align` Boolean to specify whether to align the `=` characters for |
80 | | - each section. This option will automatically enable `whitespace`. |
81 | | - Defaults to `false`. |
82 | | -* `section` String which will be the first `section` in the encoded |
83 | | - ini data. Defaults to none. |
84 | | -* `sort` Boolean to specify if all keys in each section, as well as |
85 | | - all sections, will be alphabetically sorted. Defaults to `false`. |
86 | | -* `whitespace` Boolean to specify whether to put whitespace around the |
87 | | - `=` character. By default, whitespace is omitted, to be friendly to |
88 | | - some persnickety old parsers that don't tolerate it well. But some |
89 | | - find that it's more human-readable and pretty with the whitespace. |
90 | | - Defaults to `false`. |
91 | | -* `newline` Boolean to specify whether to put an additional newline |
92 | | - after a section header. Some INI file parsers (for example the TOSHIBA |
93 | | - FlashAir one) need this to parse the file successfully. By default, |
94 | | - the additional newline is omitted. |
95 | | -* `platform` String to define which platform this INI file is expected |
96 | | - to be used with: when `platform` is `win32`, line terminations are |
97 | | - CR+LF, for other platforms line termination is LF. By default, the |
98 | | - current platform name is used. |
99 | | -* `bracketedArray` Boolean to specify whether array values are appended |
100 | | - with `[]`. By default this is true but there are some ini parsers |
101 | | - that instead treat duplicate names as arrays. |
| 110 | + /** |
| 111 | + * Whether to align the `=` character for each section. |
| 112 | + * -> Also enables the `whitespace` option |
| 113 | + */ |
102 | 114 |
|
103 | | -For backwards compatibility reasons, if a `string` options is passed |
104 | | -in, then it is assumed to be the `section` value. |
| 115 | + align : false , |
105 | 116 |
|
106 | | -### stringify(object, [options]) |
| 117 | + /** |
| 118 | + * Identifier to use for global items |
| 119 | + * and to prepend to all other sections. |
| 120 | + */ |
107 | 121 |
|
108 | | -Alias for `encode(object, [options])` |
| 122 | + section , |
109 | 123 |
|
110 | | -### safe(val) |
| 124 | + /** |
| 125 | + * Whether to sort all sections & their keys alphabetically. |
| 126 | + */ |
111 | 127 |
|
112 | | -Escapes the string `val` such that it is safe to be used as a key or |
113 | | -value in an ini-file. Basically escapes quotes. For example |
| 128 | + sort : false , |
| 129 | + |
| 130 | + /** |
| 131 | + * Whether to insert a newline after each section header. |
| 132 | + * |
| 133 | + * The TOSHIBA & FlashAir parser require this format. |
| 134 | + */ |
| 135 | + |
| 136 | + newline : false , |
| 137 | + |
| 138 | + /** |
| 139 | + * Which platforms line-endings should be used. |
| 140 | + * |
| 141 | + * win32 -> CR+LF |
| 142 | + * other -> LF |
| 143 | + * |
| 144 | + * Default is the current platform |
| 145 | + */ |
| 146 | + |
| 147 | + platform , |
| 148 | + |
| 149 | + /** |
| 150 | + * Whether to append `[]` to array keys. |
| 151 | + * |
| 152 | + * Some parsers treat duplicate names by themselves as arrays |
| 153 | + */ |
| 154 | + |
| 155 | + backetedArray : true |
| 156 | + |
| 157 | +}) |
| 158 | +``` |
| 159 | + |
| 160 | +*For backwards compatibility any string passed as the* |
| 161 | +*options parameter is treated as the `section` option.* |
114 | 162 |
|
115 | 163 | ```js |
116 | | - ini.safe('"unsafe string"') |
| 164 | +stringify(object,'section') |
117 | 165 | ``` |
118 | 166 |
|
119 | | -would result in |
| 167 | +### Un / Escape |
120 | 168 |
|
121 | | - "\"unsafe string\"" |
| 169 | +Turn the given string into a safe to |
| 170 | +use key or value in your INI file. |
122 | 171 |
|
123 | | -### unsafe(val) |
| 172 | +```js |
| 173 | +safe(`"unsafe string"`) // -> \"unsafe string\" |
| 174 | +``` |
124 | 175 |
|
125 | | -Unescapes the string `val` |
| 176 | +Or reverse the process with: |
| 177 | + |
| 178 | +```js |
| 179 | +unsafe(`\\"safe string\\"`) // -> "safe string" |
| 180 | +``` |
0 commit comments