From 73ca12275f24003aaea43ad19fb1cb6ee07da307 Mon Sep 17 00:00:00 2001 From: Mo Date: Thu, 1 Dec 2022 07:05:02 -0600 Subject: [PATCH] chore: fix date tests --- .../src/Domain/Runtime/Predicate/Utils.spec.ts | 5 +++-- packages/utils/src/Domain/Date/DateUtils.ts | 14 ++++++++++++++ packages/utils/src/Domain/index.ts | 7 ++++--- .../ContentListView/Daily/CreateDailySections.ts | 7 ++++--- .../web/src/javascripts/Utils/DateUtils.spec.ts | 12 ++++++------ packages/web/src/javascripts/Utils/DateUtils.ts | 6 ------ 6 files changed, 31 insertions(+), 20 deletions(-) create mode 100644 packages/utils/src/Domain/Date/DateUtils.ts diff --git a/packages/models/src/Domain/Runtime/Predicate/Utils.spec.ts b/packages/models/src/Domain/Runtime/Predicate/Utils.spec.ts index 99a318af6..39f9e0c10 100644 --- a/packages/models/src/Domain/Runtime/Predicate/Utils.spec.ts +++ b/packages/models/src/Domain/Runtime/Predicate/Utils.spec.ts @@ -1,15 +1,16 @@ import { dateFromDSLDateString } from './Utils' +import { addDaysToDate, addHoursToDate } from '@standardnotes/utils' describe('Predicate Utils', () => { describe('dateFromDSLDateString', () => { it('should return a date object with the correct day', () => { const date = dateFromDSLDateString('1.days.ago') - expect(date.getDate()).toEqual(new Date().getDate() - 1) + expect(date.getDate()).toEqual(addDaysToDate(new Date(), -1).getDate()) }) it('should return a date object with the correct hour', () => { const date = dateFromDSLDateString('1.hours.ago') - expect(date.getHours()).toEqual(new Date().getHours() - 1) + expect(date.getHours()).toEqual(addHoursToDate(new Date(), -1).getHours()) }) it('should return a date object with the correct month', () => { diff --git a/packages/utils/src/Domain/Date/DateUtils.ts b/packages/utils/src/Domain/Date/DateUtils.ts new file mode 100644 index 000000000..9efc72bba --- /dev/null +++ b/packages/utils/src/Domain/Date/DateUtils.ts @@ -0,0 +1,14 @@ +/** Negative numbers are supported as well */ +export function addDaysToDate(date: Date, days: number) { + const result = new Date(date) + result.setDate(result.getDate() + days) + + return result +} + +export function addHoursToDate(date: Date, hours: number) { + const result = new Date(date) + result.setHours(result.getHours() + hours) + + return result +} diff --git a/packages/utils/src/Domain/index.ts b/packages/utils/src/Domain/index.ts index 2099f52be..19418df3b 100644 --- a/packages/utils/src/Domain/index.ts +++ b/packages/utils/src/Domain/index.ts @@ -1,6 +1,7 @@ -export * from './Utils/Utils' +export * from './Date/DateUtils' +export * from './Deferred/Deferred' export * from './Utils/ClassNames' +export * from './Utils/Utils' +export * from './Uuid/Utils' export * from './Uuid/UuidGenerator' export * from './Uuid/UuidMap' -export * from './Uuid/Utils' -export * from './Deferred/Deferred' diff --git a/packages/web/src/javascripts/Components/ContentListView/Daily/CreateDailySections.ts b/packages/web/src/javascripts/Components/ContentListView/Daily/CreateDailySections.ts index ce0842f62..b06711ee5 100644 --- a/packages/web/src/javascripts/Components/ContentListView/Daily/CreateDailySections.ts +++ b/packages/web/src/javascripts/Components/ContentListView/Daily/CreateDailySections.ts @@ -1,5 +1,6 @@ +import { addDaysToDate } from '@standardnotes/utils' import { ListableContentItem } from '../Types/ListableContentItem' -import { addDays, getWeekdayName } from '@/Utils/DateUtils' +import { getWeekdayName } from '@/Utils/DateUtils' import { DailyItemsDay } from './DailyItemsDaySection' import { dateToDailyDayIdentifier } from './Utils' @@ -53,11 +54,11 @@ export function insertBlanks(entries: DailyItemsDay[], location: 'front' | 'end' for (let i = 1; i <= number; i++) { if (location === 'front') { - const plusNFromFirstDay = addDays(laterDay, i) + const plusNFromFirstDay = addDaysToDate(laterDay, i) const futureEntry = templateEntryForDate(plusNFromFirstDay) entries.unshift(futureEntry) } else { - const minusNFromLastDay = addDays(earlierDay, -i) + const minusNFromLastDay = addDaysToDate(earlierDay, -i) const pastEntry = templateEntryForDate(minusNFromLastDay) entries.push(pastEntry) } diff --git a/packages/web/src/javascripts/Utils/DateUtils.spec.ts b/packages/web/src/javascripts/Utils/DateUtils.spec.ts index 5ceb74251..1c0fe61fc 100644 --- a/packages/web/src/javascripts/Utils/DateUtils.spec.ts +++ b/packages/web/src/javascripts/Utils/DateUtils.spec.ts @@ -1,4 +1,4 @@ -import { addDays } from '@/Utils/DateUtils' +import { addDaysToDate } from '@standardnotes/utils' import { numDaysBetweenDates } from './DateUtils' describe('date utils', () => { @@ -6,16 +6,16 @@ describe('date utils', () => { it('should return full days diff accurately', () => { const today = new Date() - expect(numDaysBetweenDates(today, addDays(today, 1))).toEqual(1) - expect(numDaysBetweenDates(today, addDays(today, 2))).toEqual(2) - expect(numDaysBetweenDates(today, addDays(today, 3))).toEqual(3) + expect(numDaysBetweenDates(today, addDaysToDate(today, 1))).toEqual(1) + expect(numDaysBetweenDates(today, addDaysToDate(today, 2))).toEqual(2) + expect(numDaysBetweenDates(today, addDaysToDate(today, 3))).toEqual(3) }) it('should return absolute value of difference', () => { const today = new Date() - expect(numDaysBetweenDates(today, addDays(today, 3))).toEqual(3) - expect(numDaysBetweenDates(addDays(today, 3), today)).toEqual(3) + expect(numDaysBetweenDates(today, addDaysToDate(today, 3))).toEqual(3) + expect(numDaysBetweenDates(addDaysToDate(today, 3), today)).toEqual(3) }) it('should return 1 day difference between two dates on different days but 1 hour apart', () => { diff --git a/packages/web/src/javascripts/Utils/DateUtils.ts b/packages/web/src/javascripts/Utils/DateUtils.ts index 32ca9cddf..fed7f3e6c 100644 --- a/packages/web/src/javascripts/Utils/DateUtils.ts +++ b/packages/web/src/javascripts/Utils/DateUtils.ts @@ -63,12 +63,6 @@ export function numDaysBetweenDates(date1: Date, date2: Date): number { return Math.floor(diffInDays) } -export function addDays(date: Date, days: number) { - const result = new Date(date) - result.setDate(result.getDate() + days) - return result -} - /** * @returns Date with day equal to first day of the offseted month */