mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-11 18:13:47 +00:00
Extract shlink-web-component outside of src folder
This commit is contained in:
41
shlink-web-component/visits/utils/index.ts
Normal file
41
shlink-web-component/visits/utils/index.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import bowser from 'bowser';
|
||||
import { zipObj } from 'ramda';
|
||||
import type { Empty } from '../../../src/utils/utils';
|
||||
import { hasValue } from '../../../src/utils/utils';
|
||||
import type { Stats, UserAgent } from '../types';
|
||||
|
||||
const DEFAULT = 'Others';
|
||||
const BROWSERS_WHITELIST = [
|
||||
'Android Browser',
|
||||
'Chrome',
|
||||
'Chromium',
|
||||
'Firefox',
|
||||
'Internet Explorer',
|
||||
'Microsoft Edge',
|
||||
'Opera',
|
||||
'Safari',
|
||||
'Samsung Internet for Android',
|
||||
'Vivaldi',
|
||||
'WeChat',
|
||||
];
|
||||
|
||||
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 };
|
||||
};
|
||||
|
||||
export const extractDomain = (url: string | Empty): string => {
|
||||
if (!hasValue(url)) {
|
||||
return 'Direct';
|
||||
}
|
||||
|
||||
return url.split('/')[url.includes('://') ? 2 : 0]?.split(':')[0] ?? '';
|
||||
};
|
||||
|
||||
export const fillTheGaps = (stats: Stats, labels: string[]): number[] =>
|
||||
Object.values({ ...zipObj(labels, labels.map(() => 0)), ...stats });
|
||||
Reference in New Issue
Block a user