stringify.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. 'use strict';
  2. var escapable = /[\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
  3. var gap;
  4. var indent;
  5. var meta = { // table of character substitutions
  6. '\b': '\\b',
  7. '\t': '\\t',
  8. '\n': '\\n',
  9. '\f': '\\f',
  10. '\r': '\\r',
  11. '"': '\\"',
  12. '\\': '\\\\'
  13. };
  14. var rep;
  15. function quote(string) {
  16. // If the string contains no control characters, no quote characters, and no
  17. // backslash characters, then we can safely slap some quotes around it.
  18. // Otherwise we must also replace the offending characters with safe escape sequences.
  19. escapable.lastIndex = 0;
  20. return escapable.test(string) ? '"' + string.replace(escapable, function (a) {
  21. var c = meta[a];
  22. return typeof c === 'string' ? c
  23. : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
  24. }) + '"' : '"' + string + '"';
  25. }
  26. function str(key, holder) {
  27. // Produce a string from holder[key].
  28. var i; // The loop counter.
  29. var k; // The member key.
  30. var v; // The member value.
  31. var length;
  32. var mind = gap;
  33. var partial;
  34. var value = holder[key];
  35. // If the value has a toJSON method, call it to obtain a replacement value.
  36. if (value && typeof value === 'object' && typeof value.toJSON === 'function') {
  37. value = value.toJSON(key);
  38. }
  39. // If we were called with a replacer function, then call the replacer to obtain a replacement value.
  40. if (typeof rep === 'function') {
  41. value = rep.call(holder, key, value);
  42. }
  43. // What happens next depends on the value's type.
  44. switch (typeof value) {
  45. case 'string':
  46. return quote(value);
  47. case 'number':
  48. // JSON numbers must be finite. Encode non-finite numbers as null.
  49. return isFinite(value) ? String(value) : 'null';
  50. case 'boolean':
  51. case 'null':
  52. // If the value is a boolean or null, convert it to a string. Note:
  53. // typeof null does not produce 'null'. The case is included here in
  54. // the remote chance that this gets fixed someday.
  55. return String(value);
  56. case 'object':
  57. if (!value) {
  58. return 'null';
  59. }
  60. gap += indent;
  61. partial = [];
  62. // Array.isArray
  63. if (Object.prototype.toString.apply(value) === '[object Array]') {
  64. length = value.length;
  65. for (i = 0; i < length; i += 1) {
  66. partial[i] = str(i, value) || 'null';
  67. }
  68. // Join all of the elements together, separated with commas, and wrap them in brackets.
  69. v = partial.length === 0 ? '[]' : gap
  70. ? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']'
  71. : '[' + partial.join(',') + ']';
  72. gap = mind;
  73. return v;
  74. }
  75. // If the replacer is an array, use it to select the members to be stringified.
  76. if (rep && typeof rep === 'object') {
  77. length = rep.length;
  78. for (i = 0; i < length; i += 1) {
  79. k = rep[i];
  80. if (typeof k === 'string') {
  81. v = str(k, value);
  82. if (v) {
  83. partial.push(quote(k) + (gap ? ': ' : ':') + v);
  84. }
  85. }
  86. }
  87. } else {
  88. // Otherwise, iterate through all of the keys in the object.
  89. for (k in value) {
  90. if (Object.prototype.hasOwnProperty.call(value, k)) {
  91. v = str(k, value);
  92. if (v) {
  93. partial.push(quote(k) + (gap ? ': ' : ':') + v);
  94. }
  95. }
  96. }
  97. }
  98. // Join all of the member texts together, separated with commas, and wrap them in braces.
  99. v = partial.length === 0 ? '{}' : gap
  100. ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}'
  101. : '{' + partial.join(',') + '}';
  102. gap = mind;
  103. return v;
  104. default:
  105. }
  106. }
  107. module.exports = function (value, replacer, space) {
  108. var i;
  109. gap = '';
  110. indent = '';
  111. // If the space parameter is a number, make an indent string containing that many spaces.
  112. if (typeof space === 'number') {
  113. for (i = 0; i < space; i += 1) {
  114. indent += ' ';
  115. }
  116. } else if (typeof space === 'string') {
  117. // If the space parameter is a string, it will be used as the indent string.
  118. indent = space;
  119. }
  120. // If there is a replacer, it must be a function or an array. Otherwise, throw an error.
  121. rep = replacer;
  122. if (
  123. replacer
  124. && typeof replacer !== 'function'
  125. && (typeof replacer !== 'object' || typeof replacer.length !== 'number')
  126. ) {
  127. throw new Error('JSON.stringify');
  128. }
  129. // Make a fake root object containing our value under the key of ''.
  130. // Return the result of stringifying the value.
  131. return str('', { '': value });
  132. };