able-baker/url-remapper.tsx

41 lines
1.2 KiB
TypeScript

function remapUrl({ input, mapFrom, mapTo }: { input: string, mapFrom: string, mapTo: string}): string | undefined
{
const partsInput = input.split('/');
const partsMapFrom = mapFrom.split('/');
const partsMapTo = mapTo.split('/');
if(partsInput.length == partsMapFrom.length)
{
for(let i=0; i<partsInput.length; i++)
{
const partInput = partsInput[i];
const partMapFrom = partsMapFrom[i];
if(partMapFrom.startsWith(":"))
{
const indexMapTo = partsMapTo.indexOf(partMapFrom);
if(indexMapTo > -1)
{
const partMapTo = partsMapTo[indexMapTo];
if(partMapTo === partMapFrom)
{
partsMapTo[indexMapTo] = partInput;
continue;
}
}
return;
}
}
return partsMapTo.join("/");
}
}
const remap: string | undefined = remapUrl({
input: "/base/theproj/app01",
mapFrom: "/base/:project/:id",
mapTo: "https://vcs.com/remote/raw/:project/show/:id"
});
console.log(remap); // Output: "https://vcs.com/remote/raw/theproj/show/app01"