Skip to content

Commit

Permalink
feat(perf): hoist one more function
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLarkInn committed Jun 30, 2017
1 parent f694faa commit 082a605
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/RequestShortener.js
Expand Up @@ -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) {
Expand All @@ -26,23 +29,23 @@ class RequestShortener {

if(directory) {
const currentDirectoryRegExpString = shortenPath(directory);
this.currentDirectoryRegExp = new RegExp("^" + currentDirectoryRegExpString + "|(!)" + currentDirectoryRegExpString, "g");
this.currentDirectoryRegExp = createRegExpForTypeWith(currentDirectoryRegExpString);
}

const dirname = path.dirname(directory);
const endsWithSeperator = SEPERATOR_REGEXP.test(dirname);
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) {
const buildins = normalizeBackSlashDirection(path.join(__dirname, ".."));
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;
Expand Down

0 comments on commit 082a605

Please sign in to comment.