|
| 1 | +import { ExternalApiUsage } from "../external-api-usage"; |
| 2 | +import { Mode } from "./mode"; |
| 3 | +import { calculateModeledPercentage } from "./modeled-percentage"; |
| 4 | + |
| 5 | +export function groupMethods( |
| 6 | + externalApiUsages: ExternalApiUsage[], |
| 7 | + mode: Mode, |
| 8 | +): Record<string, ExternalApiUsage[]> { |
| 9 | + const groupedByLibrary: Record<string, ExternalApiUsage[]> = {}; |
| 10 | + |
| 11 | + for (const externalApiUsage of externalApiUsages) { |
| 12 | + // Group by package if using framework mode |
| 13 | + const key = |
| 14 | + mode === Mode.Framework |
| 15 | + ? externalApiUsage.packageName |
| 16 | + : externalApiUsage.library; |
| 17 | + |
| 18 | + groupedByLibrary[key] ??= []; |
| 19 | + groupedByLibrary[key].push(externalApiUsage); |
| 20 | + } |
| 21 | + |
| 22 | + return groupedByLibrary; |
| 23 | +} |
| 24 | + |
| 25 | +export function sortGroupNames( |
| 26 | + methods: Record<string, ExternalApiUsage[]>, |
| 27 | +): string[] { |
| 28 | + return Object.keys(methods).sort((a, b) => |
| 29 | + compareGroups(methods[a], a, methods[b], b), |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +export function sortMethods( |
| 34 | + externalApiUsages: ExternalApiUsage[], |
| 35 | +): ExternalApiUsage[] { |
| 36 | + const sortedExternalApiUsages = [...externalApiUsages]; |
| 37 | + sortedExternalApiUsages.sort((a, b) => compareMethod(a, b)); |
| 38 | + return sortedExternalApiUsages; |
| 39 | +} |
| 40 | + |
| 41 | +function compareGroups( |
| 42 | + a: ExternalApiUsage[], |
| 43 | + aName: string, |
| 44 | + b: ExternalApiUsage[], |
| 45 | + bName: string, |
| 46 | +): number { |
| 47 | + const supportedPercentageA = calculateModeledPercentage(a); |
| 48 | + const supportedPercentageB = calculateModeledPercentage(b); |
| 49 | + |
| 50 | + // Sort first by supported percentage ascending |
| 51 | + if (supportedPercentageA > supportedPercentageB) { |
| 52 | + return 1; |
| 53 | + } |
| 54 | + if (supportedPercentageA < supportedPercentageB) { |
| 55 | + return -1; |
| 56 | + } |
| 57 | + |
| 58 | + const numberOfUsagesA = a.reduce((acc, curr) => acc + curr.usages.length, 0); |
| 59 | + const numberOfUsagesB = b.reduce((acc, curr) => acc + curr.usages.length, 0); |
| 60 | + |
| 61 | + // If the number of usages is equal, sort by number of methods descending |
| 62 | + if (numberOfUsagesA === numberOfUsagesB) { |
| 63 | + const numberOfMethodsA = a.length; |
| 64 | + const numberOfMethodsB = b.length; |
| 65 | + |
| 66 | + // If the number of methods is equal, sort by library name ascending |
| 67 | + if (numberOfMethodsA === numberOfMethodsB) { |
| 68 | + return aName.localeCompare(bName); |
| 69 | + } |
| 70 | + |
| 71 | + return numberOfMethodsB - numberOfMethodsA; |
| 72 | + } |
| 73 | + |
| 74 | + // Then sort by number of usages descending |
| 75 | + return numberOfUsagesB - numberOfUsagesA; |
| 76 | +} |
| 77 | + |
| 78 | +function compareMethod(a: ExternalApiUsage, b: ExternalApiUsage): number { |
| 79 | + // Sort first by supported, putting unmodeled methods first. |
| 80 | + if (a.supported && !b.supported) { |
| 81 | + return 1; |
| 82 | + } |
| 83 | + if (!a.supported && b.supported) { |
| 84 | + return -1; |
| 85 | + } |
| 86 | + // Then sort by number of usages descending |
| 87 | + return b.usages.length - a.usages.length; |
| 88 | +} |
0 commit comments