-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobreaker.js
199 lines (181 loc) · 6.46 KB
/
obreaker.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
/*
* Obreaker.js
* A debuger to traverse and print object properties recursively to your web
* browser console.
*
* Released under MIT License
*
* --------------------------------------------------------------------------------
* The MIT License (MIT)
*
* Copyright (C) 2014 Ignatius Steven (https://github.com/isteven)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* --------------------------------------------------------------------------------
*/
obreaker =
{
/*
* SETTINGS
*/
// Debug message prefix
console_prefix: '[obreaker]',
// Traverse depth (how deep you want to recursively process object's children / property)
traverse_depth : 999,
/*
* MAIN FUNCTION
*/
log: function()
{
var toPrint = '';
for ( var key in arguments )
{
if ( typeof arguments[ key ] !== 'object' )
{
if ( key == 0 )
{
sep = '';
}
else
{
if ( toPrint != '')
{
sep = ' > ';
}
}
toPrint = toPrint + sep + arguments[ key ];
}
else
{
if ( toPrint != '' )
{
var errorStack = new Error();
var errorInfo = '';
if ( typeof errorStack.stack != 'undefined' )
{
errorStack = errorStack.stack.split("\n")[1];
errorInfo = ' (' + this.parseError( errorStack ) + ')';
}
console.log( this.console_prefix + ' ' + ' ' + toPrint + errorInfo );
}
toPrint = '';
var closure = this.getClosure( arguments[ key ] );
console.log( this.console_prefix + ' ' + closure[ 'open' ] );
this.print_object( arguments[ key ] , ' ' )
console.log( this.console_prefix + ' ' + closure[ 'close' ] );
}
}
if ( toPrint != '' )
{
var errorStack = new Error();
var errorInfo = '';
if ( typeof errorStack.stack != 'undefined' )
{
errorStack = errorStack.stack.split("\n")[1];
errorInfo = ' (' + this.parseError( errorStack ) + ')';
}
console.log( this.console_prefix + ' ' + ' ' + toPrint + errorInfo );
}
},
/*
* Recursive method to print object
* Reference: http://stackoverflow.com/questions/15058780/eclipse-ide-console-log-objects-in-javascript
*/
print_object: function( obj, padding, currLevel )
{
var ind = '';
if ( currLevel == '' || typeof currLevel === 'undefined' || currLevel == null )
{
currLevel = 0;
}
if ( currLevel == this.traverse_depth )
{
return true;
}
if (arguments.length > 1)
{
ind = arguments[ 1 ];
}
if ( typeof obj === 'undefined' || obj == null )
{
console.log( '<null>' );
return;
}
for (var key in obj)
{
if ( typeof obj[key] === 'function' )
{
continue;
}
var closure = this.getClosure( obj[key] );
if ( typeof obj[key] === 'object' && obj.hasOwnProperty( key ) )
{
console.log( this.console_prefix + ' ' + ind + key + ': ' );
console.log( this.console_prefix + ' ' + ind + closure[ 'open' ] );
this.print_object( obj[ key ], ind + ' ' , currLevel + 1 );
console.log( this.console_prefix + ' ' + ind + closure[ 'close' ] );
}
else
{
console.log( this.console_prefix + ' ' + ind + key + ': ' + obj[ key ] );
}
}
},
/* To determine whether to use [ ] or { } */
getClosure: function( item ) {
var closure = new Array();
var objectType = typeof item;
if ( objectType === 'object' ) {
if ( item ) {
if ( item instanceof Array) {
objectType = 'array';
}
} else {
objectType = 'null';
}
}
switch( objectType ) {
case 'array':
closure[ 'open' ] = '[';
closure[ 'close' ] = ']';
break;
case 'object':
closure[ 'open' ] = '{';
closure[ 'close' ] = '}';
break;
default:
closure[ 'open' ] = '{';
closure[ 'close' ] = '}';
break;
}
return closure;
},
/* Process error */
parseError: function( error ) {
var errorArr = error.split( /[@|/]/ ),
functionName = '';
if ( errorArr[0] )
{
functionName = errorArr[0] + '() - ';
}
var scriptLine = errorArr[ errorArr.length - 1];
scriptLine = scriptLine.split( ':' );
return ( functionName + scriptLine[0] + ' [line ' + scriptLine[1] + ']');
}
}