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
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) {
73 // section |key = value
74 var re = /^\[([^\]]*)\]$|^([^=]+)(=(.*))?$/i
75 var lines = str.split(/[\r\n]+/g)
77 lines.forEach(function (line, _, __) {
78 if (!line || line.match(/^\s*[;#]/)) return
79 var match = line.match(re)
81 if (match[1] !== undefined) {
82 section = unsafe(match[1])
83 p = out[section] = out[section] || {}
86 var key = unsafe(match[2])
87 var value = match[3] ? unsafe(match[4]) : true
91 case 'null': value = JSON.parse(value)
94 // Convert keys with '[]' suffix to an array
95 if (key.length > 2 && key.slice(-2) === '[]') {
96 key = key.substring(0, key.length - 2)
99 } else if (!Array.isArray(p[key])) {
104 // safeguard against resetting a previously defined
105 // array by accidentally forgetting the brackets
106 if (Array.isArray(p[key])) {
113 // {a:{y:1},"a.b":{x:2}} --> {a:{y:1,b:{x:2}}}
114 // use a filter to return the keys that have to be deleted.
115 Object.keys(out).filter(function (k, _, __) {
117 typeof out[k] !== 'object' ||
118 Array.isArray(out[k])) {
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 var 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) {
136 }).forEach(function (del, _, __) {
143 function isQuoted (val) {
144 return (val.charAt(0) === '"' && val.slice(-1) === '"') ||
145 (val.charAt(0) === "'" && val.slice(-1) === "'")
148 function safe (val) {
149 return (typeof val !== 'string' ||
150 val.match(/[=\r\n]/) ||
155 ? JSON.stringify(val)
156 : val.replace(/;/g, '\\;').replace(/#/g, '\\#')
159 function unsafe (val, doUnesc) {
160 val = (val || '').trim()
162 // remove the single quotes before calling JSON.parse
163 if (val.charAt(0) === "'") {
164 val = val.substr(1, val.length - 2)
166 try { val = JSON.parse(val) } catch (_) {}
168 // walk the val to find the first not-escaped ; character
171 for (var i = 0, l = val.length; i < l; i++) {
172 var c = val.charAt(i)
174 if ('\\;#'.indexOf(c) !== -1) {
180 } else if (';#'.indexOf(c) !== -1) {
182 } else if (c === '\\') {