Skip to content

Commit 6a3cb38

Browse files
wraithgarFrancois-Xavier Kowalski
and
Francois-Xavier Kowalski
authored
feat: add platform option to force line endings (#199)
Co-authored-by: Francois-Xavier Kowalski <[email protected]>
1 parent 5b5c9b7 commit 6a3cb38

File tree

4 files changed

+35
-19
lines changed

4 files changed

+35
-19
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ The `options` object may contain the following:
8686
after a section header. Some INI file parsers (for example the TOSHIBA
8787
FlashAir one) need this to parse the file successfully. By default,
8888
the additional newline is omitted.
89+
* `platform` String to define which platform this INI file is expected
90+
to be used with: when `platform` is `win32`, line terminations are
91+
CR+LF, for other platforms line termination is LF. By default the
92+
current platform name is used.
8993

9094
For backwards compatibility reasons, if a `string` options is passed
9195
in, then it is assumed to be the `section` value.

lib/ini.js

+11-19
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
const { hasOwnProperty } = Object.prototype
22

3-
/* istanbul ignore next */
4-
const eol = typeof process !== 'undefined' &&
5-
process.platform === 'win32' ? '\r\n' : '\n'
6-
7-
const encode = (obj, opt) => {
8-
const children = []
9-
let out = ''
10-
3+
const encode = (obj, opt = {}) => {
114
if (typeof opt === 'string') {
12-
opt = {
13-
section: opt,
14-
whitespace: false,
15-
}
16-
} else {
17-
opt = opt || Object.create(null)
18-
opt.whitespace = opt.whitespace === true
19-
opt.newline = opt.newline === true
5+
opt = { section: opt }
206
}
7+
opt.whitespace = opt.whitespace === true
8+
opt.newline = opt.newline === true
9+
/* istanbul ignore next */
10+
opt.platform = opt.platform || process?.platform
2111

12+
/* istanbul ignore next */
13+
const eol = opt.platform === 'win32' ? '\r\n' : '\n'
2214
const separator = opt.whitespace ? ' = ' : '='
15+
const children = []
16+
let out = ''
2317

2418
for (const k of Object.keys(obj)) {
2519
const val = obj[k]
@@ -41,11 +35,9 @@ const encode = (obj, opt) => {
4135
for (const k of children) {
4236
const nk = dotSplit(k).join('\\.')
4337
const section = (opt.section ? opt.section + '.' : '') + nk
44-
const { whitespace, newline } = opt
4538
const child = encode(obj[k], {
39+
...opt,
4640
section,
47-
whitespace,
48-
newline,
4941
})
5042
if (out.length && child.length) {
5143
out += eol

tap-snapshots/test/foo.js.test.cjs

+12
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ value=10
133133
134134
`
135135

136+
exports[`test/foo.js TAP encode with platform=win32 > must match snapshot 1`] = `
137+
Array [
138+
"[log]",
139+
"type=file",
140+
"",
141+
"[log.level]",
142+
"label=debug",
143+
"value=10",
144+
"",
145+
]
146+
`
147+
136148
exports[`test/foo.js TAP encode with whitespace > must match snapshot 1`] = `
137149
[log]
138150
type = file

test/foo.js

+8
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,11 @@ test('encode with newline', function (t) {
5252
t.matchSnapshot(e)
5353
t.end()
5454
})
55+
56+
test('encode with platform=win32', function (t) {
57+
const obj = { log: { type: 'file', level: { label: 'debug', value: 10 } } }
58+
const e = i.encode(obj, { platform: 'win32' })
59+
60+
t.matchSnapshot(e.split('\r\n'))
61+
t.end()
62+
})

0 commit comments

Comments
 (0)