-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutils.js
35 lines (29 loc) · 1.02 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
/*
Author: sparanoid(https://github.com/sparanoid)
Modified by nenekodev(https://github.com/nenekodev)
latest Update: 2022.3.11 0:11
The following changes has been made:
- Merge features in Utils.js and index.js
- Improve the log function
*/
import chalk from 'chalk';
import { formatDistanceToNowStrict } from 'date-fns';
export function timeAgo(timestamp, suffix = true) {
return formatDistanceToNowStrict(new Date(timestamp), {
addSuffix: suffix,
});
}
export function formatDate(timestamp) {
let date = timestamp.toString().length === 10 ? new Date(+timestamp * 1000) : new Date(+timestamp);
return `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`;
}
export function log(account, msg, type = 'normal') {
const logName = chalk.hex('#000').bgHex(account?.color ?? '#fff');
let textColor = chalk.hex('#fff');
if (type == 'error'){
textColor = chalk.hex('#ff0000');
}else if (type == 'success'){
textColor = chalk.hex('#00ff00');
}
console.log(`${logName(account.slug)} ${textColor(msg)}`);
}