Skip to content

Commit

Permalink
util: refactor debuglog() from array regex to regex string
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler committed Dec 11, 2017
1 parent bf5f3ac commit 668fa2b
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,28 +230,30 @@ function format(f) {
return str;
}

var debugs = {};
var debugEnviron;
const debugs = {};
let debugEnviron;

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

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

return new RegExp(strArr.join('|'), 'i');
}

function debuglog(set) {
if (debugEnviron === undefined) {
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.some((name) => name.test(set))) {
const regex = arrayToRegExp(debugEnviron);
if (regex.test(set)) {
var pid = process.pid;
debugs[set] = function() {
var msg = exports.format.apply(exports, arguments);
Expand Down

0 comments on commit 668fa2b

Please sign in to comment.