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

New option opt.platform to force line-endings #64

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ The `options` object may contain the following:
`=` character. By default, whitespace is omitted, to be friendly to
some persnickety old parsers that don't tolerate it well. But some
find that it's more human-readable and pretty with the whitespace.
* `newline` Boolean to specify whether to put an additional newline
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
15 changes: 10 additions & 5 deletions ini.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@ exports.stringify = exports.encode = encode
exports.safe = safe
exports.unsafe = unsafe

var eol = process.platform === 'win32' ? '\r\n' : '\n'

function encode (obj, opt) {
var children = []
var out = ''

if (typeof opt === 'string') {
opt = {
section: opt,
whitespace: false
whitespace: false,
newline: false,
platform: process.platform
}
} else {
opt = opt || {}
opt.whitespace = opt.whitespace === true
opt.newline = opt.newline === true
opt.platform = opt.platform || process.platform
}

var eol = opt.platform === 'win32' ? '\r\n' : '\n'
var separator = opt.whitespace ? ' = ' : '='

Object.keys(obj).forEach(function (k, _, __) {
Expand All @@ -36,15 +39,17 @@ function encode (obj, opt) {
})

if (opt.section && out.length) {
out = '[' + safe(opt.section) + ']' + eol + out
out = '[' + safe(opt.section) + ']' + (opt.newline ? eol + eol : eol) + out
}

children.forEach(function (k, _, __) {
var nk = dotSplit(k).join('\\.')
var section = (opt.section ? opt.section + '.' : '') + nk
var child = encode(obj[k], {
section: section,
whitespace: opt.whitespace
whitespace: opt.whitespace,
newline: opt.newline,
platform: opt.platform
})
if (out.length && child.length) {
out += eol
Expand Down
26 changes: 26 additions & 0 deletions test/foo.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ var i = require("../")
+ '[log.level]\n'
+ 'label = debug\n'
+ 'value = 10\n'
, expectH = '[log]\n\n'
+ 'type=file\n\n'
+ '[log.level]\n\n'
+ 'label=debug\n'
+ 'value=10\n'
, expectI = '[log]\r\n'
+ 'type=file\r\n\r\n'
+ '[log.level]\r\n'
+ 'label=debug\r\n'
+ 'value=10\r\n'

test("decode from file", function (t) {
var d = i.decode(data)
Expand Down Expand Up @@ -105,3 +115,19 @@ test("encode with whitespace", function (t) {
t.equal(e, expectG)
t.end()
})

test("encode with newline", function (t) {
var obj = {log: { type:'file', level: {label:'debug', value:10} } }
e = i.encode(obj, {newline: true})

t.equal(e, expectH)
t.end()
})

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

t.equal(e, expectI)
t.end()
})