aboutsummaryrefslogtreecommitdiff
path: root/utils/numberUtils.js
blob: 597a5385e428342c7933e6220da2a2a4b41aa536 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
let utils = {
    numberWithCommas: function (x) {
        if (x === undefined) { return "" }
        var parts = x.toString().split(".");
        parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
        return parts.join(".");
    },
    addNotation: function (type, value) {
        let returnVal = value;
        let notList = [];
        if (type === "shortScale") {
            //notation type
            //do notation stuff here
            notList = [
                " Thousand",
                " Million",
                " Billion",
                " Trillion",
                " Quadrillion",
                " Quintillion"
            ];
        }

        if (type === "oneLetters") {
            notList = [" K", " M", " B", " T"];
        }

        let checkNum = 1000;

        if (type !== "none" && type !== "commas") {
            let notValue = notList[notList.length - 1];
            for (let u = notList.length; u >= 1; u--) {
                notValue = notList.shift();
                for (let o = 3; o >= 1; o--) {
                    if (value >= checkNum) {
                        returnVal = value / (checkNum / 100);
                        returnVal = Math.floor(returnVal);
                        returnVal = (returnVal / Math.pow(10, o)) * 10;
                        returnVal = +returnVal.toFixed(o - 1) + notValue;
                    }
                    checkNum *= 10;
                }
            }
        } else {
            returnVal = this.numberWithCommas(value.toFixed(0));
        }

        return returnVal;
    },
    timeSince: function (date) {
        if (typeof date !== 'object') {
            date = new Date(date);
        }

        var seconds = Math.floor((new Date() - date) / 1000);
        var intervalType;

        var interval = Math.floor(seconds / 31536000);
        interval = Math.floor(seconds / 86400);
        if (interval >= 1) {
            intervalType = 'd';
        } else {
            interval = Math.floor(seconds / 3600);
            if (interval >= 1) {
                intervalType = "h";
            } else {
                interval = Math.floor(seconds / 60);
                if (interval >= 1) {
                    intervalType = "m";
                } else {
                    interval = seconds;
                    intervalType = "s";
                }
            }
        }

        return interval + '' + intervalType;
    },
    timeSince2: (date) => {
        let time = Date.now() - date

        if (time > 30 * 60000) {
            return utils.timeNumber2(time)
        }
        return utils.timeNumber(time)
    },
    timeNumber: (time) => {
        let mins = Math.floor(time / 1000 / 60)
        let secs = Math.floor(time / 1000) % 60

        if (mins === 0) return secs + "s"
        return `${mins}m ${secs}s`
    },
    timeNumber2: (time) => {
        let hours = Math.floor(time / 1000 / 60 / 60)
        let mins = Math.floor(time / 1000 / 60) % 60

        if (hours === 0) return mins + "m"
        return `${hours}h ${mins}m`
    }
}
module.exports = utils