Skip to content

Commit

Permalink
randomDate
Browse files Browse the repository at this point in the history
  • Loading branch information
ogroppo committed Jul 5, 2024
1 parent c692b71 commit 92d786a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- randomInt
- randomPositiveInt
- randomNegativeInt
- randomDate

## 0.51.1

Expand Down
14 changes: 12 additions & 2 deletions src/random/randomDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
});
});
Expand Down
40 changes: 21 additions & 19 deletions src/random/randomDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DateRange> = {}) => {
const parsedStartDate = parseDate(startDate);
const parsedEndDate = parseDate(endDate);

Expand All @@ -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<DateRange>) => {
startDate = startDate || new Date(-MAX_DATE_MILLISECONDS);
endDate = endDate || new Date(MAX_DATE_MILLISECONDS);
return randomDate({ startDate, endDate });
};

export const randomFutureDate = ({
Expand All @@ -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 = ({
Expand All @@ -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,
Expand Down

0 comments on commit 92d786a

Please sign in to comment.