code_junkie

JavaScript data formatting/pretty printer

Nov 11th, 2011
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. function DumpObject(obj)
  2. {
  3. var od = new Object;
  4. var result = "";
  5. var len = 0;
  6.  
  7. for (var property in obj)
  8. {
  9. var value = obj[property];
  10. if (typeof value == 'string')
  11. value = "'" + value + "'";
  12. else if (typeof value == 'object')
  13. {
  14. if (value instanceof Array)
  15. {
  16. value = "[ " + value + " ]";
  17. }
  18. else
  19. {
  20. var ood = DumpObject(value);
  21. value = "{ " + ood.dump + " }";
  22. }
  23. }
  24. result += "'" + property + "' : " + value + ", ";
  25. len++;
  26. }
  27. od.dump = result.replace(/, $/, "");
  28. od.len = len;
  29.  
  30. return od;
  31. }
  32.  
  33. function DumpObjectIndented(obj, indent)
  34. {
  35. var result = "";
  36. if (indent == null) indent = "";
  37.  
  38. for (var property in obj)
  39. {
  40. var value = obj[property];
  41. if (typeof value == 'string')
  42. value = "'" + value + "'";
  43. else if (typeof value == 'object')
  44. {
  45. if (value instanceof Array)
  46. {
  47. // Just let JS convert the Array to a string!
  48. value = "[ " + value + " ]";
  49. }
  50. else
  51. {
  52. // Recursive dump
  53. // (replace " " by "t" or something else if you prefer)
  54. var od = DumpObjectIndented(value, indent + " ");
  55. // If you like { on the same line as the key
  56. //value = "{n" + od + "n" + indent + "}";
  57. // If you prefer { and } to be aligned
  58. value = "n" + indent + "{n" + od + "n" + indent + "}";
  59. }
  60. }
  61. result += indent + "'" + property + "' : " + value + ",n";
  62. }
  63. return result.replace(/,n$/, "");
  64. }
  65.  
  66. var myArray = ['e', {pluribus: 'unum'}];
  67. var text = JSON.stringify(myArray, null, 't'); //you can specify a number instead of 't' and that many spaces will be used for indentation...
  68.  
  69. [
  70. "e",
  71. {
  72. "pluribus": "unum"
  73. }
  74. ]
  75.  
  76. jsDump.parse([
  77. window,
  78. document,
  79. { a : 5, '1' : 'foo' },
  80. /^[ab]+$/g,
  81. new RegExp('x(.*?)z','ig'),
  82. alert,
  83. function fn( x, y, z ){
  84. return x + y;
  85. },
  86. true,
  87. undefined,
  88. null,
  89. new Date(),
  90. document.body,
  91. document.getElementById('links')
  92. ])
  93.  
  94. [
  95. [Window],
  96. [Document],
  97. {
  98. "1": "foo",
  99. "a": 5
  100. },
  101. /^[ab]+$/g,
  102. /x(.*?)z/gi,
  103. function alert( a ){
  104. [code]
  105. },
  106. function fn( a, b, c ){
  107. [code]
  108. },
  109. true,
  110. undefined,
  111. null,
  112. "Fri Feb 19 2010 00:49:45 GMT+0300 (MSK)",
  113. <body id="body" class="node"></body>,
  114. <div id="links">
  115. ]
  116.  
  117. JSON.stringify({f:function(){}}) // "{}"
  118. JSON.stringify(document.body) // TypeError: Converting circular structure to JSON
  119.  
  120. // Usage: dump(object)
  121. function dump(object, pad){
  122. var indent = 't'
  123. if (!pad) pad = ''
  124. var out = ''
  125. if (object.constructor == Array){
  126. out += '[n'
  127. for (var i=0; i<object.length; i++){
  128. out += pad + indent + dump(object[i], pad + indent) + 'n'
  129. }
  130. out += pad + ']'
  131. }else if (object.constructor == Object){
  132. out += '{n'
  133. for (var i in object){
  134. out += pad + indent + i + ': ' + dump(object[i], pad + indent) + 'n'
  135. }
  136. out += pad + '}'
  137. }else{
  138. out += object
  139. }
  140. return out
  141. }
  142.  
  143. var temp = Array.prototype.toJSON;
  144. delete Array.prototype.toJSON;
  145. $('result').value += JSON.stringify(profile_base, null, 2);
  146. Array.prototype.toJSON = temp;
  147.  
  148. function dumpObject(obj, indent)
  149. {
  150. var CR = "<br />", SPC = "&nbsp;&nbsp;&nbsp;&nbsp;", result = "";
  151. if (indent == null) indent = "";
  152.  
  153. for (var property in obj)
  154. {
  155. var value = obj[property];
  156.  
  157. if (typeof value == 'string')
  158. {
  159. value = "'" + value + "'";
  160. }
  161. else if (typeof value == 'object')
  162. {
  163. if (value instanceof Array)
  164. {
  165. // Just let JS convert the Array to a string!
  166. value = "[ " + value + " ]";
  167. }
  168. else
  169. {
  170. var od = dumpObject(value, indent + SPC);
  171. value = CR + indent + "{" + CR + od + CR + indent + "}";
  172. }
  173. }
  174. result += indent + "'" + property + "' : " + value + "," + CR;
  175. }
  176. return result;
  177. }
  178.  
  179. function pp(object, depth, embedded) {
  180. typeof(depth) == "number" || (depth = 0)
  181. typeof(embedded) == "boolean" || (embedded = false)
  182. var newline = false
  183. var spacer = function(depth) { var spaces = ""; for (var i=0;i<depth;i++) { spaces += " "}; return spaces }
  184. var pretty = ""
  185. if ( typeof(object) == "undefined" ) { pretty += "undefined" }
  186. else if ( typeof(object) == "boolean" ||
  187. typeof(object) == "number" ) { pretty += object.toString() }
  188. else if ( typeof(object) == "string" ) { pretty += """ + object + """ }
  189. else if ( object == null) { pretty += "null" }
  190. else if ( object instanceof(Array) ) {
  191. if ( object.length > 0 ) {
  192. if (embedded) { newline = true }
  193. var content = ""
  194. for each (var item in object) { content += pp(item, depth+1) + ",n" + spacer(depth+1) }
  195. content = content.replace(/,ns*$/, "").replace(/^s*/,"")
  196. pretty += "[ " + content + "n" + spacer(depth) + "]"
  197. } else { pretty += "[]" }
  198. }
  199. else if (typeof(object) == "object") {
  200. if ( Object.keys(object).length > 0 ){
  201. if (embedded) { newline = true }
  202. var content = ""
  203. for (var key in object) {
  204. content += spacer(depth + 1) + key.toString() + ": " + pp(object[key], depth+2, true) + ",n"
  205. }
  206. content = content.replace(/,ns*$/, "").replace(/^s*/,"")
  207. pretty += "{ " + content + "n" + spacer(depth) + "}"
  208. } else { pretty += "{}"}
  209. }
  210. else { pretty += object.toString() }
  211. return ((newline ? "n" + spacer(depth) : "") + pretty)
  212. }
  213.  
  214. js> pp({foo:"bar", baz: 1})
  215. { foo: "bar",
  216. baz: 1
  217. }
  218. js> var taco
  219. js> pp({foo:"bar", baz: [1,"taco",{"blarg": "moo", "mine": "craft"}, null, taco, {}], bleep: {a:null, b:taco, c: []}})
  220. { foo: "bar",
  221. baz:
  222. [ 1,
  223. "taco",
  224. { blarg: "moo",
  225. mine: "craft"
  226. },
  227. null,
  228. undefined,
  229. {}
  230. ],
  231. bleep:
  232. { a: null,
  233. b: undefined,
  234. c: []
  235. }
  236. }
Add Comment
Please, Sign In to add comment