🎉 chore(init): project initialization
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,25 @@
|
||||
import { intlFormat } from '@shadcn/ui/lib/date-fns';
|
||||
|
||||
export function formatBytes(bytes: number) {
|
||||
if (bytes === 0) return '0 B';
|
||||
const k = 1000, // or 1024
|
||||
sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
|
||||
i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
|
||||
return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + 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,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import { startOfMonth } from '@shadcn/ui/lib/date-fns';
|
||||
|
||||
export * from './countries';
|
||||
export * from './formatting';
|
||||
export * from './unit-conversions';
|
||||
|
||||
export const isBrowser = () => typeof window !== 'undefined';
|
||||
|
||||
export function getNextResetDate(startDate: Date | number) {
|
||||
let time = new Date(startDate);
|
||||
const resetDay = time.getDate();
|
||||
const currentDate = new Date();
|
||||
if (isNaN(time.getTime())) {
|
||||
throw new Error('Invalid start date');
|
||||
}
|
||||
if (currentDate.getDate() >= resetDay) {
|
||||
const startOfMonthNextReset = startOfMonth(currentDate);
|
||||
startOfMonthNextReset.setMonth(startOfMonthNextReset.getMonth() + 1);
|
||||
startOfMonthNextReset.setDate(resetDay);
|
||||
startOfMonthNextReset.setHours(time.getHours());
|
||||
startOfMonthNextReset.setMinutes(time.getMinutes());
|
||||
startOfMonthNextReset.setSeconds(time.getSeconds());
|
||||
return startOfMonthNextReset;
|
||||
} else {
|
||||
time.setMonth(currentDate.getMonth());
|
||||
return time;
|
||||
}
|
||||
}
|
||||
|
||||
export function extractDomain(url: string): string | null {
|
||||
try {
|
||||
const hostname = new URL(url).hostname;
|
||||
|
||||
if (hostname.match(/^\d{1,3}(\.\d{1,3}){3}$/)) {
|
||||
return hostname;
|
||||
}
|
||||
|
||||
const domainParts = hostname.split('.').filter(Boolean);
|
||||
|
||||
if (domainParts.length >= 2) {
|
||||
const topLevelDomain = domainParts.slice(-2).join('.');
|
||||
return topLevelDomain;
|
||||
}
|
||||
|
||||
return hostname;
|
||||
} catch (error) {
|
||||
console.error('Invalid URL:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { evaluate } from 'mathjs';
|
||||
|
||||
export function unitConversion(
|
||||
type: 'centsToDollars' | 'dollarsToCents' | 'bitsToMb' | 'mbToBits' | 'bytesToGb' | 'gbToBytes',
|
||||
value?: number | string,
|
||||
) {
|
||||
if (!value) return;
|
||||
switch (type) {
|
||||
case 'centsToDollars':
|
||||
return evaluate(`${value} / 100`);
|
||||
case 'dollarsToCents':
|
||||
return evaluate(`${value} * 100`);
|
||||
case 'bitsToMb':
|
||||
return evaluate(`${value} / 1000 / 1000`);
|
||||
case 'mbToBits':
|
||||
return evaluate(`${value} * 1000 * 1000`);
|
||||
case 'bytesToGb':
|
||||
return evaluate(`${value} / 1000 / 1000 / 1000`);
|
||||
case 'gbToBytes':
|
||||
return evaluate(`${value} * 1000 * 1000 * 1000`);
|
||||
default:
|
||||
throw new Error('Invalid conversion type');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user