MCPcopy
hub / github.com/darkreader/darkreader / indexURLTemplateList

Function indexURLTemplateList

src/utils/url.ts:391–486  ·  view source on GitHub ↗
(list: string[], assign: ((pattern: string, index: number) => T) = () => true as T)

Source from the content-addressed store, hash-verified

389export type URLTemplateIndex = URLTrie<boolean>;
390
391export function indexURLTemplateList<T = boolean>(list: string[], assign: ((pattern: string, index: number) => T) = () => true as T): URLTrie<T> {
392 const trie: URLTrie<T> = {
393 key: '',
394 hostNodes: new Map(),
395 pathNodes: new Map(),
396 hardPatterns: [],
397 regexps: [],
398 data: null,
399 };
400
401 const templateIndices = new Map<PreparedPattern | RegExp, number>();
402
403 const patterns: PreparedPattern[] = [];
404 list.forEach((u, i) => {
405 if (isRegExp(u)) {
406 const r = createRegExp(u);
407 if (r) {
408 trie.regexps.push({regexp: r, data: assign(list[i], i)});
409 }
410 } else {
411 const p = preparePattern(u);
412 if (p) {
413 if (p.exactStart || p.exactEnd || (p.port && p.port !== '*') || p.protocol) {
414 trie.hardPatterns.push({pattern: p, data: assign(list[i], i)});
415 return;
416 }
417 patterns.push(p);
418 templateIndices.set(p, i);
419 }
420 }
421 });
422
423 patterns.forEach((pattern) => {
424 const listIndex = templateIndices.get(pattern)!;
425 const data = assign(list[listIndex], listIndex);
426
427 let node: URLTrieNode = trie;
428 pattern.hostParts.forEach((p) => {
429 const nodes = node.hostNodes;
430 if (nodes.has(p)) {
431 node = nodes.get(p)!;
432 } else {
433 node = {
434 key: p,
435 hostNodes: new Map(),
436 pathNodes: new Map(),
437 data: null,
438 };
439 nodes.set(p, node);
440 }
441 });
442 let lastHostNode = node.hostNodes.get('');
443 if (!lastHostNode) {
444 lastHostNode = {
445 key: '',
446 hostNodes: new Map(),
447 pathNodes: new Map(),
448 data: null,

Callers 4

handleDarkSitesMethod · 0.90
indexSitesFixesConfigFunction · 0.90
url.tests.tsFile · 0.90
isInListOptimizedFunction · 0.85

Calls 4

isRegExpFunction · 0.85
setMethod · 0.65
getMethod · 0.65
hasMethod · 0.65

Tested by

no test coverage detected