1 exports.parse = exports.decode = decode
3 exports.stringify = exports.encode = encode
6 exports.unsafe = unsafe
8 var eol = typeof process !== 'undefined' &&
9 process.platform === 'win32' ? '\r\n' : '\n'
11 function encode (obj, opt) {
15 if (typeof opt === 'string') {
22 opt.whitespace = opt.whitespace === true
25 var separator = opt.whitespace ? ' = ' : '='
27 Object.keys(obj).forEach(function (k, _, __) {
29 if (val && Array.isArray(val)) {
30 val.forEach(function (item) {
31 out += safe(k + '[]') + separator + safe(item) + '\n'
33 } else if (val && typeof val === 'object')
36 out += safe(k) + separator + safe(val) + eol
39 if (opt.section && out.length)
40 out = '[' + safe(opt.section) + ']' + eol + out
42 children.forEach(function (k, _, __) {
43 var nk = dotSplit(k).join('\\.')
44 var section = (opt.section ? opt.section + '.' : '') + nk
45 var child = encode(obj[k], {
47 whitespace: opt.whitespace,
49 if (out.length && child.length)
58 function dotSplit (str) {
59 return str.replace(/\1/g, '\u0002LITERAL\\1LITERAL\u0002')
60 .replace(/\\\./g, '\u0001')
61 .split(/\./).map(function (part) {
62 return part.replace(/\1/g, '\\.')
63 .replace(/\2LITERAL\\1LITERAL\2/g, '\u0001')
67 function decode (str) {
71 // section |key = value
72 var re = /^\[([^\]]*)\]$|^([^=]+)(=(.*))?$/i
73 var lines = str.split(/[\r\n]+/g)
75 lines.forEach(function (line, _, __) {
76 if (!line || line.match(/^\s*[;#]/))
78 var match = line.match(re)
81 if (match[1] !== undefined) {
82 section = unsafe(match[1])
83 if (section === '__proto__') {
85 // keep parsing the section, but don't attach it.
89 p = out[section] = out[section] || {}
92 var key = unsafe(match[2])
93 if (key === '__proto__')
95 var value = match[3] ? unsafe(match[4]) : true
99 case 'null': value = JSON.parse(value)
102 // Convert keys with '[]' suffix to an array
103 if (key.length > 2 && key.slice(-2) === '[]') {
104 key = key.substring(0, key.length - 2)
105 if (key === '__proto__')
109 else if (!Array.isArray(p[key]))
113 // safeguard against resetting a previously defined
114 // array by accidentally forgetting the brackets
115 if (Array.isArray(p[key]))
121 // {a:{y:1},"a.b":{x:2}} --> {a:{y:1,b:{x:2}}}
122 // use a filter to return the keys that have to be deleted.
123 Object.keys(out).filter(function (k, _, __) {
125 typeof out[k] !== 'object' ||
126 Array.isArray(out[k]))
129 // see if the parent section is also an object.
130 // if so, add it to that, and mark this one for deletion
131 var parts = dotSplit(k)
134 var nl = l.replace(/\\\./g, '.')
135 parts.forEach(function (part, _, __) {
136 if (part === '__proto__')
138 if (!p[part] || typeof p[part] !== 'object')
142 if (p === out && nl === l)
147 }).forEach(function (del, _, __) {
154 function isQuoted (val) {
155 return (val.charAt(0) === '"' && val.slice(-1) === '"') ||
156 (val.charAt(0) === "'" && val.slice(-1) === "'")
159 function safe (val) {
160 return (typeof val !== 'string' ||
161 val.match(/[=\r\n]/) ||
166 ? JSON.stringify(val)
167 : val.replace(/;/g, '\\;').replace(/#/g, '\\#')
170 function unsafe (val, doUnesc) {
171 val = (val || '').trim()
173 // remove the single quotes before calling JSON.parse
174 if (val.charAt(0) === "'")
175 val = val.substr(1, val.length - 2)
178 val = JSON.parse(val)
181 // walk the val to find the first not-escaped ; character
184 for (var i = 0, l = val.length; i < l; i++) {
185 var c = val.charAt(i)
187 if ('\\;#'.indexOf(c) !== -1)
193 } else if (';#'.indexOf(c) !== -1)