From 2e992a0cdaaa8bd5b49249a9d96deb696f1a0b46 Mon Sep 17 00:00:00 2001 From: Boris Okunskiy Date: Fri, 14 Mar 2014 11:57:16 +0400 Subject: [PATCH] Exposing enable() method for Node.js to address #27 --- lib/debug.js | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/debug.js b/lib/debug.js index 3b0a918..dda1050 100644 --- a/lib/debug.js +++ b/lib/debug.js @@ -17,17 +17,6 @@ module.exports = debug; var names = [] , skips = []; -(process.env.DEBUG || '') - .split(/[\s,]+/) - .forEach(function(name){ - name = name.replace('*', '.*?'); - if (name[0] === '-') { - skips.push(new RegExp('^' + name.substr(1) + '$')); - } else { - names.push(new RegExp('^' + name + '$')); - } - }); - /** * Colors. */ @@ -145,3 +134,23 @@ function coerce(val) { if (val instanceof Error) return val.stack || val.message; return val; } + +/** + * Enable specified `namespaces` for debugging. + */ +debug.enable = function(namespaces) { + namespaces.split(/[\s,]+/) + .forEach(function(name){ + name = name.replace('*', '.*?'); + if (name[0] === '-') { + skips.push(new RegExp('^' + name.substr(1) + '$')); + } else { + names.push(new RegExp('^' + name + '$')); + } + }) +}; + +/** + * Enable namespaces listed in `process.env.DEBUG` initially. + */ +debug.enable(process.env.DEBUG || ''); \ No newline at end of file