-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdyndateplugin.js
79 lines (75 loc) · 1.95 KB
/
dyndateplugin.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
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
/* eslint-disable es5/no-arrow-functions */
/* eslint-disable es5/no-block-scoping */
/* eslint-disable es5/no-shorthand-properties */
export default function myPlugin() {
return {
name: 'transform-file',
transform(src, id) {
if (/\.(js)$/.test(id)) {
return {
code: replaceDynamicDates(src),
map: null, // provide source map if available
};
}
},
};
}
const now = new Date();
const replaceDynamicDates = (src) =>
src.replace(/['|"]dyndatetime\(([^)])*\)['|"]/g, function (i) {
return parseDatestring(i);
});
const parseDatestring = (s) => {
s = s.replace(/dyndatetime/, '');
s = s.replace(/\(/, '');
s = s.replace(/\)/, '');
s = s.replace(/y/, now.getFullYear());
s = s.replace(/m/, now.getMonth() + 1);
s = s.replace(/d/, now.getDate());
s = s.replace(/h/, now.getHours());
s = s.replace(/i/, now.getMinutes());
s = s.replace(/['|"](.*)['|"]/, function (i) {
const dateDict = {
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;
};