-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpretty_print.js
197 lines (164 loc) · 6.35 KB
/
pretty_print.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
var $web17_com_au$ = $web17_com_au$ || {};
// Pretty print module for javascript
$web17_com_au$.pretty_print = function() {
var module={};
var NEWLINE = '\r\n';
var rx={},rxg={};
rx.newline = /\r?\n/;
rxg.newline = /\r?\n/g;
var map = function(f,arr) {
for(var i=0;i<arr.length;i++) {
f(arr[i]);
}
}
// Pretty print
//
// - this function will get called recursively on
// nested array or object structures
// - 'nested' parameter >0 means we are recursing; this will suppress
// printing to standard out
// - pp.nested can be set to control recursion
// - this function includes rhino and e4x functionality
module.pp = function(obj,nested) {
var i;
var indent = '';
var indent2 = ''; // what the hell is this?
var str = '';
if(nested>module.pp.nested) return '';
if(!nested) nested = 0;
if(module.pp.extended) {
for(indent=' ',i=nested;i>0;i--) {
indent+=' ';
if(i>0) indent2+=' ';
}
}
if(obj == undefined || obj == null) {
if(obj==undefined) return 'undefined';
if(obj==null) return 'null';
if(nested==0 && obj==null) return 'null';
}
switch(typeof(obj)) {
case 'xml': // XML or XMLList (E4X)
str = obj.toXMLString();
str = str.replace(rxg.newline,'\\n').replace(/ */g,' ');
if(!module.pp.extended)
str = str.replace(rxg.newline,' ').replace(/ */g,' ');
break;
case 'object':
// STRING
if(obj instanceof String) {
str = '"'+obj+'"';
// JS ARRAY
} else if(obj instanceof Array) {
// Print as array literal
str += '[';
if(module.pp.extended) str += NEWLINE;
map(function(i){
if(obj[i]!==obj) {
str+=indent+(module.pp(i,nested+1)+', ');
if(module.pp.extended) str += NEWLINE;
} else {
str+=indent+' (self), ';
// a = [1,2,3]; a[1] = a; => madness ....
if(module.pp.extended) str += NEWLINE;
}
},obj);
str += indent2+']';
if(module.pp.extended) str += NEWLINE;
} else if(!(obj instanceof Object)) {
// JAVA CLASS/OBJECT (rhino)
if(java && java.lang) {
// Suppose someone types: someObj['class'].
// They want the java class of someObj but we'll
// be giving them the class of this java class (which is Class).
if(obj['class']===java.lang.Class){str=obj;}
else str = obj['class']; //str = obj; // Alternative
}
} else {
// JAVASCRIPT OBJECT
// Print as object literal
var count=0;
str += '{';
if(module.pp.extended) str += NEWLINE;
try{
for(var n in obj) {
if(count++>module.pp.nested_property_limit && nested>0) {
str+=(indent+'...');
if(module.pp.extended) str += NEWLINE;
break;
}
if(obj!==obj[n]) {
// To prevent infinite recursion.
// !== is important; e4x Namespace and its uri
// property show that != fails.
str+=indent+
(n+': '+module.pp(obj[n],nested+1)
.replace(/\r?\n *$/,' ')+', ');
if(module.pp.extended) str += NEWLINE;
}
else {
str+=(indent+n+': (self), ');
if(module.pp.extended) str += NEWLINE;
}
}
} catch (e) { str+='[error:'+n+']' }
str += indent2+'}';
if(module.pp.extended) str += NEWLINE;
}
break;
case 'string':
str += ('"'+obj+'"');
if(!module.pp.newlines) str = str.replace(rxg.newline,'\\n');
break;
case 'function':
//str = obj.toString().replace(/^\n/,'').replace(/\n$/,'');
// Reduce clutter by collapsing function bodies when
// printing as parts of other objects...
if(nested>0) str += 'f()';
else {
try {
if(module.pp.extended) str = obj.toString();
else str = obj.toString().replace(rxg.newline,'')
.replace(/ */g,' ');
} catch(e) {
// I've forgotten why we catch an error here.
// Something to do with: referencing a java class
// rather than instance, and getting an error, even if we
// are just testing for existence of toString.
str = obj;
}
}
break;
default:
str = obj;
break;
}
// Cut long strings down...
if(module.pp.truncate)
switch(typeof(obj)) {
case 'xml':
case 'string':
case 'function':
try {
if(str.length>300)
str = str.substring(0,149)+'... ... ...'; //+
//str.substring(str.length-150,str.length);
} catch(e) {
// We probably have java, eg java.lang.String is 'function'
}
}
if(nested==0) return str;
return str+'';
}
// Don't pretty print beyond nested level.
module.pp.nested = 2;
// Truncate large xml, strings or function definitions
module.pp.truncate = true;
// Some objects or their properties have large numbers of properties;
// limit the number printed.
// For example, try pretty-printing and event object in firefox.
module.pp.nested_property_limit = 10;
// Print nested items on new line (for javascript objects).
module.pp.extended = true;
return module;
}();