Skip to content

Commit

Permalink
feat: Return namespaces string when invoking disable()
Browse files Browse the repository at this point in the history
feat: Add unit tests for disable return value

fix: Correct spelling in test case description

feat: Test that disable-string works with enable again

Closes #523

docs: Add section about disable return value
  • Loading branch information
mblarsen authored and Qix- committed Oct 8, 2018
1 parent 4236585 commit 7ef8b41
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Expand Up @@ -317,6 +317,24 @@ $ DEBUG=foo node -e 'var dbg = require("debug"); dbg.enable("bar"); console.log(
=> false
```

`disable()`

Will disable all namespaces. The functions returns the namespaces currently
enabled (and skipped). This can be useful if you want to disable debugging
temporarily without knowing what was enabled to begin with.

For example:

```js
let debug = require('debug');
debug.enable('foo:*,-foo:bar');
let namespaces = debug.disable();
debug.enable(namespaces);
```

Note: There is no guarantee that the string will be identical to the initial
enable string, but semantically they will be identical.

## Checking whether a debug target is enabled

After you've created a debug instance, you can determine whether or not it is
Expand Down
19 changes: 19 additions & 0 deletions src/common.js
Expand Up @@ -187,10 +187,16 @@ function setup(env) {
/**
* Disable debug output.
*
* @return {String} namespaces
* @api public
*/
function disable() {
const namespaces = [
...createDebug.names.map(toNamespace),
...createDebug.skips.map(toNamespace).map(namespace => '-' + namespace)
].join(',');
createDebug.enable('');
return namespaces;
}

/**
Expand Down Expand Up @@ -223,6 +229,19 @@ function setup(env) {
return false;
}

/**
* Convert regexp to namespace
*
* @param {RegExp} regxep
* @return {String} namespace
* @api private
*/
function toNamespace(regexp) {
return regexp.toString()
.substring(2, regexp.toString().length - 2)
.replace(/\.\*\?$/, '*');
}

/**
* Coerce `val`.
*
Expand Down
39 changes: 39 additions & 0 deletions test.js
Expand Up @@ -81,4 +81,43 @@ describe('debug', () => {
expect(logBar.namespace).to.be.equal('foobar');
});
});

describe('rebuild namespaces string (disable)', () => {
it('handle names, skips, and wildcards', () => {
debug.enable('test,abc*,-abc');
const namespaces = debug.disable();
expect(namespaces).to.equal('test,abc*,-abc');
});

it('handles empty', () => {
debug.enable('');
const namespaces = debug.disable();
expect(namespaces).to.equal('');
expect(debug.names).to.deep.equal([]);
expect(debug.skips).to.deep.equal([]);
});

it('handles all', () => {
debug.enable('*');
const namespaces = debug.disable();
expect(namespaces).to.equal('*');
});

it('handles skip all', () => {
debug.enable('-*');
const namespaces = debug.disable();
expect(namespaces).to.equal('-*');
});

it('names+skips same with new string', () => {
debug.enable('test,abc*,-abc');
const oldNames = [...debug.names];
const oldSkips = [...debug.skips];
const namespaces = debug.disable();
expect(namespaces).to.equal('test,abc*,-abc');
debug.enable(namespaces);
expect(oldNames.map(String)).to.deep.equal(debug.names.map(String));
expect(oldSkips.map(String)).to.deep.equal(debug.skips.map(String));
});
});
});

0 comments on commit 7ef8b41

Please sign in to comment.