11import type { Faker } from '../..' ;
2+ import { FakerError } from '../../errors/faker-error' ;
23import { deprecated } from '../../internal/deprecated' ;
34
4- const GIT_DATE_FORMAT_BASE = new Intl . DateTimeFormat ( 'en' , {
5- weekday : 'short' ,
6- month : 'short' ,
7- day : 'numeric' ,
8- hour : '2-digit' ,
9- hourCycle : 'h24' ,
10- minute : '2-digit' ,
11- second : '2-digit' ,
12- year : 'numeric' ,
13- timeZone : 'UTC' ,
14- } ) ;
15- const GIT_TIMEZONE_FORMAT = new Intl . NumberFormat ( 'en' , {
16- minimumIntegerDigits : 4 ,
17- maximumFractionDigits : 0 ,
18- useGrouping : false ,
19- signDisplay : 'always' ,
20- } ) ;
5+ const GIT_DATE_FORMAT_BASE = Intl ?. DateTimeFormat
6+ ? new Intl . DateTimeFormat ( 'en' , {
7+ weekday : 'short' ,
8+ month : 'short' ,
9+ day : 'numeric' ,
10+ hour : '2-digit' ,
11+ hourCycle : 'h24' ,
12+ minute : '2-digit' ,
13+ second : '2-digit' ,
14+ year : 'numeric' ,
15+ timeZone : 'UTC' ,
16+ } )
17+ : null ;
18+
19+ const GIT_TIMEZONE_FORMAT = Intl ?. NumberFormat
20+ ? new Intl . NumberFormat ( 'en' , {
21+ minimumIntegerDigits : 4 ,
22+ maximumFractionDigits : 0 ,
23+ useGrouping : false ,
24+ signDisplay : 'always' ,
25+ } )
26+ : null ;
2127
2228/**
2329 * Module to generate git related entries.
@@ -64,6 +70,8 @@ export class GitModule {
6470 * 'CRLF' = '\r\n'
6571 * @param options.refDate The date to use as reference point for the commit. Defaults to `new Date()`.
6672 *
73+ * @throws When the environment does not support `Intl.NumberFormat` and `Intl.DateTimeFormat`.
74+ *
6775 * @example
6876 * faker.git.commitEntry()
6977 * // commit fe8c38a965d13d9794eb36918cb24cebe49a45c2
@@ -158,6 +166,8 @@ export class GitModule {
158166 * @param options The optional options object.
159167 * @param options.refDate The date to use as reference point for the commit. Defaults to `faker.defaultRefDate()`.
160168 *
169+ * @throws When the environment does not support `Intl.NumberFormat` and `Intl.DateTimeFormat`.
170+ *
161171 * @example
162172 * faker.git.commitDate() // 'Mon Nov 7 14:40:58 2022 +0600'
163173 * faker.git.commitDate({ refDate: '2020-01-01' }) // 'Tue Dec 31 05:40:59 2019 -0400'
@@ -175,6 +185,12 @@ export class GitModule {
175185 } = { }
176186 ) : string {
177187 const { refDate = this . faker . defaultRefDate ( ) } = options ;
188+ // We check if Intl support is missing rather than if GIT_DATE_FORMAT_BASE/GIT_TIMEZONE_FORMAT is null. This allows us to test the error case in environments that do have Intl support by temporarily removing Intl at runtime.
189+ if ( ! Intl || ! Intl . DateTimeFormat || ! Intl . NumberFormat ) {
190+ throw new FakerError (
191+ 'This method requires an environment which supports Intl.NumberFormat and Intl.DateTimeFormat'
192+ ) ;
193+ }
178194
179195 const dateParts = GIT_DATE_FORMAT_BASE . format (
180196 this . faker . date . recent ( { days : 1 , refDate } )
0 commit comments