Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(date): introduce faker.defaultRefDate and setDefaultRefDate #1757

Merged
merged 15 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions scripts/apidoc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { resolve } from 'path';
import { faker } from '../src';
import {
writeApiPagesIndex,
writeApiSearchIndex,
Expand All @@ -12,6 +13,7 @@ const pathOutputJson = resolve(pathOutputDir, 'typedoc.json');

async function build(): Promise<void> {
await initMarkdownRenderer();
faker.setDefaultRefDate(Date.UTC(2023, 0, 1));

const app = newTypeDocApp();

Expand Down
25 changes: 25 additions & 0 deletions src/faker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class Faker {
locales: UsedLocales;
private _locale: UsableLocale;
private _localeFallback: UsableLocale;
private _defaultRefDate: () => Date;
xDivisionByZerox marked this conversation as resolved.
Show resolved Hide resolved
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved

get locale(): UsableLocale {
return this._locale;
Expand Down Expand Up @@ -80,6 +81,30 @@ export class Faker {
this._localeFallback = localeFallback;
}

/**
* Gets a new reference date used to generate relative dates.
*/
get defaultRefDate(): () => Date {
return this._defaultRefDate;
}

/**
* Sets the `refDate` source to use if no `refDate` date is passed to the date methods.
*
* @param dateOrSource The function or the static value used to generate the `refDate` date instance.
* The function must return a new valid `Date` instance for every call.
* Defaults to `() => new Date()`.
*/
setDefaultRefDate(
dateOrSource: string | Date | number | (() => Date) = () => new Date()
): void {
if (typeof dateOrSource === 'function') {
this._defaultRefDate = dateOrSource;
} else {
this._defaultRefDate = () => new Date(dateOrSource);
}
}

readonly definitions: LocaleDefinition = this.initDefinitions();

/** @internal */
Expand Down
62 changes: 33 additions & 29 deletions src/modules/date/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import { deprecated } from '../../internal/deprecated';

/**
* Converts date passed as a string, number or Date to a Date object.
* If nothing or a non parsable value is passed, takes current date.
* If nothing or a non parsable value is passed, then it will take the value from the given fallback.
*
* @param date Date
* @param date The date to convert.
* @param fallback The fallback date to use if the passed date is not valid.
*/
function toDate(date?: string | Date | number): Date {
function toDate(
date?: string | Date | number,
fallback: () => Date = () => new Date()
): Date {
date = new Date(date);
if (isNaN(date.valueOf())) {
date = new Date();
date = fallback();
}

return date;
Expand All @@ -38,7 +42,7 @@ export class DateModule {
*
* @param options The optional options object.
* @param options.years The range of years the date may be in the past. Defaults to `1`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
xDivisionByZerox marked this conversation as resolved.
Show resolved Hide resolved
*
* @see faker.date.recent()
*
Expand All @@ -59,15 +63,15 @@ export class DateModule {
/**
* The date to use as reference point for the newly generated date.
*
* @default new Date()
* @default faker.defaultRefDate()
*/
refDate?: string | Date | number;
}): Date;
/**
* Generates a random date in the past.
*
* @param years The range of years the date may be in the past. Defaults to `1`.
* @param refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
* @param refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
*
* @see faker.date.recent()
*
Expand All @@ -86,7 +90,7 @@ export class DateModule {
*
* @param options The optional options object.
* @param options.years The range of years the date may be in the past. Defaults to `1`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
* @param legacyRefDate Deprecated, use `options.refDate` instead.
*
* @see faker.date.recent()
Expand All @@ -111,7 +115,7 @@ export class DateModule {
/**
* The date to use as reference point for the newly generated date.
*
* @default new Date()
* @default faker.defaultRefDate()
*/
refDate?: string | Date | number;
},
Expand Down Expand Up @@ -142,7 +146,7 @@ export class DateModule {
throw new FakerError('Years must be greater than 0.');
}

const date = toDate(refDate);
const date = toDate(refDate, this.faker.defaultRefDate);
const range = {
min: 1000,
max: years * 365 * 24 * 3600 * 1000,
Expand All @@ -160,7 +164,7 @@ export class DateModule {
*
* @param options The optional options object.
* @param options.years The range of years the date may be in the future. Defaults to `1`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
*
* @see faker.date.soon()
*
Expand All @@ -181,15 +185,15 @@ export class DateModule {
/**
* The date to use as reference point for the newly generated date.
*
* @default new Date()
* @default faker.defaultRefDate()
*/
refDate?: string | Date | number;
}): Date;
/**
* Generates a random date in the future.
*
* @param years The range of years the date may be in the future. Defaults to `1`.
* @param refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
* @param refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
*
* @see faker.date.soon()
*
Expand All @@ -208,7 +212,7 @@ export class DateModule {
*
* @param options The optional options object.
* @param options.years The range of years the date may be in the future. Defaults to `1`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
* @param legacyRefDate Deprecated, use `options.refDate` instead.
*
* @see faker.date.soon()
Expand All @@ -233,7 +237,7 @@ export class DateModule {
/**
* The date to use as reference point for the newly generated date.
*
* @default new Date()
* @default faker.defaultRefDate()
*/
refDate?: string | Date | number;
},
Expand Down Expand Up @@ -264,7 +268,7 @@ export class DateModule {
throw new FakerError('Years must be greater than 0.');
}

const date = toDate(refDate);
const date = toDate(refDate, this.faker.defaultRefDate);
const range = {
min: 1000,
max: years * 365 * 24 * 3600 * 1000,
Expand Down Expand Up @@ -552,7 +556,7 @@ export class DateModule {
*
* @param options The optional options object.
* @param options.days The range of days the date may be in the past. Defaults to `1`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
*
* @see faker.date.past()
*
Expand All @@ -573,15 +577,15 @@ export class DateModule {
/**
* The date to use as reference point for the newly generated date.
*
* @default new Date()
* @default faker.defaultRefDate()
*/
refDate?: string | Date | number;
}): Date;
/**
* Generates a random date in the recent past.
*
* @param days The range of days the date may be in the past. Defaults to `1`.
* @param refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
* @param refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
*
* @see faker.date.past()
*
Expand All @@ -600,7 +604,7 @@ export class DateModule {
*
* @param options The optional options object.
* @param options.days The range of days the date may be in the past. Defaults to `1`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
* @param legacyRefDate Deprecated, use `options.refDate` instead.
*
* @see faker.date.past()
Expand All @@ -625,7 +629,7 @@ export class DateModule {
/**
* The date to use as reference point for the newly generated date.
*
* @default new Date()
* @default faker.defaultRefDate()
*/
refDate?: string | Date | number;
},
Expand All @@ -651,7 +655,7 @@ export class DateModule {
throw new FakerError('Days must be greater than 0.');
}

const date = toDate(refDate);
const date = toDate(refDate, this.faker.defaultRefDate);
const range = {
min: 1000,
max: days * 24 * 3600 * 1000,
Expand All @@ -669,7 +673,7 @@ export class DateModule {
*
* @param options The optional options object.
* @param options.days The range of days the date may be in the future. Defaults to `1`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
*
* @see faker.date.future()
*
Expand All @@ -690,15 +694,15 @@ export class DateModule {
/**
* The date to use as reference point for the newly generated date.
*
* @default new Date()
* @default faker.defaultRefDate()
*/
refDate?: string | Date | number;
}): Date;
/**
* Generates a random date in the near future.
*
* @param days The range of days the date may be in the future. Defaults to `1`.
* @param refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
* @param refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
*
* @see faker.date.future()
*
Expand All @@ -717,7 +721,7 @@ export class DateModule {
*
* @param options The optional options object.
* @param options.days The range of days the date may be in the future. Defaults to `1`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
* @param legacyRefDate Deprecated, use `options.refDate` instead.
*
* @see faker.date.future()
Expand All @@ -742,7 +746,7 @@ export class DateModule {
/**
* The date to use as reference point for the newly generated date.
*
* @default new Date()
* @default faker.defaultRefDate()
*/
refDate?: string | Date | number;
},
Expand All @@ -768,7 +772,7 @@ export class DateModule {
throw new FakerError('Days must be greater than 0.');
}

const date = toDate(refDate);
const date = toDate(refDate, this.faker.defaultRefDate);
const range = {
min: 1000,
max: days * 24 * 3600 * 1000,
Expand Down Expand Up @@ -938,7 +942,7 @@ export class DateModule {
/**
* The date to use as reference point for the newly generated date.
*
* @default new Date()
* @default faker.defaultRefDate()
*/
refDate?: string | Date | number;
} = {}
Expand Down
28 changes: 28 additions & 0 deletions test/date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -732,4 +732,32 @@ describe('date', () => {
});
}
});

describe('refDateSource', () => {
afterEach(() => {
faker.setDefaultRefDate();
});

it('should use the refDateSource when refDate is not provided (with function)', () => {
faker.setDefaultRefDate(() => new Date(Date.UTC(2020, 0, 1)));
faker.seed(20200101);
const date = faker.date.past();
expect(date).toEqual(new Date('2019-02-25T21:52:41.824Z'));

faker.seed(20200101);
const date2 = faker.date.past();
expect(date2).toEqual(new Date('2019-02-25T21:52:41.824Z'));
});

it('should use the refDateSource when refDate is not provided (with value)', () => {
faker.setDefaultRefDate(Date.UTC(2020, 0, 1));
faker.seed(20200101);
const date = faker.date.past();
expect(date).toEqual(new Date('2019-02-25T21:52:41.824Z'));

faker.seed(20200101);
const date2 = faker.date.past();
expect(date2).toEqual(new Date('2019-02-25T21:52:41.824Z'));
});
});
});