-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsDump.js
229 lines (221 loc) · 7.79 KB
/
jsDump.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* jsDump
* Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
* Licensed under BSD (http://www.opensource.org/licenses/bsd-license.php)
* Date: 5/15/2008
* @projectDescription Advanced and extensible data dumping for Javascript.
* @version 1.0.0
* @author Ariel Flesler
* @link {http://flesler.blogspot.com/2008/05/jsdump-pretty-dump-of-any-javascript.html}
*/
var jsDump;
(function(){
function quote( str ){
var prestring = str.toString().replace(/"/g, '\\"');
if(jsDump.quote_forCPP){
prestring = prestring.replace(/\\/g, '\\');
prestring = prestring.replace(/\n/g, '\\n');
prestring = prestring.replace(/\t/g, '\\t');
}
return '"' + prestring + '"';
}
function literal( o ){
return o + '';
}
function join( pre, arr, post ){
var s = jsDump.separator();
var base = jsDump.indent();
var inner = jsDump.indent(1);
if( arr.join ){
arr = arr.join( ',' + s + inner );
}
if( !arr ){
return pre + post;
}
return [ pre, inner + arr, base + post ].join(s);
}
function joinSl( pre, arr, post ){
var s = ' ';
var base = ' ';//jsDump.indent();
var inner = '';//jsDump.indent(1);
if( arr.join ){
arr = arr.join( ',' + s + inner );
}
if( !arr ){
return pre + post;
}
return [ pre, inner + arr, base + post ].join(s);
}
function lpad(str, padString, length){
while (str.length < length)
str = padString + str;
return str;
}
function array( arr ){
var i = arr.length, ret = Array(i);
this.up();
while( i-- )
ret[i] = this.parse( arr[i] );
this.down();
return join( '[', ret, ']' );
}
function arraySmr( arr ){
var i = arr.length
var ret = Array(i);
this.up();
var isAnyObjectInside = false;
while( i-- ){
if(this.typeOf(arr[i]) == 'object'){
isAnyObjectInside = true;
}
ret[i] = this.parse( arr[i] );
if(this.typeOf(arr[i]) == 'number'){
ret[i] = lpad(ret[i],' ',3);
}
}
this.down();
output = isAnyObjectInside?join( '[', ret, ']' ):joinSl( '[', ret, ']' );
return output;
}
var reName = /^function (\w+)/;
jsDump = {
parse:function( obj, type ){//type is used mostly internally, you can fix a (custom)type in advance
var parser = this.parsers[ type || this.typeOf(obj) ];
type = typeof parser;
return type == 'function' ? parser.call( this, obj ) :
type == 'string' ? parser :
this.parsers.error;
},
typeOf:function( obj ){
var type = typeof obj,
f = 'function';//we'll use it 3 times, save it
return type != 'object' && type != f ? type :
!obj ? 'null' :
obj.exec ? 'regexp' :// some browsers (FF) consider regexps functions
obj.getHours ? 'date' :
obj.scrollBy ? 'window' :
obj.nodeName == '#document' ? 'document' :
obj.nodeName ? 'node' :
obj.item ? 'nodelist' : // Safari reports nodelists as functions
obj.callee ? 'arguments' :
obj.call || obj.constructor != Array && //an array would also fall on this hack
(obj+'').indexOf(f) != -1 ? f : //IE reports functions like alert, as objects
'length' in obj ? 'array' :
type;
},
separator:function(){
return this.multiline ? (this.HTML ? '<br />' : '\n') : (this.HTML ? ' ' : ' ');
},
indent:function( extra ){// extra can be a number, shortcut for increasing-calling-decreasing
if( !this.multiline )
return '';
var chr = this.indentChar;
if( this.HTML )
chr = chr.replace(/\t/g,' ').replace(/ /g,' ');
return Array( this._depth_ + (extra||0) ).join(chr);
},
up:function( a ){
this._depth_ += a || 1;
},
down:function( a ){
this._depth_ -= a || 1;
},
setParser:function( name, parser ){
this.parsers[name] = parser;
},
// The next 3 are exposed so you can use them
quote:quote,
literal:literal,
join:join,
_depth_: 1,
// This is the list of parsers, to modify them, use jsDump.setParser
parsers:{
window: '[Window]',
document: '[Document]',
error:'[ERROR]', //when no parser is found, shouldn't happen
unknown: '[Unknown]',
'null':'null',
undefined:'undefined',
'function':function( fn ){
var ret = 'function',
name = 'name' in fn ? fn.name : (reName.exec(fn)||[])[1];//functions never have name in IE
if( name )
ret += ' ' + name;
ret += '(';
ret = [ ret, this.parse( fn, 'functionArgs' ), '){'].join('');
return join( ret, this.parse(fn,'functionCode'), '}' );
},
array: arraySmr,
nodelist: array,
arguments: array,
object:function( map ){
var ret = [ ];
this.up();
var prev_mml = this.multiline;
if(this.multiline_ignoreIfKeyInList != null){
for( var key in map ){
if(this.multiline_ignoreIfKeyInList.indexOf(key) >= 0){
this.multiline = false;
break;
}
}
}
for( var key in map ){
var obj_line = this.parse(key,'key') + ': ' + this.parse(map[key]);
ret.push( obj_line );
}
ret.sort();
this.down();
var out = join( '{', ret, '}' );
this.multiline = prev_mml;
return out;
},
node:function( node ){
var open = this.HTML ? '<' : '<',
close = this.HTML ? '>' : '>';
var tag = node.nodeName.toLowerCase(),
ret = open + tag;
for( var a in this.DOMAttrs ){
var val = node[this.DOMAttrs[a]];
if( val )
ret += ' ' + a + '=' + this.parse( val, 'attribute' );
}
return ret + close + open + '/' + tag + close;
},
functionArgs:function( fn ){//function calls it internally, it's the arguments part of the function
var l = fn.length;
if( !l ) return '';
var args = Array(l);
while( l-- )
args[l] = String.fromCharCode(97+l);//97 is 'a'
return ' ' + args.join(', ') + ' ';
},
key:quote, //object calls it internally, the key part of an item in a map
functionCode:'[code]', //function calls it internally, it's the content of the function
attribute:quote, //node calls it internally, it's an html attribute value
string:quote,
date:quote,
regexp:literal, //regex
number:literal,
'boolean':literal
},
DOMAttrs:{//attributes to dump from nodes, name=>realName
id:'id',
name:'name',
'class':'className'
},
HTML:false,//if true, entities are escaped ( <, >, \t, space and \n )
indentChar:'\t',//indentation unit
quote_forCPP: false, // Mask \n \t
multiline:true, //if true, items in a collection, are separated by a \n, else just a space.
multiline_ignoreIfKeyInList: null // Objects with keys from this list should be placed in single-lined always (ignoring multiline options)
};
})();
if (typeof exports !== 'undefined') {
exports.jsDump = jsDump;
}
//==== additional stuff...
function Set_Cookie( name, value, expires, path, domain, secure ){var today = new Date();today.setTime( today.getTime() );if ( expires ){expires = expires * 1000 * 60 * 60 * 24;}var expires_date = new Date(today.getTime() + (expires) );document.cookie = name + "=" +escape( value ) +( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +( ( path ) ? ";path=" + path : "" ) +( ( domain ) ? ";domain=" + domain : "" ) +( ( secure ) ? ";secure" : "" );}
function Get_Cookie( check_name ) {var a_all_cookies = document.cookie.split( ';' );var a_temp_cookie = '';var cookie_name = '';var cookie_value = '';var b_cookie_found = false;for ( i = 0; i < a_all_cookies.length; i++ ){a_temp_cookie = a_all_cookies[i].split( '=' );cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');if ( cookie_name == check_name ){b_cookie_found = true;if ( a_temp_cookie.length > 1 ){cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );}return cookie_value;break;}a_temp_cookie = null;cookie_name = '';}if ( !b_cookie_found ){return null;}}
function coalesceInt(e,def){if(!def){def = 0;};if(!e){return def;};return parseInt(e);};
function roundToPrecision(inputNum, desiredPrecision){var precisionGuide = Math.pow(10, desiredPrecision);return( Math.round(inputNum * precisionGuide) / precisionGuide );}