fix: move date logic to its own file with tests
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { beforeAll, describe, expect, it } from "@jest/globals"
|
||||
|
||||
import { getValidFromDate, getValidToDate } from "./getValidDates"
|
||||
|
||||
const NOW = new Date("2020-10-01T00:00:00Z")
|
||||
describe("getValidFromDate", () => {
|
||||
beforeAll(() => {
|
||||
jest.useFakeTimers({ now: NOW })
|
||||
})
|
||||
|
||||
it("returns today when empty string is provided", () => {
|
||||
const actual = getValidFromDate("")
|
||||
expect(actual.toISOString()).toBe("2020-10-01T00:00:00.000Z")
|
||||
})
|
||||
|
||||
it("returns today when undefined is provided", () => {
|
||||
const actual = getValidFromDate(undefined)
|
||||
expect(actual.toISOString()).toBe("2020-10-01T00:00:00.000Z")
|
||||
})
|
||||
|
||||
it("returns given date in utc", () => {
|
||||
const actual = getValidFromDate("2024-01-01")
|
||||
expect(actual.toISOString()).toBe("2023-12-31T23:00:00.000Z")
|
||||
})
|
||||
})
|
||||
|
||||
describe("getValidToDate", () => {
|
||||
beforeAll(() => {
|
||||
jest.useFakeTimers({ now: NOW })
|
||||
})
|
||||
|
||||
it("returns day after fromDate when empty string is provided", () => {
|
||||
const actual = getValidToDate("", NOW)
|
||||
expect(actual.toISOString()).toBe("2020-10-02T00:00:00.000Z")
|
||||
})
|
||||
|
||||
it("returns day after fromDate when undefined is provided", () => {
|
||||
const actual = getValidToDate(undefined, NOW)
|
||||
expect(actual.toISOString()).toBe("2020-10-02T00:00:00.000Z")
|
||||
})
|
||||
|
||||
it("returns given date in utc", () => {
|
||||
const actual = getValidToDate("2024-01-01", NOW)
|
||||
expect(actual.toISOString()).toBe("2023-12-31T23:00:00.000Z")
|
||||
})
|
||||
|
||||
it("fallsback to day after fromDate when given date is before fromDate", () => {
|
||||
const actual = getValidToDate("2020-09-30", NOW)
|
||||
expect(actual.toISOString()).toBe("2020-10-02T00:00:00.000Z")
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,55 @@
|
||||
import { Dayjs } from "dayjs"
|
||||
|
||||
import { dt } from "@/lib/dt"
|
||||
|
||||
/**
|
||||
* Get valid dates from stringFromDate and stringToDate making sure that they are not in the past and chronologically correct
|
||||
* @example const { fromDate, toDate} = getValidDates("2021-01-01", "2021-01-02")
|
||||
*/
|
||||
export function getValidDates(
|
||||
stringFromDate: string | undefined,
|
||||
stringToDate: string | undefined
|
||||
): { fromDate: Dayjs; toDate: Dayjs } {
|
||||
const fromDate = getValidFromDate(stringFromDate)
|
||||
const toDate = getValidToDate(stringToDate, fromDate)
|
||||
|
||||
return { fromDate, toDate }
|
||||
}
|
||||
|
||||
/**
|
||||
* Get valid fromDate from stringFromDate making sure that it is not in the past
|
||||
*/
|
||||
export function getValidFromDate(stringFromDate: string | undefined): Dayjs {
|
||||
const now = dt().utc()
|
||||
if (!stringFromDate) {
|
||||
return now
|
||||
}
|
||||
const toDate = dt(stringFromDate)
|
||||
|
||||
const yesterday = now.subtract(1, "day")
|
||||
if (!toDate.isAfter(yesterday)) {
|
||||
return now
|
||||
}
|
||||
|
||||
return toDate
|
||||
}
|
||||
|
||||
/**
|
||||
* Get valid toDate from stringToDate making sure that it is after fromDate
|
||||
*/
|
||||
export function getValidToDate(
|
||||
stringToDate: string | undefined,
|
||||
fromDate: Dayjs | Date
|
||||
): Dayjs {
|
||||
const tomorrow = dt().utc().add(1, "day")
|
||||
if (!stringToDate) {
|
||||
return tomorrow
|
||||
}
|
||||
|
||||
const toDate = dt(stringToDate)
|
||||
if (toDate.isAfter(fromDate)) {
|
||||
return toDate
|
||||
}
|
||||
|
||||
return tomorrow
|
||||
}
|
||||
Reference in New Issue
Block a user