Skip to content

Commit 668fa2b

Browse files
author
tyler
committed
util: refactor debuglog() from array regex to regex string
1 parent bf5f3ac commit 668fa2b

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

lib/util.js

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

233-
var debugs = {};
234-
var debugEnviron;
233+
const debugs = {};
234+
let debugEnviron;
235235

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

238-
function stringToRegExp(string) {
238+
function arrayToRegExp(strArr) {
239239
// 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}$`);
240+
strArr = strArr.map((str) => {
241+
return str.replace(regExpSpecialChars, '\\$&')
242+
.replace(/\*/g, '.*');
243+
});
244+
245+
return new RegExp(strArr.join('|'), 'i');
244246
}
245247

246248
function debuglog(set) {
247249
if (debugEnviron === undefined) {
248250
debugEnviron = Array.from(new Set(
249251
(process.env.NODE_DEBUG || '').split(',').map((s) => s.toUpperCase())));
250-
debugEnviron = debugEnviron.map(stringToRegExp);
251252
}
252253
set = set.toUpperCase();
253254
if (!debugs[set]) {
254-
if (debugEnviron.some((name) => name.test(set))) {
255+
const regex = arrayToRegExp(debugEnviron);
256+
if (regex.test(set)) {
255257
var pid = process.pid;
256258
debugs[set] = function() {
257259
var msg = exports.format.apply(exports, arguments);

0 commit comments

Comments
 (0)