Skip to content

Commit 082a605

Browse files
committed
feat(perf): hoist one more function
1 parent f694faa commit 082a605

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

lib/RequestShortener.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ const shortenPath = (path) => {
1818
return path.replace(PATH_CHARS_REGEXP, "\\$&");
1919
};
2020

21+
const createRegExpForTypeWith = (regexpTypePartial) => {
22+
return new RegExp(`^${regexpTypePartial}|(!)${regexpTypePartial}`, "g");
23+
};
2124

2225
class RequestShortener {
2326
constructor(directory) {
@@ -26,23 +29,23 @@ class RequestShortener {
2629

2730
if(directory) {
2831
const currentDirectoryRegExpString = shortenPath(directory);
29-
this.currentDirectoryRegExp = new RegExp("^" + currentDirectoryRegExpString + "|(!)" + currentDirectoryRegExpString, "g");
32+
this.currentDirectoryRegExp = createRegExpForTypeWith(currentDirectoryRegExpString);
3033
}
3134

3235
const dirname = path.dirname(directory);
3336
const endsWithSeperator = SEPERATOR_REGEXP.test(dirname);
3437
const parentDirectory = endsWithSeperator ? dirname.substr(0, dirname.length - 1) : dirname;
3538
if(parentDirectory && parentDirectory !== directory) {
3639
const parentDirectoryRegExpString = shortenPath(parentDirectory);
37-
this.parentDirectoryRegExp = new RegExp("^" + parentDirectoryRegExpString + "|(!)" + parentDirectoryRegExpString, "g");
40+
this.parentDirectoryRegExp = createRegExpForTypeWith(parentDirectoryRegExpString);
3841
}
3942

4043
if(__dirname.length >= 2) {
4144
const buildins = normalizeBackSlashDirection(path.join(__dirname, ".."));
4245
const buildinsAsModule = this.currentDirectoryRegExp && this.currentDirectoryRegExp.test(buildins);
4346
this.buildinsAsModule = buildinsAsModule;
4447
const buildinsRegExpString = shortenPath(buildins);
45-
this.buildinsRegExp = new RegExp("^" + buildinsRegExpString + "|(!)" + buildinsRegExpString, "g");
48+
this.buildinsRegExp = createRegExpForTypeWith(buildinsRegExpString);
4649
}
4750

4851
this.indexJsRegExp = /\/index.js(!|\?|\(query\))/g;

0 commit comments

Comments
 (0)