Skip to content

Commit a92714d

Browse files
committed
util: refactor debuglog() from array regex to regex string
1 parent bf5f3ac commit a92714d

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

lib/util.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -230,28 +230,21 @@ function format(f) {
230230
return str;
231231
}
232232

233-
var debugs = {};
234-
var debugEnviron;
235-
236-
const regExpSpecialChars = /[|\\{}()[\]^$+?.]/g;
237-
238-
function stringToRegExp(string) {
239-
// Escape special chars except wildcard.
240-
string = string.replace(regExpSpecialChars, '\\$&');
241-
// Transform wildcard to "match anything"
242-
string = string.replace(/\*/g, '.*');
243-
return new RegExp(`^${string}$`);
233+
const debugs = {};
234+
let debugEnvRegex = /^$/;
235+
if (process.env.NODE_DEBUG) {
236+
let debugEnv = process.env.NODE_DEBUG;
237+
debugEnv = debugEnv.replace(/[|\\{}()[\]^$+?.]/g, '\\$&')
238+
.replace(/\*/g, '.*')
239+
.replace(/,/g, '$|^')
240+
.toUpperCase();
241+
debugEnvRegex = new RegExp(`^${debugEnv}$`, 'i');
244242
}
245243

246244
function debuglog(set) {
247-
if (debugEnviron === undefined) {
248-
debugEnviron = Array.from(new Set(
249-
(process.env.NODE_DEBUG || '').split(',').map((s) => s.toUpperCase())));
250-
debugEnviron = debugEnviron.map(stringToRegExp);
251-
}
252245
set = set.toUpperCase();
253246
if (!debugs[set]) {
254-
if (debugEnviron.some((name) => name.test(set))) {
247+
if (debugEnvRegex.test(set)) {
255248
var pid = process.pid;
256249
debugs[set] = function() {
257250
var msg = exports.format.apply(exports, arguments);

0 commit comments

Comments
 (0)