Skip to content

Commit 6f8fc83

Browse files
committed
refactor(git): don't use Intl
1 parent bbda1d7 commit 6f8fc83

File tree

2 files changed

+30
-83
lines changed

2 files changed

+30
-83
lines changed

src/modules/git/index.ts

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,6 @@
11
import type { Faker } from '../..';
2-
import { FakerError } from '../../errors/faker-error';
32
import { deprecated } from '../../internal/deprecated';
43

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-
284
/**
295
* Module to generate git related entries.
306
*
@@ -70,8 +46,6 @@ export class GitModule {
7046
* 'CRLF' = '\r\n'
7147
* @param options.refDate The date to use as reference point for the commit. Defaults to `new Date()`.
7248
*
73-
* @throws When the environment does not support `Intl.NumberFormat` and `Intl.DateTimeFormat`.
74-
*
7549
* @example
7650
* faker.git.commitEntry()
7751
* // commit fe8c38a965d13d9794eb36918cb24cebe49a45c2
@@ -166,8 +140,6 @@ export class GitModule {
166140
* @param options The optional options object.
167141
* @param options.refDate The date to use as reference point for the commit. Defaults to `faker.defaultRefDate()`.
168142
*
169-
* @throws When the environment does not support `Intl.NumberFormat` and `Intl.DateTimeFormat`.
170-
*
171143
* @example
172144
* faker.git.commitDate() // 'Mon Nov 7 14:40:58 2022 +0600'
173145
* faker.git.commitDate({ refDate: '2020-01-01' }) // 'Tue Dec 31 05:40:59 2019 -0400'
@@ -185,28 +157,35 @@ export class GitModule {
185157
} = {}
186158
): string {
187159
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}`;
210189
}
211190

212191
/**

test/git.spec.ts

Lines changed: 1 addition & 33 deletions
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, FakerError } from '../src';
3+
import { faker } from '../src';
44
import { seededTests } from './support/seededRuns';
55
import { times } from './support/times';
66

@@ -114,22 +114,6 @@ 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-
});
133117
});
134118

135119
describe('commitMessage', () => {
@@ -154,22 +138,6 @@ describe('git', () => {
154138
const parts = commitDate.split(' ');
155139
expect(parts.length).toBe(6);
156140
});
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-
});
173141
});
174142

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

0 commit comments

Comments
 (0)