UNPKG

998 BJavaScriptView Raw
1import escapeStringRegexp from 'escape-string-regexp';
2export default function extractPathFromURL(prefixes, url) {
3 for (const prefix of prefixes) {
4 var _prefix$match$, _prefix$match;
5
6 const protocol = (_prefix$match$ = (_prefix$match = prefix.match(/^[^:]+:/)) === null || _prefix$match === void 0 ? void 0 : _prefix$match[0]) !== null && _prefix$match$ !== void 0 ? _prefix$match$ : '';
7 const host = prefix.replace(new RegExp(`^${escapeStringRegexp(protocol)}`), '').replace(/\/+/g, '/') // Replace multiple slash (//) with single ones
8 .replace(/^\//, ''); // Remove extra leading slash
9
10 const prefixRegex = new RegExp(`^${escapeStringRegexp(protocol)}(/)*${host.split('.').map(it => it === '*' ? '[^/]+' : escapeStringRegexp(it)).join('\\.')}`);
11 const normalizedURL = url.replace(/\/+/g, '/');
12
13 if (prefixRegex.test(normalizedURL)) {
14 return normalizedURL.replace(prefixRegex, '');
15 }
16 }
17
18 return undefined;
19}
20//# sourceMappingURL=extractPathFromURL.js.map
\No newline at end of file