Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
84 lines (79 loc) · 2.36 KB
/
Copy pathindex.ts
File metadata and controls
84 lines (79 loc) · 2.36 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
export function formatDate(d: any, fmt = 'yyyy-MM-dd hh:mm:ss') {
if (!(d instanceof Date)) {
d = new Date(d)
}
const o: any = {
'M+': d.getMonth() + 1, // 月份
'd+': d.getDate(), // 日
'h+': d.getHours(), // 小时
'm+': d.getMinutes(), // 分
's+': d.getSeconds(), // 秒
'q+': Math.floor((d.getMonth() + 3) / 3), // 季度
S: d.getMilliseconds() // 毫秒
}
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(
RegExp.$1,
`${d.getFullYear()}`.substr(4 - RegExp.$1.length)
)
}
// eslint-disable-next-line no-restricted-syntax
for (const k in o) {
if (new RegExp(`(${k})`).test(fmt))
fmt = fmt.replace(
RegExp.$1,
RegExp.$1.length === 1 ? o[k] : `00${o[k]}`.substr(`${o[k]}`.length)
)
}
return fmt
}
export function isCurrentWeek(date: Date, target?: Date) {
const now = target || new Date()
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate())
const oneDay = 1000 * 60 * 60 * 24
const nowWeek = today.getDay()
// 本周一的时间
const startWeek = today.getTime() - (nowWeek === 0 ? 6 : nowWeek - 1) * oneDay
return +date >= startWeek && +date <= startWeek + 7 * oneDay
}
export function formatShowDate(date: Date | string) {
const source = +new Date(date)
const now = +new Date()
const diff = now - source
const oneSeconds = 1000
const oneMinute = oneSeconds * 60
const oneHour = oneMinute * 60
const oneDay = oneHour * 24
const oneWeek = oneDay * 7
if (diff < oneMinute) {
return `${Math.floor(diff / oneSeconds)}秒前`
}
if (diff < oneHour) {
return `${Math.floor(diff / oneMinute)}分钟前`
}
if (diff < oneDay) {
return `${Math.floor(diff / oneHour)}小时前`
}
if (diff < oneWeek) {
return `${Math.floor(diff / oneDay)}天前`
}
return formatDate(new Date(date), 'yyyy-MM-dd')
}
const pattern =
/[a-zA-Z0-9_\u0392-\u03c9\u00c0-\u00ff\u0600-\u06ff\u0400-\u04ff]+|[\u4e00-\u9fff\u3400-\u4dbf\uf900-\ufaff\u3040-\u309f\uac00-\ud7af]+/g
// copy from https://github.com/youngjuning/vscode-juejin-wordcount/blob/main/count-word.ts
export default function countWord(data: string) {
const m = data.match(pattern)
let count = 0
if (!m) {
return 0
}
for (let i = 0; i < m.length; i += 1) {
if (m[i].charCodeAt(0) >= 0x4e00) {
count += m[i].length
} else {
count += 1
}
}
return count
}