From 0ef563978d93b416f88accbf02546b0df55a6358 Mon Sep 17 00:00:00 2001 From: calvintwr Date: Fri, 2 Sep 2022 16:16:54 +0800 Subject: [PATCH 01/12] fix debug format options ignored --- package.json | 5 +++-- src/node.js | 4 ++-- test.node.js | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 test.node.js diff --git a/package.json b/package.json index 3bcdc242..22ac19d8 100644 --- a/package.json +++ b/package.json @@ -26,12 +26,13 @@ "scripts": { "lint": "xo", "test": "npm run test:node && npm run test:browser && npm run lint", - "test:node": "istanbul cover _mocha -- test.js", + "test:node": "istanbul cover _mocha -- test.js test.node.js", "test:browser": "karma start --single-run", "test:coverage": "cat ./coverage/lcov.info | coveralls" }, "dependencies": { - "ms": "2.1.2" + "ms": "2.1.2", + "sinon": "^14.0.0" }, "devDependencies": { "brfs": "^2.0.1", diff --git a/src/node.js b/src/node.js index 79bc085c..715560a4 100644 --- a/src/node.js +++ b/src/node.js @@ -187,11 +187,11 @@ function getDate() { } /** - * Invokes `util.format()` with the specified arguments and writes to stderr. + * Invokes `util.formatWithOptions()` with the specified arguments and writes to stderr. */ function log(...args) { - return process.stderr.write(util.format(...args) + '\n'); + return process.stderr.write(util.formatWithOptions(exports.inspectOpts, ...args) + '\n'); } /** diff --git a/test.node.js b/test.node.js new file mode 100644 index 00000000..4cc3c051 --- /dev/null +++ b/test.node.js @@ -0,0 +1,40 @@ +/* eslint-env mocha */ + +const assert = require('assert'); +const util = require('util'); +const sinon = require('sinon'); +const debug = require('./src/node'); + +const formatWithOptionsSpy = sinon.spy(util, 'formatWithOptions'); +beforeEach(() => { + formatWithOptionsSpy.resetHistory(); +}); + +describe('debug node', () => { + describe('formatting options', () => { + it('calls util.formatWithOptions', () => { + debug.enable('*'); + const stdErrWriteStub = sinon.stub(process.stderr, 'write'); + const log = debug('formatting options'); + log('hello world'); + assert(util.formatWithOptions.callCount === 1); + stdErrWriteStub.restore(); + }); + + it('calls util.formatWithOptions with inspectOpts', () => { + debug.enable('*'); + const options = { + hideDate: true, + colors: true, + depth: 10, + showHidden: true + }; + Object.assign(debug.inspectOpts, options); + const stdErrWriteStub = sinon.stub(process.stderr, 'write'); + const log = debug('format with inspectOpts'); + log('hello world2'); + assert.deepStrictEqual(util.formatWithOptions.getCall(0).args[0], options); + stdErrWriteStub.restore(); + }); + }); +}); From 6fd1faad269116cee0701a62bd0a7fc66ca0f3d1 Mon Sep 17 00:00:00 2001 From: calvintwr Date: Fri, 2 Sep 2022 16:18:45 +0800 Subject: [PATCH 02/12] moved sinon to devDependencies --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 22ac19d8..5da3cfeb 100644 --- a/package.json +++ b/package.json @@ -31,8 +31,7 @@ "test:coverage": "cat ./coverage/lcov.info | coveralls" }, "dependencies": { - "ms": "2.1.2", - "sinon": "^14.0.0" + "ms": "2.1.2" }, "devDependencies": { "brfs": "^2.0.1", @@ -45,6 +44,7 @@ "karma-mocha": "^1.3.0", "mocha": "^5.2.0", "mocha-lcov-reporter": "^1.2.0", + "sinon": "^14.0.0", "xo": "^0.23.0" }, "peerDependenciesMeta": { From adec948df6e956c2d27ad38d89e69e134dc260a9 Mon Sep 17 00:00:00 2001 From: calvintwr Date: Sat, 24 Sep 2022 20:30:52 +0800 Subject: [PATCH 03/12] fixed missing error properties during log --- src/node.js | 98 +++++++++++++++++++++++++++++++--------------------- test.js | 40 +++++++++++++++++++-- test.node.js | 17 ++++++++- 3 files changed, 111 insertions(+), 44 deletions(-) diff --git a/src/node.js b/src/node.js index 715560a4..7dfa3854 100644 --- a/src/node.js +++ b/src/node.js @@ -15,10 +15,9 @@ exports.formatArgs = formatArgs; exports.save = save; exports.load = load; exports.useColors = useColors; -exports.destroy = util.deprecate( - () => {}, - 'Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.' -); +exports.coerce = coerce; +exports.destroy = util.deprecate(() => {}, +'Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.'); /** * Colors. @@ -121,41 +120,43 @@ try { * $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js */ -exports.inspectOpts = Object.keys(process.env).filter(key => { - return /^debug_/i.test(key); -}).reduce((obj, key) => { - // Camel-case - const prop = key - .substring(6) - .toLowerCase() - .replace(/_([a-z])/g, (_, k) => { - return k.toUpperCase(); - }); - - // Coerce string value into JS value - let val = process.env[key]; - if (/^(yes|on|true|enabled)$/i.test(val)) { - val = true; - } else if (/^(no|off|false|disabled)$/i.test(val)) { - val = false; - } else if (val === 'null') { - val = null; - } else { - val = Number(val); - } - - obj[prop] = val; - return obj; -}, {}); +exports.inspectOpts = Object.keys(process.env) + .filter(key => { + return /^debug_/i.test(key); + }) + .reduce((obj, key) => { + // Camel-case + const prop = key + .substring(6) + .toLowerCase() + .replace(/_([a-z])/g, (_, k) => { + return k.toUpperCase(); + }); + + // Coerce string value into JS value + let val = process.env[key]; + if (/^(yes|on|true|enabled)$/i.test(val)) { + val = true; + } else if (/^(no|off|false|disabled)$/i.test(val)) { + val = false; + } else if (val === 'null') { + val = null; + } else { + val = Number(val); + } + + obj[prop] = val; + return obj; + }, {}); /** * Is stdout a TTY? Colored output is enabled when `true`. */ function useColors() { - return 'colors' in exports.inspectOpts ? - Boolean(exports.inspectOpts.colors) : - tty.isatty(process.stderr.fd); + return 'colors' in exports.inspectOpts + ? Boolean(exports.inspectOpts.colors) + : tty.isatty(process.stderr.fd); } /** @@ -165,7 +166,7 @@ function useColors() { */ function formatArgs(args) { - const {namespace: name, useColors} = this; + const { namespace: name, useColors } = this; if (useColors) { const c = this.color; @@ -173,7 +174,9 @@ function formatArgs(args) { const prefix = ` ${colorCode};1m${name} \u001B[0m`; args[0] = prefix + args[0].split('\n').join('\n' + prefix); - args.push(colorCode + 'm+' + module.exports.humanize(this.diff) + '\u001B[0m'); + args.push( + colorCode + 'm+' + module.exports.humanize(this.diff) + '\u001B[0m' + ); } else { args[0] = getDate() + name + ' ' + args[0]; } @@ -191,7 +194,9 @@ function getDate() { */ function log(...args) { - return process.stderr.write(util.formatWithOptions(exports.inspectOpts, ...args) + '\n'); + return process.stderr.write( + util.formatWithOptions(exports.inspectOpts, ...args) + '\n' + ); } /** @@ -237,17 +242,30 @@ function init(debug) { } } +/** + * Coerce `val`. In Node.JS, no values need to be coerced (errors are already being formatted for us + * by util.format, so this function just returns whatever argument it is given (identitiy function). + * + * @param {Mixed} val + * @return {Mixed} + * @api private + */ +function coerce(val) { + return val; +} + module.exports = require('./common')(exports); -const {formatters} = module.exports; +const { formatters } = module.exports; /** * Map %o to `util.inspect()`, all on a single line. */ -formatters.o = function (v) { +formatters.o = function(v) { this.inspectOpts.colors = this.useColors; - return util.inspect(v, this.inspectOpts) + return util + .inspect(v, this.inspectOpts) .split('\n') .map(str => str.trim()) .join(' '); @@ -257,7 +275,7 @@ formatters.o = function (v) { * Map %O to `util.inspect()`, allowing multiple lines if needed. */ -formatters.O = function (v) { +formatters.O = function(v) { this.inspectOpts.colors = this.useColors; return util.inspect(v, this.inspectOpts); }; diff --git a/test.js b/test.js index a1d6f633..182a89b5 100644 --- a/test.js +++ b/test.js @@ -2,6 +2,7 @@ const assert = require('assert'); const debug = require('./src'); +const sinon = require('sinon'); describe('debug', () => { it('passes a basic sanity check', () => { @@ -43,6 +44,32 @@ describe('debug', () => { assert.deepStrictEqual(messages.length, 3); }); + it('should log errors with full stack', function() { + // fake the current time to be 1970-01-01T00:00:00.000Z + sinon.useFakeTimers(new Date(0)); + + const log = debug('test'); + log.useColors = false; + log.enabled = true; + log.log = sinon.fake(); + + const fakeError = new Error('test'); + fakeError.stack = 'Error: test\n at test:1:1'; + + log(fakeError); + + // +0ms format for the browser, + // ISO8601 format for node.js + assert.ok( + log.log.calledOnceWithExactly( + 'test Error: test\n at test:1:1 +0ms' + ) || + log.log.calledOnceWithExactly( + '1970-01-01T00:00:00.000Z test Error: test\n at test:1:1' + ) + ); + }); + describe('extend namespace', () => { it('should extend namespace', () => { const log = debug('foo'); @@ -114,15 +141,22 @@ describe('debug', () => { const namespaces = debug.disable(); assert.deepStrictEqual(namespaces, 'test,abc*,-abc'); debug.enable(namespaces); - assert.deepStrictEqual(oldNames.map(String), debug.names.map(String)); - assert.deepStrictEqual(oldSkips.map(String), debug.skips.map(String)); + assert.deepStrictEqual( + oldNames.map(String), + debug.names.map(String) + ); + assert.deepStrictEqual( + oldSkips.map(String), + debug.skips.map(String) + ); }); it('handles re-enabling existing instances', () => { debug.disable('*'); const inst = debug('foo'); const messages = []; - inst.log = msg => messages.push(msg.replace(/^[^@]*@([^@]+)@.*$/, '$1')); + inst.log = msg => + messages.push(msg.replace(/^[^@]*@([^@]+)@.*$/, '$1')); inst('@test@'); assert.deepStrictEqual(messages, []); diff --git a/test.node.js b/test.node.js index 4cc3c051..022637a8 100644 --- a/test.node.js +++ b/test.node.js @@ -11,6 +11,18 @@ beforeEach(() => { }); describe('debug node', () => { + it('should log errors with properties', () => { + const log = debug('formatting options'); + log.useColors = false; + log.enabled = true; + log.log = sinon.fake(); + const error = Error('hello'); + error.customProp = 'foo'; + log(error); + // console.log(log.log.args); + assert.ok(log.log.calledWith(sinon.match(/customProp: 'foo'/))); + }); + describe('formatting options', () => { it('calls util.formatWithOptions', () => { debug.enable('*'); @@ -33,7 +45,10 @@ describe('debug node', () => { const stdErrWriteStub = sinon.stub(process.stderr, 'write'); const log = debug('format with inspectOpts'); log('hello world2'); - assert.deepStrictEqual(util.formatWithOptions.getCall(0).args[0], options); + assert.deepStrictEqual( + util.formatWithOptions.getCall(0).args[0], + options + ); stdErrWriteStub.restore(); }); }); From f0faffe3217b7a789acb5932f36bb729effa4dbc Mon Sep 17 00:00:00 2001 From: calvintwr Date: Sat, 24 Sep 2022 20:33:48 +0800 Subject: [PATCH 04/12] remove console.log --- test.node.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test.node.js b/test.node.js index 022637a8..7b400493 100644 --- a/test.node.js +++ b/test.node.js @@ -19,7 +19,6 @@ describe('debug node', () => { const error = Error('hello'); error.customProp = 'foo'; log(error); - // console.log(log.log.args); assert.ok(log.log.calledWith(sinon.match(/customProp: 'foo'/))); }); From b12501b0060161f5c4792f697a0dd55b21b15bdb Mon Sep 17 00:00:00 2001 From: Calvin Tan Date: Tue, 24 Jan 2023 22:18:43 +0800 Subject: [PATCH 05/12] add caller to prefix --- src/common.js | 1 + src/node.js | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/common.js b/src/common.js index e3291b20..11309f04 100644 --- a/src/common.js +++ b/src/common.js @@ -118,6 +118,7 @@ function setup(env) { debug.color = createDebug.selectColor(namespace); debug.extend = extend; debug.destroy = createDebug.destroy; // XXX Temporary. Will be removed in the next major release. + debug.caller = null; Object.defineProperty(debug, 'enabled', { enumerable: true, diff --git a/src/node.js b/src/node.js index 7dfa3854..b3f56654 100644 --- a/src/node.js +++ b/src/node.js @@ -166,12 +166,14 @@ function useColors() { */ function formatArgs(args) { - const { namespace: name, useColors } = this; + const { namespace: name, useColors, caller } = this; if (useColors) { const c = this.color; const colorCode = '\u001B[3' + (c < 8 ? c : '8;5;' + c); - const prefix = ` ${colorCode};1m${name} \u001B[0m`; + const prefix = ` ${colorCode};1m${name} ${ + caller ? `[${caller}]` : '' + }\u001B[0m`; args[0] = prefix + args[0].split('\n').join('\n' + prefix); args.push( From 5e6661b8fb78d672a1febea7b790f967d896be8e Mon Sep 17 00:00:00 2001 From: Calvin Tan Date: Tue, 24 Jan 2023 22:51:23 +0800 Subject: [PATCH 06/12] changed caller prop to scope prop. added ms to TTY output --- src/common.js | 2 +- src/node.js | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/common.js b/src/common.js index 11309f04..b3795522 100644 --- a/src/common.js +++ b/src/common.js @@ -118,7 +118,7 @@ function setup(env) { debug.color = createDebug.selectColor(namespace); debug.extend = extend; debug.destroy = createDebug.destroy; // XXX Temporary. Will be removed in the next major release. - debug.caller = null; + debug.scope = null; Object.defineProperty(debug, 'enabled', { enumerable: true, diff --git a/src/node.js b/src/node.js index b3f56654..3eddbc3a 100644 --- a/src/node.js +++ b/src/node.js @@ -166,21 +166,23 @@ function useColors() { */ function formatArgs(args) { - const { namespace: name, useColors, caller } = this; + const { namespace: name, useColors, scope } = this; if (useColors) { const c = this.color; const colorCode = '\u001B[3' + (c < 8 ? c : '8;5;' + c); const prefix = ` ${colorCode};1m${name} ${ - caller ? `[${caller}]` : '' + scope ? `[${scope}]` : '' }\u001B[0m`; args[0] = prefix + args[0].split('\n').join('\n' + prefix); args.push( - colorCode + 'm+' + module.exports.humanize(this.diff) + '\u001B[0m' + `${colorCode}m+${module.exports.humanize(this.diff)}\u001B[0m` ); } else { - args[0] = getDate() + name + ' ' + args[0]; + args[0] = `${getDate()}${name} ${scope ? `[${scope}]` : ''} ${ + args[0] + } ${module.exports.humanize(this.diff)}`; } } From d2a71eaab158edabc3ecb75f43f89143b002a709 Mon Sep 17 00:00:00 2001 From: Calvin Tan Date: Tue, 24 Jan 2023 22:59:47 +0800 Subject: [PATCH 07/12] spacing --- src/node.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/node.js b/src/node.js index 3eddbc3a..8364575d 100644 --- a/src/node.js +++ b/src/node.js @@ -171,8 +171,8 @@ function formatArgs(args) { if (useColors) { const c = this.color; const colorCode = '\u001B[3' + (c < 8 ? c : '8;5;' + c); - const prefix = ` ${colorCode};1m${name} ${ - scope ? `[${scope}]` : '' + const prefix = ` ${colorCode};1m${name}${ + scope ? ` [${scope}]` : '' }\u001B[0m`; args[0] = prefix + args[0].split('\n').join('\n' + prefix); @@ -180,7 +180,7 @@ function formatArgs(args) { `${colorCode}m+${module.exports.humanize(this.diff)}\u001B[0m` ); } else { - args[0] = `${getDate()}${name} ${scope ? `[${scope}]` : ''} ${ + args[0] = `${getDate()}${name}${scope ? ` [${scope}]` : ''} ${ args[0] } ${module.exports.humanize(this.diff)}`; } From 9242849110a82d0bba22592baa22a16d83a0d4ba Mon Sep 17 00:00:00 2001 From: Calvin Tan Date: Tue, 24 Jan 2023 23:03:30 +0800 Subject: [PATCH 08/12] fixed wrong ms formatting for tty --- src/node.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/node.js b/src/node.js index 8364575d..d98da761 100644 --- a/src/node.js +++ b/src/node.js @@ -180,9 +180,10 @@ function formatArgs(args) { `${colorCode}m+${module.exports.humanize(this.diff)}\u001B[0m` ); } else { - args[0] = `${getDate()}${name}${scope ? ` [${scope}]` : ''} ${ + args[0] = `${getDate()}${name}}${scope ? ` [${scope}]` : ''} ${ args[0] - } ${module.exports.humanize(this.diff)}`; + }`; + args.push(module.exports.humanize(this.diff)); } } From d78dd4b2c228d76d9376d9fec53f1f6d238867d1 Mon Sep 17 00:00:00 2001 From: Calvin Tan Date: Tue, 24 Jan 2023 23:05:00 +0800 Subject: [PATCH 09/12] nit --- src/node.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/node.js b/src/node.js index d98da761..177206d8 100644 --- a/src/node.js +++ b/src/node.js @@ -180,9 +180,7 @@ function formatArgs(args) { `${colorCode}m+${module.exports.humanize(this.diff)}\u001B[0m` ); } else { - args[0] = `${getDate()}${name}}${scope ? ` [${scope}]` : ''} ${ - args[0] - }`; + args[0] = `${getDate()}${name}${scope ? ` [${scope}]` : ''} ${args[0]}`; args.push(module.exports.humanize(this.diff)); } } From 88e6f18c398406ecdbd809879a467b0148f1f504 Mon Sep 17 00:00:00 2001 From: Calvin Tan Date: Sat, 11 Feb 2023 14:35:47 +0800 Subject: [PATCH 10/12] space after scope --- src/node.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node.js b/src/node.js index 177206d8..0f9c59cb 100644 --- a/src/node.js +++ b/src/node.js @@ -172,7 +172,7 @@ function formatArgs(args) { const c = this.color; const colorCode = '\u001B[3' + (c < 8 ? c : '8;5;' + c); const prefix = ` ${colorCode};1m${name}${ - scope ? ` [${scope}]` : '' + scope ? ` [${scope}] ` : '' }\u001B[0m`; args[0] = prefix + args[0].split('\n').join('\n' + prefix); From 11c20a1cdb2727767fcc37da5d0de82b02dbe91c Mon Sep 17 00:00:00 2001 From: Calvin Tan Date: Sat, 11 Feb 2023 14:35:52 +0800 Subject: [PATCH 11/12] prettier --- src/common.js | 132 +++++++++++++++++++++++++++----------------------- src/node.js | 90 ++++------------------------------ 2 files changed, 82 insertions(+), 140 deletions(-) diff --git a/src/common.js b/src/common.js index b3795522..e2ae610e 100644 --- a/src/common.js +++ b/src/common.js @@ -1,4 +1,3 @@ - /** * This is the common logic for both the Node.js and web browser * implementations of `debug()`. @@ -14,35 +13,35 @@ function setup(env) { createDebug.humanize = require('ms'); createDebug.destroy = destroy; - Object.keys(env).forEach(key => { + Object.keys(env).forEach((key) => { createDebug[key] = env[key]; }); /** - * The currently active debug mode names, and names to skip. - */ + * The currently active debug mode names, and names to skip. + */ createDebug.names = []; createDebug.skips = []; /** - * Map of special "%n" handling functions, for the debug "format" argument. - * - * Valid key names are a single, lower or upper-case letter, i.e. "n" and "N". - */ + * Map of special "%n" handling functions, for the debug "format" argument. + * + * Valid key names are a single, lower or upper-case letter, i.e. "n" and "N". + */ createDebug.formatters = {}; /** - * Selects a color for a debug namespace - * @param {String} namespace The namespace string for the debug instance to be colored - * @return {Number|String} An ANSI color code for the given namespace - * @api private - */ + * Selects a color for a debug namespace + * @param {String} namespace The namespace string for the debug instance to be colored + * @return {Number|String} An ANSI color code for the given namespace + * @api private + */ function selectColor(namespace) { let hash = 0; for (let i = 0; i < namespace.length; i++) { - hash = ((hash << 5) - hash) + namespace.charCodeAt(i); + hash = (hash << 5) - hash + namespace.charCodeAt(i); hash |= 0; // Convert to 32bit integer } @@ -51,12 +50,12 @@ function setup(env) { createDebug.selectColor = selectColor; /** - * Create a debugger with the given `namespace`. - * - * @param {String} namespace - * @return {Function} - * @api public - */ + * Create a debugger with the given `namespace`. + * + * @param {String} namespace + * @return {Function} + * @api public + */ function createDebug(namespace) { let prevTime; let enableOverride = null; @@ -134,9 +133,9 @@ function setup(env) { return enabledCache; }, - set: v => { + set: (v) => { enableOverride = v; - } + }, }); // Env-specific initialization logic for debug instances @@ -148,18 +147,22 @@ function setup(env) { } function extend(namespace, delimiter) { - const newDebug = createDebug(this.namespace + (typeof delimiter === 'undefined' ? ':' : delimiter) + namespace); + const newDebug = createDebug( + this.namespace + + (typeof delimiter === 'undefined' ? ':' : delimiter) + + namespace + ); newDebug.log = this.log; return newDebug; } /** - * Enables a debug mode by namespaces. This can include modes - * separated by a colon and wildcards. - * - * @param {String} namespaces - * @api public - */ + * Enables a debug mode by namespaces. This can include modes + * separated by a colon and wildcards. + * + * @param {String} namespaces + * @api public + */ function enable(namespaces) { createDebug.save(namespaces); createDebug.namespaces = namespaces; @@ -168,7 +171,9 @@ function setup(env) { createDebug.skips = []; let i; - const split = (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/); + const split = (typeof namespaces === 'string' ? namespaces : '').split( + /[\s,]+/ + ); const len = split.length; for (i = 0; i < len; i++) { @@ -180,7 +185,9 @@ function setup(env) { namespaces = split[i].replace(/\*/g, '.*?'); if (namespaces[0] === '-') { - createDebug.skips.push(new RegExp('^' + namespaces.slice(1) + '$')); + createDebug.skips.push( + new RegExp('^' + namespaces.slice(1) + '$') + ); } else { createDebug.names.push(new RegExp('^' + namespaces + '$')); } @@ -188,27 +195,29 @@ function setup(env) { } /** - * Disable debug output. - * - * @return {String} namespaces - * @api public - */ + * Disable debug output. + * + * @return {String} namespaces + * @api public + */ function disable() { const namespaces = [ ...createDebug.names.map(toNamespace), - ...createDebug.skips.map(toNamespace).map(namespace => '-' + namespace) + ...createDebug.skips + .map(toNamespace) + .map((namespace) => '-' + namespace), ].join(','); createDebug.enable(''); return namespaces; } /** - * Returns true if the given mode name is enabled, false otherwise. - * - * @param {String} name - * @return {Boolean} - * @api public - */ + * Returns true if the given mode name is enabled, false otherwise. + * + * @param {String} name + * @return {Boolean} + * @api public + */ function enabled(name) { if (name[name.length - 1] === '*') { return true; @@ -233,25 +242,26 @@ function setup(env) { } /** - * Convert regexp to namespace - * - * @param {RegExp} regxep - * @return {String} namespace - * @api private - */ + * Convert regexp to namespace + * + * @param {RegExp} regxep + * @return {String} namespace + * @api private + */ function toNamespace(regexp) { - return regexp.toString() + return regexp + .toString() .substring(2, regexp.toString().length - 2) .replace(/\.\*\?$/, '*'); } /** - * Coerce `val`. - * - * @param {Mixed} val - * @return {Mixed} - * @api private - */ + * Coerce `val`. + * + * @param {Mixed} val + * @return {Mixed} + * @api private + */ function coerce(val) { if (val instanceof Error) { return val.stack || val.message; @@ -260,11 +270,13 @@ function setup(env) { } /** - * XXX DO NOT USE. This is a temporary stub function. - * XXX It WILL be removed in the next major release. - */ + * XXX DO NOT USE. This is a temporary stub function. + * XXX It WILL be removed in the next major release. + */ function destroy() { - console.warn('Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.'); + console.warn( + 'Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.' + ); } createDebug.enable(createDebug.load()); diff --git a/src/node.js b/src/node.js index 0f9c59cb..7670e3c9 100644 --- a/src/node.js +++ b/src/node.js @@ -32,82 +32,12 @@ try { if (supportsColor && (supportsColor.stderr || supportsColor).level >= 2) { exports.colors = [ - 20, - 21, - 26, - 27, - 32, - 33, - 38, - 39, - 40, - 41, - 42, - 43, - 44, - 45, - 56, - 57, - 62, - 63, - 68, - 69, - 74, - 75, - 76, - 77, - 78, - 79, - 80, - 81, - 92, - 93, - 98, - 99, - 112, - 113, - 128, - 129, - 134, - 135, - 148, - 149, - 160, - 161, - 162, - 163, - 164, - 165, - 166, - 167, - 168, - 169, - 170, - 171, - 172, - 173, - 178, - 179, - 184, - 185, - 196, - 197, - 198, - 199, - 200, - 201, - 202, - 203, - 204, - 205, - 206, - 207, - 208, - 209, - 214, - 215, - 220, - 221 + 20, 21, 26, 27, 32, 33, 38, 39, 40, 41, 42, 43, 44, 45, 56, 57, 62, + 63, 68, 69, 74, 75, 76, 77, 78, 79, 80, 81, 92, 93, 98, 99, 112, + 113, 128, 129, 134, 135, 148, 149, 160, 161, 162, 163, 164, 165, + 166, 167, 168, 169, 170, 171, 172, 173, 178, 179, 184, 185, 196, + 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, + 214, 215, 220, 221, ]; } } catch (error) { @@ -121,7 +51,7 @@ try { */ exports.inspectOpts = Object.keys(process.env) - .filter(key => { + .filter((key) => { return /^debug_/i.test(key); }) .reduce((obj, key) => { @@ -265,12 +195,12 @@ const { formatters } = module.exports; * Map %o to `util.inspect()`, all on a single line. */ -formatters.o = function(v) { +formatters.o = function (v) { this.inspectOpts.colors = this.useColors; return util .inspect(v, this.inspectOpts) .split('\n') - .map(str => str.trim()) + .map((str) => str.trim()) .join(' '); }; @@ -278,7 +208,7 @@ formatters.o = function(v) { * Map %O to `util.inspect()`, allowing multiple lines if needed. */ -formatters.O = function(v) { +formatters.O = function (v) { this.inspectOpts.colors = this.useColors; return util.inspect(v, this.inspectOpts); }; From 329eefccf7596bfd3b9ec821588b2b0dce128648 Mon Sep 17 00:00:00 2001 From: Calvin Tan Date: Sat, 11 Feb 2023 20:52:29 +0800 Subject: [PATCH 12/12] fixed spacing issue after namespace --- src/node.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/node.js b/src/node.js index 7670e3c9..96fb4657 100644 --- a/src/node.js +++ b/src/node.js @@ -102,8 +102,8 @@ function formatArgs(args) { const c = this.color; const colorCode = '\u001B[3' + (c < 8 ? c : '8;5;' + c); const prefix = ` ${colorCode};1m${name}${ - scope ? ` [${scope}] ` : '' - }\u001B[0m`; + scope ? ` [${scope}]` : '' + } \u001B[0m`; args[0] = prefix + args[0].split('\n').join('\n' + prefix); args.push(