aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/utils.js
blob: 8c7fe0fc656f1e848490bb3afb8ad6c2de9a399b (plain) (blame)
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
103
104
105
106
107
108
109
110
111
112
113
const timeRegex = /^(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/;

export function createCurlCommand(subCommand, method = null, json = null, data = null) {
    const full = window.location.protocol + '//' + window.location.hostname + (window.location.port ? ':' + window.location.port : '');

    let contentType = null;
    let content = null;
    if (json != null) {
        contentType = '    -H "Content-Type: application/json" \\\n';
        content = `    -d '${JSON.stringify(json)}'`;
    } else if (data != null) {
        contentType = '    -H "Content-Type: multipart/form-data" \\\n';
        content = "    " + Object.entries(data).map(([key, value]) => `-F "${key}=${value}"`).join(" \\\n    ");
    }

    return `curl${method != null ? " -X " + method : ""} "${full}/api${subCommand}" \\\n` + contentType + "" + content;
}

export function validateIpAddress(ipAddress) {
    let regex = /((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)|(^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))/;
    return regex.test(ipAddress);
}

export function validate24HourTime(time) {
    return timeRegex.test(time);
}

export function cleanNumber(number) {
    return number.replace(/[^\d]/gi, "").replace(/^0+/g, "");
}

export function validateMin(min) {
    return function (value) {
        return parseInt(value, 10) > min;
    };
}

export function validateMax(max) {
    return function (value) {
        return parseInt(value, 10) < max;
    };
}

export function validatePort(port) {
    return validateMin(0)(port) && validateMax(65565)(port);
}

export function timeToTimestamp(time) {
    let d = new Date();
    let matches = time.match(timeRegex);

    if (matches[1] !== undefined) {
        d.setHours(matches[1]);
    }
    if (matches[2] !== undefined) {
        d.setMinutes(matches[2]);
    }
    if (matches[3] !== undefined) {
        d.setSeconds(matches[3]);
    }

    return Math.round(d.getTime() / 1000);
}

export function timestampToTime(timestamp) {
    let d = new Date(timestamp * 1000);
    let hours = d.getHours();
    let minutes = "0" + d.getMinutes();
    let seconds = "0" + d.getSeconds();
    return hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
}

export function timestampToDateTime(timestamp) {
    let d = new Date(timestamp);
    return d.toLocaleDateString() + " " + d.toLocaleTimeString();
}

export function dateTimeToTime(dateTime) {
    if (typeof dateTime === "string") {
        dateTime = new Date(dateTime);
    }

    let hours = dateTime.getHours();
    let minutes = "0" + dateTime.getMinutes();
    let seconds = "0" + dateTime.getSeconds();
    return hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
}

export function durationBetween(from, to) {
    if (typeof from === "string") {
        from = new Date(from);
    }
    if (typeof to === "string") {
        to = new Date(to);
    }
    const duration = ((to - from) / 1000).toFixed(3);

    return (duration > 1000 || duration < -1000) ? "∞" : duration + "s";
}

export function formatSize(size) {
    if (size < 1000) {
        return `${size}`;
    } else if (size < 1000000) {
        return `${(size / 1000).toFixed(1)}K`;
    } else {
        return `${(size / 1000000).toFixed(1)}M`;
    }
}

export function randomClassName() {
    return Math.random().toString(36).slice(2);
}