From 7ef8b417a86941372074f749019b9f439a1f6ef6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20B=C3=B8cker-Larsen?= Date: Thu, 4 Oct 2018 09:45:22 +0800 Subject: [PATCH] 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 --- README.md | 18 ++++++++++++++++++ src/common.js | 19 +++++++++++++++++++ test.js | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/README.md b/README.md index 0ee7634d..88dae35d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/common.js b/src/common.js index dab9e480..0b6d0b64 100644 --- a/src/common.js +++ b/src/common.js @@ -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; } /** @@ -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`. * diff --git a/test.js b/test.js index 675d0825..888f9bd8 100644 --- a/test.js +++ b/test.js @@ -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)); + }); + }); });