-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathutils.js
40 lines (33 loc) · 933 Bytes
/
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
// utils.js: 存放工具类方法
import pkg from 'lodash';
import crypto from 'crypto';
const {cloneDeep} = pkg;
export function getTitleLength(title) {
return title.length; // 返回标题长度
}
export function getNowTime() {
return (new Date()).getTime()
}
export async function sleep(ms) {
// 模拟异步请求
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, ms);
});
}
export function sleepSync(ms) {
const end = Date.now() + ms; // 获取当前时间并计算结束时间
while (Date.now() < end) {
// 阻塞式等待,直到时间到达
}
}
/**
* 计算文件内容的 hash 值
* @param {string} content - 文件内容
* @returns {string} - 文件内容的 hash 值
*/
export function computeHash(content) {
return crypto.createHash('sha256').update(content, 'utf8').digest('hex');
}
export const deepCopy = cloneDeep