Skip to content

Commit

Permalink
instance extends feature
Browse files Browse the repository at this point in the history
  • Loading branch information
kwolfy committed Nov 23, 2017
1 parent 285dfe1 commit 75c91df
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
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);
}

/**
* 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
Original file line number Diff line number Diff line change
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');
});

});

});

0 comments on commit 75c91df

Please sign in to comment.