@code-pushup/utils 0.4.4 → 0.5.0

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.
Files changed (2) hide show
  1. package/index.js +16 -6
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -124,12 +124,14 @@ function scorableSchema(description, refSchema, duplicateCheckFn, duplicateMessa
124
124
  return z.object(
125
125
  {
126
126
  slug: slugSchema('Human-readable unique ID, e.g. "performance"'),
127
- refs: z.array(refSchema).refine(
127
+ refs: z.array(refSchema).min(1).refine(
128
128
  (refs) => !duplicateCheckFn(refs),
129
129
  (refs) => ({
130
130
  message: duplicateMessageFn(refs)
131
131
  })
132
- )
132
+ ).refine(hasWeightedRefsInCategories, () => ({
133
+ message: `In a category there has to be at least one ref with weight > 0`
134
+ }))
133
135
  },
134
136
  { description }
135
137
  );
@@ -138,6 +140,9 @@ var materialIconSchema = z.enum(
138
140
  MATERIAL_ICONS,
139
141
  { description: "Icon from VSCode Material Icons extension" }
140
142
  );
143
+ function hasWeightedRefsInCategories(categoryRefs) {
144
+ return categoryRefs.reduce((acc, { weight }) => weight + acc, 0) !== 0;
145
+ }
141
146
 
142
147
  // packages/models/src/lib/category-config.ts
143
148
  var categoryRefSchema = weightedRefSchema(
@@ -435,10 +440,10 @@ var unrefinedCoreConfigSchema = z11.object({
435
440
  upload: uploadConfigSchema.optional(),
436
441
  categories: z11.array(categoryConfigSchema, {
437
442
  description: "Categorization of individual audits"
438
- }).refine(
439
- (categoryCfg) => !getDuplicateSlugCategories(categoryCfg),
440
- (categoryCfg) => ({
441
- message: duplicateSlugCategoriesErrorMsg(categoryCfg)
443
+ }).min(1).refine(
444
+ (categories) => !getDuplicateSlugCategories(categories),
445
+ (categories) => ({
446
+ message: duplicateSlugCategoriesErrorMsg(categories)
442
447
  })
443
448
  )
444
449
  });
@@ -1474,6 +1479,11 @@ function calculateScore(refs, scoreFn) {
1474
1479
  },
1475
1480
  { numerator: 0, denominator: 0 }
1476
1481
  );
1482
+ if (!numerator && !denominator) {
1483
+ throw new Error(
1484
+ "0 division for score. This can be caused by refs only weighted with 0 or empty refs"
1485
+ );
1486
+ }
1477
1487
  return numerator / denominator;
1478
1488
  }
1479
1489
  function scoreReport(report) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@code-pushup/utils",
3
- "version": "0.4.4",
3
+ "version": "0.5.0",
4
4
  "dependencies": {
5
5
  "@code-pushup/models": "*",
6
6
  "bundle-require": "^4.0.1",