Skip to content

Commit 5806d15

Browse files
committed
feat(perf): combine two functions, fix found bug
1 parent 082a605 commit 5806d15

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

lib/RequestShortener.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,15 @@ const path = require("path");
88
const NORMALIZE_SLASH_DIRECTION_REGEXP = /\\/g;
99
const PATH_CHARS_REGEXP = /[-[\]{}()*+?.,\\^$|#\s]/g;
1010
const SEPERATOR_REGEXP = /[\/\\]$/;
11-
const FRONT_OR_BACK_BANG_REGEXP = /^!|!$/;
11+
const FRONT_OR_BACK_BANG_REGEXP = /^!|!$/g;
1212

1313
const normalizeBackSlashDirection = (request) => {
1414
return request.replace(NORMALIZE_SLASH_DIRECTION_REGEXP, "/");
1515
};
1616

17-
const shortenPath = (path) => {
18-
return path.replace(PATH_CHARS_REGEXP, "\\$&");
19-
};
20-
21-
const createRegExpForTypeWith = (regexpTypePartial) => {
22-
return new RegExp(`^${regexpTypePartial}|(!)${regexpTypePartial}`, "g");
17+
const createRegExpForPath = (path) => {
18+
const regexpTypePartial = path.replace(PATH_CHARS_REGEXP, "\\$&");
19+
return new RegExp(`(^|!)${regexpTypePartial}`, "g");
2320
};
2421

2522
class RequestShortener {
@@ -28,24 +25,21 @@ class RequestShortener {
2825
if(SEPERATOR_REGEXP.test(directory)) directory = directory.substr(0, directory.length - 1);
2926

3027
if(directory) {
31-
const currentDirectoryRegExpString = shortenPath(directory);
32-
this.currentDirectoryRegExp = createRegExpForTypeWith(currentDirectoryRegExpString);
28+
this.currentDirectoryRegExp = createRegExpForPath(directory);
3329
}
3430

3531
const dirname = path.dirname(directory);
3632
const endsWithSeperator = SEPERATOR_REGEXP.test(dirname);
3733
const parentDirectory = endsWithSeperator ? dirname.substr(0, dirname.length - 1) : dirname;
3834
if(parentDirectory && parentDirectory !== directory) {
39-
const parentDirectoryRegExpString = shortenPath(parentDirectory);
40-
this.parentDirectoryRegExp = createRegExpForTypeWith(parentDirectoryRegExpString);
35+
this.parentDirectoryRegExp = createRegExpForPath(parentDirectory);
4136
}
4237

4338
if(__dirname.length >= 2) {
4439
const buildins = normalizeBackSlashDirection(path.join(__dirname, ".."));
4540
const buildinsAsModule = this.currentDirectoryRegExp && this.currentDirectoryRegExp.test(buildins);
4641
this.buildinsAsModule = buildinsAsModule;
47-
const buildinsRegExpString = shortenPath(buildins);
48-
this.buildinsRegExp = createRegExpForTypeWith(buildinsRegExpString);
42+
this.buildinsRegExp = createRegExpForPath(buildins);
4943
}
5044

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

0 commit comments

Comments
 (0)