@@ -5,14 +5,18 @@ import { deprecated } from '../../internal/deprecated';
5
5
6
6
/**
7
7
* 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 .
9
9
*
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.
11
12
*/
12
- function toDate ( date ?: string | Date | number ) : Date {
13
+ function toDate (
14
+ date ?: string | Date | number ,
15
+ fallback : ( ) => Date = ( ) => new Date ( )
16
+ ) : Date {
13
17
date = new Date ( date ) ;
14
18
if ( isNaN ( date . valueOf ( ) ) ) {
15
- date = new Date ( ) ;
19
+ date = fallback ( ) ;
16
20
}
17
21
18
22
return date ;
@@ -38,7 +42,7 @@ export class DateModule {
38
42
*
39
43
* @param options The optional options object.
40
44
* @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 ()`.
42
46
*
43
47
* @see faker.date.recent()
44
48
*
@@ -59,15 +63,15 @@ export class DateModule {
59
63
/**
60
64
* The date to use as reference point for the newly generated date.
61
65
*
62
- * @default new Date ()
66
+ * @default faker.defaultRefDate ()
63
67
*/
64
68
refDate ?: string | Date | number ;
65
69
} ) : Date ;
66
70
/**
67
71
* Generates a random date in the past.
68
72
*
69
73
* @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 ()`.
71
75
*
72
76
* @see faker.date.recent()
73
77
*
@@ -86,7 +90,7 @@ export class DateModule {
86
90
*
87
91
* @param options The optional options object.
88
92
* @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 ()`.
90
94
* @param legacyRefDate Deprecated, use `options.refDate` instead.
91
95
*
92
96
* @see faker.date.recent()
@@ -111,7 +115,7 @@ export class DateModule {
111
115
/**
112
116
* The date to use as reference point for the newly generated date.
113
117
*
114
- * @default new Date ()
118
+ * @default faker.defaultRefDate ()
115
119
*/
116
120
refDate ?: string | Date | number ;
117
121
} ,
@@ -142,7 +146,7 @@ export class DateModule {
142
146
throw new FakerError ( 'Years must be greater than 0.' ) ;
143
147
}
144
148
145
- const date = toDate ( refDate ) ;
149
+ const date = toDate ( refDate , this . faker . defaultRefDate ) ;
146
150
const range = {
147
151
min : 1000 ,
148
152
max : years * 365 * 24 * 3600 * 1000 ,
@@ -160,7 +164,7 @@ export class DateModule {
160
164
*
161
165
* @param options The optional options object.
162
166
* @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 ()`.
164
168
*
165
169
* @see faker.date.soon()
166
170
*
@@ -181,15 +185,15 @@ export class DateModule {
181
185
/**
182
186
* The date to use as reference point for the newly generated date.
183
187
*
184
- * @default new Date ()
188
+ * @default faker.defaultRefDate ()
185
189
*/
186
190
refDate ?: string | Date | number ;
187
191
} ) : Date ;
188
192
/**
189
193
* Generates a random date in the future.
190
194
*
191
195
* @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 ()`.
193
197
*
194
198
* @see faker.date.soon()
195
199
*
@@ -208,7 +212,7 @@ export class DateModule {
208
212
*
209
213
* @param options The optional options object.
210
214
* @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 ()`.
212
216
* @param legacyRefDate Deprecated, use `options.refDate` instead.
213
217
*
214
218
* @see faker.date.soon()
@@ -233,7 +237,7 @@ export class DateModule {
233
237
/**
234
238
* The date to use as reference point for the newly generated date.
235
239
*
236
- * @default new Date ()
240
+ * @default faker.defaultRefDate ()
237
241
*/
238
242
refDate ?: string | Date | number ;
239
243
} ,
@@ -264,7 +268,7 @@ export class DateModule {
264
268
throw new FakerError ( 'Years must be greater than 0.' ) ;
265
269
}
266
270
267
- const date = toDate ( refDate ) ;
271
+ const date = toDate ( refDate , this . faker . defaultRefDate ) ;
268
272
const range = {
269
273
min : 1000 ,
270
274
max : years * 365 * 24 * 3600 * 1000 ,
@@ -552,7 +556,7 @@ export class DateModule {
552
556
*
553
557
* @param options The optional options object.
554
558
* @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 ()`.
556
560
*
557
561
* @see faker.date.past()
558
562
*
@@ -573,15 +577,15 @@ export class DateModule {
573
577
/**
574
578
* The date to use as reference point for the newly generated date.
575
579
*
576
- * @default new Date ()
580
+ * @default faker.defaultRefDate ()
577
581
*/
578
582
refDate ?: string | Date | number ;
579
583
} ) : Date ;
580
584
/**
581
585
* Generates a random date in the recent past.
582
586
*
583
587
* @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 ()`.
585
589
*
586
590
* @see faker.date.past()
587
591
*
@@ -600,7 +604,7 @@ export class DateModule {
600
604
*
601
605
* @param options The optional options object.
602
606
* @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 ()`.
604
608
* @param legacyRefDate Deprecated, use `options.refDate` instead.
605
609
*
606
610
* @see faker.date.past()
@@ -625,7 +629,7 @@ export class DateModule {
625
629
/**
626
630
* The date to use as reference point for the newly generated date.
627
631
*
628
- * @default new Date ()
632
+ * @default faker.defaultRefDate ()
629
633
*/
630
634
refDate ?: string | Date | number ;
631
635
} ,
@@ -651,7 +655,7 @@ export class DateModule {
651
655
throw new FakerError ( 'Days must be greater than 0.' ) ;
652
656
}
653
657
654
- const date = toDate ( refDate ) ;
658
+ const date = toDate ( refDate , this . faker . defaultRefDate ) ;
655
659
const range = {
656
660
min : 1000 ,
657
661
max : days * 24 * 3600 * 1000 ,
@@ -669,7 +673,7 @@ export class DateModule {
669
673
*
670
674
* @param options The optional options object.
671
675
* @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 ()`.
673
677
*
674
678
* @see faker.date.future()
675
679
*
@@ -690,15 +694,15 @@ export class DateModule {
690
694
/**
691
695
* The date to use as reference point for the newly generated date.
692
696
*
693
- * @default new Date ()
697
+ * @default faker.defaultRefDate ()
694
698
*/
695
699
refDate ?: string | Date | number ;
696
700
} ) : Date ;
697
701
/**
698
702
* Generates a random date in the near future.
699
703
*
700
704
* @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 ()`.
702
706
*
703
707
* @see faker.date.future()
704
708
*
@@ -717,7 +721,7 @@ export class DateModule {
717
721
*
718
722
* @param options The optional options object.
719
723
* @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 ()`.
721
725
* @param legacyRefDate Deprecated, use `options.refDate` instead.
722
726
*
723
727
* @see faker.date.future()
@@ -742,7 +746,7 @@ export class DateModule {
742
746
/**
743
747
* The date to use as reference point for the newly generated date.
744
748
*
745
- * @default new Date ()
749
+ * @default faker.defaultRefDate ()
746
750
*/
747
751
refDate ?: string | Date | number ;
748
752
} ,
@@ -768,7 +772,7 @@ export class DateModule {
768
772
throw new FakerError ( 'Days must be greater than 0.' ) ;
769
773
}
770
774
771
- const date = toDate ( refDate ) ;
775
+ const date = toDate ( refDate , this . faker . defaultRefDate ) ;
772
776
const range = {
773
777
min : 1000 ,
774
778
max : days * 24 * 3600 * 1000 ,
@@ -938,7 +942,7 @@ export class DateModule {
938
942
/**
939
943
* The date to use as reference point for the newly generated date.
940
944
*
941
- * @default new Date ()
945
+ * @default faker.defaultRefDate ()
942
946
*/
943
947
refDate ?: string | Date | number ;
944
948
} = { }
0 commit comments