aboutsummaryrefslogtreecommitdiff
path: root/src/FaunerieUtilities.ts
blob: 1c84300c65c227e76033d85e801fa8c834885eb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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 `<span style='float: right; margin-right: 10px; font-size: 1rem; margin-top: 5px;' class='badge bg-danger'>GIF</span>`;

            case "image/jpeg":
                return `<span style='float: right; margin-right: 10px; font-size: 1rem; margin-top: 5px;' class='badge bg-warning'>JPEG</span>`;

            case "image/png":
                return `<span style='float: right; margin-right: 10px; font-size: 1rem; margin-top: 5px;' class='badge bg-primary'>PNG</span>`;

            case "image/svg+xml":
                return `<span style='float: right; margin-right: 10px; font-size: 1rem; margin-top: 5px;' class='badge bg-light'>SVG</span>`;

            case "video/webm":
                return `<span style='float: right; margin-right: 10px; font-size: 1rem; margin-top: 5px;' class='badge bg-info'>WebM</span>`;

            default:
                return `<span style='float: right; margin-right: 10px; font-size: 1rem; margin-top: 5px;' class='badge bg-secondary'>Unknown</span>`;
        }
    }
}