Sootzani/lib/utils.ts

84 lines
1.8 KiB
TypeScript

import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import jalaliday from "jalaliday";
import JSEncrypt from "jsencrypt";
import { getCookie } from "cookies-next";
dayjs.extend(jalaliday);
dayjs.extend(utc);
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
export function convertDate(date: string) {
const convertedDate = dayjs(date + "Z")
.calendar("jalali")
.locale("fa")
.format("YYYY/M/DD");
return convertedDate;
}
export function convertFullTime(date: string) {
const convertedDate = dayjs(date + "Z")
.calendar("jalali")
.locale("fa")
.format(" HH:mm - YYYY/M/DD");
return convertedDate;
}
export function Decrypt(encryptedData: string, privateKey: string) {
const decryptor = new JSEncrypt();
decryptor.setPrivateKey(privateKey);
const decrypted = decryptor.decrypt(encryptedData);
return decrypted;
}
export function isAuthorized() {
const token = getCookie("Sootzani_Token");
if (token) {
return true;
} else {
false;
}
}
export function bytesToMB(bytes: number) {
return Math.floor(bytes / (1024 * 1024));
}
export function formatBytes(bytes: number, decimals = 2) {
console.log(bytes);
if (!+bytes) return "0 Bytes";
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = [
"بایت",
"کیلوبایت",
"مگابایت",
"گیگابایت",
"TiB",
"PiB",
"EiB",
"ZiB",
"YiB",
];
// const sizes = [
// "Bytes",
// "KB",
// "MB",
// "GiB",
// "TiB",
// "PiB",
// "EiB",
// "ZiB",
// "YiB",
// ];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
}