-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdyndateplugin.ts
75 lines (72 loc) · 1.89 KB
/
dyndateplugin.ts
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
export default function myPlugin() {
return {
name: 'transform-file',
transform(src: string, id: string) {
if (/\.(vue)$/.test(id)) {
return {
code: replaceDynamicDates(src),
map: null // provide source map if available
}
}
}
}
}
const now = new Date()
const replaceDynamicDates = (src: string) =>
src.replace(/['|"]dyndatetime\(([^)])*\)['|"]/g, (i) => parseDatestring(i))
const parseDatestring = (s: string) => {
s = s.replace(/dyndatetime/, '')
s = s.replace(/\(/, '')
s = s.replace(/\)/, '')
s = s.replace(/y/, now.getFullYear().toString())
s = s.replace(/m/, (now.getMonth() + 1).toString())
s = s.replace(/d/, now.getDate().toString())
s = s.replace(/h/, now.getHours().toString())
s = s.replace(/i/, now.getMinutes().toString())
s = s.replace(/['|"](.*)['|"]/, (i) => {
const dateDict: { [index: string]: number } = {
0: 0,
1: 0,
2: 0,
3: 0,
4: 0
}
const date = i.replace(/['|"]/g, '')
const dateArray = date.split(',')
dateArray.forEach((i, index) => {
const splittedNum = i.split(/[/+|/-]/)
if (splittedNum.length > 1) {
const minus = i.indexOf('-') !== -1
dateDict[index] = minus
? +splittedNum[0] - +splittedNum[1]
: +splittedNum[0] + +splittedNum[1]
} else {
dateDict[index] = +splittedNum[0]
}
})
const dd = new Date(dateDict[0], dateDict[1] - 1, dateDict[2], dateDict[3], dateDict[4])
const y = dd.getFullYear()
const m = dd.getMonth() + 1
const d = dd.getDate()
const h = dd.getHours()
const mm = dd.getMinutes()
return (
"'" +
y +
'-' +
(m < 10 ? '0' : '') +
m +
'-' +
(d < 10 ? '0' : '') +
d +
'T' +
(h < 10 ? '0' : '') +
h +
':' +
(mm < 10 ? '0' : '') +
mm +
"'"
)
})
return s
}