INNER CODE UNIT · JavaScript

normalizeUrl

pegaltier/awesome-utils-dev · node_scripts/wip-dedupe.js:32

function normalizeUrl(raw) {
    let s = raw.trim().replace(/^<+/, '').replace(/>+$/, '').replace(/[.,;:)\]]+$/, '');
    let u;
    try {
        u = new URL(s);
    } catch (e) {
        return null;
    }
    const host = u.hostname.toLowerCase().replace(/^www\./, '');
    const pathname = u.pathname.replace(/\/+$/, '');
    const params = [...u.searchParams.entries()]
        .filter(([k]) => !TRACKING_PARAM.test(k))
        .sort((a, b) => (a[0] + a[1]).localeCompare(b[0] + b[1]))
        .map(([k, v]) => `${k}=${v}`);
    // fragments are anchors inside the same page -> ignored for matching
    return host + pathname + (params.length ? '?' + params.join('&') : '');
}

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…