/branches/version1.x/app/Dumper.js

http://jsdoc-toolkit.googlecode.com/ · JavaScript · 143 lines · 96 code · 18 blank · 29 comment · 25 complexity · d6581834a13de19ec3aaf29f55afd438 MD5 · raw file

  1. /**
  2. * @class
  3. <pre>
  4. This is a lightly modified version of Kevin Jones' JavaScript
  5. library Data.Dump. To download the original visit:
  6. <a href="http://openjsan.org/doc/k/ke/kevinj/Data/Dump/">http://openjsan.org/doc/k/ke/kevinj/Data/Dump/</a>
  7. AUTHORS
  8. The Data.Dump JavaScript module is written by Kevin Jones
  9. (kevinj@cpan.org), based on Data::Dump by Gisle Aas (gisle@aas.no),
  10. based on Data::Dumper by Gurusamy Sarathy (gsar@umich.edu).
  11. COPYRIGHT
  12. Copyright 2007 Kevin Jones. Copyright 1998-2000,2003-2004 Gisle Aas.
  13. Copyright 1996-1998 Gurusamy Sarathy.
  14. This program is free software; you can redistribute it and/or modify
  15. it under the terms of the Perl Artistic License
  16. See http://www.perl.com/perl/misc/Artistic.html
  17. </pre>
  18. * @static
  19. */
  20. Dumper = {
  21. /** @param [...] The objects to dump. */
  22. dump: function () {
  23. if (arguments.length > 1)
  24. return this._dump(arguments);
  25. else if (arguments.length == 1)
  26. return this._dump(arguments[0]);
  27. else
  28. return "()";
  29. },
  30. _dump: function (obj) {
  31. if (typeof obj == 'undefined') return 'undefined';
  32. var out;
  33. var type = this._typeof(obj);
  34. if (obj.circularReference) obj.circularReference++;
  35. switch (type) {
  36. case 'circular':
  37. out = "{ //circularReference\n}";
  38. break;
  39. case 'object':
  40. var pairs = new Array;
  41. for (var prop in obj) {
  42. if (prop != "circularReference" && obj.hasOwnProperty(prop)) { //hide inherited properties
  43. pairs.push(prop + ': ' + this._dump(obj[prop]));
  44. }
  45. }
  46. out = '{' + this._format_list(pairs) + '}';
  47. break;
  48. case 'string':
  49. for (var prop in Dumper.ESC) {
  50. if (Dumper.ESC.hasOwnProperty(prop)) {
  51. obj = obj.replace(prop, Dumper.ESC[prop]);
  52. }
  53. }
  54. // Escape UTF-8 Strings
  55. if (obj.match(/^[\x00-\x7f]*$/)) {
  56. out = '"' + obj + '"';
  57. }
  58. else {
  59. out = "unescape('"+escape(obj)+"')";
  60. }
  61. break;
  62. case 'array':
  63. var elems = new Array;
  64. for (var i=0; i<obj.length; i++) {
  65. elems.push( this._dump(obj[i]) );
  66. }
  67. out = '[' + this._format_list(elems) + ']';
  68. break;
  69. case 'date':
  70. // firefox returns GMT strings from toUTCString()...
  71. var utc_string = obj.toUTCString().replace(/GMT/,'UTC');
  72. out = 'new Date("' + utc_string + '")';
  73. break;
  74. case 'element':
  75. // DOM element
  76. out = this._dump_dom(obj);
  77. break;
  78. default:
  79. out = obj;
  80. }
  81. out = String(out).replace(/\n/g, '\n ');
  82. out = out.replace(/\n (.*)$/,"\n$1");
  83. return out;
  84. },
  85. _format_list: function (list) {
  86. if (!list.length) return '';
  87. var nl = list.toString().length > 60 ? '\n' : ' ';
  88. return nl + list.join(',' + nl) + nl;
  89. },
  90. _typeof: function (obj) {
  91. if (obj && obj.circularReference && obj.circularReference > 1) return 'circular';
  92. if (Array.prototype.isPrototypeOf(obj)) return 'array';
  93. if (Date.prototype.isPrototypeOf(obj)) return 'date';
  94. if (typeof obj.nodeType != 'undefined') return 'element';
  95. return typeof(obj);
  96. },
  97. _dump_dom: function (obj) {
  98. return '"' + Dumper.nodeTypes[obj.nodeType] + '"';
  99. }
  100. };
  101. Dumper.ESC = {
  102. "\t": "\\t",
  103. "\n": "\\n",
  104. "\f": "\\f"
  105. };
  106. Dumper.nodeTypes = {
  107. 1: "ELEMENT_NODE",
  108. 2: "ATTRIBUTE_NODE",
  109. 3: "TEXT_NODE",
  110. 4: "CDATA_SECTION_NODE",
  111. 5: "ENTITY_REFERENCE_NODE",
  112. 6: "ENTITY_NODE",
  113. 7: "PROCESSING_INSTRUCTION_NODE",
  114. 8: "COMMENT_NODE",
  115. 9: "DOCUMENT_NODE",
  116. 10: "DOCUMENT_TYPE_NODE",
  117. 11: "DOCUMENT_FRAGMENT_NODE",
  118. 12: "NOTATION_NODE"
  119. };