Skip to content

Commit ba8a424

Browse files
committedSep 11, 2018
move to XO (closes #397)
1 parent 2d2509e commit ba8a424

13 files changed

+766
-613
lines changed
 

‎examples/node/app.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1+
const http = require('http');
12

2-
var debug = require('../../')('http')
3-
, http = require('http')
4-
, name = 'My App';
3+
const debug = require('../..')('http');
54

6-
// fake app
5+
const name = 'My App';
6+
7+
// Fake app
78

89
debug('booting %o', name);
910

10-
http.createServer(function(req, res){
11-
debug(req.method + ' ' + req.url);
12-
res.end('hello\n');
13-
}).listen(3000, function(){
14-
debug('listening');
11+
http.createServer((req, res) => {
12+
debug(req.method + ' ' + req.url);
13+
res.end('hello\n');
14+
}).listen(3000, () => {
15+
debug('listening');
1516
});
1617

17-
// fake worker of some kind
18-
18+
// Fake worker of some kind
19+
// eslint-disable-next-line import/no-unassigned-import
1920
require('./worker');

‎examples/node/colors.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
var debug = require('../../')
1+
const debug = require('../..');
22

3-
debug.enable('*')
3+
debug.enable('*');
44

5-
for (var i=0; i < debug.colors.length; i++) {
6-
const d = debug('example:' + i);
7-
d('The color is %o', d.color);
5+
for (let i = 0; i < debug.colors.length; i++) {
6+
const d = debug('example:' + i);
7+
d('The color is %o', d.color);
88
}

‎examples/node/stdout.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
var debug = require('../../');
2-
var error = debug('app:error');
1+
const debug = require('../..');
32

4-
// by default stderr is used
3+
const error = debug('app:error');
4+
5+
// By default stderr is used
56
error('goes to stderr!');
67

7-
var log = debug('app:log');
8-
// set this namespace to log via console.log
9-
log.log = console.log.bind(console); // don't forget to bind to console!
8+
const log = debug('app:log');
9+
// Set this namespace to log via console.log
10+
log.log = console.log.bind(console); // Don't forget to bind to console!
1011
log('goes to stdout');
1112
error('still goes to stderr!');
1213

13-
// set all output to go via console.info
14+
// Set all output to go via console.info
1415
// overrides all per-namespace log settings
1516
debug.log = console.info.bind(console);
1617
error('now goes to stdout via console.info');

‎examples/node/wildcards.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

2-
var debug = {
3-
foo: require('../../')('test:foo'),
4-
bar: require('../../')('test:bar'),
5-
baz: require('../../')('test:baz')
2+
const debug = {
3+
foo: require('../..')('test:foo'),
4+
bar: require('../..')('test:bar'),
5+
baz: require('../..')('test:baz')
66
};
77

8-
debug.foo('foo')
9-
debug.bar('bar')
10-
debug.baz('baz')
8+
debug.foo('foo');
9+
debug.bar('bar');
10+
debug.baz('baz');

‎examples/node/worker.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@
44
// DEBUG=worker:a node example/worker
55
// DEBUG=worker:b node example/worker
66

7-
var a = require('../../')('worker:a')
8-
, b = require('../../')('worker:b');
7+
const a = require('../..')('worker:a');
8+
9+
const b = require('../..')('worker:b');
910

1011
function work() {
11-
a('doing lots of uninteresting work');
12-
setTimeout(work, Math.random() * 1000);
12+
a('doing lots of uninteresting work');
13+
setTimeout(work, Math.random() * 1000);
1314
}
1415

1516
work();
1617

1718
function workb() {
18-
b('doing some work');
19-
setTimeout(workb, Math.random() * 2000);
19+
b('doing some work');
20+
setTimeout(workb, Math.random() * 2000);
2021
}
2122

2223
workb();
2324

24-
setTimeout(function(){
25-
b(new Error('fail'));
25+
setTimeout(() => {
26+
b(new Error('fail'));
2627
}, 5000);

‎karma.conf.js

+57-67
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,60 @@
11
// Karma configuration
22
// Generated on Fri Dec 16 2016 13:09:51 GMT+0000 (UTC)
33

4-
module.exports = function(config) {
5-
config.set({
6-
7-
// base path that will be used to resolve all patterns (eg. files, exclude)
8-
basePath: '',
9-
10-
11-
// frameworks to use
12-
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
13-
frameworks: ['mocha', 'chai', 'sinon'],
14-
15-
16-
// list of files / patterns to load in the browser
17-
files: [
18-
'dist/debug.js',
19-
'test/*spec.js'
20-
],
21-
22-
23-
// list of files to exclude
24-
exclude: [
25-
'src/node.js'
26-
],
27-
28-
29-
// preprocess matching files before serving them to the browser
30-
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
31-
preprocessors: {
32-
},
33-
34-
// test results reporter to use
35-
// possible values: 'dots', 'progress'
36-
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
37-
reporters: ['progress'],
38-
39-
40-
// web server port
41-
port: 9876,
42-
43-
44-
// enable / disable colors in the output (reporters and logs)
45-
colors: true,
46-
47-
48-
// level of logging
49-
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
50-
logLevel: config.LOG_INFO,
51-
52-
53-
// enable / disable watching file and executing tests whenever any file changes
54-
autoWatch: true,
55-
56-
57-
// start these browsers
58-
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
59-
browsers: ['PhantomJS'],
60-
61-
62-
// Continuous Integration mode
63-
// if true, Karma captures browsers, runs the tests and exits
64-
singleRun: false,
65-
66-
// Concurrency level
67-
// how many browser should be started simultaneous
68-
concurrency: Infinity
69-
})
70-
}
4+
module.exports = function (config) {
5+
config.set({
6+
7+
// Base path that will be used to resolve all patterns (eg. files, exclude)
8+
basePath: '',
9+
10+
// Frameworks to use
11+
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
12+
frameworks: ['mocha', 'chai', 'sinon'],
13+
14+
// List of files / patterns to load in the browser
15+
files: [
16+
'dist/debug.js',
17+
'test/*spec.js'
18+
],
19+
20+
// List of files to exclude
21+
exclude: [
22+
'src/node.js'
23+
],
24+
25+
// Preprocess matching files before serving them to the browser
26+
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
27+
preprocessors: {
28+
},
29+
30+
// Test results reporter to use
31+
// possible values: 'dots', 'progress'
32+
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
33+
reporters: ['progress'],
34+
35+
// Web server port
36+
port: 9876,
37+
38+
// Enable / disable colors in the output (reporters and logs)
39+
colors: true,
40+
41+
// Level of logging
42+
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
43+
logLevel: config.LOG_INFO,
44+
45+
// Enable / disable watching file and executing tests whenever any file changes
46+
autoWatch: true,
47+
48+
// Start these browsers
49+
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
50+
browsers: ['PhantomJS'],
51+
52+
// Continuous Integration mode
53+
// if true, Karma captures browsers, runs the tests and exits
54+
singleRun: false,
55+
56+
// Concurrency level
57+
// how many browser should be started simultaneous
58+
concurrency: Infinity
59+
});
60+
};

‎package.json

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
"url": "git://github.com/visionmedia/debug.git"
77
},
88
"description": "small debugging utility",
9+
"scripts": {
10+
"test": "xo && mocha"
11+
},
912
"keywords": [
1013
"debug",
1114
"log",

‎src/browser.js

+176-100
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-env browser */
2+
13
/**
24
* This is the web browser implementation of `debug()`.
35
*/
@@ -14,17 +16,82 @@ exports.storage = localstorage();
1416
*/
1517

1618
exports.colors = [
17-
'#0000CC', '#0000FF', '#0033CC', '#0033FF', '#0066CC', '#0066FF', '#0099CC',
18-
'#0099FF', '#00CC00', '#00CC33', '#00CC66', '#00CC99', '#00CCCC', '#00CCFF',
19-
'#3300CC', '#3300FF', '#3333CC', '#3333FF', '#3366CC', '#3366FF', '#3399CC',
20-
'#3399FF', '#33CC00', '#33CC33', '#33CC66', '#33CC99', '#33CCCC', '#33CCFF',
21-
'#6600CC', '#6600FF', '#6633CC', '#6633FF', '#66CC00', '#66CC33', '#9900CC',
22-
'#9900FF', '#9933CC', '#9933FF', '#99CC00', '#99CC33', '#CC0000', '#CC0033',
23-
'#CC0066', '#CC0099', '#CC00CC', '#CC00FF', '#CC3300', '#CC3333', '#CC3366',
24-
'#CC3399', '#CC33CC', '#CC33FF', '#CC6600', '#CC6633', '#CC9900', '#CC9933',
25-
'#CCCC00', '#CCCC33', '#FF0000', '#FF0033', '#FF0066', '#FF0099', '#FF00CC',
26-
'#FF00FF', '#FF3300', '#FF3333', '#FF3366', '#FF3399', '#FF33CC', '#FF33FF',
27-
'#FF6600', '#FF6633', '#FF9900', '#FF9933', '#FFCC00', '#FFCC33'
19+
'#0000CC',
20+
'#0000FF',
21+
'#0033CC',
22+
'#0033FF',
23+
'#0066CC',
24+
'#0066FF',
25+
'#0099CC',
26+
'#0099FF',
27+
'#00CC00',
28+
'#00CC33',
29+
'#00CC66',
30+
'#00CC99',
31+
'#00CCCC',
32+
'#00CCFF',
33+
'#3300CC',
34+
'#3300FF',
35+
'#3333CC',
36+
'#3333FF',
37+
'#3366CC',
38+
'#3366FF',
39+
'#3399CC',
40+
'#3399FF',
41+
'#33CC00',
42+
'#33CC33',
43+
'#33CC66',
44+
'#33CC99',
45+
'#33CCCC',
46+
'#33CCFF',
47+
'#6600CC',
48+
'#6600FF',
49+
'#6633CC',
50+
'#6633FF',
51+
'#66CC00',
52+
'#66CC33',
53+
'#9900CC',
54+
'#9900FF',
55+
'#9933CC',
56+
'#9933FF',
57+
'#99CC00',
58+
'#99CC33',
59+
'#CC0000',
60+
'#CC0033',
61+
'#CC0066',
62+
'#CC0099',
63+
'#CC00CC',
64+
'#CC00FF',
65+
'#CC3300',
66+
'#CC3333',
67+
'#CC3366',
68+
'#CC3399',
69+
'#CC33CC',
70+
'#CC33FF',
71+
'#CC6600',
72+
'#CC6633',
73+
'#CC9900',
74+
'#CC9933',
75+
'#CCCC00',
76+
'#CCCC33',
77+
'#FF0000',
78+
'#FF0033',
79+
'#FF0066',
80+
'#FF0099',
81+
'#FF00CC',
82+
'#FF00FF',
83+
'#FF3300',
84+
'#FF3333',
85+
'#FF3366',
86+
'#FF3399',
87+
'#FF33CC',
88+
'#FF33FF',
89+
'#FF6600',
90+
'#FF6633',
91+
'#FF9900',
92+
'#FF9933',
93+
'#FFCC00',
94+
'#FFCC33'
2895
];
2996

3097
/**
@@ -35,29 +102,30 @@ exports.colors = [
35102
* TODO: add a `localStorage` variable to explicitly enable/disable colors
36103
*/
37104

105+
// eslint-disable-next-line complexity
38106
function useColors() {
39-
// NB: In an Electron preload script, document will be defined but not fully
40-
// initialized. Since we know we're in Chrome, we'll just detect this case
41-
// explicitly
42-
if (typeof window !== 'undefined' && window.process && (window.process.type === 'renderer' || window.process.__nwjs)) {
43-
return true;
44-
}
45-
46-
// Internet Explorer and Edge do not support colors.
47-
if (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/)) {
48-
return false;
49-
}
50-
51-
// is webkit? http://stackoverflow.com/a/16459606/376773
52-
// document is undefined in react-native: https://github.com/facebook/react-native/pull/1632
53-
return (typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance) ||
54-
// is firebug? http://stackoverflow.com/a/398120/376773
55-
(typeof window !== 'undefined' && window.console && (window.console.firebug || (window.console.exception && window.console.table))) ||
56-
// is firefox >= v31?
57-
// https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
58-
(typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31) ||
59-
// double check webkit in userAgent just in case we are in a worker
60-
(typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/));
107+
// NB: In an Electron preload script, document will be defined but not fully
108+
// initialized. Since we know we're in Chrome, we'll just detect this case
109+
// explicitly
110+
if (typeof window !== 'undefined' && window.process && (window.process.type === 'renderer' || window.process.__nwjs)) {
111+
return true;
112+
}
113+
114+
// Internet Explorer and Edge do not support colors.
115+
if (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/)) {
116+
return false;
117+
}
118+
119+
// Is webkit? http://stackoverflow.com/a/16459606/376773
120+
// document is undefined in react-native: https://github.com/facebook/react-native/pull/1632
121+
return (typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance) ||
122+
// Is firebug? http://stackoverflow.com/a/398120/376773
123+
(typeof window !== 'undefined' && window.console && (window.console.firebug || (window.console.exception && window.console.table))) ||
124+
// Is firefox >= v31?
125+
// https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
126+
(typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31) ||
127+
// Double check webkit in userAgent just in case we are in a worker
128+
(typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/));
61129
}
62130

63131
/**
@@ -67,36 +135,38 @@ function useColors() {
67135
*/
68136

69137
function formatArgs(args) {
70-
var useColors = this.useColors;
71-
72-
args[0] = (useColors ? '%c' : '')
73-
+ this.namespace
74-
+ (useColors ? ' %c' : ' ')
75-
+ args[0]
76-
+ (useColors ? '%c ' : ' ')
77-
+ '+' + module.exports.humanize(this.diff);
78-
79-
if (!useColors) return;
80-
81-
var c = 'color: ' + this.color;
82-
args.splice(1, 0, c, 'color: inherit')
83-
84-
// the final "%c" is somewhat tricky, because there could be other
85-
// arguments passed either before or after the %c, so we need to
86-
// figure out the correct index to insert the CSS into
87-
var index = 0;
88-
var lastC = 0;
89-
args[0].replace(/%[a-zA-Z%]/g, function(match) {
90-
if ('%%' === match) return;
91-
index++;
92-
if ('%c' === match) {
93-
// we only are interested in the *last* %c
94-
// (the user may have provided their own)
95-
lastC = index;
96-
}
97-
});
98-
99-
args.splice(lastC, 0, c);
138+
args[0] = (this.useColors ? '%c' : '') +
139+
this.namespace +
140+
(this.useColors ? ' %c' : ' ') +
141+
args[0] +
142+
(this.useColors ? '%c ' : ' ') +
143+
'+' + module.exports.humanize(this.diff);
144+
145+
if (!this.useColors) {
146+
return;
147+
}
148+
149+
const c = 'color: ' + this.color;
150+
args.splice(1, 0, c, 'color: inherit');
151+
152+
// The final "%c" is somewhat tricky, because there could be other
153+
// arguments passed either before or after the %c, so we need to
154+
// figure out the correct index to insert the CSS into
155+
let index = 0;
156+
let lastC = 0;
157+
args[0].replace(/%[a-zA-Z%]/g, match => {
158+
if (match === '%%') {
159+
return;
160+
}
161+
index++;
162+
if (match === '%c') {
163+
// We only are interested in the *last* %c
164+
// (the user may have provided their own)
165+
lastC = index;
166+
}
167+
});
168+
169+
args.splice(lastC, 0, c);
100170
}
101171

102172
/**
@@ -105,13 +175,12 @@ function formatArgs(args) {
105175
*
106176
* @api public
107177
*/
108-
109-
function log() {
110-
// this hackery is required for IE8/9, where
111-
// the `console.log` function doesn't have 'apply'
112-
return 'object' === typeof console
113-
&& console.log
114-
&& Function.prototype.apply.call(console.log, console, arguments);
178+
function log(...args) {
179+
// This hackery is required for IE8/9, where
180+
// the `console.log` function doesn't have 'apply'
181+
return typeof console === 'object' &&
182+
console.log &&
183+
console.log(...args);
115184
}
116185

117186
/**
@@ -120,15 +189,17 @@ function log() {
120189
* @param {String} namespaces
121190
* @api private
122191
*/
123-
124192
function save(namespaces) {
125-
try {
126-
if (null == namespaces) {
127-
exports.storage.removeItem('debug');
128-
} else {
129-
exports.storage.setItem('debug', namespaces);
130-
}
131-
} catch(e) {}
193+
try {
194+
if (namespaces) {
195+
exports.storage.setItem('debug', namespaces);
196+
} else {
197+
exports.storage.removeItem('debug');
198+
}
199+
} catch (error) {
200+
// Swallow
201+
// XXX (@Qix-) should we be logging these?
202+
}
132203
}
133204

134205
/**
@@ -137,19 +208,21 @@ function save(namespaces) {
137208
* @return {String} returns the previously persisted debug modes
138209
* @api private
139210
*/
140-
141211
function load() {
142-
var r;
143-
try {
144-
r = exports.storage.getItem('debug');
145-
} catch(e) {}
212+
let r;
213+
try {
214+
r = exports.storage.getItem('debug');
215+
} catch (error) {
216+
// Swallow
217+
// XXX (@Qix-) should we be logging these?
218+
}
146219

147-
// If debug isn't set in LS, and we're in Electron, try to load $DEBUG
148-
if (!r && typeof process !== 'undefined' && 'env' in process) {
149-
r = process.env.DEBUG;
150-
}
220+
// If debug isn't set in LS, and we're in Electron, try to load $DEBUG
221+
if (!r && typeof process !== 'undefined' && 'env' in process) {
222+
r = process.env.DEBUG;
223+
}
151224

152-
return r;
225+
return r;
153226
}
154227

155228
/**
@@ -164,25 +237,28 @@ function load() {
164237
*/
165238

166239
function localstorage() {
167-
try {
168-
// TVMLKit (Apple TV JS Runtime) does not have a window object, just localStorage in the global context
169-
// The Browser also has localStorage in the global context.
170-
return localStorage;
171-
} catch (e) {}
240+
try {
241+
// TVMLKit (Apple TV JS Runtime) does not have a window object, just localStorage in the global context
242+
// The Browser also has localStorage in the global context.
243+
return localStorage;
244+
} catch (error) {
245+
// Swallow
246+
// XXX (@Qix-) should we be logging these?
247+
}
172248
}
173249

174250
module.exports = require('./common')(exports);
175251

176-
var formatters = module.exports.formatters;
252+
const {formatters} = module.exports;
177253

178254
/**
179255
* Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
180256
*/
181257

182-
formatters.j = function(v) {
183-
try {
184-
return JSON.stringify(v);
185-
} catch (err) {
186-
return '[UnexpectedJSONParseError]: ' + err.message;
187-
}
258+
formatters.j = function (v) {
259+
try {
260+
return JSON.stringify(v);
261+
} catch (error) {
262+
return '[UnexpectedJSONParseError]: ' + error.message;
263+
}
188264
};

‎src/common.js

+238-233
Large diffs are not rendered by default.

‎src/index.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
* treat as a browser.
44
*/
55

6-
76
if (typeof process === 'undefined' || process.type === 'renderer' || process.browser === true || process.__nwjs) {
8-
module.exports = require('./browser.js');
7+
module.exports = require('./browser.js');
98
} else {
10-
module.exports = require('./node.js');
9+
module.exports = require('./node.js');
1110
}

‎src/node.js

+157-79
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* Module dependencies.
33
*/
44

5-
var tty = require('tty');
6-
var util = require('util');
5+
const tty = require('tty');
6+
const util = require('util');
77

88
/**
99
* This is the Node.js implementation of `debug()`.
@@ -20,21 +20,95 @@ exports.useColors = useColors;
2020
* Colors.
2121
*/
2222

23-
exports.colors = [ 6, 2, 3, 4, 5, 1 ];
23+
exports.colors = [6, 2, 3, 4, 5, 1];
2424

2525
try {
26-
var supportsColor = require('supports-color');
27-
if (supportsColor && (supportsColor.stderr || supportsColor).level >= 2) {
28-
exports.colors = [
29-
20, 21, 26, 27, 32, 33, 38, 39, 40, 41, 42, 43, 44, 45, 56, 57, 62, 63, 68,
30-
69, 74, 75, 76, 77, 78, 79, 80, 81, 92, 93, 98, 99, 112, 113, 128, 129, 134,
31-
135, 148, 149, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171,
32-
172, 173, 178, 179, 184, 185, 196, 197, 198, 199, 200, 201, 202, 203, 204,
33-
205, 206, 207, 208, 209, 214, 215, 220, 221
34-
];
35-
}
36-
} catch (err) {
37-
// swallow - we only care if `supports-color` is available; it doesn't have to be.
26+
// Optional dependency (as in, doesn't need to be installed, NOT like optionalDependencies in package.json)
27+
// eslint-disable-next-line import/no-extraneous-dependencies
28+
const supportsColor = require('supports-color');
29+
30+
if (supportsColor && (supportsColor.stderr || supportsColor).level >= 2) {
31+
exports.colors = [
32+
20,
33+
21,
34+
26,
35+
27,
36+
32,
37+
33,
38+
38,
39+
39,
40+
40,
41+
41,
42+
42,
43+
43,
44+
44,
45+
45,
46+
56,
47+
57,
48+
62,
49+
63,
50+
68,
51+
69,
52+
74,
53+
75,
54+
76,
55+
77,
56+
78,
57+
79,
58+
80,
59+
81,
60+
92,
61+
93,
62+
98,
63+
99,
64+
112,
65+
113,
66+
128,
67+
129,
68+
134,
69+
135,
70+
148,
71+
149,
72+
160,
73+
161,
74+
162,
75+
163,
76+
164,
77+
165,
78+
166,
79+
167,
80+
168,
81+
169,
82+
170,
83+
171,
84+
172,
85+
173,
86+
178,
87+
179,
88+
184,
89+
185,
90+
196,
91+
197,
92+
198,
93+
199,
94+
200,
95+
201,
96+
202,
97+
203,
98+
204,
99+
205,
100+
206,
101+
207,
102+
208,
103+
209,
104+
214,
105+
215,
106+
220,
107+
221
108+
];
109+
}
110+
} catch (error) {
111+
// Swallow - we only care if `supports-color` is available; it doesn't have to be.
38112
}
39113

40114
/**
@@ -43,34 +117,41 @@ try {
43117
* $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js
44118
*/
45119

46-
exports.inspectOpts = Object.keys(process.env).filter(function (key) {
47-
return /^debug_/i.test(key);
48-
}).reduce(function (obj, key) {
49-
// camel-case
50-
var prop = key
51-
.substring(6)
52-
.toLowerCase()
53-
.replace(/_([a-z])/g, function (_, k) { return k.toUpperCase() });
54-
55-
// coerce string value into JS value
56-
var val = process.env[key];
57-
if (/^(yes|on|true|enabled)$/i.test(val)) val = true;
58-
else if (/^(no|off|false|disabled)$/i.test(val)) val = false;
59-
else if (val === 'null') val = null;
60-
else val = Number(val);
61-
62-
obj[prop] = val;
63-
return obj;
120+
exports.inspectOpts = Object.keys(process.env).filter(key => {
121+
return /^debug_/i.test(key);
122+
}).reduce((obj, key) => {
123+
// Camel-case
124+
const prop = key
125+
.substring(6)
126+
.toLowerCase()
127+
.replace(/_([a-z])/g, (_, k) => {
128+
return k.toUpperCase();
129+
});
130+
131+
// Coerce string value into JS value
132+
let val = process.env[key];
133+
if (/^(yes|on|true|enabled)$/i.test(val)) {
134+
val = true;
135+
} else if (/^(no|off|false|disabled)$/i.test(val)) {
136+
val = false;
137+
} else if (val === 'null') {
138+
val = null;
139+
} else {
140+
val = Number(val);
141+
}
142+
143+
obj[prop] = val;
144+
return obj;
64145
}, {});
65146

66147
/**
67148
* Is stdout a TTY? Colored output is enabled when `true`.
68149
*/
69150

70151
function useColors() {
71-
return 'colors' in exports.inspectOpts
72-
? Boolean(exports.inspectOpts.colors)
73-
: tty.isatty(process.stderr.fd);
152+
return 'colors' in exports.inspectOpts ?
153+
Boolean(exports.inspectOpts.colors) :
154+
tty.isatty(process.stderr.fd);
74155
}
75156

76157
/**
@@ -80,35 +161,33 @@ function useColors() {
80161
*/
81162

82163
function formatArgs(args) {
83-
var name = this.namespace;
84-
var useColors = this.useColors;
85-
86-
if (useColors) {
87-
var c = this.color;
88-
var colorCode = '\u001b[3' + (c < 8 ? c : '8;5;' + c);
89-
var prefix = ' ' + colorCode + ';1m' + name + ' ' + '\u001b[0m';
90-
91-
args[0] = prefix + args[0].split('\n').join('\n' + prefix);
92-
args.push(colorCode + 'm+' + module.exports.humanize(this.diff) + '\u001b[0m');
93-
} else {
94-
args[0] = getDate() + name + ' ' + args[0];
95-
}
164+
const {namespace: name, useColors} = this;
165+
166+
if (useColors) {
167+
const c = this.color;
168+
const colorCode = '\u001B[3' + (c < 8 ? c : '8;5;' + c);
169+
const prefix = ` ${colorCode};1m${name} \u001B[0m`;
170+
171+
args[0] = prefix + args[0].split('\n').join('\n' + prefix);
172+
args.push(colorCode + 'm+' + module.exports.humanize(this.diff) + '\u001B[0m');
173+
} else {
174+
args[0] = getDate() + name + ' ' + args[0];
175+
}
96176
}
97177

98178
function getDate() {
99-
if (exports.inspectOpts.hideDate) {
100-
return '';
101-
} else {
102-
return new Date().toISOString() + ' ';
103-
}
179+
if (exports.inspectOpts.hideDate) {
180+
return '';
181+
}
182+
return new Date().toISOString() + ' ';
104183
}
105184

106185
/**
107186
* Invokes `util.format()` with the specified arguments and writes to stderr.
108187
*/
109188

110-
function log() {
111-
return process.stderr.write(util.format.apply(util, arguments) + '\n');
189+
function log(...args) {
190+
return process.stderr.write(util.format(...args) + '\n');
112191
}
113192

114193
/**
@@ -117,15 +196,14 @@ function log() {
117196
* @param {String} namespaces
118197
* @api private
119198
*/
120-
121199
function save(namespaces) {
122-
if (null == namespaces) {
123-
// If you set a process.env field to null or undefined, it gets cast to the
124-
// string 'null' or 'undefined'. Just delete instead.
125-
delete process.env.DEBUG;
126-
} else {
127-
process.env.DEBUG = namespaces;
128-
}
200+
if (namespaces) {
201+
process.env.DEBUG = namespaces;
202+
} else {
203+
// If you set a process.env field to null or undefined, it gets cast to the
204+
// string 'null' or 'undefined'. Just delete instead.
205+
delete process.env.DEBUG;
206+
}
129207
}
130208

131209
/**
@@ -136,7 +214,7 @@ function save(namespaces) {
136214
*/
137215

138216
function load() {
139-
return process.env.DEBUG;
217+
return process.env.DEBUG;
140218
}
141219

142220
/**
@@ -146,34 +224,34 @@ function load() {
146224
* differently for a particular `debug` instance.
147225
*/
148226

149-
function init (debug) {
150-
debug.inspectOpts = {};
227+
function init(debug) {
228+
debug.inspectOpts = {};
151229

152-
var keys = Object.keys(exports.inspectOpts);
153-
for (var i = 0; i < keys.length; i++) {
154-
debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]];
155-
}
230+
const keys = Object.keys(exports.inspectOpts);
231+
for (let i = 0; i < keys.length; i++) {
232+
debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]];
233+
}
156234
}
157235

158236
module.exports = require('./common')(exports);
159237

160-
var formatters = module.exports.formatters;
238+
const {formatters} = module.exports;
161239

162240
/**
163241
* Map %o to `util.inspect()`, all on a single line.
164242
*/
165243

166-
formatters.o = function(v) {
167-
this.inspectOpts.colors = this.useColors;
168-
return util.inspect(v, this.inspectOpts)
169-
.replace(/\s*\n\s*/g, ' ');
244+
formatters.o = function (v) {
245+
this.inspectOpts.colors = this.useColors;
246+
return util.inspect(v, this.inspectOpts)
247+
.replace(/\s*\n\s*/g, ' ');
170248
};
171249

172250
/**
173251
* Map %O to `util.inspect()`, allowing multiple lines if needed.
174252
*/
175253

176-
formatters.O = function(v) {
177-
this.inspectOpts.colors = this.useColors;
178-
return util.inspect(v, this.inspectOpts);
254+
formatters.O = function (v) {
255+
this.inspectOpts.colors = this.useColors;
256+
return util.inspect(v, this.inspectOpts);
179257
};

‎test/debug-spec.js

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* global describe, it, context, beforeEach */
2+
'use strict';
3+
4+
let chai;
5+
6+
let expect;
7+
8+
let debug;
9+
10+
let sinon;
11+
12+
let sinonChai;
13+
14+
if (typeof module !== 'undefined') {
15+
chai = require('chai');
16+
expect = chai.expect;
17+
18+
debug = require('../src');
19+
sinon = require('sinon');
20+
sinonChai = require('sinon-chai');
21+
chai.use(sinonChai);
22+
}
23+
24+
describe('debug', () => {
25+
let log = debug('test');
26+
27+
log.log = sinon.stub();
28+
29+
it('passes a basic sanity check', () => {
30+
expect(log('hello world')).to.not.throw();
31+
});
32+
33+
it('allows namespaces to be a non-string value', () => {
34+
expect(debug.enable(true)).to.not.throw();
35+
});
36+
37+
context('with log function', () => {
38+
beforeEach(() => {
39+
debug.enable('test');
40+
log = debug('test');
41+
});
42+
43+
it('uses it', () => {
44+
log.log = sinon.stub();
45+
log('using custom log function');
46+
47+
expect(log.log).to.have.been.calledOnce();
48+
});
49+
});
50+
51+
describe('custom functions', () => {
52+
let log;
53+
54+
beforeEach(() => {
55+
debug.enable('test');
56+
log = debug('test');
57+
});
58+
59+
context('with log function', () => {
60+
it('uses it', () => {
61+
log.log = sinon.spy();
62+
log('using custom log function');
63+
64+
expect(log.log).to.have.been.calledOnce();
65+
});
66+
});
67+
});
68+
69+
describe('extend namespace', () => {
70+
let log;
71+
72+
beforeEach(() => {
73+
debug.enable('foo');
74+
log = debug('foo');
75+
});
76+
77+
it('should extend namespace', () => {
78+
const logBar = log.extend('bar');
79+
expect(logBar.namespace).to.be.equal('foo:bar');
80+
});
81+
82+
it('should extend namespace with custom delimiter', () => {
83+
const logBar = log.extend('bar', '--');
84+
expect(logBar.namespace).to.be.equal('foo--bar');
85+
});
86+
87+
it('should extend namespace with empty delimiter', () => {
88+
const logBar = log.extend('bar', '');
89+
expect(logBar.namespace).to.be.equal('foobar');
90+
});
91+
});
92+
});

‎test/debug_spec.js

-93
This file was deleted.

3 commit comments

Comments
 (3)

njugray commented on Sep 11, 2018

@njugray

var --> const, let and no 'use strict' provided, cause SyntaxError:

Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

debug no more support node 4 and lower ? I think this is a break change for some old project

mlem commented on Sep 11, 2018

@mlem

this change breaks our build.
don't use the let keyword!

Qix- commented on Sep 11, 2018

@Qix-
MemberAuthor

@mlem Please work to upgrade your version of Node. let is standard javascript now.

Locking this. Please continue in #603.

This conversation has been locked and limited to collaborators.