Skip to content

Commit 7ef8b41

Browse files
mblarsenQix-
authored andcommittedOct 8, 2018
feat: Return namespaces string when invoking disable()
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
1 parent 4236585 commit 7ef8b41

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed
 

‎README.md

+18
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,24 @@ $ DEBUG=foo node -e 'var dbg = require("debug"); dbg.enable("bar"); console.log(
317317
=> false
318318
```
319319

320+
`disable()`
321+
322+
Will disable all namespaces. The functions returns the namespaces currently
323+
enabled (and skipped). This can be useful if you want to disable debugging
324+
temporarily without knowing what was enabled to begin with.
325+
326+
For example:
327+
328+
```js
329+
let debug = require('debug');
330+
debug.enable('foo:*,-foo:bar');
331+
let namespaces = debug.disable();
332+
debug.enable(namespaces);
333+
```
334+
335+
Note: There is no guarantee that the string will be identical to the initial
336+
enable string, but semantically they will be identical.
337+
320338
## Checking whether a debug target is enabled
321339

322340
After you've created a debug instance, you can determine whether or not it is

‎src/common.js

+19
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,16 @@ function setup(env) {
187187
/**
188188
* Disable debug output.
189189
*
190+
* @return {String} namespaces
190191
* @api public
191192
*/
192193
function disable() {
194+
const namespaces = [
195+
...createDebug.names.map(toNamespace),
196+
...createDebug.skips.map(toNamespace).map(namespace => '-' + namespace)
197+
].join(',');
193198
createDebug.enable('');
199+
return namespaces;
194200
}
195201

196202
/**
@@ -223,6 +229,19 @@ function setup(env) {
223229
return false;
224230
}
225231

232+
/**
233+
* Convert regexp to namespace
234+
*
235+
* @param {RegExp} regxep
236+
* @return {String} namespace
237+
* @api private
238+
*/
239+
function toNamespace(regexp) {
240+
return regexp.toString()
241+
.substring(2, regexp.toString().length - 2)
242+
.replace(/\.\*\?$/, '*');
243+
}
244+
226245
/**
227246
* Coerce `val`.
228247
*

‎test.js

+39
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,43 @@ describe('debug', () => {
8181
expect(logBar.namespace).to.be.equal('foobar');
8282
});
8383
});
84+
85+
describe('rebuild namespaces string (disable)', () => {
86+
it('handle names, skips, and wildcards', () => {
87+
debug.enable('test,abc*,-abc');
88+
const namespaces = debug.disable();
89+
expect(namespaces).to.equal('test,abc*,-abc');
90+
});
91+
92+
it('handles empty', () => {
93+
debug.enable('');
94+
const namespaces = debug.disable();
95+
expect(namespaces).to.equal('');
96+
expect(debug.names).to.deep.equal([]);
97+
expect(debug.skips).to.deep.equal([]);
98+
});
99+
100+
it('handles all', () => {
101+
debug.enable('*');
102+
const namespaces = debug.disable();
103+
expect(namespaces).to.equal('*');
104+
});
105+
106+
it('handles skip all', () => {
107+
debug.enable('-*');
108+
const namespaces = debug.disable();
109+
expect(namespaces).to.equal('-*');
110+
});
111+
112+
it('names+skips same with new string', () => {
113+
debug.enable('test,abc*,-abc');
114+
const oldNames = [...debug.names];
115+
const oldSkips = [...debug.skips];
116+
const namespaces = debug.disable();
117+
expect(namespaces).to.equal('test,abc*,-abc');
118+
debug.enable(namespaces);
119+
expect(oldNames.map(String)).to.deep.equal(debug.names.map(String));
120+
expect(oldSkips.map(String)).to.deep.equal(debug.skips.map(String));
121+
});
122+
});
84123
});

0 commit comments

Comments
 (0)
Please sign in to comment.