Skip to content

Commit b893429

Browse files
committed
Code cleanup and consistency with optional empty strings
1 parent 80a1eb6 commit b893429

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

src/index.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,7 @@ const TESTS: Test[] = [
12151215
[
12161216
["route", ["route", "route"]],
12171217
["/route", null],
1218-
["", ["", undefined]],
1218+
["", ["", ""]],
12191219
["route/foobar", null],
12201220
],
12211221
[
@@ -1888,9 +1888,9 @@ const TESTS: Test[] = [
18881888
],
18891889
[
18901890
["/test", null],
1891-
["/test/", ["/test/", undefined, undefined]],
1892-
["/test/u123", ["/test/u123", "u123", undefined]],
1893-
["/test/c123", ["/test/c123", undefined, "c123"]],
1891+
["/test/", ["/test/", "", ""]],
1892+
["/test/u123", ["/test/u123", "u123", ""]],
1893+
["/test/c123", ["/test/c123", "", "c123"]],
18941894
],
18951895
[
18961896
[{ uid: "u123" }, "/test/u123"],
@@ -2060,7 +2060,7 @@ const TESTS: Test[] = [
20602060
],
20612061
[
20622062
["/foobaz", ["/foobaz", "foo"]],
2063-
["/baz", ["/baz", undefined]],
2063+
["/baz", ["/baz", ""]],
20642064
],
20652065
[
20662066
[{}, "/baz"],
@@ -2090,7 +2090,7 @@ const TESTS: Test[] = [
20902090
],
20912091
[
20922092
["/hello(world)", ["/hello(world)", "hello", "world"]],
2093-
["/hello()", ["/hello()", "hello", undefined]],
2093+
["/hello()", ["/hello()", "hello", ""]],
20942094
],
20952095
[
20962096
[{ foo: "hello", bar: "world" }, "/hello(world)"],
@@ -2117,7 +2117,7 @@ const TESTS: Test[] = [
21172117
},
21182118
],
21192119
[
2120-
["/video", ["/video", "video", undefined]],
2120+
["/video", ["/video", "video", ""]],
21212121
["/video+test", ["/video+test", "video", "+test"]],
21222122
["/video+", null],
21232123
],
@@ -2545,7 +2545,7 @@ const TESTS: Test[] = [
25452545
},
25462546
],
25472547
[
2548-
["/user/123", ["/user/123", undefined, "123"]],
2548+
["/user/123", ["/user/123", "", "123"]],
25492549
["/users/123", ["/users/123", "s", "123"]],
25502550
],
25512551
[[{ user: "123" }, "/user/123"]],

src/index.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
const DEFAULT_PREFIXES = "./";
2+
const DEFAULT_DELIMITER = "/#?";
3+
const DEFAULT_ENCODE = (x: string) => x;
4+
const DEFAULT_DECODE = (x: string) => x;
5+
16
/**
27
* Tokenizer results.
38
*/
@@ -138,10 +143,11 @@ export interface ParseOptions {
138143
* Parse a string for the raw tokens.
139144
*/
140145
export function parse(str: string, options: ParseOptions = {}): Token[] {
141-
const tokens = lexer(str);
142-
const { prefixes = "./" } = options;
143-
const defaultPattern = `[^${escapeString(options.delimiter || "/#?")}]+?`;
146+
const { prefixes = DEFAULT_PREFIXES, delimiter = DEFAULT_DELIMITER } =
147+
options;
148+
const defaultPattern = `[^${escapeString(delimiter)}]+?`;
144149
const result: Token[] = [];
150+
const tokens = lexer(str);
145151
let key = 0;
146152
let i = 0;
147153
let path = "";
@@ -265,7 +271,7 @@ export function tokensToFunction<P extends object = object>(
265271
options: TokensToFunctionOptions = {},
266272
): PathFunction<P> {
267273
const reFlags = flags(options);
268-
const { encode = (x: string) => x, validate = true } = options;
274+
const { encode = DEFAULT_ENCODE, validate = true } = options;
269275

270276
// Compile all the tokens into regexps.
271277
const matches = tokens.map((token) => {
@@ -388,7 +394,7 @@ export function regexpToFunction<P extends object = object>(
388394
keys: Key[],
389395
options: RegexpToFunctionOptions = {},
390396
): MatchFunction<P> {
391-
const { decode = (x: string) => x } = options;
397+
const { decode = DEFAULT_DECODE } = options;
392398

393399
return function (pathname: string) {
394400
const m = re.exec(pathname);
@@ -452,19 +458,17 @@ function regexpToRegexp(path: RegExp, keys?: Key[]): RegExp {
452458
if (!keys) return path;
453459

454460
const groupsRegex = /\((?:\?<(.*?)>)?(?!\?)/g;
455-
456461
let index = 0;
457-
let execResult = groupsRegex.exec(path.source);
458-
while (execResult) {
462+
let execResult: RegExpExecArray | null = null;
463+
while ((execResult = groupsRegex.exec(path.source))) {
459464
keys.push({
460-
// Use parenthesized substring match if available, index otherwise
465+
// Use parenthesized substring match if available, index otherwise.
461466
name: execResult[1] || index++,
462467
prefix: "",
463468
suffix: "",
464469
modifier: "",
465470
pattern: "",
466471
});
467-
execResult = groupsRegex.exec(path.source);
468472
}
469473

470474
return path;
@@ -536,8 +540,8 @@ export function tokensToRegexp(
536540
strict = false,
537541
start = true,
538542
end = true,
539-
encode = (x: string) => x,
540-
delimiter = "/#?",
543+
encode = DEFAULT_ENCODE,
544+
delimiter = DEFAULT_DELIMITER,
541545
endsWith = "",
542546
} = options;
543547
const endsWithRe = `[${escapeString(endsWith)}]|$`;
@@ -563,11 +567,7 @@ export function tokensToRegexp(
563567
route += `(?:${prefix}(${token.pattern})${suffix})${token.modifier}`;
564568
}
565569
} else {
566-
if (token.modifier === "+" || token.modifier === "*") {
567-
route += `((?:${token.pattern})${token.modifier})`;
568-
} else {
569-
route += `(${token.pattern})${token.modifier}`;
570-
}
570+
route += `((?:${token.pattern})${token.modifier})`;
571571
}
572572
} else {
573573
route += `(?:${prefix}${suffix})${token.modifier}`;
@@ -578,13 +578,13 @@ export function tokensToRegexp(
578578
if (end) {
579579
if (!strict) route += `${delimiterRe}?`;
580580

581-
route += !options.endsWith ? "$" : `(?=${endsWithRe})`;
581+
route += options.endsWith ? `(?=${endsWithRe})` : "$";
582582
} else {
583583
const endToken = tokens[tokens.length - 1];
584584
const isEndDelimited =
585585
typeof endToken === "string"
586-
? delimiterRe.indexOf(endToken[endToken.length - 1]) > -1
587-
: endToken === undefined;
586+
? delimiter.indexOf(endToken[endToken.length - 1]) > -1
587+
: !endToken;
588588

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

0 commit comments

Comments
 (0)