Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add platform option to force line endings #199

Merged
merged 1 commit into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ The `options` object may contain the following:
after a section header. Some INI file parsers (for example the TOSHIBA
FlashAir one) need this to parse the file successfully. By default,
the additional newline is omitted.
* `platform` String to define which platform this INI file is expected
to be used with: when `platform` is `win32`, line terminations are
CR+LF, for other platforms line termination is LF. By default the
current platform name is used.

For backwards compatibility reasons, if a `string` options is passed
in, then it is assumed to be the `section` value.
Expand Down
30 changes: 11 additions & 19 deletions lib/ini.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
const { hasOwnProperty } = Object.prototype

/* istanbul ignore next */
const eol = typeof process !== 'undefined' &&
process.platform === 'win32' ? '\r\n' : '\n'

const encode = (obj, opt) => {
const children = []
let out = ''

const encode = (obj, opt = {}) => {
if (typeof opt === 'string') {
opt = {
section: opt,
whitespace: false,
}
} else {
opt = opt || Object.create(null)
opt.whitespace = opt.whitespace === true
opt.newline = opt.newline === true
opt = { section: opt }
}
opt.whitespace = opt.whitespace === true
opt.newline = opt.newline === true
/* istanbul ignore next */
opt.platform = opt.platform || process?.platform
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to access process in a browser environment fails.
The availability of the value needs to be checked with typeof process === 'undefined'


/* istanbul ignore next */
const eol = opt.platform === 'win32' ? '\r\n' : '\n'
const separator = opt.whitespace ? ' = ' : '='
const children = []
let out = ''

for (const k of Object.keys(obj)) {
const val = obj[k]
Expand All @@ -41,11 +35,9 @@ const encode = (obj, opt) => {
for (const k of children) {
const nk = dotSplit(k).join('\\.')
const section = (opt.section ? opt.section + '.' : '') + nk
const { whitespace, newline } = opt
const child = encode(obj[k], {
...opt,
section,
whitespace,
newline,
})
if (out.length && child.length) {
out += eol
Expand Down
12 changes: 12 additions & 0 deletions tap-snapshots/test/foo.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ value=10

`

exports[`test/foo.js TAP encode with platform=win32 > must match snapshot 1`] = `
Array [
"[log]",
"type=file",
"",
"[log.level]",
"label=debug",
"value=10",
"",
]
`

exports[`test/foo.js TAP encode with whitespace > must match snapshot 1`] = `
[log]
type = file
Expand Down
8 changes: 8 additions & 0 deletions test/foo.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,11 @@ test('encode with newline', function (t) {
t.matchSnapshot(e)
t.end()
})

test('encode with platform=win32', function (t) {
const obj = { log: { type: 'file', level: { label: 'debug', value: 10 } } }
const e = i.encode(obj, { platform: 'win32' })

t.matchSnapshot(e.split('\r\n'))
t.end()
})