matchUrlPattern function exported ✓ 100.0%

Last updated: 2026-02-24T19:46:21.793Z

Metrics

LOC: 37 Complexity: 15 Params: 2 Coverage: 100.0% (20/20 lines, 23x executed)

Signature

matchUrlPattern(url: string, pattern: string): : boolean

Summary

Matches a URL against a glob-style pattern. Supports * as wildcard (matches any sequence of characters). Uses segment-based matching instead of dynamic regex to avoid ReDoS.

Architecture violations

View all

  • [warning] max-cyclomatic-complexity: 'matchUrlPattern' has cyclomatic complexity 15 (max 10)

Source Code

export function matchUrlPattern(url: string, pattern: string): boolean {
  if (!url || !pattern) return false;

  // Exact match fast path
  if (url === pattern) return true;

  // If no wildcards, fall back to a simple includes check
  if (!pattern.includes("*")) {
    return url.toLowerCase() === pattern.toLowerCase();
  }

  // Split by `*` and ensure each segment appears in order in the URL
  const segments = pattern.toLowerCase().split("*");
  let pos = 0;
  const lowerUrl = url.toLowerCase();

  for (let i = 0; i < segments.length; i++) {
    const segment = segments[i];
    if (!segment) continue;

    // First segment must be a prefix, last segment must be a suffix
    if (i === 0) {
      if (!lowerUrl.startsWith(segment)) return false;
      pos = segment.length;
    } else if (i === segments.length - 1) {
      if (!lowerUrl.endsWith(segment)) return false;
      // Ensure the suffix doesn't overlap with already-matched prefix
      if (lowerUrl.length - segment.length < pos) return false;
    } else {
      const idx = lowerUrl.indexOf(segment, pos);
      if (idx === -1) return false;
      pos = idx + segment.length;
    }
  }

  return true;
}

No outgoing dependencies.

Impact (Incoming)

graph LR matchUrlPattern["matchUrlPattern"] handle["handle"] getSavedForms["getSavedForms"] getIgnoredFields["getIgnoredFields"] getRules["getRules"] handle -->|uses| matchUrlPattern getSavedForms -->|uses| matchUrlPattern getIgnoredFields -->|uses| matchUrlPattern getRules -->|uses| matchUrlPattern style matchUrlPattern fill:#dbeafe,stroke:#2563eb,stroke-width:2px click matchUrlPattern "eb53b35bb6b3b7d8.html" click handle "57ebaea8374ad16b.html" click getSavedForms "9a1357b95618ee5e.html" click getIgnoredFields "4ae1979d28b4e80b.html" click getRules "69aeb172cce4aa1c.html"
SourceType
handle uses
getSavedForms uses
getIgnoredFields uses
getRules uses