@code-pushup/utils 0.4.5 → 0.5.2

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
@@ -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(
@@ -183,6 +188,23 @@ function getDuplicateRefsInCategoryMetrics(metrics) {
183
188
  metrics.map(({ slug, type, plugin }) => `${type} :: ${plugin} / ${slug}`)
184
189
  );
185
190
  }
191
+ var categoriesSchema = z2.array(categoryConfigSchema, {
192
+ description: "Categorization of individual audits"
193
+ }).min(1).refine(
194
+ (categoryCfg) => !getDuplicateSlugCategories(categoryCfg),
195
+ (categoryCfg) => ({
196
+ message: duplicateSlugCategoriesErrorMsg(categoryCfg)
197
+ })
198
+ );
199
+ function duplicateSlugCategoriesErrorMsg(categories) {
200
+ const duplicateStringSlugs = getDuplicateSlugCategories(categories);
201
+ return `In the categories, the following slugs are duplicated: ${errorItems(
202
+ duplicateStringSlugs
203
+ )}`;
204
+ }
205
+ function getDuplicateSlugCategories(categories) {
206
+ return hasDuplicateStrings(categories.map(({ slug }) => slug));
207
+ }
186
208
 
187
209
  // packages/models/src/lib/core-config.ts
188
210
  import { z as z11 } from "zod";
@@ -191,10 +213,10 @@ import { z as z11 } from "zod";
191
213
  import { z as z3 } from "zod";
192
214
  var formatSchema = z3.enum(["json", "md"]);
193
215
  var persistConfigSchema = z3.object({
194
- outputDir: filePathSchema("Artifacts folder"),
195
- filename: fileNameSchema("Artifacts file name (without extension)").default(
196
- "report"
197
- ),
216
+ outputDir: filePathSchema("Artifacts folder").optional(),
217
+ filename: fileNameSchema(
218
+ "Artifacts file name (without extension)"
219
+ ).optional(),
198
220
  format: z3.array(formatSchema).default(["json"]).optional()
199
221
  // @TODO remove default or optional value and otherwise it will not set defaults.
200
222
  });
@@ -430,17 +452,10 @@ var unrefinedCoreConfigSchema = z11.object({
430
452
  description: "List of plugins to be used (official, community-provided, or custom)"
431
453
  }),
432
454
  /** portal configuration for persisting results */
433
- persist: persistConfigSchema,
455
+ persist: persistConfigSchema.optional(),
434
456
  /** portal configuration for uploading results */
435
457
  upload: uploadConfigSchema.optional(),
436
- categories: z11.array(categoryConfigSchema, {
437
- description: "Categorization of individual audits"
438
- }).refine(
439
- (categoryCfg) => !getDuplicateSlugCategories(categoryCfg),
440
- (categoryCfg) => ({
441
- message: duplicateSlugCategoriesErrorMsg(categoryCfg)
442
- })
443
- )
458
+ categories: categoriesSchema
444
459
  });
445
460
  var coreConfigSchema = refineCoreConfig(unrefinedCoreConfigSchema);
446
461
  function refineCoreConfig(schema) {
@@ -491,15 +506,6 @@ function getMissingRefsForCategories(coreCfg) {
491
506
  }
492
507
  return missingRefs.length ? missingRefs : false;
493
508
  }
494
- function duplicateSlugCategoriesErrorMsg(categories) {
495
- const duplicateStringSlugs = getDuplicateSlugCategories(categories);
496
- return `In the categories, the following slugs are duplicated: ${errorItems(
497
- duplicateStringSlugs
498
- )}`;
499
- }
500
- function getDuplicateSlugCategories(categories) {
501
- return hasDuplicateStrings(categories.map(({ slug }) => slug));
502
- }
503
509
 
504
510
  // packages/models/src/lib/report.ts
505
511
  import { z as z12 } from "zod";
@@ -1474,6 +1480,11 @@ function calculateScore(refs, scoreFn) {
1474
1480
  },
1475
1481
  { numerator: 0, denominator: 0 }
1476
1482
  );
1483
+ if (!numerator && !denominator) {
1484
+ throw new Error(
1485
+ "0 division for score. This can be caused by refs only weighted with 0 or empty refs"
1486
+ );
1487
+ }
1477
1488
  return numerator / denominator;
1478
1489
  }
1479
1490
  function scoreReport(report) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@code-pushup/utils",
3
- "version": "0.4.5",
3
+ "version": "0.5.2",
4
4
  "dependencies": {
5
5
  "@code-pushup/models": "*",
6
6
  "bundle-require": "^4.0.1",
@@ -12,7 +12,7 @@ export declare function logMultipleFileResults(fileResults: MultipleFileResults,
12
12
  export declare class NoExportError extends Error {
13
13
  constructor(filepath: string);
14
14
  }
15
- export declare function importEsmModule<T = unknown>(options: Options, parse?: (d: unknown) => T): Promise<T>;
15
+ export declare function importEsmModule<T = unknown>(options: Options, parse?: (d: any) => T): Promise<T>;
16
16
  export declare function pluginWorkDir(slug: string): string;
17
17
  export type CrawlFileSystemOptions<T> = {
18
18
  directory: string;