UNPKG

browser-extension-url-match

Version:

Browser extension URL pattern matching

42 lines (41 loc) 1.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDummyUrl = getDummyUrl; var _fancyRegex = require("fancy-regex"); const DELIMS = /^|$|[/?=&\-]/; function getDummyUrl(patternSegments, replacements = {}) { const { rawHost, rawPathAndQuery } = patternSegments; const { defaultScheme = 'https', subdomain = '', pathAndQueryReplacer = '', rootDomain = 'example.com', strict = true } = replacements; let host; const scheme = patternSegments.scheme === '*' ? defaultScheme : patternSegments.scheme; if (scheme === 'file') { host = ''; } else if (rawHost === '*') { host = [subdomain, rootDomain].filter(Boolean).join('.'); } else { host = rawHost.replace(/^\*./, subdomain ? `${subdomain}.` : ''); } const pathAndQuery = (strict ? rawPathAndQuery : '/*' // start with hyphen-delimited ).replace(/\*/g, `-${pathAndQueryReplacer}-`) // remove consecutive hyphens and hyphens adjacent to delimiters .replace((0, _fancyRegex.regex)('g')`-+(${DELIMS})`, '$1').replace((0, _fancyRegex.regex)('g')`(${DELIMS})-+`, '$1') // remove consecutive slashes .replace(/\/+/g, '/'); try { return new URL(`${scheme}://${host}${pathAndQuery}`); } catch (_e) { return null; } }