-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.js
92 lines (81 loc) · 2.29 KB
/
debug.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
(function(root, factory) {
if (typeof module === 'object' && module.exports) {
// Node
module.exports = factory();
} else if (typeof define === 'function' && define.amd) {
// AMD
if (root.anchor && root.anchor.debug) {
// The debug module was loaded via a script tag. Define the module such
// that it is the same as the browser global.
define(function() {
return root.anchor.debug;
});
} else {
define(factory);
}
} else {
// Browser global
//
// The debug module is often loaded via a script tag, so that debug settings
// can be configured prior to module loading.
root.anchor = root.anchor || {};
root.anchor.debug = factory();
}
}(this, function() {
function debug(name) {
if (!enabled(name)) return function(){};
return function(fmt) {
var curr = new Date;
var ms = curr - (prev[name] || curr);
prev[name] = curr;
fmt = '['
+ name
+ '] '
+ fmt
+ ' +' + humanize(ms);
// This hackery is required for IE8
// where `console.log` doesn't have 'apply'
window.console
&& console.log
&& Function.prototype.apply.call(console.log, console, arguments);
}
}
debug.enable = function(name) {
var split = (name || '').split(/[\s,]+/)
, len = split.length;
for (var i = 0; i < len; i++) {
name = split[i].replace('*', '.*?');
if (name[0] === '-') {
skips.push(new RegExp('^' + name.substr(1) + '$'));
} else {
names.push(new RegExp('^' + name + '$'));
}
}
}
var names = [];
var skips = [];
var prev = [];
function enabled(name) {
for (var i = 0, len = skips.length; i < len; i++) {
if (skips[i].test(name)) {
return false;
}
}
for (var i = 0, len = names.length; i < len; i++) {
if (names[i].test(name)) {
return true;
}
}
return false;
}
function humanize(ms) {
var sec = 1000
, min = 60000 // 60 * 1000 = 1 min
, hour = 3600000; // 60 * 60 * 1000 = 1 hour
if (ms >= hour) return (ms / hour).toFixed(1) + 'h';
if (ms >= min) return (ms / min).toFixed(1) + 'm';
if (ms >= sec) return (ms / sec | 0) + 's';
return ms + 'ms';
}
return debug;
}));