-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathutils.js
More file actions
54 lines (41 loc) · 1.25 KB
/
utils.js
File metadata and controls
54 lines (41 loc) · 1.25 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
const fs = require('fs');
const csv = require('csv-parser');
// 将CSV转换为Objects
function convertCSVToObjectSync(filePath) {
const objects = [];
const fileData = fs.readFileSync(filePath, 'utf-8');
const lines = fileData.trim().split('\n');
const header = lines[0].split(',');
for (let i = 1; i < lines.length; i++) {
const values = lines[i].split(',');
const obj = {};
for (let j = 0; j < header.length; j++) {
obj[header[j]] = values[j];
}
objects.push(obj);
}
return objects;
};
// 暂停函数
function sleep(minutes) {
const milliseconds = minutes * 60 * 1000;
return new Promise(resolve => setTimeout(resolve, milliseconds));
};
const winston = require('winston');
// 保存日志
function saveLog(projectName, message) {
const logger = winston.createLogger({
level: 'info',
format: winston.format.simple(),
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: `./data/${projectName}.log` }),
],
});
logger.info(message);
}
// 获取随机浮点数
function getRandomFloat(min, max) {
return Math.random() * (max - min) + min;
}
module.exports = { convertCSVToObjectSync, sleep, getRandomFloat, saveLog };