Skip to content

Commit 24eb9a0

Browse files
PhoneDroidwraithgar
authored andcommitted
docs: Reworked README (#235)
Reworked README content / documentation to use current code standards ( read / modify / write example ), work better in npms page bounds ( 730 pixel width limit ) and be generally more easily comprehensible.
1 parent 2f57997 commit 24eb9a0

File tree

1 file changed

+141
-86
lines changed

1 file changed

+141
-86
lines changed

README.md

+141-86
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,180 @@
1-
An ini format parser and serializer for node.
21

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.
59

610
## Usage
711

8-
Consider an ini-file `config.ini` that looks like this:
12+
Consider an INI file such as the following:
13+
914
```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
2328
```
2429

25-
You can read, manipulate and write the ini-file like so:
30+
You can **read**, **modify** and **write** it like so:
2631

2732
```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)
3045

31-
var config = ini.parse(fs.readFileSync('./config.ini', 'utf-8'))
46+
// Modify data object
3247

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')
3853

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)
4063
```
4164

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:
4466

4567
```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
5880
```
5981

6082
## API
6183

62-
### decode(inistring)
84+
### Parse
6385

64-
Decode the ini-style formatted `inistring` into a nested object.
86+
Attempts to turn the given INI string into a nested data object.
6587

66-
### parse(inistring)
88+
```js
89+
// You can also use `decode`
90+
const object = parse(`<INI Text>`)
91+
```
92+
93+
### Stringify
6794

68-
Alias for `decode(inistring)`
95+
Encodes the given data object as an INI formatted string.
6996

70-
### encode(object, [options])
97+
```js
98+
// You can also use `encode`
99+
stringify(object,{
71100

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+
*/
76107

77-
The `options` object may contain the following:
108+
whitespace : false ,
78109

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+
*/
102114

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 ,
105116

106-
### stringify(object, [options])
117+
/**
118+
* Identifier to use for global items
119+
* and to prepend to all other sections.
120+
*/
107121

108-
Alias for `encode(object, [options])`
122+
section ,
109123

110-
### safe(val)
124+
/**
125+
* Whether to sort all sections & their keys alphabetically.
126+
*/
111127

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.*
114162

115163
```js
116-
ini.safe('"unsafe string"')
164+
stringify(object,'section')
117165
```
118166

119-
would result in
167+
### Un / Escape
120168

121-
"\"unsafe string\""
169+
Turn the given string into a safe to
170+
use key or value in your INI file.
122171

123-
### unsafe(val)
172+
```js
173+
safe(`"unsafe string"`) // -> \"unsafe string\"
174+
```
124175

125-
Unescapes the string `val`
176+
Or reverse the process with:
177+
178+
```js
179+
unsafe(`\\"safe string\\"`) // -> "safe string"
180+
```

0 commit comments

Comments
 (0)