🎉 feat: initialization

This commit is contained in:
web
2025-11-26 19:56:16 -08:00
commit a801849fb2
553 changed files with 213088 additions and 0 deletions
File diff suppressed because it is too large Load Diff
+36
View File
@@ -0,0 +1,36 @@
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 / k ** i).toFixed(2)} ${sizes[i]}`;
}
export function formatDate(date?: Date | number, showTime = 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));
}
+25
View File
@@ -0,0 +1,25 @@
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;
}
}
+40
View File
@@ -0,0 +1,40 @@
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);
}