UNPKG

@rsc-labs/medusa-store-analytics-v2

Version:
73 lines (72 loc) 2.78 kB
var ChartResolutionType = /* @__PURE__ */ ((ChartResolutionType2) => { ChartResolutionType2[ChartResolutionType2["DayMonth"] = 0] = "DayMonth"; ChartResolutionType2[ChartResolutionType2["Month"] = 1] = "Month"; return ChartResolutionType2; })(ChartResolutionType || {}); function calculateResolution(fromDate, toDate) { if (!fromDate) return 1; const calculateToDate = toDate ? new Date(toDate) : new Date(Date.now()); const diffTime = calculateToDate.getTime() - fromDate.getTime(); const weekTime = 6048e5; const monthTime = weekTime * 4; const twoMonthsTime = monthTime * 2; if (diffTime <= twoMonthsTime) { return 0; } const yearTime = monthTime * 12; if (diffTime < yearTime) { return 1; } return 1; } const compareDatesBasedOnResolutionType = (date1, date2, resolutionType) => { switch (resolutionType) { case 0: return new Date(new Date(date1).setHours(0, 0, 0, 0)).getTime() == new Date(new Date(date2).setHours(0, 0, 0, 0)).getTime(); case 1: return new Date(new Date(new Date(date1).setDate(0)).setHours(0, 0, 0, 0)).getTime() == new Date(new Date(new Date(date2).setDate(0)).setHours(0, 0, 0, 0)).getTime(); default: return new Date(new Date(date1).setHours(0, 0, 0, 0)).getTime() == new Date(new Date(date2).setHours(0, 0, 0, 0)).getTime(); } }; const getChartDateName = (date, resolutionType, startDate, endDate) => { switch (resolutionType) { case 0: if (compareDatesBasedOnResolutionType(date, startDate, resolutionType) || compareDatesBasedOnResolutionType(date, endDate, resolutionType)) { return `${date.getDate().toString()} ${getShortMonthName(date)}`; } return date.getDate().toString(); case 1: if (compareDatesBasedOnResolutionType(date, startDate, resolutionType) || compareDatesBasedOnResolutionType(date, endDate, resolutionType)) { return `${getShortMonthName(date)} ${date.getFullYear().toString()}`; } return getShortMonthName(date); default: return date.getFullYear().toString(); } }; const getChartTooltipDate = (date, resolutionType) => { switch (resolutionType) { case 0: return `${date.getDate().toString()}-${getShortMonthName(date)}`; case 1: return `${getShortMonthName(date)}-${date.getFullYear()}`; default: return date.getFullYear().toString(); } }; const getLegendName = (current) => { return current ? `Current` : `Preceding`; }; const getShortMonthName = (date) => { let days = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; return days[date.getMonth()]; }; export { ChartResolutionType, calculateResolution, compareDatesBasedOnResolutionType, getChartDateName, getChartTooltipDate, getLegendName };