@code-pushup/utils 0.8.9 → 0.8.10

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
@@ -856,13 +856,13 @@ function getGroupWithAudits(refSlug, refPlugin, plugins) {
856
856
  },
857
857
  []
858
858
  );
859
- const audits = groupAudits.sort(sortCategoryAudits);
859
+ const audits = groupAudits.sort(compareCategoryAudits);
860
860
  return {
861
861
  ...groupWithAudits,
862
862
  audits
863
863
  };
864
864
  }
865
- function sortCategoryAudits(a, b) {
865
+ function compareCategoryAudits(a, b) {
866
866
  if (a.weight !== b.weight) {
867
867
  return b.weight - a.weight;
868
868
  }
@@ -874,7 +874,7 @@ function sortCategoryAudits(a, b) {
874
874
  }
875
875
  return a.title.localeCompare(b.title);
876
876
  }
877
- function sortAudits(a, b) {
877
+ function compareAudits(a, b) {
878
878
  if (a.score !== b.score) {
879
879
  return a.score - b.score;
880
880
  }
@@ -1168,23 +1168,18 @@ function reportToCategoriesSection(report) {
1168
1168
  category.score
1169
1169
  )} Score: ${style(formatReportScore(category.score))}`;
1170
1170
  const categoryDocs = getDocsAndDescription(category);
1171
- const auditsAndGroups = category.refs.reduce(
1172
- (acc2, ref) => ({
1173
- ...acc2,
1174
- ...ref.type === "group" ? {
1175
- groups: [
1176
- ...acc2.groups,
1177
- getGroupWithAudits(ref.slug, ref.plugin, plugins)
1178
- ]
1179
- } : {
1180
- audits: [...acc2.audits, getAuditByRef(ref, plugins)]
1181
- }
1182
- }),
1183
- { groups: [], audits: [] }
1184
- );
1185
- const audits = auditsAndGroups.audits.sort(sortCategoryAudits).map((audit) => auditItemToCategorySection(audit, plugins)).join(NEW_LINE);
1186
- const groups = auditsAndGroups.groups.map((group) => groupItemToCategorySection(group, plugins)).join("");
1187
- return acc + NEW_LINE + categoryTitle + NEW_LINE + NEW_LINE + categoryDocs + categoryScore + NEW_LINE + groups + NEW_LINE + audits;
1171
+ const categoryMDItems = category.refs.reduce((acc2, ref) => {
1172
+ if (ref.type === "group") {
1173
+ const group = getGroupWithAudits(ref.slug, ref.plugin, plugins);
1174
+ const mdGroupItem = groupItemToCategorySection(group, plugins);
1175
+ return acc2 + mdGroupItem + NEW_LINE;
1176
+ } else {
1177
+ const audit = getAuditByRef(ref, plugins);
1178
+ const mdAuditItem = auditItemToCategorySection(audit, plugins);
1179
+ return acc2 + mdAuditItem + NEW_LINE;
1180
+ }
1181
+ }, "");
1182
+ return acc + NEW_LINE + categoryTitle + NEW_LINE + NEW_LINE + categoryDocs + categoryScore + NEW_LINE + categoryMDItems;
1188
1183
  }, "");
1189
1184
  return h2("\u{1F3F7} Categories") + NEW_LINE + categoryDetails;
1190
1185
  }
@@ -1223,7 +1218,7 @@ function groupItemToCategorySection(group, plugins) {
1223
1218
  }
1224
1219
  function reportToAuditsSection(report) {
1225
1220
  const auditsSection = report.plugins.reduce((acc, plugin) => {
1226
- const auditsData = plugin.audits.sort(sortAudits).reduce((acc2, audit) => {
1221
+ const auditsData = plugin.audits.reduce((acc2, audit) => {
1227
1222
  const auditTitle = `${audit.title} (${getPluginNameFromSlug(
1228
1223
  audit.plugin,
1229
1224
  report.plugins
@@ -1246,7 +1241,7 @@ function reportToAuditsSection(report) {
1246
1241
  }
1247
1242
  const detailsTableData = [
1248
1243
  detailsTableHeaders,
1249
- ...audit.details.issues.sort(compareIssues).map((issue) => {
1244
+ ...audit.details.issues.map((issue) => {
1250
1245
  const severity = `${getSeverityIcon(issue.severity)} <i>${issue.severity}</i>`;
1251
1246
  const message = issue.message;
1252
1247
  if (!issue.source) {
@@ -1350,7 +1345,7 @@ function reportToDetailSection(report) {
1350
1345
  output += addLine(chalk3.magentaBright.bold(`${title} audits`));
1351
1346
  output += addLine();
1352
1347
  const ui = cliui({ width: 80 });
1353
- audits.sort(sortAudits).forEach(({ score, title: title2, displayValue, value }) => {
1348
+ audits.forEach(({ score, title: title2, displayValue, value }) => {
1354
1349
  ui.div(
1355
1350
  {
1356
1351
  text: withColor({ score, text: "\u25CF" }),
@@ -1573,6 +1568,56 @@ var verboseUtils = (verbose) => ({
1573
1568
  log: getLogVerbose(verbose),
1574
1569
  exec: getExecVerbose(verbose)
1575
1570
  });
1571
+
1572
+ // packages/utils/src/lib/sort-report.ts
1573
+ function sortReport(report) {
1574
+ const { categories, plugins } = report;
1575
+ const sortedCategories = categories.map((category) => {
1576
+ const { audits, groups } = category.refs.reduce(
1577
+ (acc, ref) => ({
1578
+ ...acc,
1579
+ ...ref.type === "group" ? {
1580
+ groups: [
1581
+ ...acc.groups,
1582
+ getGroupWithAudits(ref.slug, ref.plugin, plugins)
1583
+ ]
1584
+ } : {
1585
+ audits: [...acc.audits, getAuditByRef(ref, plugins)]
1586
+ }
1587
+ }),
1588
+ { groups: [], audits: [] }
1589
+ );
1590
+ const sortedAuditsAndGroups = [
1591
+ ...groups,
1592
+ ...audits.sort(compareCategoryAudits)
1593
+ ];
1594
+ const sortedRefs = category.refs.slice().sort((a, b) => {
1595
+ const aIndex = sortedAuditsAndGroups.findIndex(
1596
+ (ref) => ref.slug === a.slug
1597
+ );
1598
+ const bIndex = sortedAuditsAndGroups.findIndex(
1599
+ (ref) => ref.slug === b.slug
1600
+ );
1601
+ return aIndex - bIndex;
1602
+ });
1603
+ return { ...category, refs: sortedRefs };
1604
+ });
1605
+ const sortedPlugins = plugins.map((plugin) => ({
1606
+ ...plugin,
1607
+ audits: plugin.audits.sort(compareAudits).map((audit) => ({
1608
+ ...audit,
1609
+ details: {
1610
+ ...audit.details,
1611
+ issues: audit?.details?.issues.slice().sort(compareIssues) || []
1612
+ }
1613
+ }))
1614
+ }));
1615
+ return {
1616
+ ...report,
1617
+ categories: sortedCategories,
1618
+ plugins: sortedPlugins
1619
+ };
1620
+ }
1576
1621
  export {
1577
1622
  CODE_PUSHUP_DOMAIN,
1578
1623
  FOOTER_PREFIX,
@@ -1613,6 +1658,7 @@ export {
1613
1658
  reportToStdout,
1614
1659
  scoreReport,
1615
1660
  slugify,
1661
+ sortReport,
1616
1662
  toArray,
1617
1663
  toUnixPath,
1618
1664
  truncateDescription,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@code-pushup/utils",
3
- "version": "0.8.9",
3
+ "version": "0.8.10",
4
4
  "dependencies": {
5
5
  "@code-pushup/models": "*",
6
6
  "bundle-require": "^4.0.1",
package/src/index.d.ts CHANGED
@@ -13,3 +13,4 @@ export { reportToStdout } from './lib/report-to-stdout';
13
13
  export { ScoredReport, scoreReport } from './lib/scoring';
14
14
  export { CliArgsObject, countOccurrences, distinct, factorOf, objectToCliArgs, objectToEntries, objectToKeys, toArray, toUnixPath, } from './lib/transform';
15
15
  export { verboseUtils } from './lib/verbose-utils';
16
+ export { sortReport } from './lib/sort-report';
@@ -18,8 +18,8 @@ export declare function countWeightedRefs(refs: CategoryRef[]): number;
18
18
  export declare function countCategoryAudits(refs: CategoryRef[], plugins: ScoredReport['plugins']): number;
19
19
  export declare function getAuditByRef({ slug, weight, plugin }: CategoryRef, plugins: ScoredReport['plugins']): WeighedAuditReport;
20
20
  export declare function getGroupWithAudits(refSlug: string, refPlugin: string, plugins: ScoredReport['plugins']): EnrichedScoredGroupWithAudits;
21
- export declare function sortCategoryAudits(a: WeighedAuditReport, b: WeighedAuditReport): number;
22
- export declare function sortAudits(a: EnrichedAuditReport, b: EnrichedAuditReport): number;
21
+ export declare function compareCategoryAudits(a: WeighedAuditReport, b: WeighedAuditReport): number;
22
+ export declare function compareAudits(a: EnrichedAuditReport, b: EnrichedAuditReport): number;
23
23
  export declare function compareIssueSeverity(severity1: CliIssueSeverity, severity2: CliIssueSeverity): number;
24
24
  type LoadedReportFormat<T extends Format> = T extends 'json' ? Report : string;
25
25
  export declare function loadReport<T extends Format>(options: Required<Pick<PersistConfig, 'outputDir' | 'filename'>> & {
@@ -8,7 +8,7 @@ export type WeighedAuditReport = EnrichedAuditReport & {
8
8
  export type EnrichedScoredGroupWithAudits = EnrichedScoredGroup & {
9
9
  audits: AuditReport[];
10
10
  };
11
- type ScoredCategoryConfig = CategoryConfig & {
11
+ export type ScoredCategoryConfig = CategoryConfig & {
12
12
  score: number;
13
13
  };
14
14
  export type EnrichedScoredGroup = Group & {
@@ -26,4 +26,3 @@ export declare function calculateScore<T extends {
26
26
  weight: number;
27
27
  }>(refs: T[], scoreFn: (ref: T) => number): number;
28
28
  export declare function scoreReport(report: Report): ScoredReport;
29
- export {};
@@ -0,0 +1,2 @@
1
+ import { ScoredReport } from './scoring';
2
+ export declare function sortReport(report: ScoredReport): ScoredReport;