This repository has been archived by the owner on Sep 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dateColumnFormatter.js
71 lines (67 loc) · 2.34 KB
/
dateColumnFormatter.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
import { isDate, identity } from 'underscore';
import dayjs from 'dayjs';
/**
* Creates a date formatter based on a dw date column object
*
* @exports dateColumnFormatter
* @kind function
*
* @description
* This function returns a date formatting function based on a
* dw column object. The implementation is backwards-compatible with
* our old Globalize-based date formatting, but uses dayjs under the
* hood.
*
* @param {object} column - the date column object
* @returns {function}
*/
export default function dateColumnFormatter(column) {
const format = column.format();
if (!format) return identity;
switch (column.precision()) {
case 'year':
return function (d) {
return !isDate(d) ? d : d.getFullYear();
};
case 'half':
return function (d) {
return !isDate(d) ? d : d.getFullYear() + ' H' + (d.getMonth() / 6 + 1);
};
case 'quarter':
return function (d) {
return !isDate(d) ? d : d.getFullYear() + ' Q' + (d.getMonth() / 3 + 1);
};
case 'month':
return function (d) {
return !isDate(d) ? d : dayjs(d).format('MMM YY');
};
case 'week':
return function (d) {
return !isDate(d) ? d : dateToIsoWeek(d).slice(0, 2).join(' W');
};
case 'day':
return function (d, verbose) {
return !isDate(d) ? d : dayjs(d).format(verbose ? 'dddd, MMMM DD, YYYY' : 'l');
};
case 'day-minutes':
return function (d) {
return !isDate(d)
? d
: dayjs(d).format('MMM DD').replace(' ', ' ') +
' - ' +
dayjs(d).format('LT').replace(' ', ' ');
};
case 'day-seconds':
return function (d) {
return !isDate(d) ? d : dayjs(d).format('LTS').replace(' ', ' ');
};
}
}
function dateToIsoWeek(date) {
const d = date.getUTCDay();
const t = new Date(date.valueOf());
t.setDate(t.getDate() - ((d + 6) % 7) + 3);
const isoYear = t.getUTCFullYear();
const w = Math.floor((t.getTime() - new Date(isoYear, 0, 1, -6)) / 864e5);
return [isoYear, 1 + Math.floor(w / 7), d > 0 ? d : 7];
}