From a92714d73897d83481b06ce392ebd99afc9884d3 Mon Sep 17 00:00:00 2001 From: Tyler Date: Fri, 15 Dec 2017 10:39:25 +0800 Subject: [PATCH] util: refactor debuglog() from array regex to regex string --- lib/util.js | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/lib/util.js b/lib/util.js index 2f69ddc19b63c5..0a7af758f43cb3 100644 --- a/lib/util.js +++ b/lib/util.js @@ -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);