I have theses functions that helps me to replace dynamic parameters in URL path in a…
- User
I have theses functions that helps me to replace dynamic parameters in URL path in a localized context. Help me to find some good naming, to improve clarity.
/**
-
captureDynamicParameters("project/foo", "/project/[slug]/") returns // {slug: "foo"} */ function captureDynamicParameters(text: string, template: string): Record<string, string> { const regexString = template.replace(/[([^]]+)]/g, "(?<$1>[^/]+)"); const regex = new RegExp(regexString); const matchValues = regex.exec(text);
return matchValues?.groups ?? {}; }
/**
- setDynamicParameters("projet/[slug]", {slug: "foo"}) returns "projet/foo"
*/
function setDynamicParameters(template: string, capturedValues: Record<string, string>): string {
let result = template;
for (const key in capturedValues) {
const value = capturedValues[key];
result = result.replaceAll(
[${key}], value); } return result; }
-
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.