2019-08-31 20:51:14 -07:00
|
|
|
/**
|
|
|
|
* Generates an absolute URL from a passed in relative url. If url is already absolute,
|
|
|
|
* same url is returned. An IIFE is used to ensure that multiple invocations of the function
|
|
|
|
* do not create multiple anchor elements that will need to be garbage collected
|
|
|
|
* @export
|
|
|
|
* @type {(url: string) => string}
|
|
|
|
*/
|
2020-08-26 15:44:50 -07:00
|
|
|
export const getAbsoluteUrl: (url: string) => string = ((): ((url: string) => string) => {
|
2019-08-31 20:51:14 -07:00
|
|
|
let anchor: HTMLAnchorElement;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returned closure function extracts the absolute url from the anchor element
|
|
|
|
* in the closure.
|
|
|
|
* @param {string} url
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
return (url: string): string => {
|
|
|
|
if (!anchor) {
|
|
|
|
anchor = document.createElement('a');
|
|
|
|
}
|
|
|
|
anchor.href = url;
|
|
|
|
|
|
|
|
return anchor.href;
|
|
|
|
};
|
|
|
|
})();
|