diff options
author | isaacs <i@izs.me> | 2012-08-15 06:46:53 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2012-08-15 06:47:43 +0400 |
commit | fdd5b7ec7bea41e548cabedafb7a55ec60aeb5b1 (patch) | |
tree | 7b4a6b3e77211ca21ea50c1ab36a4ffc978a0682 /node_modules/ini/ini.js | |
parent | be6389a0c40b132a09d211944480d5ec4bdc2f2f (diff) |
ini@1.0.4
Diffstat (limited to 'node_modules/ini/ini.js')
-rw-r--r-- | node_modules/ini/ini.js | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/node_modules/ini/ini.js b/node_modules/ini/ini.js index e8a949f94..7939f20e5 100644 --- a/node_modules/ini/ini.js +++ b/node_modules/ini/ini.js @@ -5,6 +5,8 @@ exports.stringify = exports.encode = encode exports.safe = safe exports.unsafe = unsafe +var eol = process.platform === "win32" ? "\r\n" : "\n" + function encode (obj, section) { var children = [] , out = "" @@ -14,18 +16,19 @@ function encode (obj, section) { if (val && typeof val === "object") { children.push(k) } else { - out += safe(k) + " = " + safe(val) + "\n" + out += safe(k) + " = " + safe(val) + eol } }) if (section && out.length) { - out = "[" + safe(section) + "]" + "\n" + out + out = "[" + safe(section) + "]" + eol + out } children.forEach(function (k, _, __) { - var child = encode(obj[k], (section ? section + "." : "") + k) + var nk = dotSplit(k).join('\\.') + var child = encode(obj[k], (section ? section + "." : "") + nk) if (out.length && child.length) { - out += "\n" + out += eol } out += child }) @@ -33,6 +36,15 @@ function encode (obj, section) { return out } +function dotSplit (str) { + return str.replace(/\1/g, '\2LITERAL\\1LITERAL\2') + .replace(/\\\./g, '\1') + .split(/\./).map(function (part) { + return part.replace(/\1/g, '\\.') + .replace(/\2LITERAL\\1LITERAL\2/g, '\1') + }) +} + function decode (str) { var out = {} , p = out @@ -57,6 +69,11 @@ function decode (str) { } var key = unsafe(match[2]) , value = match[3] ? unsafe((match[4] || "")) : true + switch (value) { + case 'true': + case 'false': + case 'null': value = JSON.parse(value) + } p[key] = value }) @@ -66,15 +83,16 @@ function decode (str) { if (!out[k] || typeof out[k] !== "object") return false // see if the parent section is also an object. // if so, add it to that, and mark this one for deletion - var parts = k.split(".") + var parts = dotSplit(k) , p = out , l = parts.pop() + , nl = l.replace(/\\\./g, '.') parts.forEach(function (part, _, __) { if (!p[part] || typeof p[part] !== "object") p[part] = {} p = p[part] }) - if (p === out) return false - p[l] = out[k] + if (p === out && nl === l) return false + p[nl] = out[k] return true }).forEach(function (del, _, __) { delete out[del] |