| 413 | * Creates a function to filter a target URL. |
| 414 | */ |
| 415 | export const createTargetFilter = ( |
| 416 | ...targetUrls: ReadonlyArray<string> |
| 417 | ): ((testUrl: string) => boolean) => { |
| 418 | const standardizeMatch = (aUrl: string) => { |
| 419 | aUrl = aUrl.toLowerCase(); |
| 420 | |
| 421 | const fileUrl = fileUrlToAbsolutePath(aUrl); |
| 422 | if (fileUrl) { |
| 423 | // Strip file:///, if present |
| 424 | aUrl = fileUrl; |
| 425 | } else if (isValidUrl(aUrl) && aUrl.includes('://')) { |
| 426 | // Strip the protocol, if present |
| 427 | aUrl = aUrl.substr(aUrl.indexOf('://') + 3); |
| 428 | } |
| 429 | |
| 430 | // Remove optional trailing / |
| 431 | if (aUrl.endsWith('/')) { |
| 432 | aUrl = aUrl.substr(0, aUrl.length - 1); |
| 433 | } |
| 434 | |
| 435 | return aUrl; |
| 436 | }; |
| 437 | |
| 438 | const escaped = targetUrls.map(url => |
| 439 | escapeRegexSpecialChars(standardizeMatch(url), '/*').replace(/\*/g, '.*'), |
| 440 | ); |
| 441 | const targetUrlRegex = new RegExp('^(' + escaped.join('|') + ')$', 'g'); |
| 442 | |
| 443 | return testUrl => { |
| 444 | targetUrlRegex.lastIndex = 0; |
| 445 | return targetUrlRegex.test(standardizeMatch(testUrl)); |
| 446 | }; |
| 447 | }; |