From 914bcbb474f6f186c212b2da0d9d864b5e75d8e4 Mon Sep 17 00:00:00 2001 From: RaindropsSys Date: Sat, 29 Jun 2024 20:59:16 +0200 Subject: Rename to Faunerie --- src/FaunerieUtilities.ts | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100755 src/FaunerieUtilities.ts (limited to 'src/FaunerieUtilities.ts') diff --git a/src/FaunerieUtilities.ts b/src/FaunerieUtilities.ts new file mode 100755 index 0000000..1c84300 --- /dev/null +++ b/src/FaunerieUtilities.ts @@ -0,0 +1,75 @@ +export class FaunerieUtilities { + static timeAgo(time: number | Date | string) { + if (!isNaN(parseInt(time as string))) { + time = new Date(time).getTime(); + } + + let periods = ["second", "minute", "hour", "day", "week", "month", "year", "age"]; + + let lengths = [60, 60, 24, 7, 4.35, 12, 100]; + + let now = new Date().getTime(); + + let difference = Math.round((now - (time as number)) / 1000); + let tense: string; + let period: string; + + if (difference <= 10 && difference >= 0) { + return "now"; + } else if (difference > 0) { + tense = "ago"; + } else { + tense = "later"; + } + + let j: number; + + for (j = 0; difference >= lengths[j] && j < lengths.length - 1; j++) { + difference /= lengths[j]; + } + + difference = Math.round(difference); + + period = periods[j]; + + return `${difference} ${period}${difference > 1 ? "s" : ""} ${tense}`; + } + + static formatSize(size: number) { + let sizeString: string; + + if (size > 1024 ** 3) { + sizeString = (size / 1024 ** 3).toFixed(1) + " GB"; + } else if (size > 1024 ** 2) { + sizeString = (size / 1024 ** 2).toFixed(1) + " MB"; + } else if (size > 1024) { + sizeString = (size / 1024).toFixed(0) + " KB"; + } else { + sizeString = size + " B"; + } + + return sizeString ?? size; + } + + static getMimeBadge(type: string) { + switch (type) { + case "image/gif": + return `GIF`; + + case "image/jpeg": + return `JPEG`; + + case "image/png": + return `PNG`; + + case "image/svg+xml": + return `SVG`; + + case "video/webm": + return `WebM`; + + default: + return `Unknown`; + } + } +} -- cgit