Skip to content

Commit d0eb475

Browse files
committed
fix(utils): use shields.io for colorizing changed values
1 parent c52ecb5 commit d0eb475

File tree

9 files changed

+49
-47
lines changed

9 files changed

+49
-47
lines changed

packages/utils/src/lib/reports/__snapshots__/report-diff-added.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
|🏷️ Category|⭐ Current score|⭐ Previous score|🗠 Score change|
88
|:--|:--:|:--:|:--:|
9-
|Bug prevention|🟡 **63**|🟡 68|<span style="color: red">▼ **-5**</span>|
9+
|Bug prevention|🟡 **63**|🟡 68|![🠋 −5](https://img.shields.io/badge/%F0%9F%A0%8B%20%E2%88%925-red)|
1010
|Performance|🟢 **94**|n/a|n/a|
1111
|Code style|🟡 **54**|🟡 54||
1212

@@ -21,7 +21,7 @@ All of 1 group is unchanged.
2121

2222
|🔌 Plugin|🛡️ Audit|📏 Current value|📏 Previous value|🗠 Value change|
2323
|:--|:--|:--:|:--:|:--:|
24-
|ESLint|Disallow unused variables|🟥 **1 error**|🟩 passed|<span style="color: red">▲ **+∞%**</span>|
24+
|ESLint|Disallow unused variables|🟥 **1 error**|🟩 passed|![🠉 +∞%](https://img.shields.io/badge/%F0%9F%A0%89%20%2B%E2%88%9E%25-red)|
2525

2626
48 other audits are unchanged.
2727

packages/utils/src/lib/reports/__snapshots__/report-diff-improved.md

+17-17
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
|🏷️ Category|⭐ Current score|⭐ Previous score|🗠 Score change|
88
|:--|:--:|:--:|:--:|
9-
|Code style|🟢 **100**|🟡 54|<span style="color: green">▲ **+46**</span>|
10-
|Bug prevention|🟢 **95**|🟡 68|<span style="color: green">▲ **+27**</span>|
11-
|Performance|🟢 **94**|🟢 92|<span style="color: green">▲ **+2**</span>|
9+
|Code style|🟢 **100**|🟡 54|![🠉 +46](https://img.shields.io/badge/%F0%9F%A0%89%20%2B46-green)|
10+
|Bug prevention|🟢 **95**|🟡 68|![🠉 +27](https://img.shields.io/badge/%F0%9F%A0%89%20%2B27-green)|
11+
|Performance|🟢 **94**|🟢 92|![🠉 +2](https://img.shields.io/badge/%F0%9F%A0%89%20%2B2-green)|
1212

1313
## 🎗️ Groups
1414

@@ -17,8 +17,8 @@
1717

1818
|🔌 Plugin|🎗️ Group|⭐ Current score|⭐ Previous score|🗠 Score change|
1919
|:--|:--|:--:|:--:|:--:|
20-
|ESLint|Maximum lines limitation|🟢 **100**|🟡 50|<span style="color: green">▲ **+50**</span>|
21-
|Lighthouse|Performance|🟢 **94**|🟢 92|<span style="color: green">▲ **+2**</span>|
20+
|ESLint|Maximum lines limitation|🟢 **100**|🟡 50|![🠉 +50](https://img.shields.io/badge/%F0%9F%A0%89%20%2B50-green)|
21+
|Lighthouse|Performance|🟢 **94**|🟢 92|![🠉 +2](https://img.shields.io/badge/%F0%9F%A0%89%20%2B2-green)|
2222

2323
</details>
2424

@@ -30,18 +30,18 @@
3030

3131
|🔌 Plugin|🛡️ Audit|📏 Current value|📏 Previous value|🗠 Value change|
3232
|:--|:--|:--:|:--:|:--:|
33-
|ESLint|Disallow variable declarations from shadowing variables declared in the outer scope|🟩 **passed**|🟥 3 warnings|<span style="color: green">▼ **-100%**</span>|
34-
|ESLint|Require or disallow method and property shorthand syntax for object literals|🟩 **passed**|🟥 3 warnings|<span style="color: green">▼ **-100%**</span>|
35-
|ESLint|verifies the list of dependencies for Hooks like useEffect and similar|🟩 **passed**|🟥 2 warnings|<span style="color: green">▼ **-100%**</span>|
36-
|ESLint|Disallow unused variables|🟩 **passed**|🟥 1 warning|<span style="color: green">▼ **-100%**</span>|
37-
|ESLint|Require braces around arrow function bodies|🟩 **passed**|🟥 1 warning|<span style="color: green">▼ **-100%**</span>|
38-
|ESLint|Require the use of `===` and `!==`|🟩 **passed**|🟥 1 warning|<span style="color: green">▼ **-100%**</span>|
39-
|ESLint|Enforce a maximum number of lines of code in a function|🟩 **passed**|🟥 1 warning|<span style="color: green">▼ **-100%**</span>|
40-
|ESLint|Require `const` declarations for variables that are never reassigned after declared|🟩 **passed**|🟥 1 warning|<span style="color: green">▼ **-100%**</span>|
41-
|ESLint|Disallow missing `key` props in iterators/collection literals|🟩 **passed**|🟥 1 warning|<span style="color: green">▼ **-100%**</span>|
42-
|Lighthouse|Largest Contentful Paint|🟨 **1.4 s**|🟨 1.5 s|<span style="color: green">▼ **-8%**</span>|
43-
|Lighthouse|First Contentful Paint|🟨 **1.1 s**|🟨 1.2 s|<span style="color: green">▼ **-4%**</span>|
44-
|Lighthouse|Speed Index|🟩 **1.1 s**|🟩 1.2 s|<span style="color: green">▼ **-4%**</span>|
33+
|ESLint|Disallow variable declarations from shadowing variables declared in the outer scope|🟩 **passed**|🟥 3 warnings|![🠋 −100%](https://img.shields.io/badge/%F0%9F%A0%8B%20%E2%88%92100%25-green)|
34+
|ESLint|Require or disallow method and property shorthand syntax for object literals|🟩 **passed**|🟥 3 warnings|![🠋 −100%](https://img.shields.io/badge/%F0%9F%A0%8B%20%E2%88%92100%25-green)|
35+
|ESLint|verifies the list of dependencies for Hooks like useEffect and similar|🟩 **passed**|🟥 2 warnings|![🠋 −100%](https://img.shields.io/badge/%F0%9F%A0%8B%20%E2%88%92100%25-green)|
36+
|ESLint|Disallow unused variables|🟩 **passed**|🟥 1 warning|![🠋 −100%](https://img.shields.io/badge/%F0%9F%A0%8B%20%E2%88%92100%25-green)|
37+
|ESLint|Require braces around arrow function bodies|🟩 **passed**|🟥 1 warning|![🠋 −100%](https://img.shields.io/badge/%F0%9F%A0%8B%20%E2%88%92100%25-green)|
38+
|ESLint|Require the use of `===` and `!==`|🟩 **passed**|🟥 1 warning|![🠋 −100%](https://img.shields.io/badge/%F0%9F%A0%8B%20%E2%88%92100%25-green)|
39+
|ESLint|Enforce a maximum number of lines of code in a function|🟩 **passed**|🟥 1 warning|![🠋 −100%](https://img.shields.io/badge/%F0%9F%A0%8B%20%E2%88%92100%25-green)|
40+
|ESLint|Require `const` declarations for variables that are never reassigned after declared|🟩 **passed**|🟥 1 warning|![🠋 −100%](https://img.shields.io/badge/%F0%9F%A0%8B%20%E2%88%92100%25-green)|
41+
|ESLint|Disallow missing `key` props in iterators/collection literals|🟩 **passed**|🟥 1 warning|![🠋 −100%](https://img.shields.io/badge/%F0%9F%A0%8B%20%E2%88%92100%25-green)|
42+
|Lighthouse|Largest Contentful Paint|🟨 **1.4 s**|🟨 1.5 s|![🠋 −8%](https://img.shields.io/badge/%F0%9F%A0%8B%20%E2%88%928%25-green)|
43+
|Lighthouse|First Contentful Paint|🟨 **1.1 s**|🟨 1.2 s|![🠋 −4%](https://img.shields.io/badge/%F0%9F%A0%8B%20%E2%88%924%25-green)|
44+
|Lighthouse|Speed Index|🟩 **1.1 s**|🟩 1.2 s|![🠋 −4%](https://img.shields.io/badge/%F0%9F%A0%8B%20%E2%88%924%25-green)|
4545

4646
40 other audits are unchanged.
4747

packages/utils/src/lib/reports/__snapshots__/report-diff-minimal.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
|🔌 Plugin|🛡️ Audit|📏 Current value|📏 Previous value|🗠 Value change|
1111
|:--|:--|:--:|:--:|:--:|
12-
|NPM|Check for outdates NPM packages|🟨 **3 packages are out of date**|🟩 1 package is out of date|<span style="color: red">▲ **+200%**</span>|
12+
|NPM|Check for outdates NPM packages|🟨 **3 packages are out of date**|🟩 1 package is out of date|![🠉 +200%](https://img.shields.io/badge/%F0%9F%A0%89%20%2B200%25-red)|
1313

1414
</details>

packages/utils/src/lib/reports/__snapshots__/report-diff-mixed.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
|🏷️ Category|⭐ Current score|⭐ Previous score|🗠 Score change|
88
|:--|:--:|:--:|:--:|
9-
|Bug prevention|🟡 **63**|🟡 68|<span style="color: red">▼ **-5**</span>|
10-
|Performance|🟢 **94**|🟢 92|<span style="color: green">▲ **+2**</span>|
9+
|Bug prevention|🟡 **63**|🟡 68|![🠋 −5](https://img.shields.io/badge/%F0%9F%A0%8B%20%E2%88%925-red)|
10+
|Performance|🟢 **94**|🟢 92|![🠉 +2](https://img.shields.io/badge/%F0%9F%A0%89%20%2B2-green)|
1111
|Code style|🟡 **54**|🟡 54||
1212

1313
## 🎗️ Groups
@@ -17,7 +17,7 @@
1717

1818
|🔌 Plugin|🎗️ Group|⭐ Current score|⭐ Previous score|🗠 Score change|
1919
|:--|:--|:--:|:--:|:--:|
20-
|Lighthouse|Performance|🟢 **94**|🟢 92|<span style="color: green">▲ **+2**</span>|
20+
|Lighthouse|Performance|🟢 **94**|🟢 92|![🠉 +2](https://img.shields.io/badge/%F0%9F%A0%89%20%2B2-green)|
2121

2222
1 other group is unchanged.
2323

@@ -31,10 +31,10 @@
3131

3232
|🔌 Plugin|🛡️ Audit|📏 Current value|📏 Previous value|🗠 Value change|
3333
|:--|:--|:--:|:--:|:--:|
34-
|ESLint|Disallow unused variables|🟥 **1 error**|🟩 passed|<span style="color: red">▲ **+∞%**</span>|
35-
|Lighthouse|Largest Contentful Paint|🟨 **1.4 s**|🟨 1.5 s|<span style="color: green">▼ **-8%**</span>|
36-
|Lighthouse|First Contentful Paint|🟨 **1.1 s**|🟨 1.2 s|<span style="color: green">▼ **-4%**</span>|
37-
|Lighthouse|Speed Index|🟩 **1.1 s**|🟩 1.2 s|<span style="color: green">▼ **-4%**</span>|
34+
|ESLint|Disallow unused variables|🟥 **1 error**|🟩 passed|![🠉 +∞%](https://img.shields.io/badge/%F0%9F%A0%89%20%2B%E2%88%9E%25-red)|
35+
|Lighthouse|Largest Contentful Paint|🟨 **1.4 s**|🟨 1.5 s|![🠋 −8%](https://img.shields.io/badge/%F0%9F%A0%8B%20%E2%88%928%25-green)|
36+
|Lighthouse|First Contentful Paint|🟨 **1.1 s**|🟨 1.2 s|![🠋 −4%](https://img.shields.io/badge/%F0%9F%A0%8B%20%E2%88%924%25-green)|
37+
|Lighthouse|Speed Index|🟩 **1.1 s**|🟩 1.2 s|![🠋 −4%](https://img.shields.io/badge/%F0%9F%A0%8B%20%E2%88%924%25-green)|
3838

3939
48 other audits are unchanged.
4040

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ function formatGroupsOrAuditsDetails<T extends 'group' | 'audit'>(
182182

183183
function formatScoreChange(diff: number): string {
184184
const marker = getDiffMarker(diff);
185-
const text = style(formatDiffNumber(Math.round(diff * 100)));
185+
const text = formatDiffNumber(Math.round(diff * 100));
186186
return colorByScoreDiff(`${marker} ${text}`, diff);
187187
}
188188

@@ -197,7 +197,7 @@ function formatValueChange({
197197
? Number.POSITIVE_INFINITY
198198
: Number.NEGATIVE_INFINITY
199199
: Math.round((100 * values.diff) / values.before);
200-
const text = style(`${formatDiffNumber(percentage)}%`);
200+
const text = `${formatDiffNumber(percentage)}%`;
201201
return colorByScoreDiff(`${marker} ${text}`, scores.diff);
202202
}
203203

packages/utils/src/lib/reports/md/html-color.ts

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function image(src: string, alt: string) {
2+
return `![${alt}](${src})`;
3+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export * from './details';
22
export * from './font-style';
33
export * from './headline';
4-
export * from './html-color';
4+
export * from './image';
55
export * from './link';
66
export * from './list';
77
export * from './paragraphs';

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

+16-14
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
readTextFile,
1717
} from '../file-system';
1818
import { SCORE_COLOR_RANGE } from './constants';
19-
import { htmlColor, style } from './md';
19+
import { image, style } from './md';
2020
import { ScoredReport, SortableAuditReport, SortableGroup } from './types';
2121

2222
export function formatReportScore(score: number): string {
@@ -55,29 +55,31 @@ export function getSquaredScoreMarker(score: number): string {
5555

5656
export function getDiffMarker(diff: number): string {
5757
if (diff > 0) {
58-
return '';
58+
return '🠉';
5959
}
6060
if (diff < 0) {
61-
return '';
61+
return '🠋';
6262
}
6363
return '';
6464
}
6565

6666
export function colorByScoreDiff(text: string, diff: number): string {
67-
return htmlColor(text, diff > 0 ? 'green' : diff < 0 ? 'red' : 'gray');
67+
const color = diff > 0 ? 'green' : diff < 0 ? 'red' : 'gray';
68+
return shieldsBadge(text, color);
69+
}
70+
71+
export function shieldsBadge(text: string, color: string): string {
72+
return image(
73+
`https://img.shields.io/badge/${encodeURIComponent(text)}-${color}`,
74+
text,
75+
);
6876
}
6977

7078
export function formatDiffNumber(diff: number): string {
71-
if (diff === Number.POSITIVE_INFINITY) {
72-
return '+∞';
73-
}
74-
if (diff === Number.NEGATIVE_INFINITY) {
75-
return '-∞';
76-
}
77-
if (diff > 0) {
78-
return `+${diff}`;
79-
}
80-
return `${diff}`;
79+
const number =
80+
Math.abs(diff) === Number.POSITIVE_INFINITY ? '∞' : `${Math.abs(diff)}`;
81+
const sign = diff < 0 ? '−' : '+';
82+
return `${sign}${number}`;
8183
}
8284

8385
export function getSeverityIcon(

0 commit comments

Comments
 (0)