From 1ec72513c0e4232d5dc15fecd6bc368214e6355d Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Thu, 19 Jan 2023 00:13:43 +0100 Subject: [PATCH 1/9] feat(date): introduce refDateSource --- src/index.ts | 1 + src/modules/date/index.ts | 59 ++++++++++++++++++++++++--------------- test/date.spec.ts | 15 +++++++++- 3 files changed, 52 insertions(+), 23 deletions(-) diff --git a/src/index.ts b/src/index.ts index 5d5c280c86c..4f8ba9dabaa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -49,6 +49,7 @@ export type { CommerceModule } from './modules/commerce'; export type { CompanyModule } from './modules/company'; export type { DatabaseModule } from './modules/database'; export type { DatatypeModule } from './modules/datatype'; +export { setRefDateSource } from './modules/date'; export type { DateModule } from './modules/date'; export type { FinanceModule } from './modules/finance'; export type { GitModule } from './modules/git'; diff --git a/src/modules/date/index.ts b/src/modules/date/index.ts index b3c2d71073b..cd1256a7016 100644 --- a/src/modules/date/index.ts +++ b/src/modules/date/index.ts @@ -12,12 +12,27 @@ import { deprecated } from '../../internal/deprecated'; function toDate(date?: string | Date | number): Date { date = new Date(date); if (isNaN(date.valueOf())) { - date = new Date(); + date = refDateSource(); } return date; } +/** + * The ref date source to use if no ref date is passed to the methods. + */ +let refDateSource: () => Date = () => new Date(); + +/** + * Sets the `refDate` source to use if `refDate` date is passed to the date methods. + * + * @param source The function used to generate the `refDate` date instance. Defaults to `() => new Date()`. + */ +// TODO @ST-DDT 2023-01-19: This should be a config option per faker instance +export function setRefDateSource(source: () => Date = () => new Date()): void { + refDateSource = source; +} + /** * Module to generate dates. */ @@ -38,7 +53,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 `refDateSource()`. * * @see faker.date.recent() * @@ -59,7 +74,7 @@ export class DateModule { /** * The date to use as reference point for the newly generated date. * - * @default new Date() + * @default refDateSource() */ refDate?: string | Date | number; }): Date; @@ -67,7 +82,7 @@ export class DateModule { * 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 `refDateSource()`. * * @see faker.date.recent() * @@ -86,7 +101,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 `refDateSource()`. * @param legacyRefDate Deprecated, use `options.refDate` instead. * * @see faker.date.recent() @@ -111,7 +126,7 @@ export class DateModule { /** * The date to use as reference point for the newly generated date. * - * @default new Date() + * @default refDateSource() */ refDate?: string | Date | number; }, @@ -160,7 +175,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 `refDateSource()`. * * @see faker.date.soon() * @@ -181,7 +196,7 @@ export class DateModule { /** * The date to use as reference point for the newly generated date. * - * @default new Date() + * @default refDateSource() */ refDate?: string | Date | number; }): Date; @@ -189,7 +204,7 @@ export class DateModule { * 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 `refDateSource()`. * * @see faker.date.soon() * @@ -208,7 +223,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 `refDateSource()`. * @param legacyRefDate Deprecated, use `options.refDate` instead. * * @see faker.date.soon() @@ -233,7 +248,7 @@ export class DateModule { /** * The date to use as reference point for the newly generated date. * - * @default new Date() + * @default refDateSource() */ refDate?: string | Date | number; }, @@ -552,7 +567,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 `refDateSource()`. * * @see faker.date.past() * @@ -573,7 +588,7 @@ export class DateModule { /** * The date to use as reference point for the newly generated date. * - * @default new Date() + * @default refDateSource() */ refDate?: string | Date | number; }): Date; @@ -581,7 +596,7 @@ export class DateModule { * 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 `refDateSource()`. * * @see faker.date.past() * @@ -600,7 +615,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 `refDateSource()`. * @param legacyRefDate Deprecated, use `options.refDate` instead. * * @see faker.date.past() @@ -625,7 +640,7 @@ export class DateModule { /** * The date to use as reference point for the newly generated date. * - * @default new Date() + * @default refDateSource() */ refDate?: string | Date | number; }, @@ -669,7 +684,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 `refDateSource()`. * * @see faker.date.future() * @@ -690,7 +705,7 @@ export class DateModule { /** * The date to use as reference point for the newly generated date. * - * @default new Date() + * @default refDateSource() */ refDate?: string | Date | number; }): Date; @@ -698,7 +713,7 @@ export class DateModule { * 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 `refDateSource()`. * * @see faker.date.future() * @@ -717,7 +732,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 `refDateSource()`. * @param legacyRefDate Deprecated, use `options.refDate` instead. * * @see faker.date.future() @@ -742,7 +757,7 @@ export class DateModule { /** * The date to use as reference point for the newly generated date. * - * @default new Date() + * @default refDateSource() */ refDate?: string | Date | number; }, @@ -938,7 +953,7 @@ export class DateModule { /** * The date to use as reference point for the newly generated date. * - * @default new Date() + * @default refDateSource() */ refDate?: string | Date | number; } = {} diff --git a/test/date.spec.ts b/test/date.spec.ts index d8e617d6529..8414daa62b2 100644 --- a/test/date.spec.ts +++ b/test/date.spec.ts @@ -1,5 +1,5 @@ import { afterEach, describe, expect, it } from 'vitest'; -import { faker, FakerError } from '../src'; +import { faker, FakerError, setRefDateSource } from '../src'; import { seededTests } from './support/seededRuns'; const converterMap = [ @@ -732,4 +732,17 @@ describe('date', () => { }); } }); + + describe('refDateSource', () => { + afterEach(() => { + setRefDateSource(); + }); + + it('should use the refDateSource when refDate is not provided', () => { + setRefDateSource(() => new Date(2020, 0, 1)); + faker.seed(20200101); + const date = faker.date.past({ years: 1 }); + expect(date).toEqual(new Date('2019-02-25T20:52:41.824Z')); + }); + }); }); From bd14609a78ec162b83c068118e5457b01c819a2f Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Thu, 19 Jan 2023 00:49:54 +0100 Subject: [PATCH 2/9] chore: improvements --- scripts/apidoc.ts | 2 ++ src/modules/date/index.ts | 15 +++++++++++---- test/date.spec.ts | 23 +++++++++++++++++++---- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/scripts/apidoc.ts b/scripts/apidoc.ts index 921859e7a77..c95d68bc4f1 100644 --- a/scripts/apidoc.ts +++ b/scripts/apidoc.ts @@ -1,4 +1,5 @@ import { resolve } from 'path'; +import { setRefDateSource } from '../src'; import { writeApiPagesIndex, writeApiSearchIndex, @@ -12,6 +13,7 @@ const pathOutputJson = resolve(pathOutputDir, 'typedoc.json'); async function build(): Promise { await initMarkdownRenderer(); + setRefDateSource(Date.UTC(2023, 0, 1)); const app = newTypeDocApp(); diff --git a/src/modules/date/index.ts b/src/modules/date/index.ts index cd1256a7016..7c06f79ad2e 100644 --- a/src/modules/date/index.ts +++ b/src/modules/date/index.ts @@ -26,11 +26,18 @@ let refDateSource: () => Date = () => new Date(); /** * Sets the `refDate` source to use if `refDate` date is passed to the date methods. * - * @param source The function used to generate the `refDate` date instance. Defaults to `() => new Date()`. + * @param source The function or the static value used to generate the `refDate` date instance. + * The function must return a new `Date` instance for every call. + * Defaults to `() => new Date()`. */ -// TODO @ST-DDT 2023-01-19: This should be a config option per faker instance -export function setRefDateSource(source: () => Date = () => new Date()): void { - refDateSource = source; +export function setRefDateSource( + source: string | Date | number | (() => Date) = () => new Date() +): void { + if (typeof source === 'function') { + refDateSource = source; + } else { + refDateSource = () => toDate(source); + } } /** diff --git a/test/date.spec.ts b/test/date.spec.ts index 8414daa62b2..40ee4f4d2f0 100644 --- a/test/date.spec.ts +++ b/test/date.spec.ts @@ -738,11 +738,26 @@ describe('date', () => { setRefDateSource(); }); - it('should use the refDateSource when refDate is not provided', () => { - setRefDateSource(() => new Date(2020, 0, 1)); + it('should use the refDateSource when refDate is not provided (with function)', () => { + setRefDateSource(() => new Date(Date.UTC(2020, 0, 1))); faker.seed(20200101); - const date = faker.date.past({ years: 1 }); - expect(date).toEqual(new Date('2019-02-25T20:52:41.824Z')); + 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)', () => { + setRefDateSource(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')); }); }); }); From 39c41d0a995b73c13b88ae80dcd7f505f3162927 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Thu, 19 Jan 2023 01:06:43 +0100 Subject: [PATCH 3/9] chore: jsdocs improvements --- src/modules/date/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/modules/date/index.ts b/src/modules/date/index.ts index 7c06f79ad2e..1c708f696e1 100644 --- a/src/modules/date/index.ts +++ b/src/modules/date/index.ts @@ -5,7 +5,7 @@ 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 `refDateSource()`. * * @param date Date */ @@ -27,7 +27,7 @@ let refDateSource: () => Date = () => new Date(); * Sets the `refDate` source to use if `refDate` date is passed to the date methods. * * @param source The function or the static value used to generate the `refDate` date instance. - * The function must return a new `Date` instance for every call. + * The function must return a new valid `Date` instance for every call. * Defaults to `() => new Date()`. */ export function setRefDateSource( @@ -36,7 +36,7 @@ export function setRefDateSource( if (typeof source === 'function') { refDateSource = source; } else { - refDateSource = () => toDate(source); + refDateSource = () => new Date(source); } } From 5208b42f93bffde0c78c6665a4654cfb00fc6016 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Sat, 21 Jan 2023 14:06:45 +0100 Subject: [PATCH 4/9] refactor: use faker.defaultRefDate as fallback --- scripts/apidoc.ts | 4 +- src/faker.ts | 25 ++++++++++++ src/index.ts | 1 - src/modules/date/index.ts | 84 +++++++++++++++------------------------ test/date.spec.ts | 8 ++-- 5 files changed, 64 insertions(+), 58 deletions(-) diff --git a/scripts/apidoc.ts b/scripts/apidoc.ts index c95d68bc4f1..8a3a6a00d9c 100644 --- a/scripts/apidoc.ts +++ b/scripts/apidoc.ts @@ -1,5 +1,5 @@ import { resolve } from 'path'; -import { setRefDateSource } from '../src'; +import { faker } from '../src'; import { writeApiPagesIndex, writeApiSearchIndex, @@ -13,7 +13,7 @@ const pathOutputJson = resolve(pathOutputDir, 'typedoc.json'); async function build(): Promise { await initMarkdownRenderer(); - setRefDateSource(Date.UTC(2023, 0, 1)); + faker.setDefaultRefDate(Date.UTC(2023, 0, 1)); const app = newTypeDocApp(); diff --git a/src/faker.ts b/src/faker.ts index a5a6dbc275b..5018ee76569 100644 --- a/src/faker.ts +++ b/src/faker.ts @@ -51,6 +51,7 @@ export class Faker { locales: UsedLocales; private _locale: UsableLocale; private _localeFallback: UsableLocale; + private _defaultRefDate: () => Date; get locale(): UsableLocale { return this._locale; @@ -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() + ) { + if (typeof dateOrSource === 'function') { + this._defaultRefDate = dateOrSource; + } else { + this._defaultRefDate = () => new Date(dateOrSource); + } + } + readonly definitions: LocaleDefinition = this.initDefinitions(); /** @internal */ diff --git a/src/index.ts b/src/index.ts index 4f8ba9dabaa..5d5c280c86c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -49,7 +49,6 @@ export type { CommerceModule } from './modules/commerce'; export type { CompanyModule } from './modules/company'; export type { DatabaseModule } from './modules/database'; export type { DatatypeModule } from './modules/datatype'; -export { setRefDateSource } from './modules/date'; export type { DateModule } from './modules/date'; export type { FinanceModule } from './modules/finance'; export type { GitModule } from './modules/git'; diff --git a/src/modules/date/index.ts b/src/modules/date/index.ts index 1c708f696e1..5c2c49eb4d5 100644 --- a/src/modules/date/index.ts +++ b/src/modules/date/index.ts @@ -5,41 +5,23 @@ 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, then it will take the value from `refDateSource()`. + * 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 = refDateSource(); + date = fallback(); } return date; } -/** - * The ref date source to use if no ref date is passed to the methods. - */ -let refDateSource: () => Date = () => new Date(); - -/** - * Sets the `refDate` source to use if `refDate` date is passed to the date methods. - * - * @param source 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()`. - */ -export function setRefDateSource( - source: string | Date | number | (() => Date) = () => new Date() -): void { - if (typeof source === 'function') { - refDateSource = source; - } else { - refDateSource = () => new Date(source); - } -} - /** * Module to generate dates. */ @@ -60,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 `refDateSource()`. + * @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`. * * @see faker.date.recent() * @@ -81,7 +63,7 @@ export class DateModule { /** * The date to use as reference point for the newly generated date. * - * @default refDateSource() + * @default faker.defaultRefDate() */ refDate?: string | Date | number; }): Date; @@ -89,7 +71,7 @@ export class DateModule { * 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 `refDateSource()`. + * @param refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`. * * @see faker.date.recent() * @@ -108,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 `refDateSource()`. + * @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() @@ -133,7 +115,7 @@ export class DateModule { /** * The date to use as reference point for the newly generated date. * - * @default refDateSource() + * @default faker.defaultRefDate() */ refDate?: string | Date | number; }, @@ -164,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, @@ -182,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 `refDateSource()`. + * @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`. * * @see faker.date.soon() * @@ -203,7 +185,7 @@ export class DateModule { /** * The date to use as reference point for the newly generated date. * - * @default refDateSource() + * @default faker.defaultRefDate() */ refDate?: string | Date | number; }): Date; @@ -211,7 +193,7 @@ export class DateModule { * 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 `refDateSource()`. + * @param refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`. * * @see faker.date.soon() * @@ -230,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 `refDateSource()`. + * @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() @@ -255,7 +237,7 @@ export class DateModule { /** * The date to use as reference point for the newly generated date. * - * @default refDateSource() + * @default faker.defaultRefDate() */ refDate?: string | Date | number; }, @@ -286,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, @@ -574,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 `refDateSource()`. + * @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`. * * @see faker.date.past() * @@ -595,7 +577,7 @@ export class DateModule { /** * The date to use as reference point for the newly generated date. * - * @default refDateSource() + * @default faker.defaultRefDate() */ refDate?: string | Date | number; }): Date; @@ -603,7 +585,7 @@ export class DateModule { * 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 `refDateSource()`. + * @param refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`. * * @see faker.date.past() * @@ -622,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 `refDateSource()`. + * @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() @@ -647,7 +629,7 @@ export class DateModule { /** * The date to use as reference point for the newly generated date. * - * @default refDateSource() + * @default faker.defaultRefDate() */ refDate?: string | Date | number; }, @@ -673,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, @@ -691,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 `refDateSource()`. + * @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`. * * @see faker.date.future() * @@ -712,7 +694,7 @@ export class DateModule { /** * The date to use as reference point for the newly generated date. * - * @default refDateSource() + * @default faker.defaultRefDate() */ refDate?: string | Date | number; }): Date; @@ -720,7 +702,7 @@ export class DateModule { * 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 `refDateSource()`. + * @param refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`. * * @see faker.date.future() * @@ -739,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 `refDateSource()`. + * @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() @@ -764,7 +746,7 @@ export class DateModule { /** * The date to use as reference point for the newly generated date. * - * @default refDateSource() + * @default faker.defaultRefDate() */ refDate?: string | Date | number; }, @@ -790,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, @@ -960,7 +942,7 @@ export class DateModule { /** * The date to use as reference point for the newly generated date. * - * @default refDateSource() + * @default faker.defaultRefDate() */ refDate?: string | Date | number; } = {} diff --git a/test/date.spec.ts b/test/date.spec.ts index 40ee4f4d2f0..a9eaeb732b3 100644 --- a/test/date.spec.ts +++ b/test/date.spec.ts @@ -1,5 +1,5 @@ import { afterEach, describe, expect, it } from 'vitest'; -import { faker, FakerError, setRefDateSource } from '../src'; +import { faker, FakerError } from '../src'; import { seededTests } from './support/seededRuns'; const converterMap = [ @@ -735,11 +735,11 @@ describe('date', () => { describe('refDateSource', () => { afterEach(() => { - setRefDateSource(); + faker.setDefaultRefDate(); }); it('should use the refDateSource when refDate is not provided (with function)', () => { - setRefDateSource(() => new Date(Date.UTC(2020, 0, 1))); + 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')); @@ -750,7 +750,7 @@ describe('date', () => { }); it('should use the refDateSource when refDate is not provided (with value)', () => { - setRefDateSource(Date.UTC(2020, 0, 1)); + 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')); From 6c4a701a4926eee536240ef693261107ae441b6d Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Sat, 21 Jan 2023 14:11:12 +0100 Subject: [PATCH 5/9] chore: missing return type --- src/faker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/faker.ts b/src/faker.ts index 5018ee76569..b0eeb16e54d 100644 --- a/src/faker.ts +++ b/src/faker.ts @@ -97,7 +97,7 @@ export class Faker { */ setDefaultRefDate( dateOrSource: string | Date | number | (() => Date) = () => new Date() - ) { + ): void { if (typeof dateOrSource === 'function') { this._defaultRefDate = dateOrSource; } else { From d9d2ac0f78daaba9f79d1902c554b0a9bf0423c6 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Sun, 22 Jan 2023 16:23:32 +0100 Subject: [PATCH 6/9] chore: also configure new default for git module --- src/modules/git/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/modules/git/index.ts b/src/modules/git/index.ts index 76e786ab9b6..05d24f71e55 100644 --- a/src/modules/git/index.ts +++ b/src/modules/git/index.ts @@ -143,7 +143,7 @@ export class GitModule { * Generates a date string for a git commit using the same format as `git log`. * * @param options The optional options object. - * @param options.refDate The date to use as reference point for the commit. Defaults to `new Date()`. + * @param options.refDate The date to use as reference point for the commit. Defaults to `faker.defaultRefDate()`. * * @example * faker.git.commitDate() // 'Mon Nov 7 14:40:58 2022 +0600' @@ -156,12 +156,12 @@ export class GitModule { /** * The date to use as reference point for the commit. * - * @default new Date() + * @default faker.defaultRefDate() */ refDate?: string | Date | number; } = {} ): string { - const { refDate = new Date() } = options; + const { refDate = this.faker.defaultRefDate() } = options; const dateParts = GIT_DATE_FORMAT_BASE.format( this.faker.date.recent({ days: 1, refDate }) From 192cb826f06d727cdb3dae5867f792ebceef497d Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Mon, 23 Jan 2023 21:00:23 +0100 Subject: [PATCH 7/9] chore: apply suggestions Co-authored-by: Shinigami --- src/faker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/faker.ts b/src/faker.ts index b0eeb16e54d..7224c501d31 100644 --- a/src/faker.ts +++ b/src/faker.ts @@ -51,7 +51,7 @@ export class Faker { locales: UsedLocales; private _locale: UsableLocale; private _localeFallback: UsableLocale; - private _defaultRefDate: () => Date; + private _defaultRefDate: () => Date = () => new Date(); get locale(): UsableLocale { return this._locale; From 7f5bd2457db9a95d2dae3cc734dd3fa7a166f906 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Mon, 23 Jan 2023 21:13:44 +0100 Subject: [PATCH 8/9] test: ensure the defaultRefDate is set --- test/faker.spec.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/faker.spec.ts b/test/faker.spec.ts index d418f19bdfa..eebe6fc69b4 100644 --- a/test/faker.spec.ts +++ b/test/faker.spec.ts @@ -141,4 +141,14 @@ describe('faker', () => { expect(actual).toBe('Oriental'); }); }); + + describe('defaultRefDate', () => { + it('should be a defined', () => { + expect(faker.defaultRefDate).toBeDefined(); + }); + + it('should be a date in the past', () => { + expect(faker.defaultRefDate().getTime()).toBeLessThanOrEqual(Date.now()); + }); + }); }); From 87d9965d6224b8aa2d8dce2b13f2afb3d8c6f2e2 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Mon, 23 Jan 2023 21:13:59 +0100 Subject: [PATCH 9/9] test: ensure the defaultRefDate is set --- test/faker.spec.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/faker.spec.ts b/test/faker.spec.ts index eebe6fc69b4..50579f60d6f 100644 --- a/test/faker.spec.ts +++ b/test/faker.spec.ts @@ -147,8 +147,12 @@ describe('faker', () => { expect(faker.defaultRefDate).toBeDefined(); }); - it('should be a date in the past', () => { - expect(faker.defaultRefDate().getTime()).toBeLessThanOrEqual(Date.now()); + it('should be a date in the very recent past', () => { + const start = Date.now(); + const refDate = faker.defaultRefDate().getTime(); + const end = Date.now(); + expect(refDate).toBeGreaterThanOrEqual(start); + expect(refDate).toBeLessThanOrEqual(end); }); }); });