Skip to content

Commit 03d2b97

Browse files
committed
Optimize path function to handle templates without parameters more efficiently.
1 parent 14b8455 commit 03d2b97

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,22 @@ export function subst(template: string, params: ParamMap): string {
155155
}
156156

157157
function path(template: string, params: ParamMap) {
158+
// Fast path: if the template has no colons, no params to substitute
159+
if (template.indexOf(':') === -1) {
160+
return { renderedPath: template, remainingParams: params }
161+
}
162+
158163
const remainingParams = { ...params }
164+
159165
const renderedPath = template.replace(/:[_A-Za-z]+\w*/g, (p) => {
160166
const key = p.slice(1)
167+
161168
validatePathParam(params, key)
169+
162170
delete remainingParams[key]
163171
return encodeURIComponent(params[key] as string | number | boolean)
164172
})
173+
165174
return { renderedPath, remainingParams }
166175
}
167176

0 commit comments

Comments
 (0)