File tree 4 files changed +28
-2
lines changed
4 files changed +28
-2
lines changed Original file line number Diff line number Diff line change @@ -82,6 +82,10 @@ The `options` object may contain the following:
82
82
` = ` character. By default, whitespace is omitted, to be friendly to
83
83
some persnickety old parsers that don't tolerate it well. But some
84
84
find that it's more human-readable and pretty with the whitespace.
85
+ * ` newline ` Boolean to specify whether to put an additional newline
86
+ after a section header. Some INI file parsers (for example the TOSHIBA
87
+ FlashAir one) need this to parse the file successfully. By default,
88
+ the additional newline is omitted.
85
89
86
90
For backwards compatibility reasons, if a ` string ` options is passed
87
91
in, then it is assumed to be the ` section ` value.
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ const encode = (obj, opt) => {
16
16
} else {
17
17
opt = opt || Object . create ( null )
18
18
opt . whitespace = opt . whitespace === true
19
+ opt . newline = opt . newline === true
19
20
}
20
21
21
22
const separator = opt . whitespace ? ' = ' : '='
@@ -34,16 +35,17 @@ const encode = (obj, opt) => {
34
35
}
35
36
36
37
if ( opt . section && out . length ) {
37
- out = '[' + safe ( opt . section ) + ']' + eol + out
38
+ out = '[' + safe ( opt . section ) + ']' + ( opt . newline ? eol + eol : eol ) + out
38
39
}
39
40
40
41
for ( const k of children ) {
41
42
const nk = dotSplit ( k ) . join ( '\\.' )
42
43
const section = ( opt . section ? opt . section + '.' : '' ) + nk
43
- const { whitespace } = opt
44
+ const { whitespace, newline } = opt
44
45
const child = encode ( obj [ k ] , {
45
46
section,
46
47
whitespace,
48
+ newline,
47
49
} )
48
50
if ( out . length && child . length ) {
49
51
out += eol
Original file line number Diff line number Diff line change @@ -110,6 +110,18 @@ noHashComment=this\\# this is not a comment
110
110
111
111
`
112
112
113
+ exports [ `test/foo.js TAP encode with newline > must match snapshot 1` ] = `
114
+ [log]
115
+
116
+ type=file
117
+
118
+ [log.level]
119
+
120
+ label=debug
121
+ value=10
122
+
123
+ `
124
+
113
125
exports [ `test/foo.js TAP encode with option > must match snapshot 1` ] = `
114
126
[prefix.log]
115
127
type=file
Original file line number Diff line number Diff line change @@ -44,3 +44,11 @@ test('encode with whitespace', function (t) {
44
44
t . matchSnapshot ( e )
45
45
t . end ( )
46
46
} )
47
+
48
+ test ( 'encode with newline' , function ( t ) {
49
+ const obj = { log : { type : 'file' , level : { label : 'debug' , value : 10 } } }
50
+ const e = i . encode ( obj , { newline : true } )
51
+
52
+ t . matchSnapshot ( e )
53
+ t . end ( )
54
+ } )
You can’t perform that action at this time.
0 commit comments