2021-07-19 22:02:02 -07:00
|
|
|
import UAParser from 'ua-parser-js';
|
|
|
|
|
2020-10-25 18:57:23 -07:00
|
|
|
export function formatIPAddress(ipAddress: string): string {
|
2021-07-19 22:02:02 -07:00
|
|
|
const ipAddressComponents = ipAddress.split(':');
|
2020-10-25 18:57:23 -07:00
|
|
|
|
|
|
|
// Wipe out the port component
|
|
|
|
ipAddressComponents[ipAddressComponents.length - 1] = '';
|
|
|
|
|
2021-07-19 22:02:02 -07:00
|
|
|
let ip = ipAddressComponents.join(':');
|
|
|
|
ip = ip.slice(0, ip.length - 1);
|
2020-10-26 16:13:25 -07:00
|
|
|
if (ip === '[::1]' || ip === '127.0.0.1') {
|
2021-07-19 22:02:02 -07:00
|
|
|
return 'Localhost';
|
2020-10-25 18:57:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return ip;
|
2020-10-28 00:53:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if obj is {}
|
|
|
|
export function isEmptyObject(obj) {
|
2020-12-29 14:59:43 -08:00
|
|
|
return !obj || (Object.keys(obj).length === 0 && obj.constructor === Object);
|
2020-10-28 00:53:24 -07:00
|
|
|
}
|
2020-11-02 17:23:32 -08:00
|
|
|
|
2023-05-20 21:15:25 -07:00
|
|
|
function padLeft(text, pad, size) {
|
2020-12-29 15:35:54 -08:00
|
|
|
return String(pad.repeat(size) + text).slice(-size);
|
|
|
|
}
|
2020-12-29 14:59:43 -08:00
|
|
|
|
2020-11-02 17:23:32 -08:00
|
|
|
export function parseSecondsToDurationString(seconds = 0) {
|
|
|
|
const finiteSeconds = Number.isFinite(+seconds) ? Math.abs(seconds) : 0;
|
|
|
|
|
|
|
|
const days = Math.floor(finiteSeconds / 86400);
|
|
|
|
const daysString = days > 0 ? `${days} day${days > 1 ? 's' : ''} ` : '';
|
|
|
|
|
|
|
|
const hours = Math.floor((finiteSeconds / 3600) % 24);
|
|
|
|
const hoursString = hours || days ? padLeft(`${hours}:`, '0', 3) : '';
|
|
|
|
|
|
|
|
const mins = Math.floor((finiteSeconds / 60) % 60);
|
|
|
|
const minString = padLeft(`${mins}:`, '0', 3);
|
|
|
|
|
|
|
|
const secs = Math.floor(finiteSeconds % 60);
|
|
|
|
const secsString = padLeft(`${secs}`, '0', 2);
|
|
|
|
|
|
|
|
return daysString + hoursString + minString + secsString;
|
|
|
|
}
|
2021-07-19 22:02:02 -07:00
|
|
|
|
|
|
|
export function formatUAstring(uaString: string) {
|
|
|
|
const parser = UAParser(uaString);
|
|
|
|
const { device, os, browser } = parser;
|
|
|
|
const { major: browserVersion, name } = browser;
|
|
|
|
const { version: osVersion, name: osName } = os;
|
|
|
|
const { model, type } = device;
|
2021-07-22 15:18:38 -07:00
|
|
|
|
2022-03-06 17:39:52 -08:00
|
|
|
if (uaString === 'libmpv') {
|
|
|
|
return 'mpv media player';
|
|
|
|
}
|
2021-07-22 15:18:38 -07:00
|
|
|
// Fallback to just displaying the raw agent string.
|
|
|
|
if (!name || !browserVersion || !osName) {
|
|
|
|
return uaString;
|
|
|
|
}
|
|
|
|
|
2021-07-19 22:02:02 -07:00
|
|
|
const deviceString = model || type ? ` (${model || type})` : '';
|
|
|
|
return `${name} ${browserVersion} on ${osName} ${osVersion}
|
|
|
|
${deviceString}`;
|
|
|
|
}
|