Skip to content

Commit

Permalink
Default encode/decode, allow disabling
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Jun 4, 2024
1 parent 578b072 commit e796ace
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 20 deletions.
72 changes: 64 additions & 8 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,22 @@ const COMPILE_TESTS: CompileTestSet[] = [
{ input: undefined, expected: null },
{ input: {}, expected: null },
{ input: { test: "123" }, expected: "/123" },
{ input: { test: "123/xyz" }, expected: null }, // Requires encoding.
{ input: { test: "123/xyz" }, expected: "/123%2Fxyz" },
],
},
{
path: "/:test",
options: { validate: false },
tests: [
{ input: undefined, expected: null },
{ input: {}, expected: null },
{ input: { test: "123" }, expected: "/123" },
{ input: { test: "123/xyz" }, expected: "/123%2Fxyz" },
],
},
{
path: "/:test",
options: { validate: false, encode: false },
tests: [
{ input: undefined, expected: null },
{ input: {}, expected: null },
Expand Down Expand Up @@ -116,16 +126,18 @@ const COMPILE_TESTS: CompileTestSet[] = [
},
{
path: "/:test?",
options: { encode: false },
tests: [
{ input: undefined, expected: "" },
{ input: {}, expected: "" },
{ input: { test: undefined }, expected: "" },
{ input: { test: "123" }, expected: "/123" },
{ input: { test: "123/xyz" }, expected: null }, // Requires encoding.
{ input: { test: "123/xyz" }, expected: null },
],
},
{
path: "/:test(.*)",
options: { encode: false },
tests: [
{ input: undefined, expected: null },
{ input: {}, expected: null },
Expand All @@ -134,6 +146,30 @@ const COMPILE_TESTS: CompileTestSet[] = [
{ input: { test: "123/xyz" }, expected: "/123/xyz" },
],
},
{
path: "/:test*",
tests: [
{ input: undefined, expected: "" },
{ input: {}, expected: "" },
{ input: { test: [] }, expected: "" },
{ input: { test: [""] }, expected: null },
{ input: { test: ["123"] }, expected: "/123" },
{ input: { test: "123/xyz" }, expected: null },
{ input: { test: ["123", "xyz"] }, expected: "/123/xyz" },
],
},
{
path: "/:test*",
options: { encode: false },
tests: [
{ input: undefined, expected: "" },
{ input: {}, expected: "" },
{ input: { test: "" }, expected: null },
{ input: { test: "123" }, expected: "/123" },
{ input: { test: "123/xyz" }, expected: "/123/xyz" },
{ input: { test: ["123", "xyz"] }, expected: null },
],
},
];

/**
Expand Down Expand Up @@ -235,7 +271,7 @@ const MATCH_TESTS: MatchTestSet[] = [
expected: {
path: "/caf%C3%A9",
index: 0,
params: { test: "caf%C3%A9" },
params: { test: "café" },
},
},
{
Expand Down Expand Up @@ -531,7 +567,7 @@ const MATCH_TESTS: MatchTestSet[] = [
expected: {
path: "/caf%C3%A9",
index: 0,
params: { test: "caf%C3%A9" },
params: { test: "café" },
},
},
],
Expand Down Expand Up @@ -2257,13 +2293,17 @@ const MATCH_TESTS: MatchTestSet[] = [
{
path: "/:foo",
options: {
decode: encodeURIComponent,
decode: false,
},
tests: [
{
input: "/café",
matches: ["/café", "café"],
expected: { path: "/café", index: 0, params: { foo: "caf%C3%A9" } },
input: "/caf%C3%A9",
matches: ["/caf%C3%A9", "caf%C3%A9"],
expected: {
path: "/caf%C3%A9",
index: 0,
params: { foo: "caf%C3%A9" },
},
},
],
},
Expand Down Expand Up @@ -2771,6 +2811,22 @@ const MATCH_TESTS: MatchTestSet[] = [
},
],
},
{
path: "*",
options: { decode: false },
tests: [
{
input: "/",
matches: ["/", "/"],
expected: { path: "/", index: 0, params: { "0": "/" } },
},
{
input: "/test",
matches: ["/test", "/test"],
expected: { path: "/test", index: 0, params: { "0": "/test" } },
},
],
},

/**
* No loose.
Expand Down
24 changes: 12 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export interface MatchOptions extends PathToRegexpOptions {
/**
* Function for decoding strings for params.
*/
decode?: Decode;
decode?: Decode | false;
}

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

type TokenType =
Expand Down Expand Up @@ -383,20 +383,21 @@ export type PathFunction<P extends ParamData> = (data?: P) => string;
*/
function tokenToFunction(
token: Token,
encode: Encode,
encode: Encode | false,
): (data: ParamData) => string {
if (typeof token === "string") {
return () => token;
}

const optional = token.modifier === "?" || token.modifier === "*";
const encodeValue = encode || NOOP_VALUE;

if (token.separator) {
if (encode && token.separator) {
const stringify = (value: string, index: number) => {
if (typeof value !== "string") {
throw new TypeError(`Expected "${token.name}/${index}" to be a string`);
}
return encode(value);
return encodeValue(value);
};

const compile = (value: unknown) => {
Expand Down Expand Up @@ -429,7 +430,7 @@ function tokenToFunction(
if (typeof value !== "string") {
throw new TypeError(`Expected "${token.name}" to be a string`);
}
return token.prefix + encode(value) + token.suffix;
return token.prefix + encodeValue(value) + token.suffix;
};

if (optional) {
Expand All @@ -454,9 +455,9 @@ function compileTokens<P extends ParamData>(
options: CompileOptions,
): PathFunction<P> {
const {
encode = NOOP_VALUE,
validate = true,
encode = encodeURIComponent,
loose = DEFAULT_DELIMITER,
validate = true,
} = options;
const reFlags = flags(options);
const stringify = toStringify(loose);
Expand Down Expand Up @@ -527,17 +528,16 @@ function matchRegexp<P extends ParamData>(
re: PathRegExp,
options: MatchOptions,
): MatchFunction<P> {
const { decode = NOOP_VALUE, loose = DEFAULT_DELIMITER } = options;
const { decode = decodeURIComponent, loose = DEFAULT_DELIMITER } = options;
const stringify = toStringify(loose);

const decoders = re.keys.map((key) => {
if (key.separator) {
if (decode && key.separator) {
const re = new RegExp(stringify(key.separator), "g");

return (value: string) => value.split(re).map(decode);
}

return decode;
return decode || NOOP_VALUE;
});

return function match(pathname: string) {
Expand Down

0 comments on commit e796ace

Please sign in to comment.