-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
42 lines (32 loc) · 1.32 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
const https = require('https');
const {writeFileSync, createWriteStream} = require("fs");
const timestamp = () => {
const d = new Date();
return d.getFullYear() + "-" + d.getMonth() + "-" + d.getDay() + "-" + d.getHours() + "-" + d.getMinutes() + "-" + d.getMilliseconds();
};
const saveOutput = (prefix, content) => {
writeFileSync(process.cwd() + '/output/' + prefix + "_" + timestamp() + ".json", JSON.stringify(content.data, null, 2), "utf8");
};
const saveImage = (prefix, url) => {
https.get(url, (res) => {
res.pipe(createWriteStream(process.cwd() + '/output/' + prefix + "_" + timestamp() + ".png"));
});
};
const saveBase64 = (prefix, content) => {
content = content.replace(/^data:image\/png;base64,/, "");
writeFileSync(process.cwd() + '/output/' + prefix + "_" + timestamp() + ".png", content, 'base64');
};
const shuffle = (array) => {
let currentIndex = array.length, randomIndex;
// While there remain elements to shuffle.
while (currentIndex != 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
};
module.exports = {saveOutput, shuffle, saveBase64, timestamp, saveImage};