-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathREADME.md
180 lines (128 loc) · 3.11 KB
/
README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
An INI format parser & serializer.
## Note
- Sections are treated as nested objects.
- Section-less items are treated as globals.
## Usage
Consider an INI file such as the following:
```ini
; This comment is being ignored
scope = global
[database]
user = dbuser
password = dbpassword
database = use_this_database
[paths.default]
datadir = /var/lib/data
array[] = first value
array[] = second value
array[] = third value
```
You can **read**, **modify** and **write** it like so:
```js
import { writeFile , readFile } from 'node:fs/promises'
import { stringify , parse } from 'ini'
// Read INI file as text
let text = await readFile(`./Original.ini`,{
encoding : 'utf-8'
})
// Parse text data to object
const config = parse(text)
// Modify data object
config.scope = 'local'
config.database.database = 'use_another_database'
config.paths.default.tmpdir = '/tmp'
delete config.paths.default.datadir
config.paths.default.array.push('fourth value')
// Stringify data object
text = stringify(config,{
section : 'section'
})
// Write INI file as text
await writeFile(`./Modified.ini`,text)
```
The written file will contain the following:
```ini
[section]
scope=local
[section.database]
user=dbuser
password=dbpassword
database=use_another_database
[section.paths.default]
tmpdir=/tmp
array[]=first value
array[]=second value
array[]=third value
array[]=fourth value
```
## API
### Parse
Attempts to turn the given INI string into a nested data object.
```js
// You can also use `decode`
const object = parse(`<INI Text>`)
```
### Stringify
Encodes the given data object as an INI formatted string.
```js
// You can also use `encode`
stringify(object,{
/**
* Whether to insert spaces before & after `=`
*
* Disabled by default to have better
* compatibility with old picky parsers.
*/
whitespace : false ,
/**
* Whether to align the `=` character for each section.
* -> Also enables the `whitespace` option
*/
align : false ,
/**
* Identifier to use for global items
* and to prepend to all other sections.
*/
section ,
/**
* Whether to sort all sections & their keys alphabetically.
*/
sort : false ,
/**
* Whether to insert a newline after each section header.
*
* The TOSHIBA & FlashAir parser require this format.
*/
newline : false ,
/**
* Which platforms line-endings should be used.
*
* win32 -> CR+LF
* other -> LF
*
* Default is the current platform
*/
platform ,
/**
* Whether to append `[]` to array keys.
*
* Some parsers treat duplicate names by themselves as arrays
*/
backetedArray : true
})
```
*For backwards compatibility any string passed as the*
*options parameter is treated as the `section` option.*
```js
stringify(object,'section')
```
### Un / Escape
Turn the given string into a safe to
use key or value in your INI file.
```js
safe(`"unsafe string"`) // -> \"unsafe string\"
```
Or reverse the process with:
```js
unsafe(`\\"safe string\\"`) // -> "safe string"
```