More elements migrated to typescript

This commit is contained in:
Alejandro Celaya
2020-08-22 19:03:25 +02:00
parent 62df46d648
commit 2eba607874
9 changed files with 47 additions and 45 deletions

View File

@@ -1,6 +1,6 @@
import bowser from 'bowser';
import { zipObj } from 'ramda';
import { hasValue } from '../utils';
import { Empty, hasValue } from '../utils';
const DEFAULT = 'Others';
const BROWSERS_WHITELIST = [
@@ -17,17 +17,22 @@ const BROWSERS_WHITELIST = [
'WeChat',
];
export const parseUserAgent = (userAgent) => {
interface UserAgent {
browser: string;
os: string;
}
export const parseUserAgent = (userAgent: string | Empty): UserAgent => {
if (!hasValue(userAgent)) {
return { browser: DEFAULT, os: DEFAULT };
}
const { browser: { name: browser }, os: { name: os } } = bowser.parse(userAgent);
return { os: os || DEFAULT, browser: browser && BROWSERS_WHITELIST.includes(browser) ? browser : DEFAULT };
return { os: os ?? DEFAULT, browser: browser && BROWSERS_WHITELIST.includes(browser) ? browser : DEFAULT };
};
export const extractDomain = (url) => {
export const extractDomain = (url: string | Empty): string => {
if (!hasValue(url)) {
return 'Direct';
}
@@ -37,4 +42,5 @@ export const extractDomain = (url) => {
return domain.split(':')[0];
};
export const fillTheGaps = (stats, labels) => Object.values({ ...zipObj(labels, labels.map(() => 0)), ...stats });
export const fillTheGaps = (stats: Record<string, number>, labels: string[]): number[] =>
Object.values({ ...zipObj(labels, labels.map(() => 0)), ...stats });