Skip to content

Commit

Permalink
fix(utils): set fixed locale for date in report.md
Browse files Browse the repository at this point in the history
  • Loading branch information
matejchalk committed Jan 24, 2024
1 parent 7742031 commit aa4694b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
10 changes: 10 additions & 0 deletions packages/utils/src/lib/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ export function formatDuration(duration: number): string {
return `${(duration / 1000).toFixed(2)} s`;
}

export function formatDate(date: Date): string {
const locale = 'en-US'; // fixed locale to ensure consistency
return date
.toLocaleString(locale, {
dateStyle: 'medium',
timeStyle: 'short',
})
.replace(/\u202F/g, ' '); // see https://github.com/nodejs/node/issues/45171
}

export function truncateText(text: string, maxChars: number): string {
if (text.length <= maxChars) {
return text;
Expand Down
14 changes: 14 additions & 0 deletions packages/utils/src/lib/formatting.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest';
import {
formatBytes,
formatDate,
formatDuration,
pluralize,
pluralizeToken,
Expand Down Expand Up @@ -75,6 +76,19 @@ describe('formatDuration', () => {
});
});

describe('formatDate', () => {
it('should produce human-readable date and time in English', () => {
expect(formatDate(new Date('2024-01-23T09:50:09.606Z'))).toBe(
'Jan 23, 2024, 9:50 AM',
);
});

// see https://github.com/nodejs/node/issues/45171
it('should not include narrow non-breaking space', () => {
expect(formatDate(new Date())).not.toMatch(' ');
});
});

describe('truncateText', () => {
it('should replace overflowing text with ellipsis', () => {
expect(truncateText('All work and no play makes Jack a dull boy', 32)).toBe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ ESLint rule **prefer-arrow-callback**. [📖 Docs](https://eslint.org/docs/lates
## About
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).
Report was created by [Code PushUp](https://github.com/flowup/quality-metrics-cli#readme) on Sep 15, 2021, 12:00 AM.
|Commit|Version|Duration|Plugins|Categories|Audits|
|:--|:--:|:--:|:--:|:--:|:--:|
Expand Down
5 changes: 3 additions & 2 deletions packages/utils/src/lib/reports/generate-md-report.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AuditReport, CategoryConfig, Issue } from '@code-pushup/models';
import { formatDuration, slugify } from '../formatting';
import { formatDate, formatDuration, slugify } from '../formatting';
import { CommitData } from '../git';
import { NEW_LINE } from './constants';
import {
Expand Down Expand Up @@ -229,7 +229,8 @@ function reportToAboutSection(
report: ScoredReport,
commitData: CommitData | null,
): string {
const date = new Date().toString();
const date = formatDate(new Date());

const { duration, version, plugins, categories } = report;
const commitInfo = commitData
? `${commitData.message} (${commitData.hash.slice(0, 7)})`
Expand Down

0 comments on commit aa4694b

Please sign in to comment.