Skip to content

Commit 43507c6

Browse files
committed
refactor(model): rename alignment enums
1 parent cad869d commit 43507c6

File tree

11 files changed

+55
-48
lines changed

11 files changed

+55
-48
lines changed

packages/models/src/lib/table.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { z } from 'zod';
22
import { primitiveValueSchema } from './implementation/schemas';
33

4-
export const tableAlignmentSchema = z.enum(['l', 'c', 'r'], {
5-
description: 'Cell alignment (l:left, r:right, c:center)',
4+
export const tableAlignmentSchema = z.enum(['left', 'center', 'right'], {
5+
description: 'Cell alignment',
66
});
77
export type TableAlignment = z.infer<typeof tableAlignmentSchema>;
88

packages/models/src/lib/table.unit.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe('tableSchema', () => {
5252
it('should parse table with rows and headings and alignments', () => {
5353
const table: Table = {
5454
rows: [{ metrics: 'TTFB' }],
55-
headings: [{ key: 'metrics', label: 'Metrics Name', align: 'l' }],
55+
headings: [{ key: 'metrics', label: 'Metrics Name', align: 'left' }],
5656
};
5757
expect(() => tableSchema().parse(table)).not.toThrow();
5858
});
@@ -62,8 +62,8 @@ describe('tableSchema', () => {
6262
headings: [
6363
// center is often the default when rendering in MD or HTML
6464
{ key: 'phase', label: 'Phase' },
65-
{ key: 'percentageLcp', label: '% of LCP', align: 'r' },
66-
{ key: 'timing', label: 'Timing', align: 'l' },
65+
{ key: 'percentageLcp', label: '% of LCP', align: 'right' },
66+
{ key: 'timing', label: 'Timing', align: 'left' },
6767
],
6868
rows: [
6969
{

packages/utils/src/lib/reports/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ export const SCORE_COLOR_RANGE = {
1111
/* eslint-enable no-magic-numbers */
1212

1313
export const CATEGORIES_TITLE = '🏷 Categories';
14-
export const FOOTER_PREFIX = 'Made with ❤ by'; // replace ❤️ with ❤, because ❤️ has output issues
14+
export const FOOTER_PREFIX = 'Made with ❤ by'; // replace ❤️ with ❤, because ❤️ has output issues in terminal
1515
export const CODE_PUSHUP_DOMAIN = 'code-pushup.dev';
1616
export const README_LINK = 'https://github.com/code-pushup/cli#readme';
1717
export const reportHeadlineText = 'Code PushUp Report';
1818
export const reportOverviewTableHeaders = [
1919
{
2020
key: 'category',
2121
label: '🏷 Category',
22-
align: 'l',
22+
align: 'left',
2323
},
2424
{
2525
key: 'score',

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ describe('tableSection', () => {
99
{
1010
headings: [
1111
{ key: 'phase', label: 'Phase' },
12-
{ key: 'percentageLcp', label: '% of LCP', align: 'l' },
13-
{ key: 'timing', label: 'Timing', align: 'r' },
12+
{ key: 'percentageLcp', label: '% of LCP', align: 'left' },
13+
{ key: 'timing', label: 'Timing', align: 'right' },
1414
],
1515
rows: [
1616
{

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export function reportPluginMeta({ plugins }: Pick<Report, 'plugins'>) {
138138
headings: [
139139
{
140140
key: 'plugin',
141-
align: 'l' as const,
141+
align: 'left' as const,
142142
},
143143
{
144144
key: 'audits',
@@ -184,7 +184,7 @@ export function reportMetaData({
184184
headings: [
185185
{
186186
key: 'commit',
187-
align: 'l',
187+
align: 'left',
188188
},
189189
{
190190
key: 'version',

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,12 @@ describe('generateMdReport', () => {
562562
table: {
563563
headings: [
564564
{ key: 'phase', label: 'Phase' },
565-
{ key: 'percentageLcp', label: '% of LCP', align: 'l' },
566-
{ key: 'timing', label: 'Timing', align: 'r' },
565+
{
566+
key: 'percentageLcp',
567+
label: '% of LCP',
568+
align: 'left',
569+
},
570+
{ key: 'timing', label: 'Timing', align: 'right' },
567571
],
568572
rows: [
569573
{

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function formatDiffCategoriesSection(diff: ReportsDiff): string {
6969
}
7070

7171
const headings: TableHeading[] = [
72-
{ key: 'category', label: '🏷️ Category', align: 'l' },
72+
{ key: 'category', label: '🏷️ Category', align: 'left' },
7373
{ key: 'after', label: hasChanges ? '⭐ Current score' : '⭐ Score' },
7474
{ key: 'before', label: '⭐ Previous score' },
7575
{ key: 'change', label: '🔄 Score change' },
@@ -116,8 +116,8 @@ function formatDiffGroupsSection(diff: ReportsDiff): string {
116116
h2('🗃️ Groups'),
117117
formatGroupsOrAuditsDetails('group', diff.groups, {
118118
headings: [
119-
{ key: 'plugin', label: '🔌 Plugin', align: 'l' },
120-
{ key: 'group', label: '🗃️ Group', align: 'l' },
119+
{ key: 'plugin', label: '🔌 Plugin', align: 'left' },
120+
{ key: 'group', label: '🗃️ Group', align: 'left' },
121121
{ key: 'after', label: '⭐ Current score' },
122122
{ key: 'before', label: '⭐ Previous score' },
123123
{ key: 'change', label: '🔄 Score change' },
@@ -138,8 +138,8 @@ function formatDiffAuditsSection(diff: ReportsDiff): string {
138138
h2('🛡️ Audits'),
139139
formatGroupsOrAuditsDetails('audit', diff.audits, {
140140
headings: [
141-
{ key: 'plugin', label: '🔌 Plugin', align: 'l' },
142-
{ key: 'audit', label: '🛡️ Audit', align: 'l' },
141+
{ key: 'plugin', label: '🔌 Plugin', align: 'left' },
142+
{ key: 'audit', label: '🛡️ Audit', align: 'left' },
143143
{ key: 'after', label: '📏 Current value' },
144144
{ key: 'before', label: '📏 Previous value' },
145145
{ key: 'change', label: '🔄 Value change' },

packages/utils/src/lib/reports/md/table.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { Table } from '@code-pushup/models';
1+
import { Table, TableAlignment } from '@code-pushup/models';
22
import { getColumnAlignments, tableToStringArray } from '../../transform';
33
import { lines, section } from './section';
44

5-
export type Alignment = 'l' | 'c' | 'r';
6-
const alignString = new Map<Alignment, string>([
7-
['l', ':--'],
8-
['c', ':--:'],
9-
['r', '--:'],
5+
const alignString = new Map<TableAlignment, string>([
6+
['left', ':--'],
7+
['center', ':--:'],
8+
['right', '--:'],
109
]);
1110

1211
function tableRow(rows: (string | number)[]): string {
@@ -29,7 +28,7 @@ export function tableMd<T extends Table>(data: T): string {
2928
const stringArr = tableToStringArray(data);
3029

3130
const alignmentRow = getColumnAlignments(rows, headings).map(
32-
s => alignString.get(s) ?? String(alignString.get('c')),
31+
s => alignString.get(s) ?? String(alignString.get('center')),
3332
);
3433

3534
return section(

packages/utils/src/lib/reports/md/table.unit.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('table function', () => {
1111
it('should create a table with specified alignment', () => {
1212
const data: Table = {
1313
rows: [[1, 2, 3]],
14-
headings: [{ align: 'l' }, {}, { align: 'r' }],
14+
headings: [{ align: 'left' }, {}, { align: 'right' }],
1515
};
1616
const result = tableMd(data);
1717
expect(result).toMatch('|:--|:--:|--:|');
@@ -45,9 +45,9 @@ describe('table function', () => {
4545
it('should create a complete table', () => {
4646
const data: Table = {
4747
headings: [
48-
{ key: 'date', label: 'Date of Action', align: 'r' },
49-
{ key: 'time', label: 'Time of Action', align: 'l' },
50-
{ key: 'action', label: 'Action', align: 'c' },
48+
{ key: 'date', label: 'Date of Action', align: 'right' },
49+
{ key: 'time', label: 'Time of Action', align: 'left' },
50+
{ key: 'action', label: 'Action', align: 'center' },
5151
],
5252
rows: [{ date: '2025.01.01', time: '00:00:00', action: 'add item' }],
5353
};

packages/utils/src/lib/transform.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,14 @@ export function getColumnAlignmentForKey(
246246
targetKey: string,
247247
headings: TableHeading[] = [],
248248
): TableAlignment {
249-
return headings.find(({ key }) => targetKey === key)?.align ?? ('c' as const);
249+
return (
250+
headings.find(({ key }) => targetKey === key)?.align ?? ('center' as const)
251+
);
250252
}
251253

252254
export function getColumnAlignmentForIndex(
253255
idx: number,
254256
headings: TableHeading[] = [],
255257
): TableAlignment {
256-
return headings.at(idx)?.align ?? ('c' as const);
258+
return headings.at(idx)?.align ?? ('center' as const);
257259
}

0 commit comments

Comments
 (0)