Skip to content

Commit a5e73f8

Browse files
authored
fix(git): limit need for Intl to specific method (#2172)
1 parent 012ba2c commit a5e73f8

2 files changed

Lines changed: 66 additions & 18 deletions

File tree

src/modules/git/index.ts

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
import type { Faker } from '../..';
2+
import { FakerError } from '../../errors/faker-error';
23
import { 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 })

test/git.spec.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import validator from 'validator';
22
import { describe, expect, it } from 'vitest';
3-
import { faker } from '../src';
3+
import { faker, FakerError } from '../src';
44
import { seededTests } from './support/seededRuns';
55
import { times } from './support/times';
66

@@ -114,6 +114,22 @@ describe('git', () => {
114114

115115
expect(commitEntry).not.contains('\r\n');
116116
});
117+
118+
it('should throw if Intl is unavailable', () => {
119+
const backup = globalThis.Intl.DateTimeFormat;
120+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
121+
(globalThis as any).Intl.DateTimeFormat = undefined;
122+
123+
expect(() => {
124+
faker.git.commitEntry();
125+
}).toThrow(
126+
new FakerError(
127+
'This method requires an environment which supports Intl.NumberFormat and Intl.DateTimeFormat'
128+
)
129+
);
130+
131+
globalThis.Intl.DateTimeFormat = backup;
132+
});
117133
});
118134

119135
describe('commitMessage', () => {
@@ -138,6 +154,22 @@ describe('git', () => {
138154
const parts = commitDate.split(' ');
139155
expect(parts.length).toBe(6);
140156
});
157+
158+
it('should throw if Intl is unavailable', () => {
159+
const backup = globalThis.Intl.DateTimeFormat;
160+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
161+
(globalThis as any).Intl.DateTimeFormat = undefined;
162+
163+
expect(() => {
164+
faker.git.commitDate();
165+
}).toThrow(
166+
new FakerError(
167+
'This method requires an environment which supports Intl.NumberFormat and Intl.DateTimeFormat'
168+
)
169+
);
170+
171+
globalThis.Intl.DateTimeFormat = backup;
172+
});
141173
});
142174

143175
describe('commitSha', () => {

0 commit comments

Comments
 (0)