@code-pushup/utils 0.10.5 → 0.10.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1470,24 +1470,6 @@ function toUnixPath(path, options) {
1470
1470
  }
1471
1471
 
1472
1472
  // packages/utils/src/lib/reports/scoring.ts
1473
- function calculateScore(refs, scoreFn) {
1474
- const { numerator, denominator } = refs.reduce(
1475
- (acc, ref) => {
1476
- const score = scoreFn(ref);
1477
- return {
1478
- numerator: acc.numerator + score * ref.weight,
1479
- denominator: acc.denominator + ref.weight
1480
- };
1481
- },
1482
- { numerator: 0, denominator: 0 }
1483
- );
1484
- if (!numerator && !denominator) {
1485
- throw new Error(
1486
- "0 division for score. This can be caused by refs only weighted with 0 or empty refs"
1487
- );
1488
- }
1489
- return numerator / denominator;
1490
- }
1491
1473
  function scoreReport(report) {
1492
1474
  const allScoredAuditsAndGroups = /* @__PURE__ */ new Map();
1493
1475
  const scoredPlugins = report.plugins.map((plugin) => {
@@ -1537,6 +1519,36 @@ function scoreReport(report) {
1537
1519
  categories: scoredCategories
1538
1520
  };
1539
1521
  }
1522
+ function calculateScore(refs, scoreFn) {
1523
+ const validatedRefs = parseScoringParameters(refs, scoreFn);
1524
+ const { numerator, denominator } = validatedRefs.reduce(
1525
+ (acc, ref) => ({
1526
+ numerator: acc.numerator + ref.score * ref.weight,
1527
+ denominator: acc.denominator + ref.weight
1528
+ }),
1529
+ { numerator: 0, denominator: 0 }
1530
+ );
1531
+ return numerator / denominator;
1532
+ }
1533
+ function parseScoringParameters(refs, scoreFn) {
1534
+ if (refs.length === 0) {
1535
+ throw new Error("Reference array cannot be empty.");
1536
+ }
1537
+ if (refs.some((ref) => ref.weight < 0)) {
1538
+ throw new Error("Weight cannot be negative.");
1539
+ }
1540
+ if (refs.every((ref) => ref.weight === 0)) {
1541
+ throw new Error("All references cannot have zero weight.");
1542
+ }
1543
+ const scoredRefs = refs.map((ref) => ({
1544
+ weight: ref.weight,
1545
+ score: scoreFn(ref)
1546
+ }));
1547
+ if (scoredRefs.some((ref) => ref.score < 0 || ref.score > 1)) {
1548
+ throw new Error("All scores must be in range 0-1.");
1549
+ }
1550
+ return scoredRefs;
1551
+ }
1540
1552
 
1541
1553
  // packages/utils/src/lib/reports/sorting.ts
1542
1554
  function sortReport(report) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@code-pushup/utils",
3
- "version": "0.10.5",
3
+ "version": "0.10.7",
4
4
  "dependencies": {
5
5
  "@code-pushup/models": "*",
6
6
  "bundle-require": "^4.0.1",
@@ -22,7 +22,7 @@ export type ScoredReport = Omit<Report, 'plugins' | 'categories'> & {
22
22
  })[];
23
23
  categories: ScoredCategoryConfig[];
24
24
  };
25
+ export declare function scoreReport(report: Report): ScoredReport;
25
26
  export declare function calculateScore<T extends {
26
27
  weight: number;
27
28
  }>(refs: T[], scoreFn: (ref: T) => number): number;
28
- export declare function scoreReport(report: Report): ScoredReport;