fix: pc端修改
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,33 @@
|
||||
import { differenceInMilliseconds, intlFormat } from 'date-fns';
|
||||
|
||||
export function formatBytes(bytes: number) {
|
||||
if (bytes === 0) return '0 B';
|
||||
|
||||
const k = 1024;
|
||||
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
|
||||
return (bytes / Math.pow(k, i)).toFixed(2) + ' ' + sizes[i];
|
||||
}
|
||||
|
||||
export function formatDate(date?: Date | number, showTime: boolean = true) {
|
||||
if (!date) return;
|
||||
return intlFormat(date, {
|
||||
year: 'numeric',
|
||||
month: 'numeric',
|
||||
day: 'numeric',
|
||||
...(showTime && {
|
||||
hour: 'numeric',
|
||||
minute: 'numeric',
|
||||
second: 'numeric',
|
||||
}),
|
||||
hour12: false,
|
||||
});
|
||||
}
|
||||
|
||||
export function differenceInDays(dateLeft: Date | number, dateRight: Date | number) {
|
||||
const diffInMs = differenceInMilliseconds(dateLeft, dateRight);
|
||||
const diffInDays = diffInMs / (1000 * 60 * 60 * 24);
|
||||
if (diffInDays >= 1) return diffInDays.toFixed(0);
|
||||
return Number(diffInDays.toFixed(2));
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
export * from '@workspace/airo-ui/utils/countries';
|
||||
export * from '@workspace/airo-ui/utils/formatting';
|
||||
export * from '@workspace/airo-ui/utils/unit-conversions';
|
||||
|
||||
export const isBrowser = () => typeof window !== 'undefined';
|
||||
|
||||
/**
|
||||
* Extracts the full domain or root domain from a URL.
|
||||
*
|
||||
* @param url - The URL to extract the domain from.
|
||||
* @param extractRoot - If true, extracts the root domain (e.g., example.com). If false, extracts the full domain (e.g., sub.example.com).
|
||||
* @returns The extracted domain or root domain, or null if the URL is invalid.
|
||||
*/
|
||||
export function extractDomain(url: string, extractRoot = true): string | null {
|
||||
try {
|
||||
const { hostname } = new URL(url);
|
||||
if (/^\d{1,3}(\.\d{1,3}){3}$/.test(hostname)) {
|
||||
return hostname;
|
||||
}
|
||||
const domainParts = hostname.split('.').filter(Boolean);
|
||||
if (extractRoot && domainParts.length > 2) {
|
||||
return domainParts.slice(-2).join('.');
|
||||
}
|
||||
return hostname;
|
||||
} catch (error) {
|
||||
console.error('Invalid URL:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { evaluate, format } from 'mathjs';
|
||||
|
||||
type ConversionType =
|
||||
| 'centsToDollars'
|
||||
| 'dollarsToCents'
|
||||
| 'bitsToMb'
|
||||
| 'mbToBits'
|
||||
| 'bytesToGb'
|
||||
| 'gbToBytes';
|
||||
|
||||
const conversionConfig: Record<ConversionType, { formula: string; precision: number }> = {
|
||||
centsToDollars: { formula: 'value / 100', precision: 2 },
|
||||
dollarsToCents: { formula: 'value * 100', precision: 0 },
|
||||
bitsToMb: { formula: 'value / 1024 / 1024', precision: 2 },
|
||||
mbToBits: { formula: 'value * 1024 * 1024', precision: 0 },
|
||||
bytesToGb: { formula: 'value / 1024 / 1024 / 1024', precision: 2 },
|
||||
gbToBytes: { formula: 'value * 1024 * 1024 * 1024', precision: 0 },
|
||||
};
|
||||
|
||||
export function unitConversion(type: ConversionType, value?: number | string) {
|
||||
if (!value) return 0;
|
||||
|
||||
const config = conversionConfig[type];
|
||||
if (!config) throw new Error('Invalid conversion type');
|
||||
|
||||
const formula = config.formula.replace('value', `${value}`);
|
||||
const result = evaluate(formula);
|
||||
return Number(format(result, { notation: 'fixed', precision: config.precision }));
|
||||
}
|
||||
|
||||
export function evaluateWithPrecision(expression: string) {
|
||||
const result = evaluate(expression);
|
||||
const formatted = format(result, { notation: 'fixed', precision: 2 });
|
||||
return Number(formatted);
|
||||
}
|
||||
Reference in New Issue
Block a user