2020-08-23 04:05:07 +02:00
|
|
|
import firstBy from 'thenby';
|
2020-08-07 04:37:19 +02:00
|
|
|
import { BROWSERS, ISO_COUNTRIES, DEVICES } from './constants';
|
2020-08-29 06:34:20 +02:00
|
|
|
import { removeTrailingSlash, getDomainName } from './url';
|
2020-08-01 12:34:56 +02:00
|
|
|
|
2020-08-29 06:34:20 +02:00
|
|
|
export const urlFilter = (data, { raw }) => {
|
2020-08-23 04:05:07 +02:00
|
|
|
const isValidUrl = url => {
|
|
|
|
return url !== '' && !url.startsWith('#');
|
|
|
|
};
|
2020-08-01 12:34:56 +02:00
|
|
|
|
2020-08-23 07:01:14 +02:00
|
|
|
if (raw) {
|
|
|
|
return data.filter(({ x }) => isValidUrl(x));
|
|
|
|
}
|
|
|
|
|
2020-08-23 04:05:07 +02:00
|
|
|
const cleanUrl = url => {
|
|
|
|
try {
|
2020-09-03 00:13:45 +02:00
|
|
|
const { pathname, search, searchParams } = new URL(url);
|
|
|
|
|
|
|
|
if (search.startsWith('?/')) {
|
|
|
|
return `${pathname}${search}`;
|
|
|
|
}
|
2020-08-23 04:05:07 +02:00
|
|
|
|
|
|
|
const path = removeTrailingSlash(pathname);
|
|
|
|
const ref = searchParams.get('ref');
|
|
|
|
const query = ref ? `?ref=${ref}` : '';
|
|
|
|
|
|
|
|
return `${path}${query}`;
|
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const map = data.reduce((obj, { x, y }) => {
|
|
|
|
if (!isValidUrl(x)) {
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2020-08-29 06:34:20 +02:00
|
|
|
const url = cleanUrl(`http://x${x}`);
|
2020-08-23 04:05:07 +02:00
|
|
|
|
|
|
|
if (url) {
|
|
|
|
if (!obj[url]) {
|
|
|
|
obj[url] = y;
|
|
|
|
} else {
|
|
|
|
obj[url] += y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
return Object.keys(map)
|
|
|
|
.map(key => ({ x: key, y: map[key] }))
|
|
|
|
.sort(firstBy('y', -1).thenBy('x'));
|
|
|
|
};
|
|
|
|
|
2020-08-23 07:01:14 +02:00
|
|
|
export const refFilter = (data, { domain, domainOnly, raw }) => {
|
2020-08-29 06:34:20 +02:00
|
|
|
const domainName = getDomainName(domain);
|
|
|
|
const regex = new RegExp(`http[s]?://${domainName}`);
|
2020-08-28 08:45:37 +02:00
|
|
|
|
2020-08-23 04:05:07 +02:00
|
|
|
const isValidRef = ref => {
|
|
|
|
return ref !== '' && !ref.startsWith('/') && !ref.startsWith('#');
|
|
|
|
};
|
|
|
|
|
2020-08-23 07:01:14 +02:00
|
|
|
if (raw) {
|
|
|
|
return data.filter(({ x }) => isValidRef(x) && !regex.test(x));
|
|
|
|
}
|
|
|
|
|
2020-08-23 04:05:07 +02:00
|
|
|
const cleanUrl = url => {
|
|
|
|
try {
|
|
|
|
const { hostname, origin, pathname, searchParams, protocol } = new URL(url);
|
|
|
|
|
2020-08-29 06:34:20 +02:00
|
|
|
if (hostname === domainName) {
|
2020-08-23 04:05:07 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-08-23 07:01:14 +02:00
|
|
|
if (domainOnly && hostname) {
|
2020-08-23 04:05:07 +02:00
|
|
|
return hostname;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!origin || origin === 'null') {
|
|
|
|
return `${protocol}${removeTrailingSlash(pathname)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (protocol.startsWith('http')) {
|
|
|
|
const path = removeTrailingSlash(pathname);
|
|
|
|
const ref = searchParams.get('ref');
|
|
|
|
const query = ref ? `?ref=${ref}` : '';
|
|
|
|
|
|
|
|
return `${origin}${path}${query}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const map = data.reduce((obj, { x, y }) => {
|
|
|
|
if (!isValidRef(x)) {
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
const url = cleanUrl(x);
|
|
|
|
|
|
|
|
if (url) {
|
|
|
|
if (!obj[url]) {
|
|
|
|
obj[url] = y;
|
|
|
|
} else {
|
|
|
|
obj[url] += y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
return Object.keys(map)
|
|
|
|
.map(key => ({ x: key, y: map[key] }))
|
|
|
|
.sort(firstBy('y', -1).thenBy('x'));
|
2020-08-07 11:46:21 +02:00
|
|
|
};
|
2020-08-01 12:34:56 +02:00
|
|
|
|
2020-08-24 03:01:27 +02:00
|
|
|
export const browserFilter = data =>
|
|
|
|
data.map(({ x, y }) => ({ x: BROWSERS[x], y })).filter(({ x }) => x);
|
|
|
|
|
|
|
|
export const osFilter = data => data.filter(({ x }) => x);
|
|
|
|
|
2020-08-07 04:37:19 +02:00
|
|
|
export const deviceFilter = data =>
|
2020-08-24 03:01:27 +02:00
|
|
|
data.map(({ x, y }) => ({ x: DEVICES[x], y })).filter(({ x }) => x);
|
2020-08-01 12:34:56 +02:00
|
|
|
|
|
|
|
export const countryFilter = data =>
|
2020-08-24 03:01:27 +02:00
|
|
|
data.map(({ x, y }) => ({ x: ISO_COUNTRIES[x], y })).filter(({ x }) => x);
|
2020-08-01 12:34:56 +02:00
|
|
|
|
|
|
|
export const percentFilter = data => {
|
|
|
|
const total = data.reduce((n, { y }) => n + y, 0);
|
2020-08-25 08:49:14 +02:00
|
|
|
return data.map(({ x, y, ...props }) => ({ x, y, z: total ? (y / total) * 100 : 0, ...props }));
|
2020-08-01 12:34:56 +02:00
|
|
|
};
|