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

Added option for array handling for ini files #77

Closed
wants to merge 7 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/coverage
/node_modules
/npm-debug.log
/.vscode
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ 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.
* `ìsArray` Boolean to specify how arrays should be handled. By default an array
value will be suffixed with `[]`. If isArray is set to false multiple occurances
of the same key will be written underneath each other in the file
e.g. the way the _DNS_ setting is handled in [systemd networkd files](https://wiki.archlinux.org/index.php/Systemd-networkd).

For backwards compatibility reasons, if a `string` options is passed
in, then it is assumed to be the `section` value.
Expand Down
34 changes: 22 additions & 12 deletions ini.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ exports.stringify = exports.encode = encode
exports.safe = safe
exports.unsafe = unsafe

var eol = typeof process !== 'undefined' &&
process.platform === 'win32' ? '\r\n' : '\n'
var eol = require('os').EOL

function encode (obj, opt) {
var children = []
Expand All @@ -15,20 +14,25 @@ function encode (obj, opt) {
if (typeof opt === 'string') {
opt = {
section: opt,
whitespace: false
whitespace: false,
isArray: true
}
} else {
opt = opt || {}
opt.whitespace = opt.whitespace === true
opt.isArray = opt.isArray === true
}

var separator = opt.whitespace ? ' = ' : '='

Object.keys(obj).forEach(function (k, _, __) {
var val = obj[k]
if (val && Array.isArray(val)) {
val.forEach(function (item) {
out += safe(k + '[]') + separator + safe(item) + '\n'
if (opt.isArray) {
out += safe(k + '[]') + separator + safe(item) + eol
} else {
out += safe(k) + separator + safe(item) + eol
}
})
} else if (val && typeof val === 'object') {
children.push(k)
Expand All @@ -46,7 +50,8 @@ function encode (obj, opt) {
var section = (opt.section ? opt.section + '.' : '') + nk
var child = encode(obj[k], {
section: section,
whitespace: opt.whitespace
whitespace: opt.whitespace,
isArray: opt.isArray
})
if (out.length && child.length) {
out += eol
Expand All @@ -62,7 +67,7 @@ function dotSplit (str) {
.replace(/\\\./g, '\u0001')
.split(/\./).map(function (part) {
return part.replace(/\1/g, '\\.')
.replace(/\2LITERAL\\1LITERAL\2/g, '\u0001')
.replace(/\2LITERAL\\1LITERAL\2/g, '\u0001')
})
}

Expand All @@ -73,7 +78,6 @@ function decode (str) {
// section |key = value
var re = /^\[([^\]]*)\]$|^([^=]+)(=(.*))?$/i
var lines = str.split(/[\r\n]+/g)

lines.forEach(function (line, _, __) {
if (!line || line.match(/^\s*[;#]/)) return
var match = line.match(re)
Expand All @@ -92,13 +96,19 @@ function decode (str) {
}

// Convert keys with '[]' suffix to an array
if (key.length > 2 && key.slice(-2) === '[]') {
if ((key.length > 2 && key.slice(-2) === '[]')) {
key = key.substring(0, key.length - 2)
if (!p[key]) {
p[key] = []
} else if (!Array.isArray(p[key])) {
p[key] = [p[key]]
}
} else if (p[key]) {
if (!Array.isArray(p[key])) {
p[key] = [p[key]]
} else {
p[key] = p[key]
}
}

// safeguard against resetting a previously defined
Expand Down Expand Up @@ -150,10 +160,10 @@ function safe (val) {
val.match(/[=\r\n]/) ||
val.match(/^\[/) ||
(val.length > 1 &&
isQuoted(val)) ||
isQuoted(val)) ||
val !== val.trim())
? JSON.stringify(val)
: val.replace(/;/g, '\\;').replace(/#/g, '\\#')
? JSON.stringify(val)
: val.replace(/;/g, '\\;').replace(/#/g, '\\#')
}

function unsafe (val, doUnesc) {
Expand Down
Loading