2 exports.parse = exports.decode = decode
3 exports.stringify = exports.encode = encode
6 exports.unsafe = unsafe
8 var eol = process.platform === "win32" ? "\r\n" : "\n"
10 function encode (obj, opt) {
14 if (typeof opt === "string") {
21 opt.whitespace = opt.whitespace === true
24 var separator = opt.whitespace ? " = " : "="
26 Object.keys(obj).forEach(function (k, _, __) {
28 if (val && Array.isArray(val)) {
29 val.forEach(function(item) {
30 out += safe(k + "[]") + separator + safe(item) + "\n"
33 else if (val && typeof val === "object") {
36 out += safe(k) + separator + safe(val) + eol
40 if (opt.section && out.length) {
41 out = "[" + safe(opt.section) + "]" + eol + out
44 children.forEach(function (k, _, __) {
45 var nk = dotSplit(k).join('\\.')
46 var section = (opt.section ? opt.section + "." : "") + nk
47 var child = encode(obj[k], {
49 whitespace: opt.whitespace
51 if (out.length && child.length) {
60 function dotSplit (str) {
61 return str.replace(/\1/g, '\u0002LITERAL\\1LITERAL\u0002')
62 .replace(/\\\./g, '\u0001')
63 .split(/\./).map(function (part) {
64 return part.replace(/\1/g, '\\.')
65 .replace(/\2LITERAL\\1LITERAL\2/g, '\u0001')
69 function decode (str) {
74 // section |key = value
75 , re = /^\[([^\]]*)\]$|^([^=]+)(=(.*))?$/i
76 , lines = str.split(/[\r\n]+/g)
79 lines.forEach(function (line, _, __) {
80 if (!line || line.match(/^\s*[;#]/)) return
81 var match = line.match(re)
83 if (match[1] !== undefined) {
84 section = unsafe(match[1])
85 p = out[section] = out[section] || {}
88 var key = unsafe(match[2])
89 , value = match[3] ? unsafe((match[4] || "")) : true
93 case 'null': value = JSON.parse(value)
96 // Convert keys with '[]' suffix to an array
97 if (key.length > 2 && key.slice(-2) === "[]") {
98 key = key.substring(0, key.length - 2)
102 else if (!Array.isArray(p[key])) {
107 // safeguard against resetting a previously defined
108 // array by accidentally forgetting the brackets
109 if (Array.isArray(p[key])) {
117 // {a:{y:1},"a.b":{x:2}} --> {a:{y:1,b:{x:2}}}
118 // use a filter to return the keys that have to be deleted.
119 Object.keys(out).filter(function (k, _, __) {
120 if (!out[k] || typeof out[k] !== "object" || Array.isArray(out[k])) return false
121 // see if the parent section is also an object.
122 // if so, add it to that, and mark this one for deletion
123 var parts = dotSplit(k)
126 , nl = l.replace(/\\\./g, '.')
127 parts.forEach(function (part, _, __) {
128 if (!p[part] || typeof p[part] !== "object") p[part] = {}
131 if (p === out && nl === l) return false
134 }).forEach(function (del, _, __) {
141 function isQuoted (val) {
142 return (val.charAt(0) === "\"" && val.slice(-1) === "\"")
143 || (val.charAt(0) === "'" && val.slice(-1) === "'")
146 function safe (val) {
147 return ( typeof val !== "string"
148 || val.match(/[=\r\n]/)
152 || val !== val.trim() )
153 ? JSON.stringify(val)
154 : val.replace(/;/g, '\\;').replace(/#/g, "\\#")
157 function unsafe (val, doUnesc) {
158 val = (val || "").trim()
160 // remove the single quotes before calling JSON.parse
161 if (val.charAt(0) === "'") {
162 val = val.substr(1, val.length - 2);
164 try { val = JSON.parse(val) } catch (_) {}
166 // walk the val to find the first not-escaped ; character
169 for (var i = 0, l = val.length; i < l; i++) {
170 var c = val.charAt(i)
172 if ("\\;#".indexOf(c) !== -1)
177 } else if (";#".indexOf(c) !== -1) {
179 } else if (c === "\\") {