forked from patik/console.log-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consolelog.detailprint.js
174 lines (142 loc) · 5.62 KB
/
consolelog.detailprint.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
/**
* Cross-Browser console.log() Wrapper
* Detailed Print Plugin
*
* Version 2.0.1, 2013-12-21
* By Craig Patik
* https://github.com/patik/console.log-wrapper/
*/
window.log = window.log || function() {};
// Checks whether it's necessary to parse details for this browser
window.log.needsDetailPrint = (function _log_needsDetailPrint() {
var ua = window.navigator.userAgent,
uaCheck, uaVersion;
// Look for iOS <6 (thanks to Jörn Berkefeld)
if (/iPad|iPhone|iPod/.test(window.navigator.platform)) {
uaCheck = ua.match(/OS\s([0-9]{1})_([0-9]{1})/);
uaVersion = uaCheck ? parseInt(uaCheck[1], 10) : 0;
if (uaVersion >= 6) {
return true;
}
}
// Check for Opera version 11 or lower
else if (window.opera) {
uaCheck = /Version\/(\d+)\.\d+/;
if (uaCheck.test(ua)) {
if (parseInt(uaCheck.exec(ua)[1], 10) <= 11) {
return true;
}
}
}
// Check for Internet Explorer up through version 10
else if (/MSIE\s\d/.test(ua)) {
return true;
}
return false;
}());
// List arguments separately for easier deciphering in some browsers
window.log.detailPrint = function _log_detailPrint(args) {
var getSpecificType, detailedArgs, i, j, thisArg, argType, str, beginStr;
// Checks for special JavaScript types that inherit from Object
getSpecificType = function _getSpecificType(obj) {
var reportedType = Object.prototype.toString.call(obj),
types = ['Array', 'Date', 'RegExp', 'Null'],
found = '',
n;
// Look for special types that inherit from Object
n = types.length;
while (n--) {
if (reportedType === '[object ' + types[n] + ']') {
found = types[n].toLowerCase();
break;
}
}
if (found.length) {
return found;
}
// DOM element (DOM level 2 and level 1, respectively)
if ((typeof HTMLElement === 'object' && obj instanceof HTMLElement) || (typeof obj.nodeName === 'string' && obj.nodeType === 1)) {
found = 'element';
}
// DOM node (DOM level 2 and level 1, respectively)
else if ((typeof Node === 'object' && obj instanceof Node) || (typeof obj.nodeType === 'number' && typeof obj.nodeName === 'string')) {
found = 'node';
}
// Node list
if (/^\[object (HTMLCollection|NodeList|Object)\]$/.test(reportedType) &&
typeof obj.length === 'number' &&
typeof obj.item !== 'undefined' &&
(obj.length === 0 || (typeof obj[0] === 'object' && obj[0].nodeType > 0))) {
found = 'node';
}
return found.length ? found : typeof obj;
};
// Loop through each argument and collect details for each one
detailedArgs = [];
i = 0;
while (i < args.length) {
thisArg = args[i];
// Get argument type
argType = typeof thisArg;
beginStr = 'Item ' + (i + 1) + '/' + args.length + ' ';
// Be more specific about objects
if (argType === 'object') {
argType = getSpecificType(thisArg);
switch(argType) {
case 'array':
// Include array length and contents' types
if (!thisArg.length) {
detailedArgs.push(beginStr + '(array, empty) ', thisArg);
}
else {
// Get the types of up to 3 items
j = thisArg.length > 3 ? 3 : thisArg.length;
str = '';
while (j--) {
str = getSpecificType(thisArg[j]) + ', ' + str;
}
if (thisArg.length > 3) {
str += '...';
}
else {
str = str.replace(/,+\s+$/, '');
}
detailedArgs.push(beginStr + '(array, length=' + thisArg.length + ', [' + str + ']) ', thisArg);
}
break;
case 'element':
str = thisArg.nodeName.toLowerCase();
if (thisArg.id) {
str += '#' + thisArg.id;
}
if (thisArg.className) {
str += '.' + thisArg.className.replace(/\s+/g, '.');
}
detailedArgs.push(beginStr + '(element, ' + str + ') ', thisArg);
break;
case 'date':
detailedArgs.push(beginStr + '(date) ', thisArg.toUTCString());
break;
default:
detailedArgs.push(beginStr + '(' + argType + ')', thisArg);
if (argType === 'object') {
// Print properties for plain objects (first level only)
if (typeof thisArg.hasOwnProperty === 'function') {
for (j in thisArg) {
if (thisArg.hasOwnProperty(j)) {
detailedArgs.push(' --> "' + j + '" = (' + getSpecificType(thisArg[j]) + ') ', thisArg[j]);
}
}
}
}
break;
}
}
// Print non-objects as-is
else {
detailedArgs.push(beginStr + '(' + typeof thisArg + ') ', thisArg);
}
i++;
}
return detailedArgs;
};