Skip to content

Commit dfb620c

Browse files
committed
Encode using identity function by default
1 parent e1ccbe1 commit dfb620c

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

src/index.spec.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,9 @@ const TESTS: Test[] = [
189189
[
190190
[{}, null],
191191
[{ test: "abc" }, "/abc"],
192-
[{ test: "a+b" }, "/a+b", { encode: value => value }],
192+
[{ test: "a+b" }, "/a+b"],
193193
[{ test: "a+b" }, "/test", { encode: (_, token) => String(token.name) }],
194-
[{ test: "a+b" }, "/a%2Bb"]
194+
[{ test: "a+b" }, "/a%2Bb", { encode: encodeURIComponent }]
195195
]
196196
],
197197
[
@@ -285,9 +285,9 @@ const TESTS: Test[] = [
285285
[
286286
[{}, null],
287287
[{ test: "abc" }, "/abc"],
288-
[{ test: "a+b" }, "/a+b", { encode: value => value }],
288+
[{ test: "a+b" }, "/a+b"],
289289
[{ test: "a+b" }, "/test", { encode: (_, token) => String(token.name) }],
290-
[{ test: "a+b" }, "/a%2Bb"]
290+
[{ test: "a+b" }, "/a%2Bb", { encode: encodeURIComponent }]
291291
]
292292
],
293293
[
@@ -576,8 +576,16 @@ const TESTS: Test[] = [
576576
],
577577
[
578578
[{ test: "route" }, "/route"],
579-
[{ test: "something/else" }, "/something%2Felse"],
580-
[{ test: "something/else/more" }, "/something%2Felse%2Fmore"]
579+
[
580+
{ test: "something/else" },
581+
"/something%2Felse",
582+
{ encode: encodeURIComponent }
583+
],
584+
[
585+
{ test: "something/else/more" },
586+
"/something%2Felse%2Fmore",
587+
{ encode: encodeURIComponent }
588+
]
581589
]
582590
],
583591
[
@@ -1050,8 +1058,12 @@ const TESTS: Test[] = [
10501058
[
10511059
[{ test: "" }, "/"],
10521060
[{ test: "abc" }, "/abc"],
1053-
[{ test: "abc/123" }, "/abc%2F123"],
1054-
[{ test: "abc/123/456" }, "/abc%2F123%2F456"]
1061+
[{ test: "abc/123" }, "/abc%2F123", { encode: encodeURIComponent }],
1062+
[
1063+
{ test: "abc/123/456" },
1064+
"/abc%2F123%2F456",
1065+
{ encode: encodeURIComponent }
1066+
]
10551067
]
10561068
],
10571069
[
@@ -2059,7 +2071,7 @@ const TESTS: Test[] = [
20592071
],
20602072
[
20612073
[{ foo: "foo" }, "/foobaz"],
2062-
[{ foo: "foo/bar" }, "/foo%2Fbarbaz"],
2074+
[{ foo: "foo/bar" }, "/foo%2Fbarbaz", { encode: encodeURIComponent }],
20632075
[{ foo: ["foo", "bar"] }, "/foo/barbaz"]
20642076
]
20652077
],
@@ -2236,7 +2248,10 @@ const TESTS: Test[] = [
22362248
}
22372249
],
22382250
[["/café", ["/café", "café"]]],
2239-
[[{ foo: "café" }, "/caf%C3%A9"]]
2251+
[
2252+
[{ foo: "café" }, "/café"],
2253+
[{ foo: "café" }, "/caf%C3%A9", { encode: encodeURIComponent }]
2254+
]
22402255
],
22412256
["/café", undefined, ["/café"], [["/café", ["/café"]]], [[null, "/café"]]],
22422257
[

src/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ export function tokensToFunction<P extends object = object>(
212212
options: TokensToFunctionOptions = {}
213213
): PathFunction<P> {
214214
const reFlags = flags(options);
215-
const { encode = encodeURIComponent, validate = true } = options;
215+
const { encode = (x: string) => x, validate = true } = options;
216216

217217
// Compile all the tokens into regexps.
218218
const matches = tokens.map(token => {
@@ -452,7 +452,8 @@ export function tokensToRegexp(
452452
strict,
453453
start = true,
454454
end = true,
455-
delimiter = DEFAULT_DELIMITER
455+
delimiter = DEFAULT_DELIMITER,
456+
encode = (x: string) => x
456457
} = options;
457458
const endsWith = (typeof options.endsWith === "string"
458459
? options.endsWith.split("")
@@ -466,7 +467,7 @@ export function tokensToRegexp(
466467
// Iterate over the tokens and create our regexp string.
467468
for (const token of tokens) {
468469
if (typeof token === "string") {
469-
route += escapeString(token);
470+
route += escapeString(encode(token));
470471
} else {
471472
const capture = token.repeat
472473
? `(?:${token.pattern})(?:${escapeString(token.delimiter)}(?:${
@@ -537,6 +538,10 @@ export interface RegexpOptions {
537538
* List of characters that can also be "end" characters.
538539
*/
539540
endsWith?: string | string[];
541+
/**
542+
* Encode path tokens for use in the `RegExp`.
543+
*/
544+
encode?: (value: string) => string;
540545
}
541546

542547
export interface ParseOptions {

0 commit comments

Comments
 (0)