umami/lib/filters.js
2020-08-06 19:37:19 -07:00

21 lines
770 B
JavaScript

import { BROWSERS, ISO_COUNTRIES, DEVICES } from './constants';
export const browserFilter = data =>
data.map(({ x, ...props }) => ({ x: BROWSERS[x] || x, ...props }));
export const urlFilter = data => data.filter(({ x }) => x !== '' && !x.startsWith('#'));
export const refFilter = data =>
data.filter(({ x }) => x !== '' && !x.startsWith('/') && !x.startsWith('#'));
export const deviceFilter = data =>
data.map(({ x, ...props }) => ({ x: DEVICES[x] || x, ...props }));
export const countryFilter = data =>
data.map(({ x, ...props }) => ({ x: ISO_COUNTRIES[x] || x, ...props }));
export const percentFilter = data => {
const total = data.reduce((n, { y }) => n + y, 0);
return data.map(({ x, y }) => ({ x, y, z: total ? (y / total) * 100 : 0 }));
};