diff --git a/CHANGELOG.md b/CHANGELOG.md index f8b029e..e34cde1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - randomInt - randomPositiveInt - randomNegativeInt + - randomDate ## 0.51.1 diff --git a/src/random/randomDate.test.ts b/src/random/randomDate.test.ts index b1fa530..7a89b80 100644 --- a/src/random/randomDate.test.ts +++ b/src/random/randomDate.test.ts @@ -6,8 +6,18 @@ describe(`randomDate`, () => { expect(randomDate().getTime()).toBeGreaterThan(0); }); it(`args`, () => { - expect(randomDate("2010", "2011").toISOString().substring(0, 3)).toBe( - "201" + expect( + randomDate({ startDate: "2010", endDate: "2011" }) + .toISOString() + .substring(0, 3) + ).toBe("201"); + }); + it(`no start`, () => { + expect(randomDate({ endDate: "2011" }).getFullYear()).toBeLessThan(2011); + }); + it(`no end`, () => { + expect(randomDate({ startDate: "2012" }).getFullYear()).toBeGreaterThan( + 2011 ); }); }); diff --git a/src/random/randomDate.ts b/src/random/randomDate.ts index dc82a27..c6bc606 100644 --- a/src/random/randomDate.ts +++ b/src/random/randomDate.ts @@ -4,13 +4,19 @@ import { MILLISECONDS_IN_MINUTE, } from "../constants/time"; import { parseDate } from "../helpers/parseDate"; -import { DateLike, DateRange } from "../types"; +import { DateRange } from "../types"; import { isFutureDate, isPastDate } from "../validators"; import { randomInt } from "./randomInt"; -const nowPlusMs = (ms: number) => new Date(new Date().getTime() + ms); +const datePlusDecade = (date: Date = new Date()) => + datePlusMs(date, MILLISECONDS_IN_DECADE); -export const randomDate = (startDate?: DateLike, endDate?: DateLike) => { +const dateMinusDecade = (date: Date = new Date()) => + datePlusMs(date, -MILLISECONDS_IN_DECADE); + +const datePlusMs = (date: Date, ms: number) => new Date(date.getTime() + ms); + +export const randomDate = ({ startDate, endDate }: Partial = {}) => { const parsedStartDate = parseDate(startDate); const parsedEndDate = parseDate(endDate); @@ -19,26 +25,22 @@ export const randomDate = (startDate?: DateLike, endDate?: DateLike) => { } const finalStartDate = - parsedStartDate || - (parsedEndDate - ? new Date(parsedEndDate.getTime() - MILLISECONDS_IN_DECADE) - : nowPlusMs(-MILLISECONDS_IN_DECADE)); + parsedStartDate || // + dateMinusDecade(parsedEndDate); // uses now if undefined const finalEndDate = - parsedEndDate || - (parsedStartDate - ? new Date(parsedStartDate.getTime() + MILLISECONDS_IN_DECADE) - : nowPlusMs(MILLISECONDS_IN_DECADE)); + parsedEndDate || // + datePlusDecade(parsedStartDate); // uses now if undefined return new Date( randomInt({ min: finalStartDate.getTime(), max: finalEndDate.getTime() }) ); }; -export const randomMaxDate = (start?: Date, end?: Date) => { - const startDate = start || new Date(-MAX_DATE_MILLISECONDS); - const endDate = end || new Date(MAX_DATE_MILLISECONDS); - return randomDate(startDate, endDate); +export const randomMaxDate = ({ startDate, endDate }: Partial) => { + startDate = startDate || new Date(-MAX_DATE_MILLISECONDS); + endDate = endDate || new Date(MAX_DATE_MILLISECONDS); + return randomDate({ startDate, endDate }); }; export const randomFutureDate = ({ @@ -53,9 +55,9 @@ export const randomFutureDate = ({ } const finalStartDate = - parseDate(startDate) || nowPlusMs(5 * MILLISECONDS_IN_MINUTE); // Add a safe margin in the future (i.e. lagging tests) + parseDate(startDate) || datePlusMs(new Date(), 5 * MILLISECONDS_IN_MINUTE); // Add a safe margin in the future (i.e. lagging tests) - return randomDate(finalStartDate, endDate); + return randomDate({ startDate: finalStartDate, endDate }); }; export const randomPastDate = ({ @@ -70,12 +72,12 @@ export const randomPastDate = ({ } const finalEndDate = parseDate(endDate) || new Date(); - return randomDate(startDate, finalEndDate); + return randomDate({ startDate, endDate: finalEndDate }); }; export const randomDateRange = () => { const startDate = randomDate(); - const endDate = randomDate(startDate); + const endDate = randomDate({ startDate }); return { endDate,