PageRenderTime 39ms CodeModel.GetById 17ms app.highlight 17ms RepoModel.GetById 2ms app.codeStats 0ms

/tags/jsdoc_toolkit-1.3.3/app/Dumper.js

http://jsdoc-toolkit.googlecode.com/
JavaScript | 142 lines | 95 code | 18 blank | 29 comment | 23 complexity | 02fb603653a0e8316401f710517ed95a 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		var out;
 39		var type = this._typeof(obj);
 40		if (obj.circularReference) obj.circularReference++;
 41		switch (type) {
 42			case 'circular':
 43				out = '{/*circularReference*/}';
 44				break;
 45			case 'object':
 46				var pairs = new Array;
 47				
 48				for (var prop in obj) {
 49					if (prop != "circularReference" && obj.hasOwnProperty(prop)) { //hide inherited properties
 50						pairs.push(prop + ': ' + this._dump(obj[prop]));
 51					}
 52				}
 53	
 54				out = '{' + this._format_list(pairs) + '}';
 55				break;
 56	
 57			case 'string':
 58				for (var prop in Dumper.ESC) {
 59					if (Dumper.ESC.hasOwnProperty(prop)) {
 60						obj = obj.replace(prop, Dumper.ESC[prop]);
 61					}
 62				}
 63	
 64			// Escape UTF-8 Strings
 65				if (obj.match(/^[\x00-\x7f]*$/)) {
 66					out = '"' + obj + '"';
 67				}
 68				else {
 69					out = "unescape('"+escape(obj)+"')";
 70				}
 71				break;
 72	
 73			case 'array':
 74				var elems = new Array;
 75	
 76				for (var i=0; i<obj.length; i++) {
 77					elems.push( this._dump(obj[i]) );
 78				}
 79	
 80				out = '[' + this._format_list(elems) + ']';
 81				break;
 82	
 83			case 'date':
 84			// firefox returns GMT strings from toUTCString()...
 85			var utc_string = obj.toUTCString().replace(/GMT/,'UTC');
 86				out = 'new Date("' + utc_string + '")';
 87				break;
 88	
 89			case 'element':
 90				// DOM element
 91				out = this._dump_dom(obj);
 92				break;
 93		
 94				default:
 95					out = obj;
 96		}
 97	
 98		out = String(out).replace(/\n/g, '\n    ');
 99		out = out.replace(/\n    (.*)$/,"\n$1");
100	
101		return out;
102	},
103	
104	_format_list: function (list) {
105		if (!list.length) return '';
106		var nl = list.toString().length > 60 ? '\n' : ' ';
107		return nl + list.join(',' + nl) + nl;
108    },
109    
110    _typeof: function (obj) {
111    	if (obj && obj.circularReference && obj.circularReference > 1) return 'circular';
112		if (Array.prototype.isPrototypeOf(obj)) return 'array';
113		if (Date.prototype.isPrototypeOf(obj)) return 'date';
114		if (typeof(obj.nodeType) != 'undefined') return 'element';
115		return typeof(obj);
116	},
117	
118	_dump_dom: function (obj) {
119		return '"' + Dumper.nodeTypes[obj.nodeType] + '"';
120	}
121};
122
123Dumper.ESC = {
124    "\t": "\\t",
125    "\n": "\\n",
126    "\f": "\\f"
127};
128
129Dumper.nodeTypes = {
130    1: "ELEMENT_NODE",
131    2: "ATTRIBUTE_NODE",
132    3: "TEXT_NODE",
133    4: "CDATA_SECTION_NODE",
134    5: "ENTITY_REFERENCE_NODE",
135    6: "ENTITY_NODE",
136    7: "PROCESSING_INSTRUCTION_NODE",
137    8: "COMMENT_NODE",
138    9: "DOCUMENT_NODE",
139    10: "DOCUMENT_TYPE_NODE",
140    11: "DOCUMENT_FRAGMENT_NODE",
141    12: "NOTATION_NODE"
142};