PageRenderTime 76ms CodeModel.GetById 50ms app.highlight 19ms RepoModel.GetById 2ms app.codeStats 0ms

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