1
1
import type { Faker } from '../..' ;
2
- import { FakerError } from '../../errors/faker-error' ;
3
2
import { deprecated } from '../../internal/deprecated' ;
4
3
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 ;
27
-
28
4
/**
29
5
* Module to generate git related entries.
30
6
*
@@ -70,8 +46,6 @@ export class GitModule {
70
46
* 'CRLF' = '\r\n'
71
47
* @param options.refDate The date to use as reference point for the commit. Defaults to `new Date()`.
72
48
*
73
- * @throws When the environment does not support `Intl.NumberFormat` and `Intl.DateTimeFormat`.
74
- *
75
49
* @example
76
50
* faker.git.commitEntry()
77
51
* // commit fe8c38a965d13d9794eb36918cb24cebe49a45c2
@@ -166,8 +140,6 @@ export class GitModule {
166
140
* @param options The optional options object.
167
141
* @param options.refDate The date to use as reference point for the commit. Defaults to `faker.defaultRefDate()`.
168
142
*
169
- * @throws When the environment does not support `Intl.NumberFormat` and `Intl.DateTimeFormat`.
170
- *
171
143
* @example
172
144
* faker.git.commitDate() // 'Mon Nov 7 14:40:58 2022 +0600'
173
145
* faker.git.commitDate({ refDate: '2020-01-01' }) // 'Tue Dec 31 05:40:59 2019 -0400'
@@ -185,28 +157,35 @@ export class GitModule {
185
157
} = { }
186
158
) : string {
187
159
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
- }
194
-
195
- const dateParts = GIT_DATE_FORMAT_BASE . format (
196
- this . faker . date . recent ( { days : 1 , refDate } )
197
- )
198
- . replace ( / , / g, '' )
199
- . split ( ' ' ) ;
200
- [ dateParts [ 3 ] , dateParts [ 4 ] ] = [ dateParts [ 4 ] , dateParts [ 3 ] ] ;
201
-
202
- // Timezone offset
203
- dateParts . push (
204
- GIT_TIMEZONE_FORMAT . format (
205
- this . faker . number . int ( { min : - 11 , max : 12 } ) * 100
206
- )
207
- ) ;
208
-
209
- return dateParts . join ( ' ' ) ;
160
+ const days = [ 'Sun' , 'Mon' , 'Tue' , 'Wed' , 'Thu' , 'Fri' , 'Sat' ] ;
161
+ const months = [
162
+ 'Jan' ,
163
+ 'Feb' ,
164
+ 'Mar' ,
165
+ 'Apr' ,
166
+ 'May' ,
167
+ 'Jun' ,
168
+ 'Jul' ,
169
+ 'Aug' ,
170
+ 'Sep' ,
171
+ 'Oct' ,
172
+ 'Nov' ,
173
+ 'Dec' ,
174
+ ] ;
175
+
176
+ const date = this . faker . date . recent ( { days : 1 , refDate } ) ;
177
+ const day = days [ date . getUTCDay ( ) ] ;
178
+ const month = months [ date . getUTCMonth ( ) ] ;
179
+ const dayOfMonth = date . getUTCDate ( ) ;
180
+ const hours = date . getUTCHours ( ) . toString ( ) . padStart ( 2 , '0' ) ;
181
+ const minutes = date . getUTCMinutes ( ) . toString ( ) . padStart ( 2 , '0' ) ;
182
+ const seconds = date . getUTCSeconds ( ) . toString ( ) . padStart ( 2 , '0' ) ;
183
+ const year = date . getUTCFullYear ( ) ;
184
+ const timezone = this . faker . number . int ( { min : - 11 , max : 12 } ) ;
185
+ const timezoneHours = Math . abs ( timezone ) . toString ( ) . padStart ( 2 , '0' ) ;
186
+ const timezoneMinutes = '00' ;
187
+ const timezoneSign = timezone >= 0 ? '+' : '-' ;
188
+ return `${ day } ${ month } ${ dayOfMonth } ${ hours } :${ minutes } :${ seconds } ${ year } ${ timezoneSign } ${ timezoneHours } ${ timezoneMinutes } ` ;
210
189
}
211
190
212
191
/**
0 commit comments