social-butterfly
Version:
Incorporate federated social network protocols easily. Used with Hello, world federated blog.
43 lines (35 loc) • 1.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.buildUrl = buildUrl;
exports.prettifyUrl = prettifyUrl;
exports.ensureAbsoluteUrl = ensureAbsoluteUrl;
function buildUrl({
req,
isAbsolute,
pathname,
searchParams
}) {
let url = '';
if (req) {
const protocol = req.get('x-scheme') || req.protocol;
url += `${protocol}://${req.get('host')}`;
} else if (isAbsolute) {
url += `${window.location.origin}`;
}
url += !pathname || pathname.startsWith('/') ? pathname : new URL(pathname).pathname;
if (searchParams) {
url += '?' + new URLSearchParams(searchParams).toString();
}
return prettifyUrl(url);
}
function prettifyUrl(url) {
return url.replace(/ /g, '+');
}
function ensureAbsoluteUrl(basisAbsoluteUrl, urlOrPath) {
const parsedUrl = new URL(basisAbsoluteUrl);
const hostnameAndProtocol = `${parsedUrl.protocol}//${parsedUrl.host}`;
urlOrPath = urlOrPath || '';
return urlOrPath[0] === '/' ? `${hostnameAndProtocol}${urlOrPath}` : urlOrPath;
}