Skip to content

Commit

Permalink
fix: πŸ› Fix rounding errors
Browse files Browse the repository at this point in the history
  • Loading branch information
robvanderleek committed Jan 14, 2025
1 parent bf06e1f commit 1fb900e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
19 changes: 14 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -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];
}
}
});

Expand Down Expand Up @@ -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"
};
}
});
Expand Down
16 changes: 13 additions & 3 deletions src/codelimit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

32 changes: 21 additions & 11 deletions tests/codelimit.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {makeStatusBadgeSvg} from "../src/codelimit";
import {makeStatusBadgeSvg, qualityProfilePercentage} from "../src/codelimit";

test('make 100% badge', () => {
const codebase = {
Expand All @@ -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%</text>');
})
});

test('make 80% badge', () => {
const codebase = {
Expand All @@ -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%</text>');
Expand All @@ -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%</text>');
Expand All @@ -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%</text>');
})
});

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]);
});

0 comments on commit 1fb900e

Please sign in to comment.