Skip to content

Commit 91ca6cf

Browse files
committed
refactor(utils): unify colour range limit values
1 parent 7d4f5aa commit 91ca6cf

3 files changed

Lines changed: 20 additions & 12 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const SCORE_COLOR_RANGE = {
2+
GREEN_MIN: 0.9,
3+
YELLOW_MIN: 0.5,
4+
};

packages/utils/src/lib/report-to-stdout.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import cliui from '@isaacs/cliui';
22
import chalk from 'chalk';
33
import Table from 'cli-table3';
4+
import { SCORE_COLOR_RANGE } from './constants';
45
import { NEW_LINE } from './md';
56
import {
67
CODE_PUSHUP_DOMAIN,
@@ -101,14 +102,16 @@ function reportToDetailSection(report: ScoredReport): string {
101102
}
102103

103104
function withColor({ score, text }: { score: number; text?: string }) {
104-
let str = text ?? formatReportScore(score);
105+
const formattedScore = text ?? formatReportScore(score);
105106
const style = text ? chalk : chalk.bold;
106-
if (score < 0.5) {
107-
str = style.red(str);
108-
} else if (score < 0.9) {
109-
str = style.yellow(str);
110-
} else {
111-
str = style.green(str);
107+
108+
if (score >= SCORE_COLOR_RANGE.GREEN_MIN) {
109+
return style.green(formattedScore);
110+
}
111+
112+
if (score >= SCORE_COLOR_RANGE.YELLOW_MIN) {
113+
return style.yellow(formattedScore);
112114
}
113-
return str;
115+
116+
return style.red(formattedScore);
114117
}

packages/utils/src/lib/report.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Report,
1010
reportSchema,
1111
} from '@code-pushup/models';
12+
import { SCORE_COLOR_RANGE } from './constants';
1213
import {
1314
ensureDirectoryExists,
1415
readJsonFile,
@@ -62,20 +63,20 @@ export function formatReportScore(score: number): string {
6263
}
6364

6465
export function getRoundScoreMarker(score: number): string {
65-
if (score >= 0.9) {
66+
if (score >= SCORE_COLOR_RANGE.GREEN_MIN) {
6667
return '🟢';
6768
}
68-
if (score >= 0.5) {
69+
if (score >= SCORE_COLOR_RANGE.YELLOW_MIN) {
6970
return '🟡';
7071
}
7172
return '🔴';
7273
}
7374

7475
export function getSquaredScoreMarker(score: number): string {
75-
if (score >= 0.9) {
76+
if (score >= SCORE_COLOR_RANGE.GREEN_MIN) {
7677
return '🟩';
7778
}
78-
if (score >= 0.5) {
79+
if (score >= SCORE_COLOR_RANGE.YELLOW_MIN) {
7980
return '🟨';
8081
}
8182
return '🟥';

0 commit comments

Comments
 (0)