Skip to content

100% test coverage #396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: be/debug-original-path
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/cases.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1645,6 +1645,35 @@ export const MATCH_TESTS: MatchTestSet[] = [
},
],
},
{
path: "%25:foo..:bar",
options: {
delimiter: "%25",
},
tests: [
{
input: "%25hello..world",
expected: {
path: "%25hello..world",
params: { foo: "hello", bar: "world" },
},
},
{
input: "%25555..222",
expected: {
path: "%25555..222",
params: { foo: "555", bar: "222" },
},
},
{
input: "%25555....222%25",
expected: {
path: "%25555....222%25",
params: { foo: "555..", bar: "222" },
},
},
],
},

/**
* Array input is normalized.
Expand Down
8 changes: 8 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ describe("path-to-regexp", () => {
});
});

describe("stringify errors", () => {
it("should error on unknown token", () => {
expect(() =>
stringify({ tokens: [{ type: "unknown", value: "test" } as any] }),
).toThrow(new TypeError("Unknown token type: unknown"));
});
});

describe.each(PARSER_TESTS)(
"parse $path with $options",
({ path, options, expected }) => {
Expand Down
65 changes: 45 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,40 +619,65 @@ function negate(delimiter: string, backtrack: string) {
}

/**
* Stringify token data into a path string.
* Stringify an array of tokens into a path string.
*/
export function stringify(data: TokenData) {
return data.tokens
.map(function stringifyToken(token, index, tokens): string {
if (token.type === "text") return escapeText(token.value);
if (token.type === "group") {
return `{${token.tokens.map(stringifyToken).join("")}}`;
}
function stringifyTokens(tokens: Token[]): string {
let value = "";
let i = 0;

function name(value: string) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pulled name function out (which adds bytes) because it's needed to avoid a crash on name being missing before the token type error.

const isSafe = isNameSafe(value) && isNextNameSafe(tokens[i]);
return isSafe ? value : JSON.stringify(value);
}

while (i < tokens.length) {
const token = tokens[i++];

const isSafe =
isNameSafe(token.name) && isNextNameSafe(tokens[index + 1]);
const key = isSafe ? token.name : JSON.stringify(token.name);
if (token.type === "text") {
value += escapeText(token.value);
continue;
}

if (token.type === "param") return `:${key}`;
if (token.type === "wildcard") return `*${key}`;
throw new TypeError(`Unexpected token: ${token}`);
})
.join("");
if (token.type === "group") {
value += `{${stringifyTokens(token.tokens)}}`;
continue;
}

if (token.type === "param") {
value += `:${name(token.name)}`;
continue;
}

if (token.type === "wildcard") {
value += `*${name(token.name)}`;
continue;
}

throw new TypeError(`Unknown token type: ${(token as any).type}`);
}

return value;
}

/**
* Stringify token data into a path string.
*/
export function stringify(data: TokenData) {
return stringifyTokens(data.tokens);
}

/**
* Validate the parameter name contains valid ID characters.
*/
function isNameSafe(name: string) {
const [first, ...rest] = name;
if (!ID_START.test(first)) return false;
return rest.every((char) => ID_CONTINUE.test(char));
return ID_START.test(first) && rest.every((char) => ID_CONTINUE.test(char));
}

/**
* Validate the next token does not interfere with the current param name.
*/
function isNextNameSafe(token: Token | undefined) {
if (!token || token.type !== "text") return true;
return !ID_CONTINUE.test(token.value[0]);
if (token && token.type === "text") return !ID_CONTINUE.test(token.value[0]);
return true;
}