Skip to content

Commit

Permalink
feat(perf): combine two functions, fix found bug
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLarkInn committed Jul 1, 2017
1 parent 082a605 commit 5806d15
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions lib/RequestShortener.js
Expand Up @@ -8,18 +8,15 @@ const path = require("path");
const NORMALIZE_SLASH_DIRECTION_REGEXP = /\\/g;
const PATH_CHARS_REGEXP = /[-[\]{}()*+?.,\\^$|#\s]/g;
const SEPERATOR_REGEXP = /[\/\\]$/;
const FRONT_OR_BACK_BANG_REGEXP = /^!|!$/;
const FRONT_OR_BACK_BANG_REGEXP = /^!|!$/g;

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

const shortenPath = (path) => {
return path.replace(PATH_CHARS_REGEXP, "\\$&");
};

const createRegExpForTypeWith = (regexpTypePartial) => {
return new RegExp(`^${regexpTypePartial}|(!)${regexpTypePartial}`, "g");
const createRegExpForPath = (path) => {
const regexpTypePartial = path.replace(PATH_CHARS_REGEXP, "\\$&");
return new RegExp(`(^|!)${regexpTypePartial}`, "g");
};

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

if(directory) {
const currentDirectoryRegExpString = shortenPath(directory);
this.currentDirectoryRegExp = createRegExpForTypeWith(currentDirectoryRegExpString);
this.currentDirectoryRegExp = createRegExpForPath(directory);
}

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 = createRegExpForTypeWith(parentDirectoryRegExpString);
this.parentDirectoryRegExp = createRegExpForPath(parentDirectory);
}

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 = createRegExpForTypeWith(buildinsRegExpString);
this.buildinsRegExp = createRegExpForPath(buildins);
}

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

0 comments on commit 5806d15

Please sign in to comment.