stringify.js 5.4 KB

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