Skip to content

Commit 3a44d5f

Browse files
ST-DDTShinigami92
andauthored
feat(date): introduce faker.defaultRefDate and setDefaultRefDate (#1757)
Co-authored-by: Shinigami <chrissi92@hotmail.de>
1 parent 28a9848 commit 3a44d5f

File tree

6 files changed

+105
-32
lines changed

6 files changed

+105
-32
lines changed

scripts/apidoc.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { resolve } from 'path';
2+
import { faker } from '../src';
23
import {
34
writeApiPagesIndex,
45
writeApiSearchIndex,
@@ -13,6 +14,7 @@ const pathOutputJson = resolve(pathOutputDir, 'typedoc.json');
1314

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

1719
const app = newTypeDocApp();
1820

src/faker.ts

+25
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export class Faker {
5151
locales: UsedLocales;
5252
private _locale: UsableLocale;
5353
private _localeFallback: UsableLocale;
54+
private _defaultRefDate: () => Date = () => new Date();
5455

5556
get locale(): UsableLocale {
5657
return this._locale;
@@ -80,6 +81,30 @@ export class Faker {
8081
this._localeFallback = localeFallback;
8182
}
8283

84+
/**
85+
* Gets a new reference date used to generate relative dates.
86+
*/
87+
get defaultRefDate(): () => Date {
88+
return this._defaultRefDate;
89+
}
90+
91+
/**
92+
* Sets the `refDate` source to use if no `refDate` date is passed to the date methods.
93+
*
94+
* @param dateOrSource The function or the static value used to generate the `refDate` date instance.
95+
* The function must return a new valid `Date` instance for every call.
96+
* Defaults to `() => new Date()`.
97+
*/
98+
setDefaultRefDate(
99+
dateOrSource: string | Date | number | (() => Date) = () => new Date()
100+
): void {
101+
if (typeof dateOrSource === 'function') {
102+
this._defaultRefDate = dateOrSource;
103+
} else {
104+
this._defaultRefDate = () => new Date(dateOrSource);
105+
}
106+
}
107+
83108
readonly definitions: LocaleDefinition = this.initDefinitions();
84109

85110
/** @internal */

src/modules/date/index.ts

+33-29
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ import { deprecated } from '../../internal/deprecated';
55

66
/**
77
* Converts date passed as a string, number or Date to a Date object.
8-
* If nothing or a non parsable value is passed, takes current date.
8+
* If nothing or a non parsable value is passed, then it will take the value from the given fallback.
99
*
10-
* @param date Date
10+
* @param date The date to convert.
11+
* @param fallback The fallback date to use if the passed date is not valid.
1112
*/
12-
function toDate(date?: string | Date | number): Date {
13+
function toDate(
14+
date?: string | Date | number,
15+
fallback: () => Date = () => new Date()
16+
): Date {
1317
date = new Date(date);
1418
if (isNaN(date.valueOf())) {
15-
date = new Date();
19+
date = fallback();
1620
}
1721

1822
return date;
@@ -38,7 +42,7 @@ export class DateModule {
3842
*
3943
* @param options The optional options object.
4044
* @param options.years The range of years the date may be in the past. Defaults to `1`.
41-
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
45+
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
4246
*
4347
* @see faker.date.recent()
4448
*
@@ -59,15 +63,15 @@ export class DateModule {
5963
/**
6064
* The date to use as reference point for the newly generated date.
6165
*
62-
* @default new Date()
66+
* @default faker.defaultRefDate()
6367
*/
6468
refDate?: string | Date | number;
6569
}): Date;
6670
/**
6771
* Generates a random date in the past.
6872
*
6973
* @param years The range of years the date may be in the past. Defaults to `1`.
70-
* @param refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
74+
* @param refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
7175
*
7276
* @see faker.date.recent()
7377
*
@@ -86,7 +90,7 @@ export class DateModule {
8690
*
8791
* @param options The optional options object.
8892
* @param options.years The range of years the date may be in the past. Defaults to `1`.
89-
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
93+
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
9094
* @param legacyRefDate Deprecated, use `options.refDate` instead.
9195
*
9296
* @see faker.date.recent()
@@ -111,7 +115,7 @@ export class DateModule {
111115
/**
112116
* The date to use as reference point for the newly generated date.
113117
*
114-
* @default new Date()
118+
* @default faker.defaultRefDate()
115119
*/
116120
refDate?: string | Date | number;
117121
},
@@ -142,7 +146,7 @@ export class DateModule {
142146
throw new FakerError('Years must be greater than 0.');
143147
}
144148

145-
const date = toDate(refDate);
149+
const date = toDate(refDate, this.faker.defaultRefDate);
146150
const range = {
147151
min: 1000,
148152
max: years * 365 * 24 * 3600 * 1000,
@@ -160,7 +164,7 @@ export class DateModule {
160164
*
161165
* @param options The optional options object.
162166
* @param options.years The range of years the date may be in the future. Defaults to `1`.
163-
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
167+
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
164168
*
165169
* @see faker.date.soon()
166170
*
@@ -181,15 +185,15 @@ export class DateModule {
181185
/**
182186
* The date to use as reference point for the newly generated date.
183187
*
184-
* @default new Date()
188+
* @default faker.defaultRefDate()
185189
*/
186190
refDate?: string | Date | number;
187191
}): Date;
188192
/**
189193
* Generates a random date in the future.
190194
*
191195
* @param years The range of years the date may be in the future. Defaults to `1`.
192-
* @param refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
196+
* @param refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
193197
*
194198
* @see faker.date.soon()
195199
*
@@ -208,7 +212,7 @@ export class DateModule {
208212
*
209213
* @param options The optional options object.
210214
* @param options.years The range of years the date may be in the future. Defaults to `1`.
211-
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
215+
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
212216
* @param legacyRefDate Deprecated, use `options.refDate` instead.
213217
*
214218
* @see faker.date.soon()
@@ -233,7 +237,7 @@ export class DateModule {
233237
/**
234238
* The date to use as reference point for the newly generated date.
235239
*
236-
* @default new Date()
240+
* @default faker.defaultRefDate()
237241
*/
238242
refDate?: string | Date | number;
239243
},
@@ -264,7 +268,7 @@ export class DateModule {
264268
throw new FakerError('Years must be greater than 0.');
265269
}
266270

267-
const date = toDate(refDate);
271+
const date = toDate(refDate, this.faker.defaultRefDate);
268272
const range = {
269273
min: 1000,
270274
max: years * 365 * 24 * 3600 * 1000,
@@ -552,7 +556,7 @@ export class DateModule {
552556
*
553557
* @param options The optional options object.
554558
* @param options.days The range of days the date may be in the past. Defaults to `1`.
555-
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
559+
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
556560
*
557561
* @see faker.date.past()
558562
*
@@ -573,15 +577,15 @@ export class DateModule {
573577
/**
574578
* The date to use as reference point for the newly generated date.
575579
*
576-
* @default new Date()
580+
* @default faker.defaultRefDate()
577581
*/
578582
refDate?: string | Date | number;
579583
}): Date;
580584
/**
581585
* Generates a random date in the recent past.
582586
*
583587
* @param days The range of days the date may be in the past. Defaults to `1`.
584-
* @param refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
588+
* @param refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
585589
*
586590
* @see faker.date.past()
587591
*
@@ -600,7 +604,7 @@ export class DateModule {
600604
*
601605
* @param options The optional options object.
602606
* @param options.days The range of days the date may be in the past. Defaults to `1`.
603-
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
607+
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
604608
* @param legacyRefDate Deprecated, use `options.refDate` instead.
605609
*
606610
* @see faker.date.past()
@@ -625,7 +629,7 @@ export class DateModule {
625629
/**
626630
* The date to use as reference point for the newly generated date.
627631
*
628-
* @default new Date()
632+
* @default faker.defaultRefDate()
629633
*/
630634
refDate?: string | Date | number;
631635
},
@@ -651,7 +655,7 @@ export class DateModule {
651655
throw new FakerError('Days must be greater than 0.');
652656
}
653657

654-
const date = toDate(refDate);
658+
const date = toDate(refDate, this.faker.defaultRefDate);
655659
const range = {
656660
min: 1000,
657661
max: days * 24 * 3600 * 1000,
@@ -669,7 +673,7 @@ export class DateModule {
669673
*
670674
* @param options The optional options object.
671675
* @param options.days The range of days the date may be in the future. Defaults to `1`.
672-
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
676+
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
673677
*
674678
* @see faker.date.future()
675679
*
@@ -690,15 +694,15 @@ export class DateModule {
690694
/**
691695
* The date to use as reference point for the newly generated date.
692696
*
693-
* @default new Date()
697+
* @default faker.defaultRefDate()
694698
*/
695699
refDate?: string | Date | number;
696700
}): Date;
697701
/**
698702
* Generates a random date in the near future.
699703
*
700704
* @param days The range of days the date may be in the future. Defaults to `1`.
701-
* @param refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
705+
* @param refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
702706
*
703707
* @see faker.date.future()
704708
*
@@ -717,7 +721,7 @@ export class DateModule {
717721
*
718722
* @param options The optional options object.
719723
* @param options.days The range of days the date may be in the future. Defaults to `1`.
720-
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date()`.
724+
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`.
721725
* @param legacyRefDate Deprecated, use `options.refDate` instead.
722726
*
723727
* @see faker.date.future()
@@ -742,7 +746,7 @@ export class DateModule {
742746
/**
743747
* The date to use as reference point for the newly generated date.
744748
*
745-
* @default new Date()
749+
* @default faker.defaultRefDate()
746750
*/
747751
refDate?: string | Date | number;
748752
},
@@ -768,7 +772,7 @@ export class DateModule {
768772
throw new FakerError('Days must be greater than 0.');
769773
}
770774

771-
const date = toDate(refDate);
775+
const date = toDate(refDate, this.faker.defaultRefDate);
772776
const range = {
773777
min: 1000,
774778
max: days * 24 * 3600 * 1000,
@@ -938,7 +942,7 @@ export class DateModule {
938942
/**
939943
* The date to use as reference point for the newly generated date.
940944
*
941-
* @default new Date()
945+
* @default faker.defaultRefDate()
942946
*/
943947
refDate?: string | Date | number;
944948
} = {}

src/modules/git/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export class GitModule {
143143
* Generates a date string for a git commit using the same format as `git log`.
144144
*
145145
* @param options The optional options object.
146-
* @param options.refDate The date to use as reference point for the commit. Defaults to `new Date()`.
146+
* @param options.refDate The date to use as reference point for the commit. Defaults to `faker.defaultRefDate()`.
147147
*
148148
* @example
149149
* faker.git.commitDate() // 'Mon Nov 7 14:40:58 2022 +0600'
@@ -156,12 +156,12 @@ export class GitModule {
156156
/**
157157
* The date to use as reference point for the commit.
158158
*
159-
* @default new Date()
159+
* @default faker.defaultRefDate()
160160
*/
161161
refDate?: string | Date | number;
162162
} = {}
163163
): string {
164-
const { refDate = new Date() } = options;
164+
const { refDate = this.faker.defaultRefDate() } = options;
165165

166166
const dateParts = GIT_DATE_FORMAT_BASE.format(
167167
this.faker.date.recent({ days: 1, refDate })

test/date.spec.ts

+28
Original file line numberDiff line numberDiff line change
@@ -732,4 +732,32 @@ describe('date', () => {
732732
});
733733
}
734734
});
735+
736+
describe('refDateSource', () => {
737+
afterEach(() => {
738+
faker.setDefaultRefDate();
739+
});
740+
741+
it('should use the refDateSource when refDate is not provided (with function)', () => {
742+
faker.setDefaultRefDate(() => new Date(Date.UTC(2020, 0, 1)));
743+
faker.seed(20200101);
744+
const date = faker.date.past();
745+
expect(date).toEqual(new Date('2019-02-25T21:52:41.824Z'));
746+
747+
faker.seed(20200101);
748+
const date2 = faker.date.past();
749+
expect(date2).toEqual(new Date('2019-02-25T21:52:41.824Z'));
750+
});
751+
752+
it('should use the refDateSource when refDate is not provided (with value)', () => {
753+
faker.setDefaultRefDate(Date.UTC(2020, 0, 1));
754+
faker.seed(20200101);
755+
const date = faker.date.past();
756+
expect(date).toEqual(new Date('2019-02-25T21:52:41.824Z'));
757+
758+
faker.seed(20200101);
759+
const date2 = faker.date.past();
760+
expect(date2).toEqual(new Date('2019-02-25T21:52:41.824Z'));
761+
});
762+
});
735763
});

test/faker.spec.ts

+14
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,18 @@ describe('faker', () => {
141141
expect(actual).toBe('Oriental');
142142
});
143143
});
144+
145+
describe('defaultRefDate', () => {
146+
it('should be a defined', () => {
147+
expect(faker.defaultRefDate).toBeDefined();
148+
});
149+
150+
it('should be a date in the very recent past', () => {
151+
const start = Date.now();
152+
const refDate = faker.defaultRefDate().getTime();
153+
const end = Date.now();
154+
expect(refDate).toBeGreaterThanOrEqual(start);
155+
expect(refDate).toBeLessThanOrEqual(end);
156+
});
157+
});
144158
});

0 commit comments

Comments
 (0)