Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,21 @@ function setup(env) {

for (const ns of split) {
if (ns[0] === '-') {
createDebug.skips.push(ns.slice(1));
createDebug.skips.push(toNamespace(ns.slice(1)));
} else {
createDebug.names.push(ns);
createDebug.names.push(toNamespace(ns));
}
}
}

// Invert historic '*' → '.*?' encoding so disable()/enable() can round-trip.
function toNamespace(namespace) {
const value = namespace instanceof RegExp ?
namespace.source.replace(/^\^/, '').replace(/\$$/, '') :
String(namespace);
return value.replace(/\.\*\?/g, '*');
}

/**
* Checks if the given string matches a namespace template, honoring
* asterisks as wildcards.
Expand Down Expand Up @@ -232,8 +240,8 @@ function setup(env) {
*/
function disable() {
const namespaces = [
...createDebug.names,
...createDebug.skips.map(namespace => '-' + namespace)
...createDebug.names.map(toNamespace),
...createDebug.skips.map(namespace => '-' + toNamespace(namespace))
].join(',');
createDebug.enable('');
return namespaces;
Expand Down
19 changes: 19 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,25 @@ describe('debug', () => {
assert.deepStrictEqual(oldSkips.map(String), debug.skips.map(String));
});

it('round-trips wildcard namespaces including skips', () => {
debug.enable('*:error:*,-*:error:ignore');
const namespaces = debug.disable();
assert.deepStrictEqual(namespaces, '*:error:*,-*:error:ignore');
assert.doesNotThrow(() => debug.enable(namespaces));
assert.deepStrictEqual(debug.enabled('api:error:db'), true);
assert.deepStrictEqual(debug.enabled('api:error:ignore'), false);
assert.deepStrictEqual(debug.enabled('api:warn:db'), false);
});

it('accepts historic disable() wildcard encoding', () => {
debug.enable('.*?:error:*,-.*?:verbose');
const namespaces = debug.disable();
assert.deepStrictEqual(namespaces, '*:error:*,-*:verbose');
assert.doesNotThrow(() => debug.enable(namespaces));
assert.deepStrictEqual(debug.enabled('api:error:db'), true);
assert.deepStrictEqual(debug.enabled('api:verbose'), false);
});

it('handles re-enabling existing instances', () => {
debug.disable('*');
const inst = debug('foo');
Expand Down