Skip to content

Commit

Permalink
Use ISO datetime parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
AJGeel committed Oct 20, 2024
1 parent 1909532 commit 3ef8aad
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/services/history/organiseHistory.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { format, formatISO } from "date-fns";

import { HistoryItem, OrganisedHistory } from "./types";

/**
Expand All @@ -7,19 +9,19 @@ import { HistoryItem, OrganisedHistory } from "./types";
export const organiseHistory = (history: HistoryItem[]): OrganisedHistory => {
const grouped = history.reduce<OrganisedHistory>((acc, item) => {
const date = new Date(item.lastVisitTime || 0);
const [dateString] = date.toISOString().split("T");
const hourString = date.getHours().toString().padStart(2, "0");
const dateKey = formatISO(date, { representation: "date" });
const hourKey = format(date, "HH");

if (!acc.has(dateString)) {
acc.set(dateString, new Map());
if (!acc.has(dateKey)) {
acc.set(dateKey, new Map());
}
const dayMap = acc.get(dateString)!;

if (!dayMap.has(hourString)) {
dayMap.set(hourString, []);
const dayMap = acc.get(dateKey)!;
if (!dayMap.has(hourKey)) {
dayMap.set(hourKey, []);
}
dayMap.get(hourString)!.push(item);

dayMap.get(hourKey)!.push(item);
return acc;
}, new Map());

Expand Down

0 comments on commit 3ef8aad

Please sign in to comment.