Skip to content

Commit

Permalink
refactor(model): rename alignment enums
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed May 13, 2024
1 parent cad869d commit 43507c6
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 48 deletions.
4 changes: 2 additions & 2 deletions packages/models/src/lib/table.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { z } from 'zod';
import { primitiveValueSchema } from './implementation/schemas';

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

Expand Down
6 changes: 3 additions & 3 deletions packages/models/src/lib/table.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('tableSchema', () => {
it('should parse table with rows and headings and alignments', () => {
const table: Table = {
rows: [{ metrics: 'TTFB' }],
headings: [{ key: 'metrics', label: 'Metrics Name', align: 'l' }],
headings: [{ key: 'metrics', label: 'Metrics Name', align: 'left' }],
};
expect(() => tableSchema().parse(table)).not.toThrow();
});
Expand All @@ -62,8 +62,8 @@ describe('tableSchema', () => {
headings: [
// center is often the default when rendering in MD or HTML
{ key: 'phase', label: 'Phase' },
{ key: 'percentageLcp', label: '% of LCP', align: 'r' },
{ key: 'timing', label: 'Timing', align: 'l' },
{ key: 'percentageLcp', label: '% of LCP', align: 'right' },
{ key: 'timing', label: 'Timing', align: 'left' },
],
rows: [
{
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/src/lib/reports/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ export const SCORE_COLOR_RANGE = {
/* eslint-enable no-magic-numbers */

export const CATEGORIES_TITLE = '🏷 Categories';
export const FOOTER_PREFIX = 'Made with ❤ by'; // replace ❤️ with ❤, because ❤️ has output issues
export const FOOTER_PREFIX = 'Made with ❤ by'; // replace ❤️ with ❤, because ❤️ has output issues in terminal
export const CODE_PUSHUP_DOMAIN = 'code-pushup.dev';
export const README_LINK = 'https://github.com/code-pushup/cli#readme';
export const reportHeadlineText = 'Code PushUp Report';
export const reportOverviewTableHeaders = [
{
key: 'category',
label: '🏷 Category',
align: 'l',
align: 'left',
},
{
key: 'score',
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/src/lib/reports/formatting.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ describe('tableSection', () => {
{
headings: [
{ key: 'phase', label: 'Phase' },
{ key: 'percentageLcp', label: '% of LCP', align: 'l' },
{ key: 'timing', label: 'Timing', align: 'r' },
{ key: 'percentageLcp', label: '% of LCP', align: 'left' },
{ key: 'timing', label: 'Timing', align: 'right' },
],
rows: [
{
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/src/lib/reports/generate-md-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export function reportPluginMeta({ plugins }: Pick<Report, 'plugins'>) {
headings: [
{
key: 'plugin',
align: 'l' as const,
align: 'left' as const,
},
{
key: 'audits',
Expand Down Expand Up @@ -184,7 +184,7 @@ export function reportMetaData({
headings: [
{
key: 'commit',
align: 'l',
align: 'left',
},
{
key: 'version',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,12 @@ describe('generateMdReport', () => {
table: {
headings: [
{ key: 'phase', label: 'Phase' },
{ key: 'percentageLcp', label: '% of LCP', align: 'l' },
{ key: 'timing', label: 'Timing', align: 'r' },
{
key: 'percentageLcp',
label: '% of LCP',
align: 'left',
},
{ key: 'timing', label: 'Timing', align: 'right' },
],
rows: [
{
Expand Down
10 changes: 5 additions & 5 deletions packages/utils/src/lib/reports/generate-md-reports-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function formatDiffCategoriesSection(diff: ReportsDiff): string {
}

const headings: TableHeading[] = [
{ key: 'category', label: '🏷️ Category', align: 'l' },
{ key: 'category', label: '🏷️ Category', align: 'left' },
{ key: 'after', label: hasChanges ? '⭐ Current score' : '⭐ Score' },
{ key: 'before', label: '⭐ Previous score' },
{ key: 'change', label: '🔄 Score change' },
Expand Down Expand Up @@ -116,8 +116,8 @@ function formatDiffGroupsSection(diff: ReportsDiff): string {
h2('🗃️ Groups'),
formatGroupsOrAuditsDetails('group', diff.groups, {
headings: [
{ key: 'plugin', label: '🔌 Plugin', align: 'l' },
{ key: 'group', label: '🗃️ Group', align: 'l' },
{ key: 'plugin', label: '🔌 Plugin', align: 'left' },
{ key: 'group', label: '🗃️ Group', align: 'left' },
{ key: 'after', label: '⭐ Current score' },
{ key: 'before', label: '⭐ Previous score' },
{ key: 'change', label: '🔄 Score change' },
Expand All @@ -138,8 +138,8 @@ function formatDiffAuditsSection(diff: ReportsDiff): string {
h2('🛡️ Audits'),
formatGroupsOrAuditsDetails('audit', diff.audits, {
headings: [
{ key: 'plugin', label: '🔌 Plugin', align: 'l' },
{ key: 'audit', label: '🛡️ Audit', align: 'l' },
{ key: 'plugin', label: '🔌 Plugin', align: 'left' },
{ key: 'audit', label: '🛡️ Audit', align: 'left' },
{ key: 'after', label: '📏 Current value' },
{ key: 'before', label: '📏 Previous value' },
{ key: 'change', label: '🔄 Value change' },
Expand Down
13 changes: 6 additions & 7 deletions packages/utils/src/lib/reports/md/table.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Table } from '@code-pushup/models';
import { Table, TableAlignment } from '@code-pushup/models';
import { getColumnAlignments, tableToStringArray } from '../../transform';
import { lines, section } from './section';

export type Alignment = 'l' | 'c' | 'r';
const alignString = new Map<Alignment, string>([
['l', ':--'],
['c', ':--:'],
['r', '--:'],
const alignString = new Map<TableAlignment, string>([
['left', ':--'],
['center', ':--:'],
['right', '--:'],
]);

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

const alignmentRow = getColumnAlignments(rows, headings).map(
s => alignString.get(s) ?? String(alignString.get('c')),
s => alignString.get(s) ?? String(alignString.get('center')),
);

return section(
Expand Down
8 changes: 4 additions & 4 deletions packages/utils/src/lib/reports/md/table.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('table function', () => {
it('should create a table with specified alignment', () => {
const data: Table = {
rows: [[1, 2, 3]],
headings: [{ align: 'l' }, {}, { align: 'r' }],
headings: [{ align: 'left' }, {}, { align: 'right' }],
};
const result = tableMd(data);
expect(result).toMatch('|:--|:--:|--:|');
Expand Down Expand Up @@ -45,9 +45,9 @@ describe('table function', () => {
it('should create a complete table', () => {
const data: Table = {
headings: [
{ key: 'date', label: 'Date of Action', align: 'r' },
{ key: 'time', label: 'Time of Action', align: 'l' },
{ key: 'action', label: 'Action', align: 'c' },
{ key: 'date', label: 'Date of Action', align: 'right' },
{ key: 'time', label: 'Time of Action', align: 'left' },
{ key: 'action', label: 'Action', align: 'center' },
],
rows: [{ date: '2025.01.01', time: '00:00:00', action: 'add item' }],
};
Expand Down
6 changes: 4 additions & 2 deletions packages/utils/src/lib/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,14 @@ export function getColumnAlignmentForKey(
targetKey: string,
headings: TableHeading[] = [],
): TableAlignment {
return headings.find(({ key }) => targetKey === key)?.align ?? ('c' as const);
return (
headings.find(({ key }) => targetKey === key)?.align ?? ('center' as const)
);
}

export function getColumnAlignmentForIndex(

Check failure on line 254 in packages/utils/src/lib/transform.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<↗> Code coverage | Function coverage

Function getColumnAlignmentForIndex is not called in any test case.
idx: number,
headings: TableHeading[] = [],
): TableAlignment {
return headings.at(idx)?.align ?? ('c' as const);
return headings.at(idx)?.align ?? ('center' as const);
}

Check warning on line 259 in packages/utils/src/lib/transform.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<↗> Code coverage | Line coverage

Lines 255-259 are not covered in any test case.
36 changes: 19 additions & 17 deletions packages/utils/src/lib/transform.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ describe('rowToStringArray', () => {

describe('getColumnAlignmentForKey', () => {
it('return center align for a key and no heading definitions', () => {
expect(getColumnAlignmentForKey('value')).toBe('c');
expect(getColumnAlignmentForKey('value')).toBe('center');
});

it('return center align for a key and heading definitions without align', () => {
Expand All @@ -397,51 +397,53 @@ describe('getColumnAlignmentForKey', () => {
{ key: 'value' },
{ key: 'other-prop' },
]),
).toBe('c');
).toBe('center');
});

it('return defined align for a key', () => {
expect(
getColumnAlignmentForKey('value', [
{ key: 'value', align: 'l' },
{ key: 'value', align: 'left' },
{ key: 'other-prop' },
]),
).toBe('l');
).toBe('left');
});
});

describe('getColumnAlignmentForIndex', () => {
it('return center align for a index and no heading definitions', () => {
expect(getColumnAlignmentForIndex(60)).toBe('c');
expect(getColumnAlignmentForIndex(60)).toBe('center');
});

it('return center align for a index and heading definitions without align', () => {
expect(
getColumnAlignmentForIndex(1, [{ key: 'other-prop' }, { key: 'value' }]),
).toBe('c');
).toBe('center');
});

it('return defined align for a index', () => {
expect(
getColumnAlignmentForIndex(1, [
{ key: 'other-prop' },
{ key: 'value', align: 'l' },
{ key: 'value', align: 'left' },
]),
).toBe('l');
).toBe('left');
});
});

describe('getColumnAlignments', () => {
it('return center align for primitive rows without heading definitions', () => {
expect(getColumnAlignments([[1, 2, 3]])).toStrictEqual(['c', 'c', 'c']);
expect(getColumnAlignments([[1, 2, 3]])).toStrictEqual([
'center',
'center',
'center',
]);
});

it('return accept align for primitive rows and heading definitions', () => {
expect(getColumnAlignments([[1, 2, 3]], [{ align: 'l' }])).toStrictEqual([
'l',
'c',
'c',
]);
expect(getColumnAlignments([[1, 2, 3]], [{ align: 'left' }])).toStrictEqual(
['left', 'center', 'center'],
);
});

it('return center align for object rows without heading definitions', () => {
Expand All @@ -451,7 +453,7 @@ describe('getColumnAlignments', () => {
{ value: 2, name: 'second' },
{ value: 3, name: 'third' },
]),
).toStrictEqual(['c', 'c']);
).toStrictEqual(['center', 'center']);
});

it('return accept align for object rows and heading definitions', () => {
Expand All @@ -462,9 +464,9 @@ describe('getColumnAlignments', () => {
{ value: 2, name: 'second' },
{ value: 3, name: 'third' },
],
[{ key: 'value', align: 'l' }],
[{ key: 'value', align: 'left' }],
),
).toStrictEqual(['l', 'c']);
).toStrictEqual(['left', 'center']);
});

it('throws for a undefined row', () => {
Expand Down

0 comments on commit 43507c6

Please sign in to comment.