818
Du
dustymarkdown / files
dustymarkdown / node_modules / dust / vendor / jsdump.js
4.9 KB
1/** 2 * jsDump 3 * Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com 4 * Licensed under BSD (http://www.opensource.org/licenses/bsd-license.php) 5 * Date: 5/15/2008 6 * @projectDescription Advanced and extensible data dumping for Javascript. 7 * @version 1.0.0 8 * @author Ariel Flesler 9 * @link {http://flesler.blogspot.com/2008/05/jsdump-pretty-dump-of-any-javascript.html} 10 */ 11var jsDump; 12 13(function(){ 14 function quote( str ){ 15 return '"' + str.toString().replace(/"/g, '\\"') + '"'; 16 } 17 function literal( o ){ 18 return o + ''; 19 } 20 function join( pre, arr, post ){ 21 var s = jsDump.separator(), 22 base = jsDump.indent(), 23 inner = jsDump.indent(1); 24 if( arr.join ) 25 arr = arr.join( ',' + s + inner ); 26 if( !arr ) 27 return pre + post; 28 return [ pre, inner + arr, base + post ].join(s); 29 } 30 function array( arr ){ 31 var i = arr.length, ret = Array(i); 32 this.up(); 33 while( i-- ) 34 ret[i] = this.parse( arr[i] ); 35 this.down(); 36 return join( '[', ret, ']' ); 37 } 38 39 var reName = /^function (\w+)/; 40 41 jsDump = { 42 parse:function( obj, type ){//type is used mostly internally, you can fix a (custom)type in advance 43 var parser = this.parsers[ type || this.typeOf(obj) ]; 44 type = typeof parser; 45 46 return type == 'function' ? parser.call( this, obj ) : 47 type == 'string' ? parser : 48 this.parsers.error; 49 }, 50 typeOf:function( obj ){ 51 var type = typeof obj, 52 f = 'function';//we'll use it 3 times, save it 53 return type != 'object' && type != f ? type : 54 !obj ? 'null' : 55 obj.exec ? 'regexp' :// some browsers (FF) consider regexps functions 56 obj.getHours ? 'date' : 57 obj.scrollBy ? 'window' : 58 obj.nodeName == '#document' ? 'document' : 59 obj.nodeName ? 'node' : 60 obj.item ? 'nodelist' : // Safari reports nodelists as functions 61 obj.callee ? 'arguments' : 62 obj.call || obj.constructor != Array && //an array would also fall on this hack 63 (obj+'').indexOf(f) != -1 ? f : //IE reports functions like alert, as objects 64 'length' in obj ? 'array' : 65 type; 66 }, 67 separator:function(){ 68 return this.multiline ? this.HTML ? '<br />' : '\n' : this.HTML ? ' ' : ' '; 69 }, 70 indent:function( extra ){// extra can be a number, shortcut for increasing-calling-decreasing 71 if( !this.multiline ) 72 return ''; 73 var chr = this.indentChar; 74 if( this.HTML ) 75 chr = chr.replace(/\t/g,' ').replace(/ /g,' '); 76 return Array( this._depth_ + (extra||0) ).join(chr); 77 }, 78 up:function( a ){ 79 this._depth_ += a || 1; 80 }, 81 down:function( a ){ 82 this._depth_ -= a || 1; 83 }, 84 setParser:function( name, parser ){ 85 this.parsers[name] = parser; 86 }, 87 // The next 3 are exposed so you can use them 88 quote:quote, 89 literal:literal, 90 join:join, 91 _depth_: 1, 92 // This is the list of parsers, to modify them, use jsDump.setParser 93 parsers:{ 94 window: '[Window]', 95 document: '[Document]', 96 error:'[ERROR]', //when no parser is found, shouldn't happen 97 unknown: '[Unknown]', 98 'null':'null', 99 undefined:'undefined', 100 'function':function( fn ){ 101 var ret = 'function', 102 name = 'name' in fn ? fn.name : (reName.exec(fn)||[])[1];//functions never have name in IE 103 if( name ) 104 ret += ' ' + name; 105 ret += '('; 106 ret = [ ret, this.parse( fn, 'functionArgs' ), '){'].join(''); 107 return join( ret, this.parse(fn,'functionCode'), '}' ); 108 }, 109 array: array, 110 nodelist: array, 111 arguments: array, 112 object:function( map ){ 113 var ret = [ ]; 114 this.up(); 115 for( var key in map ) 116 ret.push( this.parse(key,'key') + ': ' + this.parse(map[key]) ); 117 this.down(); 118 return join( '{', ret, '}' ); 119 }, 120 node:function( node ){ 121 var open = this.HTML ? '<' : '<', 122 close = this.HTML ? '>' : '>'; 123 var tag = node.nodeName.toLowerCase(), 124 ret = open + tag; 125 for( var a in this.DOMAttrs ){ 126 var val = node[this.DOMAttrs[a]]; 127 if( val ) 128 ret += ' ' + a + '=' + this.parse( val, 'attribute' ); 129 } 130 return ret + close + open + '/' + tag + close; 131 }, 132 functionArgs:function( fn ){//function calls it internally, it's the arguments part of the function 133 var l = fn.length; 134 if( !l ) return ''; 135 var args = Array(l); 136 while( l-- ) 137 args[l] = String.fromCharCode(97+l);//97 is 'a' 138 return ' ' + args.join(', ') + ' '; 139 }, 140 key:quote, //object calls it internally, the key part of an item in a map 141 functionCode:'[code]', //function calls it internally, it's the content of the function 142 attribute:quote, //node calls it internally, it's an html attribute value 143 string:quote, 144 date:quote, 145 regexp:literal, //regex 146 number:literal, 147 'boolean':literal 148 }, 149 DOMAttrs:{//attributes to dump from nodes, name=>realName 150 id:'id', 151 name:'name', 152 'class':'className' 153 }, 154 HTML:false,//if true, entities are escaped ( <, >, \t, space and \n ) 155 indentChar:' ',//indentation unit 156 multiline:true //if true, items in a collection, are separated by a \n, else just a space. 157 }; 158 159})(); 160 161if (typeof exports !== 'undefined') { 162 exports.jsDump = jsDump; 163}