From 082a605791280c08a350d6bb5493e240ec90f30b Mon Sep 17 00:00:00 2001 From: Sean Larkin Date: Fri, 30 Jun 2017 15:07:02 -0500 Subject: [PATCH] feat(perf): hoist one more function --- lib/RequestShortener.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/RequestShortener.js b/lib/RequestShortener.js index 3d5efddd6bf..36fbc961381 100644 --- a/lib/RequestShortener.js +++ b/lib/RequestShortener.js @@ -18,6 +18,9 @@ const shortenPath = (path) => { return path.replace(PATH_CHARS_REGEXP, "\\$&"); }; +const createRegExpForTypeWith = (regexpTypePartial) => { + return new RegExp(`^${regexpTypePartial}|(!)${regexpTypePartial}`, "g"); +}; class RequestShortener { constructor(directory) { @@ -26,7 +29,7 @@ class RequestShortener { if(directory) { const currentDirectoryRegExpString = shortenPath(directory); - this.currentDirectoryRegExp = new RegExp("^" + currentDirectoryRegExpString + "|(!)" + currentDirectoryRegExpString, "g"); + this.currentDirectoryRegExp = createRegExpForTypeWith(currentDirectoryRegExpString); } const dirname = path.dirname(directory); @@ -34,7 +37,7 @@ class RequestShortener { const parentDirectory = endsWithSeperator ? dirname.substr(0, dirname.length - 1) : dirname; if(parentDirectory && parentDirectory !== directory) { const parentDirectoryRegExpString = shortenPath(parentDirectory); - this.parentDirectoryRegExp = new RegExp("^" + parentDirectoryRegExpString + "|(!)" + parentDirectoryRegExpString, "g"); + this.parentDirectoryRegExp = createRegExpForTypeWith(parentDirectoryRegExpString); } if(__dirname.length >= 2) { @@ -42,7 +45,7 @@ class RequestShortener { const buildinsAsModule = this.currentDirectoryRegExp && this.currentDirectoryRegExp.test(buildins); this.buildinsAsModule = buildinsAsModule; const buildinsRegExpString = shortenPath(buildins); - this.buildinsRegExp = new RegExp("^" + buildinsRegExpString + "|(!)" + buildinsRegExpString, "g"); + this.buildinsRegExp = createRegExpForTypeWith(buildinsRegExpString); } this.indexJsRegExp = /\/index.js(!|\?|\(query\))/g;