chore: fix date tests
This commit is contained in:
@@ -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', () => {
|
||||
|
||||
14
packages/utils/src/Domain/Date/DateUtils.ts
Normal file
14
packages/utils/src/Domain/Date/DateUtils.ts
Normal file
@@ -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
|
||||
}
|
||||
@@ -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'
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user