Skip to content

Commit

Permalink
feat(date): introduce anytime
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Apr 25, 2023
1 parent 4d0458c commit 5d0fa81
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
35 changes: 35 additions & 0 deletions src/modules/date/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,41 @@ export class DateModule {
}
}

/**
* Generates a random date that can be either in the past or in the future.
*
* @param options The optional options object.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
*
* @see faker.date.between() For dates in a specific range.
* @see faker.date.past() For dates explicitly in the past.
* @see faker.date.future() For dates explicitly in the future.
*
* @example
* faker.date.anytime() // '2022-07-31T01:33:29.567Z'
*
* @since 8.0.0
*/
anytime(
options: {
/**
* The date to use as reference point for the newly generated date.
*
* @default faker.defaultRefDate()
*/
refDate?: string | Date | number;
} = {}
): Date {
const { refDate } = options;

const date = toDate(refDate, this.faker.defaultRefDate);

return this.between({
from: new Date(date.getTime() - 1000 * 60 * 60 * 24 * 365),
to: new Date(date.getTime() + 1000 * 60 * 60 * 24 * 365),
});
}

/**
* Generates a random date in the past.
*
Expand Down
18 changes: 18 additions & 0 deletions test/__snapshots__/date.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`date > 42 > anytime > with only Date refDate 1`] = `2020-11-22T03:05:49.801Z`;

exports[`date > 42 > anytime > with only number refDate 1`] = `2020-11-22T03:05:49.801Z`;

exports[`date > 42 > anytime > with only string refDate 1`] = `2020-11-22T03:05:49.801Z`;

exports[`date > 42 > between > with Date dates 1`] = `2021-03-15T19:30:57.091Z`;

exports[`date > 42 > between > with mixed dates 1`] = `2021-03-15T19:30:57.091Z`;
Expand Down Expand Up @@ -121,6 +127,12 @@ exports[`date > 42 > weekday > with abbreviated = true and context = true 1`] =

exports[`date > 42 > weekday > with context = true 1`] = `"Tuesday"`;

exports[`date > 1211 > anytime > with only Date refDate 1`] = `2021-12-31T12:49:38.848Z`;

exports[`date > 1211 > anytime > with only number refDate 1`] = `2021-12-31T12:49:38.848Z`;

exports[`date > 1211 > anytime > with only string refDate 1`] = `2021-12-31T12:49:38.848Z`;

exports[`date > 1211 > between > with Date dates 1`] = `2021-04-17T11:58:13.327Z`;

exports[`date > 1211 > between > with mixed dates 1`] = `2021-04-17T11:58:13.327Z`;
Expand Down Expand Up @@ -243,6 +255,12 @@ exports[`date > 1211 > weekday > with abbreviated = true and context = true 1`]

exports[`date > 1211 > weekday > with context = true 1`] = `"Saturday"`;

exports[`date > 1337 > anytime > with only Date refDate 1`] = `2020-08-31T23:49:36.088Z`;

exports[`date > 1337 > anytime > with only number refDate 1`] = `2020-08-31T23:49:36.088Z`;

exports[`date > 1337 > anytime > with only string refDate 1`] = `2020-08-31T23:49:36.088Z`;

exports[`date > 1337 > between > with Date dates 1`] = `2021-03-09T04:11:24.667Z`;

exports[`date > 1337 > between > with mixed dates 1`] = `2021-03-09T04:11:24.667Z`;
Expand Down
19 changes: 18 additions & 1 deletion test/date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ const refDate = '2021-02-21T17:09:15.711Z';

describe('date', () => {
seededTests(faker, 'date', (t) => {
t.describe('anytime', (t) => {
t.it('with only string refDate', { refDate })
.it('with only Date refDate', { refDate: new Date(refDate) })
.it('with only number refDate', {
refDate: new Date(refDate).getTime(),
});
});

t.describeEach(
'past',
'future'
Expand Down Expand Up @@ -188,12 +196,21 @@ describe('date', () => {
});

// No changes to these methods
t.skip('birthdate').skip('month').skip('weekday');
t.skip('anytime').skip('birthdate').skip('month').skip('weekday');
});
});

describe(`random seeded tests for seed ${faker.seed()}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe('anytime()', () => {
it('should return a date', () => {
const actual = faker.date.anytime();

expect(actual).toBeDefined();
expect(actual).toBeInstanceOf(Date);
});
});

describe('past()', () => {
it('should return a date 5 years in the past', () => {
const today = new Date();
Expand Down

0 comments on commit 5d0fa81

Please sign in to comment.