Skip to content

Commit

Permalink
Compare delimiter string over regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed May 6, 2022
1 parent 762bc6b commit 86baef8
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,11 @@ export function tokensToRegexp(
start = true,
end = true,
encode = (x: string) => x,
delimiter = "/#?",
endsWith = "",
} = options;
const endsWith = `[${escapeString(options.endsWith || "")}]|$`;
const delimiter = `[${escapeString(options.delimiter || "/#?")}]`;
const endsWithRe = `[${escapeString(endsWith)}]|$`;
const delimiterRe = `[${escapeString(delimiter)}]`;
let route = start ? "^" : "";

// Iterate over the tokens and create our regexp string.
Expand Down Expand Up @@ -576,23 +578,23 @@ export function tokensToRegexp(
}

if (end) {
if (!strict) route += `${delimiter}?`;
if (!strict) route += `${delimiterRe}?`;

route += !options.endsWith ? "$" : `(?=${endsWith})`;
route += !options.endsWith ? "$" : `(?=${endsWithRe})`;
} else {
const endToken = tokens[tokens.length - 1];
const isEndDelimited =
typeof endToken === "string"
? delimiter.indexOf(endToken[endToken.length - 1]) > -1
? delimiterRe.indexOf(endToken[endToken.length - 1]) > -1
: // tslint:disable-next-line
endToken === undefined;

if (!strict) {
route += `(?:${delimiter}(?=${endsWith}))?`;
route += `(?:${delimiterRe}(?=${endsWithRe}))?`;
}

if (!isEndDelimited) {
route += `(?=${delimiter}|${endsWith})`;
route += `(?=${delimiterRe}|${endsWithRe})`;
}
}

Expand Down

0 comments on commit 86baef8

Please sign in to comment.