diff --git a/dist/index.js b/dist/index.js index b60b3c0..04d3738 100644 --- a/dist/index.js +++ b/dist/index.js @@ -52330,6 +52330,7 @@ var require_codelimit = __commonJS({ exports2.getReportContent = getReportContent; exports2.makeNotFoundBadgeSvg = makeNotFoundBadgeSvg; exports2.makeStatusBadgeSvg = makeStatusBadgeSvg; + exports2.qualityProfilePercentage = qualityProfilePercentage; var node_fetch_1 = __importDefault2(require_lib3()); var path_1 = __importDefault2(require("path")); var fs_12 = __importDefault2(require("fs")); @@ -52399,11 +52400,19 @@ var require_codelimit = __commonJS({ if (profile[3] > 0) { return makeBadgeSvg("Needs refactoring", "red"); } else { - const profile2Percentage = Math.round(profile[2] / (profile[0] + profile[1] + profile[2]) * 100); - const color = profile2Percentage > 20 ? "orange" : "brightgreen"; - return makeBadgeSvg(`${100 - profile2Percentage}%`, color); + const percentages = qualityProfilePercentage(profile); + const color = percentages[2] > 20 ? "orange" : "brightgreen"; + return makeBadgeSvg(`${100 - percentages[2]}%`, color); } } + function qualityProfilePercentage(profile) { + const total = profile.reduce((num, val) => val + num, 0); + const unmaintainable = profile[3] > 0 ? Math.ceil(profile[3] / total * 100 - 1e-3) : 0; + const hardToMaintain = profile[2] > 0 ? Math.ceil(profile[2] / total * 100 - 1e-3) : 0; + const verbose = profile[1] > 0 ? Math.ceil(profile[1] / total * 100 - 1e-3) : 0; + const easy = 100 - unmaintainable - hardToMaintain - verbose; + return [easy, verbose, hardToMaintain, unmaintainable]; + } } }); @@ -52492,8 +52501,8 @@ var require_version = __commonJS({ Object.defineProperty(exports2, "__esModule", { value: true }); exports2.version = void 0; exports2.version = { - "revision": "ba7cbe7", - "year": "2025" + "revision": "61e09ec", + "year": "2024" }; } }); diff --git a/src/codelimit.ts b/src/codelimit.ts index 21d679f..9af57d1 100644 --- a/src/codelimit.ts +++ b/src/codelimit.ts @@ -72,8 +72,18 @@ export function makeStatusBadgeSvg(codebase: Codebase): string { if (profile[3] > 0) { return makeBadgeSvg('Needs refactoring', 'red'); } else { - const profile2Percentage = Math.round((profile[2] / (profile[0] + profile[1] + profile[2])) * 100); - const color = profile2Percentage > 20 ? 'orange' : 'brightgreen'; - return makeBadgeSvg(`${100 - profile2Percentage}%`, color); + const percentages = qualityProfilePercentage(profile); + const color = percentages[2] > 20 ? 'orange' : 'brightgreen'; + return makeBadgeSvg(`${100 - percentages[2]}%`, color); } } + +export function qualityProfilePercentage(profile: number[]): [number, number, number, number] { + const total = profile.reduce((num, val) => val + num, 0); + const unmaintainable = profile[3] > 0 ? Math.ceil((profile[3] / total) * 100 - 0.001) : 0; + const hardToMaintain = profile[2] > 0 ? Math.ceil((profile[2] / total) * 100 - 0.001) : 0; + const verbose = profile[1] > 0 ? Math.ceil((profile[1] / total) * 100 - 0.001) : 0; + const easy = 100 - unmaintainable - hardToMaintain - verbose + return [easy, verbose, hardToMaintain, unmaintainable]; +} + diff --git a/tests/codelimit.test.ts b/tests/codelimit.test.ts index 0798996..1f2815a 100644 --- a/tests/codelimit.test.ts +++ b/tests/codelimit.test.ts @@ -1,4 +1,4 @@ -import {makeStatusBadgeSvg} from "../src/codelimit"; +import {makeStatusBadgeSvg, qualityProfilePercentage} from "../src/codelimit"; test('make 100% badge', () => { const codebase = { @@ -8,13 +8,13 @@ test('make 100% badge', () => { profile: [1000, 0, 0, 0] } } - } + }; - const result = makeStatusBadgeSvg(codebase) + const result = makeStatusBadgeSvg(codebase); expect(result).toContain('#4c1'); expect(result).toContain('100%'); -}) +}); test('make 80% badge', () => { const codebase = { @@ -24,9 +24,9 @@ test('make 80% badge', () => { profile: [400, 400, 200, 0] } } - } + }; - const result = makeStatusBadgeSvg(codebase) + const result = makeStatusBadgeSvg(codebase); expect(result).toContain('#4c1'); expect(result).toContain('80%'); @@ -40,9 +40,9 @@ test('make 60% badge', () => { profile: [300, 300, 400, 0] } } - } + }; - const result = makeStatusBadgeSvg(codebase) + const result = makeStatusBadgeSvg(codebase); expect(result).toContain('#fe7d37'); expect(result).toContain('60%'); @@ -56,10 +56,20 @@ test('make 93% badge', () => { profile: [630, 300, 70, 0] } } - } + }; - const result = makeStatusBadgeSvg(codebase) + const result = makeStatusBadgeSvg(codebase); expect(result).toContain('#4c1'); expect(result).toContain('93%'); -}) \ No newline at end of file +}); + +test('quality profile percentages', () => { + let result = qualityProfilePercentage([2530, 2883, 1395, 0]); + + expect(result).toEqual([36, 43, 21, 0]); + + result = qualityProfilePercentage([630, 300, 70, 0]); + + expect(result).toEqual([63, 30, 7, 0]); +}); \ No newline at end of file