ini.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. exports.parse = exports.decode = decode
  2. exports.stringify = exports.encode = encode
  3. exports.safe = safe
  4. exports.unsafe = unsafe
  5. var eol = process.platform === "win32" ? "\r\n" : "\n"
  6. function encode (obj, opt) {
  7. var children = []
  8. , out = ""
  9. if (typeof opt === "string") {
  10. opt = {
  11. section: opt,
  12. whitespace: false
  13. }
  14. } else {
  15. opt = opt || {}
  16. opt.whitespace = opt.whitespace === true
  17. }
  18. var separator = opt.whitespace ? " = " : "="
  19. Object.keys(obj).forEach(function (k, _, __) {
  20. var val = obj[k]
  21. if (val && Array.isArray(val)) {
  22. val.forEach(function(item) {
  23. out += safe(k + "[]") + separator + safe(item) + "\n"
  24. })
  25. }
  26. else if (val && typeof val === "object") {
  27. children.push(k)
  28. } else {
  29. out += safe(k) + separator + safe(val) + eol
  30. }
  31. })
  32. if (opt.section && out.length) {
  33. out = "[" + safe(opt.section) + "]" + eol + out
  34. }
  35. children.forEach(function (k, _, __) {
  36. var nk = dotSplit(k).join('\\.')
  37. var section = (opt.section ? opt.section + "." : "") + nk
  38. var child = encode(obj[k], {
  39. section: section,
  40. whitespace: opt.whitespace
  41. })
  42. if (out.length && child.length) {
  43. out += eol
  44. }
  45. out += child
  46. })
  47. return out
  48. }
  49. function dotSplit (str) {
  50. return str.replace(/\1/g, '\u0002LITERAL\\1LITERAL\u0002')
  51. .replace(/\\\./g, '\u0001')
  52. .split(/\./).map(function (part) {
  53. return part.replace(/\1/g, '\\.')
  54. .replace(/\2LITERAL\\1LITERAL\2/g, '\u0001')
  55. })
  56. }
  57. function decode (str) {
  58. var out = {}
  59. , p = out
  60. , section = null
  61. , state = "START"
  62. // section |key = value
  63. , re = /^\[([^\]]*)\]$|^([^=]+)(=(.*))?$/i
  64. , lines = str.split(/[\r\n]+/g)
  65. , section = null
  66. lines.forEach(function (line, _, __) {
  67. if (!line || line.match(/^\s*[;#]/)) return
  68. var match = line.match(re)
  69. if (!match) return
  70. if (match[1] !== undefined) {
  71. section = unsafe(match[1])
  72. p = out[section] = out[section] || {}
  73. return
  74. }
  75. var key = unsafe(match[2])
  76. , value = match[3] ? unsafe((match[4] || "")) : true
  77. switch (value) {
  78. case 'true':
  79. case 'false':
  80. case 'null': value = JSON.parse(value)
  81. }
  82. // Convert keys with '[]' suffix to an array
  83. if (key.length > 2 && key.slice(-2) === "[]") {
  84. key = key.substring(0, key.length - 2)
  85. if (!p[key]) {
  86. p[key] = []
  87. }
  88. else if (!Array.isArray(p[key])) {
  89. p[key] = [p[key]]
  90. }
  91. }
  92. // safeguard against resetting a previously defined
  93. // array by accidentally forgetting the brackets
  94. if (Array.isArray(p[key])) {
  95. p[key].push(value)
  96. }
  97. else {
  98. p[key] = value
  99. }
  100. })
  101. // {a:{y:1},"a.b":{x:2}} --> {a:{y:1,b:{x:2}}}
  102. // use a filter to return the keys that have to be deleted.
  103. Object.keys(out).filter(function (k, _, __) {
  104. if (!out[k] || typeof out[k] !== "object" || Array.isArray(out[k])) return false
  105. // see if the parent section is also an object.
  106. // if so, add it to that, and mark this one for deletion
  107. var parts = dotSplit(k)
  108. , p = out
  109. , l = parts.pop()
  110. , nl = l.replace(/\\\./g, '.')
  111. parts.forEach(function (part, _, __) {
  112. if (!p[part] || typeof p[part] !== "object") p[part] = {}
  113. p = p[part]
  114. })
  115. if (p === out && nl === l) return false
  116. p[nl] = out[k]
  117. return true
  118. }).forEach(function (del, _, __) {
  119. delete out[del]
  120. })
  121. return out
  122. }
  123. function isQuoted (val) {
  124. return (val.charAt(0) === "\"" && val.slice(-1) === "\"")
  125. || (val.charAt(0) === "'" && val.slice(-1) === "'")
  126. }
  127. function safe (val) {
  128. return ( typeof val !== "string"
  129. || val.match(/[=\r\n]/)
  130. || val.match(/^\[/)
  131. || (val.length > 1
  132. && isQuoted(val))
  133. || val !== val.trim() )
  134. ? JSON.stringify(val)
  135. : val.replace(/;/g, '\\;').replace(/#/g, "\\#")
  136. }
  137. function unsafe (val, doUnesc) {
  138. val = (val || "").trim()
  139. if (isQuoted(val)) {
  140. // remove the single quotes before calling JSON.parse
  141. if (val.charAt(0) === "'") {
  142. val = val.substr(1, val.length - 2);
  143. }
  144. try { val = JSON.parse(val) } catch (_) {}
  145. } else {
  146. // walk the val to find the first not-escaped ; character
  147. var esc = false
  148. var unesc = "";
  149. for (var i = 0, l = val.length; i < l; i++) {
  150. var c = val.charAt(i)
  151. if (esc) {
  152. if ("\\;#".indexOf(c) !== -1)
  153. unesc += c
  154. else
  155. unesc += "\\" + c
  156. esc = false
  157. } else if (";#".indexOf(c) !== -1) {
  158. break
  159. } else if (c === "\\") {
  160. esc = true
  161. } else {
  162. unesc += c
  163. }
  164. }
  165. if (esc)
  166. unesc += "\\"
  167. return unesc
  168. }
  169. return val
  170. }