Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 9x 9x 7x 7x 16x 6x 10x 17x 17x 2x 15x | import url from 'node:url';
export default function pathTemplater(str: string, options: Record<string, string> = {}) {
const parsedUrl = url.parse(str);
parsedUrl.pathname = replace(parsedUrl.pathname, options);
parsedUrl.search = replace(parsedUrl.search, options);
return url.format(parsedUrl);
}
function replace(str: string, options: Record<string, string>) {
if (!str) {
return str;
}
return str.replace(/:(\w+)/gi, (match, p1) => {
const replacement = options[p1];
if (!replacement) {
throw new Error('Could not find url parameter ' + p1 + ' in passed options object');
}
return replacement;
});
} |