Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FLAG-1238: replace moment for date-fns in getDatesData function #4905

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions components/widgets/utils/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import minBy from 'lodash/minBy';
import sumBy from 'lodash/sumBy';
import moment from 'moment';
import range from 'lodash/range';
import upperCase from 'lodash';
import { startOfISOWeekYear, addWeeks, setDay, format } from 'date-fns';

const translateMeans = (means, latest) => {
if (!means || !means.length) return null;
Expand Down Expand Up @@ -308,6 +308,23 @@ export const getCumulativeStatsData = (data) => {
return parsedData;
};

/**
* Create a date from a given year and ISO week number.
* @param {number} year - The year (e.g. 2024).
* @param {number} isoWeek - The ISO week number (e.g. 1, 5, 12).
* @returns {Date} - The date of the Monday of the given ISO week.
*/
function getDateFromIsoWeek(year, isoWeek) {
// Get the first day of the given year in the ISO calendar (Monday of the first ISO week)
const firstDayOfYear = startOfISOWeekYear(new Date(year, 0, 1));

// Add (isoWeek - 1) weeks to the first day of the ISO year
const targetDate = addWeeks(firstDayOfYear, isoWeek - 1);

// Set the date to Monday of that week (this is redundant as startOfISOYear already gives Monday)
return setDay(targetDate, 1); // 1 represents Monday
}

export const getDatesData = (data) =>
data.map((d, index) => {
const firstDayOfWeek = moment()
Expand All @@ -323,12 +340,8 @@ export const getDatesData = (data) =>
}),
date: d.date
? moment(d.date).format('YYYY-MM-DD')
: moment()
.year(d.year)
.isoWeek(d.week)
.startOf('isoWeek')
.format('YYYY-MM-DD'),
month: upperCase(moment().year(d.year).isoWeek(d.week).format('MMM')),
: format(getDateFromIsoWeek(d.year, d.week), 'yyyy-MM-dd'),
month: format(getDateFromIsoWeek(d.year, d.week), 'MMM'),
};
});

Expand Down
Loading