-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathconsoleFormat.js
More file actions
117 lines (90 loc) · 2.38 KB
/
Copy pathconsoleFormat.js
File metadata and controls
117 lines (90 loc) · 2.38 KB
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
/* can be required directly from other client-side code */
module.exports = function(args) {
return Array.from(args).map(format).join(', ');
}
function format(value, depth = 0) {
if (value == null) return 'null';
if (typeof value == 'function') {
return formatFunction(value, depth);
}
if (Array.isArray(value)) {
return formatArray(value, depth);
}
if (typeof window == 'object') {
if (value instanceof Node) {
return format(value.outerHTML, depth);
}
if (value instanceof Event) {
let copyProps = [Symbol.toStringTag, 'type', 'clientX', 'clientY', 'key', 'code'];
let obj = {};
for(let prop of copyProps) {
if (prop in value) {
obj[prop] = value[prop];
}
}
return format(obj, depth);
}
}
if (typeof value == 'object') {
return formatObject(value, depth);
}
if (typeof value == 'string') {
return formatString(value, depth);
}
return JSON.stringify(value);
}
function formatFunction(f, depth = 0) {
if (depth) {
return 'function ' + f.name;
}
f = f.toString();
f = f.split('\n');
if (f.length > 10) {
f = f.slice(0, 10).join('\n') + '\n...';
}
return f;
}
function formatArray(value, depth = 0) {
if (depth > 2) return '[...]';
let limit = depth == 1 ? 3 : 10;
if (value.length > limit) {
value = value.slice(0, limit);
value.push('...');
}
return '[' + value.map(v => format(v, depth + 1)).join(', ') + ']';
}
function formatString(value, depth = 0) {
let limit = depth == 1 ? 20 : 60;
if (value.length > limit) {
value = value.slice(0, value.limit - 1) + '…';
}
return JSON.stringify(value);
// was: `"${value}"`;
// but this doesn't excape special chars, such as \n
}
function formatObject(value, depth = 0) {
let name = value.constructor.name;
if (name == 'Object' && value[Symbol.toStringTag]) {
name = value[Symbol.toStringTag];
}
if (name != 'Object' && value.toString != Object.prototype.toString) {
return value.toString();
}
let result = '';
if (name != 'Object') {
result += name + ' ';
}
result += '{';
if (depth > 1) {
result += '...';
} else {
let items = [];
for(let prop in value) {
if (!value.hasOwnProperty(prop)) continue;
items.push(`${prop}: ${format(value[prop], depth + 1)}`);
}
result += items.join(', ');
}
result += '}';
return result;
}