Resolving Relative URLs with a Base URL in TypeScript
Many web applications utilize relative URLs for resources like images, scripts, and stylesheets. This challenge asks you to implement a function that resolves these relative URLs against a provided base URL, producing absolute URLs. This is a common task in frontend development and understanding how to handle it correctly is crucial for building robust web applications.
Problem Description
You need to create a TypeScript function called resolveUrl that takes two string arguments: baseUrl and relativeUrl. The function should return a single string representing the absolute URL formed by resolving the relativeUrl against the baseUrl.
What needs to be achieved:
The function must combine the baseUrl and relativeUrl to create a complete, absolute URL. It should handle various scenarios, including:
relativeUrlstarting with a forward slash/: In this case, thebaseUrl's hostname and protocol should be discarded, and therelativeUrlshould be appended directly.relativeUrlstarting with../: This indicates navigating up the directory structure. The function should remove segments from thebaseUrluntil therelativeUrlis fully resolved.relativeUrlstarting with a single/: Similar to the first case, thebaseUrl's hostname and protocol should be discarded, and therelativeUrlshould be appended directly.relativeUrlbeing an absolute URL (starting withhttp://orhttps://): In this case, the function should simply return therelativeUrlunchanged.baseUrlbeing an empty string: The function should return therelativeUrlas is, if it's not an absolute URL. IfrelativeUrlis an absolute URL, return it unchanged.relativeUrlbeing an empty string: The function should return thebaseUrlas is.
Key Requirements:
- The function must be written in TypeScript.
- The function must handle all the scenarios described above correctly.
- The function should be robust and avoid errors when given unexpected input.
- The function should return a string.
Expected Behavior:
The function should return a valid absolute URL string. The URL should be properly formatted, with the correct protocol, hostname, and path.
Edge Cases to Consider:
- URLs with query parameters and fragments.
- URLs with encoded characters.
- Very long URLs.
- Invalid URLs (though the function doesn't need to validate the URL's validity, it should still resolve it as best as possible).
Examples
Example 1:
Input: baseUrl = "https://www.example.com/path/to/resource", relativeUrl = "images/logo.png"
Output: "https://www.example.com/path/to/resource/images/logo.png"
Explanation: The relative URL is appended to the base URL's path.
Example 2:
Input: baseUrl = "https://www.example.com/path/to/resource", relativeUrl = "/images/logo.png"
Output: "/images/logo.png"
Explanation: The relative URL starts with a forward slash, so the base URL's hostname and protocol are discarded.
Example 3:
Input: baseUrl = "https://www.example.com/path/to/resource", relativeUrl = "../images/logo.png"
Output: "https://www.example.com/path/images/logo.png"
Explanation: The relative URL navigates up one directory level.
Example 4:
Input: baseUrl = "https://www.example.com/path/to/resource", relativeUrl = "https://www.anotherdomain.com/images/logo.png"
Output: "https://www.anotherdomain.com/images/logo.png"
Explanation: The relative URL is an absolute URL, so it's returned unchanged.
Example 5:
Input: baseUrl = "", relativeUrl = "images/logo.png"
Output: "images/logo.png"
Explanation: Base URL is empty, so relative URL is returned.
Example 6:
Input: baseUrl = "https://www.example.com", relativeUrl = ""
Output: "https://www.example.com"
Explanation: Relative URL is empty, so base URL is returned.
Constraints
baseUrlandrelativeUrlare strings.- The length of
baseUrlandrelativeUrlcan be up to 2048 characters. - The function should execute in under 100 milliseconds for typical URLs.
- The function should not throw errors.
Notes
- Consider using string manipulation methods like
split,join, andstartsWithto efficiently resolve the URLs. - Pay close attention to the edge cases involving forward slashes and
../. - You don't need to validate the final URL's validity (e.g., whether it points to a real resource). Focus on correctly resolving the relative path.
- Think about how to handle cases where the
baseUrlends with a forward slash and therelativeUrlalso starts with one.