-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
109 lines (68 loc) · 2.35 KB
/
utils.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
const fs = require("fs");
const os = require("os");
module.exports.getRandomId = function() {
return "_" + Math.random().toString(36).substr(2, 9);
};
module.exports.getCookie = function(cookieString, name) {
let matches;
if (cookieString && name) {
matches = cookieString.match(new RegExp(
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, "\\$1") + "=([^;]*)"
));
}
return matches ? decodeURIComponent(matches[1]) : undefined;
};
module.exports.getQueryParams = function(url) {
let params = {};
url = url.split("?")[1];
if (url) {
url = url.split("#")[0];
let arr = url.split("&");
for (let i = 0; i < arr.length; i++) {
let a = arr[i].split("=");
let paramName = a[0];
let paramValue = a[1];
if (paramName.match(/\[(\d+)?\]$/)) {
let key = paramName.replace(/\[(\d+)?\]/, "");
if (!params[key]) params[key] = [];
if (paramName.match(/\[\d+\]$/)) {
let index = /\[(\d+)\]/.exec(paramName)[1];
params[key][index] = paramValue;
} else {
params[key].push(paramValue);
}
} else {
if (!params[paramName]) {
params[paramName] = paramValue;
} else if (params[paramName] && typeof params[paramName] === "string") {
params[paramName] = [params[paramName]];
params[paramName].push(paramValue);
} else {
params[paramName].push(paramValue);
}
}
}
}
return params;
}
module.exports.randomDice = function() {
return 1 + Math.floor(Math.random() * 6);
// return 1;
};
module.exports.readFileAsync = async function (filename) {
return new Promise(function(resolve, reject) {
fs.readFile(filename, "utf8", (err, data) => {
err ? reject(err) : resolve(data);
});
});
}
module.exports.getIpAddress = function() {
let ifaces = os.networkInterfaces();
let firstIp = null;
try {
firstIp = ifaces.eth0[0].address;
} catch (excp) {
console.log("Can't recieve local IP address");
}
return firstIp;
}