Skip to content

Commit

Permalink
feat: Return namespaces string when invoking disable()
Browse files Browse the repository at this point in the history
Closes debug-js#523

feat: Add unit tests for disable return value

fix: Correct spelling in test case description
  • Loading branch information
mblarsen committed Oct 5, 2018
1 parent 4490cd9 commit 1844758
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
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
26 changes: 26 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,30 @@ 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('');
});

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('-*');
});
});
});

0 comments on commit 1844758

Please sign in to comment.