json-beautifier.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* eslint-disable no-misleading-character-class */
  2. let
  3. escapable = /[\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
  4. keyable = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/,
  5. gap,
  6. indent,
  7. meta = { // table of character substitutions
  8. '\b': '\\b',
  9. '\t': '\\t',
  10. '\n': '\\n',
  11. '\f': '\\f',
  12. '\r': '\\r',
  13. '"': '\\"',
  14. "'": "\\'",
  15. '\\': '\\\\'
  16. },
  17. rep;
  18. export default function convert(object, options = {}) {
  19. var space = options.space || 2,
  20. dropQuotesOnKeys = options.dropQuotesOnKeys == false ? false: true,
  21. dropQuotesOnNumbers = options.dropQuotesOnNumbers || false,
  22. inlineShortArrays = options.inlineShortArrays || false,
  23. inlineShortArraysDepth = options.inlineShortArraysDepth || 1,
  24. quoteType = options.quoteType || 'single',
  25. minify = options.minify || false;
  26. if (dropQuotesOnNumbers) walkObjectAndDropQuotesOnNumbers(object);
  27. var result = stringify(object, null, minify ? undefined : space, dropQuotesOnKeys, quoteType);
  28. if (inlineShortArrays && !minify) {
  29. var newResult = inlineShortArraysInResult(result);
  30. if (inlineShortArraysDepth > 1) {
  31. for (var i = 1; i < inlineShortArraysDepth; i++) {
  32. result = newResult;
  33. newResult = inlineShortArraysInResult(result);
  34. if (newResult == result) break;
  35. }
  36. }
  37. result = newResult;
  38. }
  39. return result;
  40. }
  41. function walkObjectAndDropQuotesOnNumbers(object) {
  42. if (!isObject(object)) return;
  43. var keys = Object.keys(object);
  44. if (!keys) return;
  45. keys.forEach(function (key) {
  46. var value = object[key];
  47. if (typeof value == 'string') {
  48. var number = value - 0;
  49. object[key] = isNaN(number) ? value : number;
  50. } else if (isObject(value) || Array.isArray(value)) {
  51. walkObjectAndDropQuotesOnNumbers(value);
  52. }
  53. });
  54. }
  55. function isObject(o) {
  56. return o && typeof o == 'object';
  57. }
  58. // Collapses arrays inline when they fit inside the specified width
  59. // in characters (including indentation).
  60. function inlineShortArraysInResult(result, width) {
  61. width || (width = 80);
  62. if (typeof width != 'number' || width < 20) {
  63. throw "Invalid width '" + width + "'. Expecting number equal or larger than 20."
  64. }
  65. var list = result.split('\n'),
  66. i = 0,
  67. start = null,
  68. content = [];
  69. while (i < list.length) {
  70. var startMatch = !!list[i].match(/\[/),
  71. endMatch = !!list[i].match(/\],?/);
  72. if (startMatch && !endMatch) {
  73. content = [list[i]];
  74. start = i;
  75. } else if (endMatch && !startMatch && start) {
  76. content.push((list[i] || '').trim());
  77. var inline = content.join(' ');
  78. if (inline.length < width) {
  79. list.splice(start, i - start + 1, inline);
  80. i = start;
  81. }
  82. start = null;
  83. content = [];
  84. } else {
  85. if (start) content.push((list[i] || '').trim());
  86. }
  87. i += 1;
  88. }
  89. return list.join('\n');
  90. }
  91. function stringify(value, replacer, space, dropQuotesOnKeys, quoteType) {
  92. // The stringify method takes a value and an optional replacer, and an optional
  93. // space parameter, and returns a JSON text. The replacer can be a function
  94. // that can replace values, or an array of strings that will select the keys.
  95. // A default replacer method can be provided. Use of the space parameter can
  96. // produce text that is more easily readable.
  97. var i;
  98. gap = '';
  99. indent = '';
  100. // If the space parameter is a number, make an indent string containing that
  101. // many spaces.
  102. if (typeof space === 'number') {
  103. for (i = 0; i < space; i += 1) {
  104. indent += ' ';
  105. }
  106. // If the space parameter is a string, it will be used as the indent string.
  107. } else if (typeof space === 'string') {
  108. indent = space;
  109. }
  110. // If there is a replacer, it must be a function or an array.
  111. // Otherwise, throw an error.
  112. rep = replacer;
  113. if (replacer && typeof replacer !== 'function' &&
  114. (typeof replacer !== 'object' ||
  115. typeof replacer.length !== 'number')) {
  116. throw new Error('JSON.stringify');
  117. }
  118. // Make a fake root object containing our value under the key of ''.
  119. // Return the result of stringifying the value.
  120. return str('', { '': value }, dropQuotesOnKeys, quoteType);
  121. }
  122. function str(key, holder, dropQuotesOnKeys, quoteType) {
  123. // Produce a string from holder[key].
  124. var i, // The loop counter.
  125. k, // The member key.
  126. v, // The member value.
  127. length,
  128. mind = gap,
  129. partial,
  130. value = holder[key];
  131. // If the value has a toJSON method, call it to obtain a replacement value.
  132. if (value && typeof value === 'object' &&
  133. typeof value.toJSON === 'function') {
  134. value = value.toJSON(key);
  135. }
  136. // If we were called with a replacer function, then call the replacer to
  137. // obtain a replacement value.
  138. if (typeof rep === 'function') {
  139. value = rep.call(holder, key, value);
  140. }
  141. // What happens next depends on the value's type.
  142. switch (typeof value) {
  143. case 'function':
  144. return value;
  145. case 'string':
  146. return quote(value, quoteType);
  147. case 'number':
  148. // JSON numbers must be finite. Encode non-finite numbers as null.
  149. return isFinite(value) ? String(value) : 'null';
  150. case 'boolean':
  151. case 'null':
  152. // If the value is a boolean or null, convert it to a string. Note:
  153. // typeof null does not produce 'null'. The case is included here in
  154. // the remote chance that this gets fixed someday.
  155. return String(value);
  156. // If the type is 'object', we might be dealing with an object or an array or
  157. // null.
  158. case 'object':
  159. // Due to a specification blunder in ECMAScript, typeof null is 'object',
  160. // so watch out for that case.
  161. if (!value) {
  162. return 'null';
  163. }
  164. // Make an array to hold the partial results of stringifying this object value.
  165. gap += indent;
  166. partial = [];
  167. // Is the value an array?
  168. if (Object.prototype.toString.apply(value) === '[object Array]') {
  169. // The value is an array. Stringify every element. Use null as a placeholder
  170. // for non-JSON values.
  171. length = value.length;
  172. for (i = 0; i < length; i += 1) {
  173. partial[i] = str(i, value, dropQuotesOnKeys, quoteType) || 'null';
  174. }
  175. // Join all of the elements together, separated with commas, and wrap them in
  176. // brackets.
  177. v = partial.length === 0
  178. ? '[]'
  179. : gap
  180. ? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']'
  181. : '[' + partial.join(',') + ']';
  182. gap = mind;
  183. return v;
  184. }
  185. // If the replacer is an array, use it to select the members to be stringified.
  186. if (rep && typeof rep === 'object') {
  187. length = rep.length;
  188. for (i = 0; i < length; i += 1) {
  189. if (typeof rep[i] === 'string') {
  190. k = rep[i];
  191. v = str(k, value, dropQuotesOnKeys, quoteType);
  192. if (v) {
  193. partial.push((dropQuotesOnKeys ? condQuoteKey(k, quoteType) : quote(k, quoteType)) + (gap ? ': ' : ':') + v);
  194. }
  195. }
  196. }
  197. } else {
  198. // Otherwise, iterate through all of the keys in the object.
  199. for (k in value) {
  200. if (Object.prototype.hasOwnProperty.call(value, k)) {
  201. v = str(k, value, dropQuotesOnKeys, quoteType);
  202. if (v) {
  203. partial.push((dropQuotesOnKeys ? condQuoteKey(k, quoteType) : quote(k, quoteType)) + (gap ? ': ' : ':') + v);
  204. }
  205. }
  206. }
  207. }
  208. // Join all of the member texts together, separated with commas,
  209. // and wrap them in braces.
  210. v = partial.length === 0
  211. ? '{}'
  212. : gap
  213. ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}'
  214. : '{' + partial.join(',') + '}';
  215. gap = mind;
  216. return v;
  217. }
  218. }
  219. function quote(string, quoteType) {
  220. // If the string contains no control characters, no quote characters, and no
  221. // backslash characters, then we can safely slap some quotes around it.
  222. // Otherwise we must also replace the offending characters with safe escape
  223. // sequences.
  224. escapable.lastIndex = 0;
  225. var surroundingQuote = '"';
  226. if (quoteType === 'single') {
  227. surroundingQuote = "'";
  228. }
  229. return escapable.test(string) ? surroundingQuote + string.replace(escapable, function (a) {
  230. var c = meta[a];
  231. return typeof c === 'string'
  232. ? c
  233. : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
  234. }) + surroundingQuote : surroundingQuote + string + surroundingQuote;
  235. }
  236. function condQuoteKey(string, quoteType) {
  237. return keyable.test(string) ? string : quote(string, quoteType);
  238. }