Skip to content

Commit 5069746

Browse files
committed
Fix - useTimeLabels composable - Fix display of months when timestamps corresponding to hours intervals are passed
1 parent c312485 commit 5069746

File tree

3 files changed

+84
-45
lines changed

3 files changed

+84
-45
lines changed

src/useDateTime.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ export function useDateTime({
2323
// VALIDATION & PARSING
2424

2525
function isValidDate(date) {
26-
if (typeof date === 'number') return false;
26+
if (typeof date === 'number') return true;
2727
return !isNaN(parseDate(date));
2828
}
2929

3030
function getTimeStamp(dateStr) {
31-
if (!Date.parse(dateStr)) return dateStr;
31+
const parsed = Date.parse(dateStr);
32+
if (isNaN(parsed)) {
33+
return dateStr;
34+
}
3235
if (!useUTC) {
33-
return new Date(dateStr).getTime();
36+
return parsed;
3437
}
3538
const stripped = String(dateStr).replace(/([+-]\d{2}:\d{2}|Z)$/, '');
3639
return Date.parse(stripped + 'Z');

src/useTimeLabels.js

Lines changed: 77 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,66 +5,102 @@ import locales from "./locales/locales.json";
55
* @param {Array<string|number>} values
66
* @param {number} maxDatapoints
77
* @param {{ enable:boolean, useUTC:boolean, locale:string, januaryAsYear:boolean, options:Record<string,string> }} formatter
8-
* @param {number} start
9-
* @param {number} end
8+
* @param {number} start // index de début (inclus)
9+
* @param {number} end // index de fin (exclus)
1010
*/
1111
export function useTimeLabels({
1212
values,
1313
maxDatapoints,
14-
formatter,
15-
start,
16-
end
14+
formatter: xl,
15+
start: sliceStart,
16+
end: sliceEnd
1717
}) {
1818
const raw = values;
19-
const xl = formatter;
20-
const max = maxDatapoints;
2119
const out = [];
2220

2321
if (!xl.enable) {
24-
for (let i = 0; i < max; i += 1) {
25-
out.push({
26-
text: raw[i] ?? String(i),
27-
absoluteIndex: i,
28-
});
22+
for (let i = sliceStart; i < sliceEnd; i += 1) {
23+
out.push({ text: raw[i] ?? String(i), absoluteIndex: i });
2924
}
30-
} else {
31-
const dt = useDateTime({
32-
useUTC: xl.useUTC,
33-
min: raw[0] ?? "0",
34-
max: raw[max - 1] ?? String(max - 1),
35-
locale: locales[xl.locale],
36-
januaryAsYear: xl.januaryAsYear,
37-
});
25+
return out;
26+
}
3827

39-
const start = new Date(raw[0]);
40-
const end = new Date(raw[max - 1]);
41-
const monthsDiff = (end.getFullYear() - start.getFullYear()) * 12 + (end.getMonth() - start.getMonth());
28+
const windowValues = raw.slice(sliceStart, sliceEnd);
4229

43-
let xUnit = "second";
44-
const u = dt.getTimeUnitsfromTimestamp(raw[0], raw[max - 1]);
30+
const dt = useDateTime({
31+
useUTC: xl.useUTC,
32+
min: windowValues[0],
33+
max: windowValues[windowValues.length - 1],
34+
locale: locales[xl.locale],
35+
januaryAsYear: xl.januaryAsYear,
36+
});
4537

46-
if (monthsDiff >= 1) xUnit = "month";
47-
else if (u.minDate !== u.maxDate) xUnit = "day";
48-
else if (u.minHour !== u.maxHour) xUnit = "hour";
49-
else if (u.minMinute !== u.maxMinute) xUnit = "minute";
38+
const ws = new Date(windowValues[0]);
39+
const we = new Date(windowValues[windowValues.length - 1]);
40+
const getMonthFn = xl.useUTC ? "getUTCMonth" : "getMonth";
41+
const getYearFn = xl.useUTC ? "getUTCFullYear" : "getFullYear";
42+
const monthsDiff =
43+
(we[getYearFn]() - ws[getYearFn]()) * 12 +
44+
(we[getMonthFn]() - ws[getMonthFn]());
5045

51-
const fmt = xl.options[xUnit];
52-
const yearFmt = xl.options.year;
46+
const u = dt.getTimeUnitsfromTimestamp(
47+
windowValues[0],
48+
windowValues[windowValues.length - 1]
49+
);
5350

54-
for (let i = 0; i < max; i += 1) {
55-
const d = new Date(raw[i]);
56-
const m = xl.useUTC ? d.getUTCMonth() : d.getMonth();
51+
let xUnit = "second";
52+
if (u.minMinute !== u.maxMinute) xUnit = "minute";
53+
else if (u.minHour !== u.maxHour) xUnit = "hour";
54+
else if (u.minDate !== u.maxDate) xUnit = "day";
55+
else if (monthsDiff >= 1) xUnit = "month";
5756

58-
let text;
59-
if (xl.januaryAsYear && xUnit === "month" && m === 0) {
60-
text = dt.formatDate(d, yearFmt);
57+
const fmt = xl.options[xUnit];
58+
const yearFmt = xl.options.year;
59+
const monthFmt = xl.options.month;
60+
const dayFmt = xl.options.day;
61+
62+
windowValues.forEach((ts, idx) => {
63+
const d = new Date(ts);
64+
const hours = xl.useUTC ? d.getUTCHours() : d.getHours();
65+
const minutes = xl.useUTC ? d.getUTCMinutes() : d.getMinutes();
66+
67+
let text;
68+
if (idx === 0) {
69+
if (xUnit === "month" && (hours === 0 && minutes === 0) && ((xl.useUTC ? d.getUTCDate() : d.getDate()) === 1)) {
70+
text = dt.formatDate(d, monthFmt);
71+
} else if (hours === 0 && minutes === 0) {
72+
text = dt.formatDate(d, dayFmt);
6173
} else {
6274
text = dt.formatDate(d, fmt);
6375
}
76+
} else {
77+
const prev = new Date(windowValues[idx - 1]);
78+
const prevY = xl.useUTC ? prev.getUTCFullYear() : prev.getFullYear();
79+
const prevM = xl.useUTC ? prev.getUTCMonth() : prev.getMonth();
80+
const prevD = xl.useUTC ? prev.getUTCDate() : prev.getDate();
81+
const currY = xl.useUTC ? d.getUTCFullYear() : d.getFullYear();
82+
const currM = xl.useUTC ? d.getUTCMonth() : d.getMonth();
83+
const currD = xl.useUTC ? d.getUTCDate() : d.getDate();
6484

65-
out.push({ text, absoluteIndex: i });
85+
if (currY !== prevY) {
86+
text = dt.formatDate(d, yearFmt);
87+
} else if (currM !== prevM) {
88+
text = xUnit === "month"
89+
? dt.formatDate(d, monthFmt)
90+
: dt.formatDate(d, dayFmt);
91+
} else if (currD !== prevD) {
92+
text = dt.formatDate(d, dayFmt);
93+
} else if (hours === 0 && minutes === 0) {
94+
text = dt.formatDate(d, dayFmt);
95+
} else if (xl.januaryAsYear && xUnit === "month" && currM === 0) {
96+
text = dt.formatDate(d, yearFmt);
97+
} else {
98+
text = dt.formatDate(d, fmt);
99+
}
66100
}
67-
}
68101

69-
return out.slice(start, end);
70-
}
102+
out.push({ text, absoluteIndex: sliceStart + idx });
103+
});
104+
105+
return out;
106+
}

tests/useDateTime.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('useDateTime composable', () => {
2323

2424
test('isValidDate returns true for ISO date string and false for number or invalid', () => {
2525
expect(dtLocal.isValidDate('2021-08-15')).toBe(true)
26-
expect(dtLocal.isValidDate(1628985600000)).toBe(false)
26+
expect(dtLocal.isValidDate(1628985600000)).toBe(true)
2727
expect(dtLocal.isValidDate('not-a-date')).toBe(false)
2828
})
2929

0 commit comments

Comments
 (0)