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
TylerYang committed Dec 15, 2017
1 parent bf5f3ac commit a92714d
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,28 +230,21 @@ function format(f) {
return str;
}

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}$`);
const debugs = {};
let debugEnvRegex = /^$/;
if (process.env.NODE_DEBUG) {
let debugEnv = process.env.NODE_DEBUG;
debugEnv = debugEnv.replace(/[|\\{}()[\]^$+?.]/g, '\\$&')
.replace(/\*/g, '.*')
.replace(/,/g, '$|^')
.toUpperCase();
debugEnvRegex = new RegExp(`^${debugEnv}$`, '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))) {
if (debugEnvRegex.test(set)) {
var pid = process.pid;
debugs[set] = function() {
var msg = exports.format.apply(exports, arguments);
Expand Down

0 comments on commit a92714d

Please sign in to comment.