Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,25 @@ function format(f) {
var debugs = {};
var debugEnviron;

const regExpSpecialChars = /[|\\{}()[\]^$+?.]/g;

function stringToRegExp(string) {
// Escape special chars except wildcard.
string = string.replace(regExpSpecialChars, '\\$&');
// Transform wildcard to "match anything"
string = string.replace(/\*/g, '.*');
return new RegExp(`^${string}$`);
}

function debuglog(set) {
if (debugEnviron === undefined) {
debugEnviron = new Set(
(process.env.NODE_DEBUG || '').split(',').map((s) => s.toUpperCase()));
debugEnviron = Array.from(new Set(
(process.env.NODE_DEBUG || '').split(',').map((s) => s.toUpperCase())));
debugEnviron = debugEnviron.map(stringToRegExp);
}
set = set.toUpperCase();
if (!debugs[set]) {
if (debugEnviron.has(set)) {
if (debugEnviron.some((name) => name.test(set))) {
var pid = process.pid;
debugs[set] = function() {
var msg = exports.format.apply(exports, arguments);
Expand Down
9 changes: 9 additions & 0 deletions test/sequential/test-util-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ function parent() {
test('f$oo', true, 'f$oo');
test('f$oo', false, 'f.oo');
test('no-bar-at-all', false, 'bar');

test('test-abc', true, 'test-abc');
test('test-a', false, 'test-abc');
test('test-*', true, 'test-abc');
test('test-*c', true, 'test-abc');
test('test-*abc', true, 'test-abc');
test('abc-test', true, 'abc-test');
test('a*-test', true, 'abc-test');
test('*-test', true, 'abc-test');
}

function test(environ, shouldWrite, section) {
Expand Down