Skip to content

Commit 4997ef8

Browse files
committed
refactor
1 parent 179b526 commit 4997ef8

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

packages/react-router/lib/href.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,24 @@ export function href<Path extends keyof Args>(
3131
.replace(/\/*\*?$/, "") // Ignore trailing / and /*, we'll handle it below
3232
.replace(
3333
/\/:([\w-]+)(\?)?/g, // same regex as in .\router\utils.ts: compilePath().
34-
(_: string, param: string, isOptional) => {
34+
(_: string, param: string, questionMark: string | undefined) => {
35+
const isRequired = questionMark === undefined;
3536
const value = params ? params[param] : undefined;
36-
if (isOptional == null && value == null) {
37+
if (isRequired && value === undefined) {
3738
throw new Error(
3839
`Path '${path}' requires param '${param}' but it was not provided`,
3940
);
4041
}
41-
return value == null ? "" : "/" + value;
42+
return value === undefined ? "" : "/" + value;
4243
},
4344
);
4445

4546
if (path.endsWith("*")) {
4647
// treat trailing splat the same way as compilePath, and force it to be as if it were `/*`.
4748
// `react-router typegen` will not generate the params for a malformed splat, causing a type error, but we can still do the correct thing here.
48-
if (params && params["*"] != null) {
49-
result += "/" + params["*"];
49+
const value = params ? params["*"] : undefined;
50+
if (value !== undefined) {
51+
result += "/" + value;
5052
}
5153
}
5254

0 commit comments

Comments
 (0)