-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclog.js
145 lines (124 loc) · 4.27 KB
/
clog.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
// CLog.js 1.1.0
// https://github.com/CVarisco/CLog.js
// http://www.christianvarisco.com
// Copyright (c) 2016 Christian Varisco
(function(w) {
// Creates the CLog object function
var CLog = function(options) {
// On input it assigns a custom color, otherwise it sets the default color
this.options = {
group: options.group || null,
colors: options.colors || null
};
this.options.colors.default = "#000000";
};
// Returns an array where argument[0] is always a color.
// Color is set based on input (id or hex) or is set to default (black)
var _normalizeArguments = function(args) {
// Get colors from options
var colors = this.options.colors;
// Init color
var color = '';
// Convert arguments into an Array
var argumentsArray = [].slice.apply(args);
// Search in options.colors or set color
if (!!colors && typeof argumentsArray[0] === "string") {
if (argumentsArray[0][0] === "#") {
color = argumentsArray[0];
} else {
for (var c in colors) {
if (argumentsArray[0] === c) {
color = colors[argumentsArray[0]];
}
}
}
}
// If color in the arguments doesn't exist, unshift the default color
if (color === '') {
argumentsArray.unshift(this.options.colors.default);
} else {
argumentsArray[0] = color;
}
return argumentsArray;
};
// Formats the output
var _createOutput = function(args) {
var output = '%c ';
var outputArgs = [];
var isGrouped = this.options.group;
for (var i = 1; i < args.length; i++) {
switch (typeof args[i]) {
case 'string':
case 'number':
case 'boolean':
output += args[i] + ' ';
break;
case 'undefined':
output += "'undefined' ";
break;
case 'object':
case 'array':
case 'function':
outputArgs.push(args[i]);
output += (isGrouped) ? '' : '(%O) ';
break;
default:
output += args[i] + ' ';
break;
}
}
// If group isn't active, slice the arguments output
if (!isGrouped) {
outputArgs.splice(0, 0, output);
outputArgs.splice(1, 0, "color:" + args[0] + ";");
}
return {
color: args[0],
message: output,
args: outputArgs
}
};
// Prints to console
var _print = function(output) {
var isGrouped = this.options.group;
// If group is active, create group in console
if (isGrouped) {
console.groupCollapsed(output.message, "color:" + output.color + ";");
output.args.forEach(function(arg) {
console.dir(arg);
});
console.groupEnd();
} else {
console.log.apply(console, output.args);
}
};
//Here's comes the Magic!
CLog.prototype.log = function() {
// Check if Browser is Chrome
if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
//Returns a normalized array (with [0] being a color)
var input = _normalizeArguments(arguments);
var output = _createOutput(input);
_print(output);
} else {
console.log.apply(console, arguments);
}
};
// Clear console if called
CLog.prototype.clear = function() {
console.clear();
};
// Compatibility for the old require() API. If we’re in the browser, add CLog as a global object.
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = CLog;
}
exports.CLog = CLog;
} else {
w.CLog = CLog;
if (typeof define === 'function' && define.amd) {
define('CLog', [], function() {
return CLog; });
}
}
}(this));