-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.js
executable file
·124 lines (102 loc) · 3.62 KB
/
functions.js
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
114
115
116
117
118
119
120
121
122
123
124
const { AbortController } = require('abort-controller');
// const fetch = require('node-fetch');
const fetch = (...args) => import("node-fetch").then(module => module.default(...args));
// Shuffle function
function shuffle(array) {
let currentIndex = array.length;
let temporaryValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function log(...messages) {
// Get the current timestamp
const timestamp = new Date().toISOString();
// Format the messages with the timestamp
console.log(`[${timestamp}]`, ...messages);
}
function compareVersion(version1, version2) {
const v1 = version1.split('.').map(Number);
const v2 = version2.split('.').map(Number);
const len = Math.max(v1.length, v2.length);
for (let i = 0; i < len; i++) {
const num1 = v1[i] || 0; // Treat missing parts as 0
const num2 = v2[i] || 0;
if (num1 > num2) {
return 1;
} else if (num1 < num2) {
return -1;
}
}
return 0; // Versions are equal
}
function limitStringMaxLength(s, len) {
if (s.length <= len) {
return s;
}
return s.slice(0, len) + "...";
}
function secondsToTimeDict(seconds) {
const timeDict = {};
// Define time unit conversions
const SECONDS_IN_MINUTE = 60;
const SECONDS_IN_HOUR = 3600;
const SECONDS_IN_DAY = 86400;
const SECONDS_IN_MONTH = 2592000; // 30 days
const SECONDS_IN_YEAR = 31536000; // 365 days
// Calculate each time unit
timeDict.years = Math.floor(seconds / SECONDS_IN_YEAR);
seconds %= SECONDS_IN_YEAR;
timeDict.months = Math.floor(seconds / SECONDS_IN_MONTH);
seconds %= SECONDS_IN_MONTH;
timeDict.days = Math.floor(seconds / SECONDS_IN_DAY);
seconds %= SECONDS_IN_DAY;
timeDict.hours = Math.floor(seconds / SECONDS_IN_HOUR);
seconds %= SECONDS_IN_HOUR;
timeDict.minutes = Math.floor(seconds / SECONDS_IN_MINUTE);
timeDict.seconds = seconds % SECONDS_IN_MINUTE;
return timeDict;
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function isObjectEmptyOrNullOrUndefined(obj) {
// console.log(isObjectEmptyOrNullOrUndefined(null)); // true
// console.log(isObjectEmptyOrNullOrUndefined(undefined)); // true
// console.log(isObjectEmptyOrNullOrUndefined({})); // true (empty object)
// console.log(isObjectEmptyOrNullOrUndefined([])); // false (an array is not considered empty)
// console.log(isObjectEmptyOrNullOrUndefined({ a: 1 })); // false (not empty)
// console.log(isObjectEmptyOrNullOrUndefined(0)); // false (not an object)
// console.log(isObjectEmptyOrNullOrUndefined("")); // false (not an object)
return (obj == null) || (typeof obj === "object" && Object.keys(obj).length === 0);
}
async function fetchWithTimeout(url, options = {}, timeout = 3000) {
const controller = new AbortController();
const signal = controller.signal;
const timeoutId = setTimeout(() => controller.abort(), timeout);
try {
const response = await fetch(url, { ...options, signal });
clearTimeout(timeoutId); // Clear timeout if request completes successfully
return response;
} catch (error) {
if (error.name === 'AbortError') {
throw new Error(`Fetch request to ${url} timed out after ${timeout} ms`);
}
throw error;
}
}
module.exports = {
shuffle,
log,
compareVersion,
limitStringMaxLength,
secondsToTimeDict,
sleep,
isObjectEmptyOrNullOrUndefined,
fetchWithTimeout
}