Skip to content

Commit

Permalink
Default noop encode/decode, use ID start/continue
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed May 23, 2024
1 parent afd1402 commit 425266f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
23 changes: 14 additions & 9 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ const TESTS: Test[] = [
[
"/caf%C3%A9",
["/caf%C3%A9", "caf%C3%A9"],
{ path: "/caf%C3%A9", index: 0, params: { test: "café" } },
{ path: "/caf%C3%A9", index: 0, params: { test: "caf%C3%A9" } },
],
],
[
[{}, null],
[{ test: "abc" }, "/abc"],
[{ test: "a+b" }, "/a+b", { encode: (x) => x }],
[{ test: "a+b" }, "/a+b"],
[{ test: "a+b" }, "/test", { encode: () => "test" }],
[{ test: "a+b" }, "/a%2Bb", { encode: encodeURIComponent }],
],
Expand Down Expand Up @@ -272,7 +272,7 @@ const TESTS: Test[] = [
[
[{}, null],
[{ test: "abc" }, "/abc"],
[{ test: "a+b" }, "/a+b", { encode: (x) => x }],
[{ test: "a+b" }, "/a+b"],
[{ test: "a+b" }, "/test", { encode: () => "test" }],
[{ test: "a+b" }, "/a%2Bb", { encode: encodeURIComponent }],
],
Expand Down Expand Up @@ -1034,8 +1034,13 @@ const TESTS: Test[] = [
[
[{ test: "" }, "/"],
[{ test: "abc" }, "/abc"],
[{ test: "abc/123" }, "/abc%2F123"],
[{ test: "abc/123/456" }, "/abc%2F123%2F456"],
[{ test: "abc/123" }, "/abc/123"],
[{ test: "abc/123" }, "/abc%2F123", { encode: encodeURIComponent }],
[
{ test: "abc/123/456" },
"/abc%2F123%2F456",
{ encode: encodeURIComponent },
],
],
],
[
Expand Down Expand Up @@ -1639,7 +1644,7 @@ const TESTS: Test[] = [
")",
],
[["/route(\\123\\)", ["/route(\\123\\)", "123\\"]]],
[[["123\\"], "/route(\\123\\)", { encode: (x) => x }]],
[[["123\\"], "/route(\\123\\)"]],
],
[
"{/login}?",
Expand Down Expand Up @@ -2281,8 +2286,8 @@ const TESTS: Test[] = [
],
[["/café", ["/café", "café"]]],
[
[{ foo: "café" }, "/café", { encode: (x) => x }],
[{ foo: "café" }, "/caf%C3%A9"],
[{ foo: "café" }, "/café"],
[{ foo: "café" }, "/caf%C3%A9", { encode: encodeURIComponent }],
],
],
[
Expand Down Expand Up @@ -2792,7 +2797,7 @@ describe("path-to-regexp", () => {
it("should throw on missing name", () => {
expect(() => {
pathToRegexp.pathToRegexp("/:(test)");
}).toThrow(new TypeError("Missing parameter name at 1"));
}).toThrow(new TypeError("Missing parameter name at 2"));
});

it("should throw on nested groups", () => {
Expand Down
48 changes: 24 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const DEFAULT_PREFIXES = "./";
const DEFAULT_DELIMITER = "/";
const GROUPS_RE = /\((?:\?<(.*?)>)?(?!\?)/g;
const NOOP_VALUE = (value: string) => value;
const NAME_RE = /^[\p{L}\p{Nl}\p{Mn}\p{Mc}\p{Nd}\p{Pc}$]$/u;
const ID_START = /^[$_\p{ID_Start}]$/u;
const ID_CONTINUE = /^[$_\u200C\u200D\p{ID_Continue}]$/u;

/**
* Encode a string into another string.
Expand Down Expand Up @@ -133,56 +134,55 @@ function lexer(str: string) {
}

if (char === ":") {
let name = "";
let j = i + 1;
let name = chars[++i];

while (NAME_RE.test(chars[j])) {
name += chars[j++];
if (!ID_START.test(chars[i])) {
throw new TypeError(`Missing parameter name at ${i}`);
}

if (!name) throw new TypeError(`Missing parameter name at ${i}`);
while (ID_CONTINUE.test(chars[++i])) {
name += chars[i];
}

tokens.push({ type: "NAME", index: i, value: name });
i = j;
continue;
}

if (char === "(") {
const pos = i++;
let count = 1;
let pattern = "";
let j = i + 1;

if (chars[j] === "?") {
throw new TypeError(`Pattern cannot start with "?" at ${j}`);
if (chars[i] === "?") {
throw new TypeError(`Pattern cannot start with "?" at ${i}`);
}

while (j < chars.length) {
if (chars[j] === "\\") {
pattern += chars[j++] + chars[j++];
while (i < chars.length) {
if (chars[i] === "\\") {
pattern += chars[i++] + chars[i++];
continue;
}

if (chars[j] === ")") {
if (chars[i] === ")") {
count--;
if (count === 0) {
j++;
i++;
break;
}
} else if (chars[j] === "(") {
} else if (chars[i] === "(") {
count++;
if (chars[j + 1] !== "?") {
throw new TypeError(`Capturing groups are not allowed at ${j}`);
if (chars[i + 1] !== "?") {
throw new TypeError(`Capturing groups are not allowed at ${i}`);
}
}

pattern += chars[j++];
pattern += chars[i++];
}

if (count) throw new TypeError(`Unbalanced pattern at ${i}`);
if (!pattern) throw new TypeError(`Missing pattern at ${i}`);
if (count) throw new TypeError(`Unbalanced pattern at ${pos}`);
if (!pattern) throw new TypeError(`Missing pattern at ${pos}`);

tokens.push({ type: "PATTERN", index: i, value: pattern });
i = j;
continue;
}

Expand Down Expand Up @@ -440,7 +440,7 @@ function compileTokens<P extends ParamData>(
options: CompileOptions,
): PathFunction<P> {
const {
encode = encodeURIComponent,
encode = NOOP_VALUE,
validate = true,
loose = DEFAULT_DELIMITER,
} = options;
Expand Down Expand Up @@ -514,7 +514,7 @@ function matchRegexp<P extends ParamData>(
keys: Key[],
options: MatchOptions,
): MatchFunction<P> {
const { decode = decodeURIComponent, loose = DEFAULT_DELIMITER } = options;
const { decode = NOOP_VALUE, loose = DEFAULT_DELIMITER } = options;
const stringify = toStringify(loose);

const decoders = keys.map((key) => {
Expand Down

0 comments on commit 425266f

Please sign in to comment.