Skip to content

Commit aa4694b

Browse files
committed
fix(utils): set fixed locale for date in report.md
1 parent 7742031 commit aa4694b

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

packages/utils/src/lib/formatting.ts

+10
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ export function formatDuration(duration: number): string {
5252
return `${(duration / 1000).toFixed(2)} s`;
5353
}
5454

55+
export function formatDate(date: Date): string {
56+
const locale = 'en-US'; // fixed locale to ensure consistency
57+
return date
58+
.toLocaleString(locale, {
59+
dateStyle: 'medium',
60+
timeStyle: 'short',
61+
})
62+
.replace(/\u202F/g, ' '); // see https://github.com/nodejs/node/issues/45171
63+
}
64+
5565
export function truncateText(text: string, maxChars: number): string {
5666
if (text.length <= maxChars) {
5767
return text;

packages/utils/src/lib/formatting.unit.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it } from 'vitest';
22
import {
33
formatBytes,
4+
formatDate,
45
formatDuration,
56
pluralize,
67
pluralizeToken,
@@ -75,6 +76,19 @@ describe('formatDuration', () => {
7576
});
7677
});
7778

79+
describe('formatDate', () => {
80+
it('should produce human-readable date and time in English', () => {
81+
expect(formatDate(new Date('2024-01-23T09:50:09.606Z'))).toBe(
82+
'Jan 23, 2024, 9:50 AM',
83+
);
84+
});
85+
86+
// see https://github.com/nodejs/node/issues/45171
87+
it('should not include narrow non-breaking space', () => {
88+
expect(formatDate(new Date())).not.toMatch(' ');
89+
});
90+
});
91+
7892
describe('truncateText', () => {
7993
it('should replace overflowing text with ellipsis', () => {
8094
expect(truncateText('All work and no play makes Jack a dull boy', 32)).toBe(

packages/utils/src/lib/reports/__snapshots__/generate-md-report.integration.test.ts.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ ESLint rule **prefer-arrow-callback**. [📖 Docs](https://eslint.org/docs/lates
421421
422422
## About
423423
424-
Report was created by [Code PushUp](https://github.com/flowup/quality-metrics-cli#readme) on Wed Sep 15 2021 00:00:00 GMT+0000 (Coordinated Universal Time).
424+
Report was created by [Code PushUp](https://github.com/flowup/quality-metrics-cli#readme) on Sep 15, 2021, 12:00 AM.
425425
426426
|Commit|Version|Duration|Plugins|Categories|Audits|
427427
|:--|:--:|:--:|:--:|:--:|:--:|

packages/utils/src/lib/reports/generate-md-report.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AuditReport, CategoryConfig, Issue } from '@code-pushup/models';
2-
import { formatDuration, slugify } from '../formatting';
2+
import { formatDate, formatDuration, slugify } from '../formatting';
33
import { CommitData } from '../git';
44
import { NEW_LINE } from './constants';
55
import {
@@ -229,7 +229,8 @@ function reportToAboutSection(
229229
report: ScoredReport,
230230
commitData: CommitData | null,
231231
): string {
232-
const date = new Date().toString();
232+
const date = formatDate(new Date());
233+
233234
const { duration, version, plugins, categories } = report;
234235
const commitInfo = commitData
235236
? `${commitData.message} (${commitData.hash.slice(0, 7)})`

0 commit comments

Comments
 (0)