INNER CODE UNIT · JavaScript
TRACKING_PARAM
pegaltier/awesome-utils-dev · node_scripts/wip-dedupe.js:27
const TRACKING_PARAM = /^(utm_|gad_|gbraid|wbraid|gclid|fbclid|msclkid|mc_cid|mc_eid|igshid|si|ref|ref_src|source)/i;
const dryRun = process.argv.includes('--dry-run');
/** Canonical form of a URL, so cosmetic differences still match. */
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]))