Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

instance extends feature #524

Merged
merged 3 commits into from Sep 11, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/common.js
Expand Up @@ -123,6 +123,7 @@ module.exports = function setup(env) {
debug.useColors = createDebug.useColors();
debug.color = selectColor(namespace);
debug.destroy = destroy;
debug.extend = extend;
//debug.formatArgs = formatArgs;
//debug.rawLog = rawLog;

Expand All @@ -146,6 +147,14 @@ module.exports = function setup(env) {
}
}

function extend (namespace, delimiter) {
if(typeof delimiter === 'undefined') {
delimiter = ':';
}

return createDebug(this.namespace + delimiter + namespace);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove the if check and just change the + delimiter + to + (delimiter || ':') +

}

/**
* Enables a debug mode by namespaces. This can include modes
* separated by a colon and wildcards.
Expand Down
26 changes: 26 additions & 0 deletions test/debug_spec.js
Expand Up @@ -64,4 +64,30 @@ describe('debug', function () {
});
});


describe('extend namespace', function () {
var log;

beforeEach(function () {
debug.enable('foo');
log = debug('foo');
});

it('should extend namespace', function () {
var logBar = log.extend('bar');
expect(logBar.namespace).to.be.equal('foo:bar');
});

it('should extend namespace with custom delimiter', function () {
var logBar = log.extend('bar', '--');
expect(logBar.namespace).to.be.equal('foo--bar');
});

it('should extend namespace with empty delimiter', function () {
var logBar = log.extend('bar', '');
expect(logBar.namespace).to.be.equal('foobar');
});

});

});