Skip to content

Commit e796ace

Browse files
committed
Default encode/decode, allow disabling
1 parent 578b072 commit e796ace

File tree

2 files changed

+76
-20
lines changed

2 files changed

+76
-20
lines changed

src/index.spec.ts

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,22 @@ const COMPILE_TESTS: CompileTestSet[] = [
8181
{ input: undefined, expected: null },
8282
{ input: {}, expected: null },
8383
{ input: { test: "123" }, expected: "/123" },
84-
{ input: { test: "123/xyz" }, expected: null }, // Requires encoding.
84+
{ input: { test: "123/xyz" }, expected: "/123%2Fxyz" },
8585
],
8686
},
8787
{
8888
path: "/:test",
8989
options: { validate: false },
90+
tests: [
91+
{ input: undefined, expected: null },
92+
{ input: {}, expected: null },
93+
{ input: { test: "123" }, expected: "/123" },
94+
{ input: { test: "123/xyz" }, expected: "/123%2Fxyz" },
95+
],
96+
},
97+
{
98+
path: "/:test",
99+
options: { validate: false, encode: false },
90100
tests: [
91101
{ input: undefined, expected: null },
92102
{ input: {}, expected: null },
@@ -116,16 +126,18 @@ const COMPILE_TESTS: CompileTestSet[] = [
116126
},
117127
{
118128
path: "/:test?",
129+
options: { encode: false },
119130
tests: [
120131
{ input: undefined, expected: "" },
121132
{ input: {}, expected: "" },
122133
{ input: { test: undefined }, expected: "" },
123134
{ input: { test: "123" }, expected: "/123" },
124-
{ input: { test: "123/xyz" }, expected: null }, // Requires encoding.
135+
{ input: { test: "123/xyz" }, expected: null },
125136
],
126137
},
127138
{
128139
path: "/:test(.*)",
140+
options: { encode: false },
129141
tests: [
130142
{ input: undefined, expected: null },
131143
{ input: {}, expected: null },
@@ -134,6 +146,30 @@ const COMPILE_TESTS: CompileTestSet[] = [
134146
{ input: { test: "123/xyz" }, expected: "/123/xyz" },
135147
],
136148
},
149+
{
150+
path: "/:test*",
151+
tests: [
152+
{ input: undefined, expected: "" },
153+
{ input: {}, expected: "" },
154+
{ input: { test: [] }, expected: "" },
155+
{ input: { test: [""] }, expected: null },
156+
{ input: { test: ["123"] }, expected: "/123" },
157+
{ input: { test: "123/xyz" }, expected: null },
158+
{ input: { test: ["123", "xyz"] }, expected: "/123/xyz" },
159+
],
160+
},
161+
{
162+
path: "/:test*",
163+
options: { encode: false },
164+
tests: [
165+
{ input: undefined, expected: "" },
166+
{ input: {}, expected: "" },
167+
{ input: { test: "" }, expected: null },
168+
{ input: { test: "123" }, expected: "/123" },
169+
{ input: { test: "123/xyz" }, expected: "/123/xyz" },
170+
{ input: { test: ["123", "xyz"] }, expected: null },
171+
],
172+
},
137173
];
138174

139175
/**
@@ -235,7 +271,7 @@ const MATCH_TESTS: MatchTestSet[] = [
235271
expected: {
236272
path: "/caf%C3%A9",
237273
index: 0,
238-
params: { test: "caf%C3%A9" },
274+
params: { test: "café" },
239275
},
240276
},
241277
{
@@ -531,7 +567,7 @@ const MATCH_TESTS: MatchTestSet[] = [
531567
expected: {
532568
path: "/caf%C3%A9",
533569
index: 0,
534-
params: { test: "caf%C3%A9" },
570+
params: { test: "café" },
535571
},
536572
},
537573
],
@@ -2257,13 +2293,17 @@ const MATCH_TESTS: MatchTestSet[] = [
22572293
{
22582294
path: "/:foo",
22592295
options: {
2260-
decode: encodeURIComponent,
2296+
decode: false,
22612297
},
22622298
tests: [
22632299
{
2264-
input: "/café",
2265-
matches: ["/café", "café"],
2266-
expected: { path: "/café", index: 0, params: { foo: "caf%C3%A9" } },
2300+
input: "/caf%C3%A9",
2301+
matches: ["/caf%C3%A9", "caf%C3%A9"],
2302+
expected: {
2303+
path: "/caf%C3%A9",
2304+
index: 0,
2305+
params: { foo: "caf%C3%A9" },
2306+
},
22672307
},
22682308
],
22692309
},
@@ -2771,6 +2811,22 @@ const MATCH_TESTS: MatchTestSet[] = [
27712811
},
27722812
],
27732813
},
2814+
{
2815+
path: "*",
2816+
options: { decode: false },
2817+
tests: [
2818+
{
2819+
input: "/",
2820+
matches: ["/", "/"],
2821+
expected: { path: "/", index: 0, params: { "0": "/" } },
2822+
},
2823+
{
2824+
input: "/test",
2825+
matches: ["/test", "/test"],
2826+
expected: { path: "/test", index: 0, params: { "0": "/test" } },
2827+
},
2828+
],
2829+
},
27742830

27752831
/**
27762832
* No loose.

src/index.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export interface MatchOptions extends PathToRegexpOptions {
5454
/**
5555
* Function for decoding strings for params.
5656
*/
57-
decode?: Decode;
57+
decode?: Decode | false;
5858
}
5959

6060
export interface CompileOptions extends ParseOptions {
@@ -73,7 +73,7 @@ export interface CompileOptions extends ParseOptions {
7373
/**
7474
* Function for encoding input strings for output into the path. (default: `encodeURIComponent`)
7575
*/
76-
encode?: Encode;
76+
encode?: Encode | false;
7777
}
7878

7979
type TokenType =
@@ -383,20 +383,21 @@ export type PathFunction<P extends ParamData> = (data?: P) => string;
383383
*/
384384
function tokenToFunction(
385385
token: Token,
386-
encode: Encode,
386+
encode: Encode | false,
387387
): (data: ParamData) => string {
388388
if (typeof token === "string") {
389389
return () => token;
390390
}
391391

392392
const optional = token.modifier === "?" || token.modifier === "*";
393+
const encodeValue = encode || NOOP_VALUE;
393394

394-
if (token.separator) {
395+
if (encode && token.separator) {
395396
const stringify = (value: string, index: number) => {
396397
if (typeof value !== "string") {
397398
throw new TypeError(`Expected "${token.name}/${index}" to be a string`);
398399
}
399-
return encode(value);
400+
return encodeValue(value);
400401
};
401402

402403
const compile = (value: unknown) => {
@@ -429,7 +430,7 @@ function tokenToFunction(
429430
if (typeof value !== "string") {
430431
throw new TypeError(`Expected "${token.name}" to be a string`);
431432
}
432-
return token.prefix + encode(value) + token.suffix;
433+
return token.prefix + encodeValue(value) + token.suffix;
433434
};
434435

435436
if (optional) {
@@ -454,9 +455,9 @@ function compileTokens<P extends ParamData>(
454455
options: CompileOptions,
455456
): PathFunction<P> {
456457
const {
457-
encode = NOOP_VALUE,
458-
validate = true,
458+
encode = encodeURIComponent,
459459
loose = DEFAULT_DELIMITER,
460+
validate = true,
460461
} = options;
461462
const reFlags = flags(options);
462463
const stringify = toStringify(loose);
@@ -527,17 +528,16 @@ function matchRegexp<P extends ParamData>(
527528
re: PathRegExp,
528529
options: MatchOptions,
529530
): MatchFunction<P> {
530-
const { decode = NOOP_VALUE, loose = DEFAULT_DELIMITER } = options;
531+
const { decode = decodeURIComponent, loose = DEFAULT_DELIMITER } = options;
531532
const stringify = toStringify(loose);
532533

533534
const decoders = re.keys.map((key) => {
534-
if (key.separator) {
535+
if (decode && key.separator) {
535536
const re = new RegExp(stringify(key.separator), "g");
536-
537537
return (value: string) => value.split(re).map(decode);
538538
}
539539

540-
return decode;
540+
return decode || NOOP_VALUE;
541541
});
542542

543543
return function match(pathname: string) {

0 commit comments

Comments
 (0)