UNPKG

mongodb-rag-core

Version:

Common elements used by MongoDB Chatbot Framework components.

45 lines 1.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ensureProtocol = exports.normalizeUrl = void 0; // Regex used to get just the "front part" of a URL const optionalRegex = { REMOVE_HASH: /^[^#]+/, REMOVE_QUERY: /^[^?]+/, REMOVE_BOTH: /^[^?#]+/, }; /** Utility function that normalizes a URL. Removes http/s protocol, www, trailing backslashes. Optionally removes query string and hash fragment. */ function normalizeUrl({ url, removeHash = true, removeQueryString = true, }) { if (removeHash && removeQueryString) { url = (url.match(optionalRegex.REMOVE_BOTH) ?? [url])[0]; } else if (removeHash) { url = (url.match(optionalRegex.REMOVE_HASH) ?? [url])[0]; } else if (removeQueryString) { // Splitting on hash so we retain the hash fragment const [frontUrl, hashFragment] = url.split("#"); url = (frontUrl.match(optionalRegex.REMOVE_QUERY) ?? [url])[0]; url += hashFragment ? `#${hashFragment}` : ""; } return url .replace(/^https?:\/\//i, "") .replace(/^www\./, "") .replace(/\/+$/, ""); } exports.normalizeUrl = normalizeUrl; /** Adds protocol ("https://") to a URL if it is missing. Intended for use in URL constructor. */ function ensureProtocol(url) { if (url.startsWith("http://") || url.startsWith("https://")) { return url; } return `https://${url}`; } exports.ensureProtocol = ensureProtocol; //# sourceMappingURL=normalizeUrl.js.map