add url remapper file

This commit is contained in:
Seth Trowbridge 2023-07-24 17:41:13 -04:00
parent c3e5319d47
commit 915fdf2d54

41
url-remapper.tsx Normal file
View File

@ -0,0 +1,41 @@
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"