-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.js
More file actions
62 lines (56 loc) · 1.66 KB
/
Copy pathbase.js
File metadata and controls
62 lines (56 loc) · 1.66 KB
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
function getFloat(value, decimalPlaces = 2) {
return parseFloat(value.toFixed(decimalPlaces));
}
function getParamByName(name, url) {
if (!url)
url = window.location.href;
name = name.replace(/[\[\]]/g, '\\$&');
let regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)', 'i');
let match = regex.exec(url);
if (!match)
return null;
if (!match[2])
return '';
return decodeURIComponent(match[2].replace(/\+/g, ' '));
}
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function randomInt(max, min = 0) { return Math.round(Math.random() * (max - min)) + min; }
function randomFloat(max, min = 0) { return Math.random() * (max - min) + min; }
function randomElement(arr) { return arr[randomInt(arr.length - 1)]; }
function readFile(file, callback) {
let xhr = new XMLHttpRequest();
xhr.overrideMimeType('application/json');
xhr.open('GET', file, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status == '200') {
callback(xhr.responseText);
}
};
xhr.send();
}
async function readFileAsync(file) {
var xhr = new XMLHttpRequest();
xhr.open("HEAD", file);
//xhr.send();
return new Promise(function (resolve, reject) {
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status >= 300) {
reject("Error, status code = " + xhr.status);
} else {
resolve(xhr.responseText);
}
}
};
xhr.overrideMimeType("application/json");
xhr.open("get", file, true);
xhr.send();
});
}