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

introduce debug.id helper method #543

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ module.exports = function setup(env) {

function createDebug(namespace) {
var prevTime;
var lastId = 0;

function debug() {
// disabled?
Expand Down Expand Up @@ -118,11 +119,19 @@ module.exports = function setup(env) {
logFn.apply(self, args);
}

function id () {
// disabled?
if (!debug.enabled) return;

return lastId++;
}

debug.namespace = namespace;
debug.enabled = createDebug.enabled(namespace);
debug.useColors = createDebug.useColors();
debug.color = selectColor(namespace);
debug.destroy = destroy;
debug.id = id;
//debug.formatArgs = formatArgs;
//debug.rawLog = rawLog;

Expand Down
15 changes: 15 additions & 0 deletions test/debug_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ describe('debug', function () {
expect(log.log).to.have.been.calledOnce;
});
});

context('with `log.id` function', function () {
it('increments internal id when enabled', function () {
expect(log.id()).to.equal(0);
expect(log.id()).to.equal(1);
expect(log.id()).to.equal(2);
});

it('does not increment internal id when disabled', function () {
debug.disable('test');

expect(log.id()).to.equal(undefined);
expect(log.id()).to.equal(undefined);
});
});
});

});