From 56767cf36d1066dd28370271fe021bf1a50f3579 Mon Sep 17 00:00:00 2001 From: Chris Walsh Date: Sat, 4 Dec 2021 14:10:01 -0800 Subject: [PATCH] Move all time breakpoints to constant --- lib/date.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/date.js b/lib/date.js index 76f76f54..e38d477e 100644 --- a/lib/date.js +++ b/lib/date.js @@ -41,31 +41,37 @@ export function getDateRange(value, locale = 'en-US', createdAt = null) { if (value === 'all') { createdAt = new Date(createdAt); + const diff = Math.abs(differenceInCalendarDays(createdAt, now)); + const breakpoints = { + day: 1, + month: 90, + year: 1095, + }; if (createdAt) { - if (diff <= 1) { + if (diff <= breakpoints.day) { return { startDate: startOfDay(createdAt), endDate: endOfDay(now), unit: 'hour', value, }; - } else if (diff <= 90 && diff > 1) { + } else if (diff <= breakpoints.month && diff > breakpoints.day) { return { startDate: startOfWeek(createdAt), endDate: endOfWeek(now), unit: 'day', value, }; - } else if (diff <= 1095 && diff > 90) { + } else if (diff <= breakpoints.year && diff > breakpoints.month) { return { startDate: startOfMonth(createdAt), endDate: endOfMonth(now), unit: 'month', value, }; - } else if (diff > 1095) { + } else if (diff > breakpoints.year) { return { startDate: startOfYear(createdAt), endDate: endOfYear(now), @@ -74,8 +80,9 @@ export function getDateRange(value, locale = 'en-US', createdAt = null) { }; } } + return { - startDate: startOfYear(createdAt), + startDate: startOfYear(now), endDate: endOfYear(now), unit: 'year', value,