Filter subdomains in referrers regex test. Closes #632

pull/1030/head
Mike Cao 2022-03-17 19:27:43 -07:00
parent e38f08b8f6
commit 7530de87d2
1 changed files with 7 additions and 8 deletions

View File

@ -1,5 +1,5 @@
import { BROWSERS } from './constants'; import { BROWSERS } from './constants';
import { removeTrailingSlash, removeWWW, getDomainName } from './url'; import { removeTrailingSlash, removeWWW } from './url';
export const urlFilter = (data, { raw }) => { export const urlFilter = (data, { raw }) => {
const isValidUrl = url => { const isValidUrl = url => {
@ -46,23 +46,18 @@ export const urlFilter = (data, { raw }) => {
}; };
export const refFilter = (data, { domain, domainOnly, raw }) => { export const refFilter = (data, { domain, domainOnly, raw }) => {
const domainName = getDomainName(domain); const regex = new RegExp(`http[s]?://([a-z0-9-]+\\.)*${domain}`);
const regex = new RegExp(`http[s]?://${domainName}`);
const links = {}; const links = {};
const isValidRef = ref => { const isValidRef = ref => {
return ref !== '' && ref !== null && !ref.startsWith('/') && !ref.startsWith('#'); return ref !== '' && ref !== null && !ref.startsWith('/') && !ref.startsWith('#');
}; };
if (raw) {
return data.filter(({ x }) => isValidRef(x) && !regex.test(x));
}
const cleanUrl = url => { const cleanUrl = url => {
try { try {
const { hostname, origin, pathname, searchParams, protocol } = new URL(url); const { hostname, origin, pathname, searchParams, protocol } = new URL(url);
if (hostname === domainName) { if (regex.test(url)) {
return null; return null;
} }
@ -88,6 +83,10 @@ export const refFilter = (data, { domain, domainOnly, raw }) => {
} }
}; };
if (raw) {
return data.filter(({ x }) => isValidRef(x) && !regex.test(x));
}
const map = data.reduce((obj, { x, y }) => { const map = data.reduce((obj, { x, y }) => {
if (!isValidRef(x)) { if (!isValidRef(x)) {
return obj; return obj;