mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 05:51:06 +00:00
refactor(pe): share bounded resource count row construction (#16591)
This commit is contained in:
1 parent
0da1f029d2
commit
09c84ecaff
1 file changed
+15
-19
@@ -83,11 +83,12 @@ const summaryIdentity = (identity: PeResourceIdentity): Identity =>
|
||||
};
|
||||
|
||||
/** Count resources per identity; names are keyed by their exact UTF-16 bytes. */
|
||||
const countBy = (
|
||||
const countBy = <Row extends { resource_count: number }>(
|
||||
resources: PeResources["resources"],
|
||||
key: "type" | "language",
|
||||
): readonly (readonly [Identity, number])[] => {
|
||||
const counts = new Map<string, [Identity, number]>();
|
||||
toRow: (identity: Identity) => Row,
|
||||
) => {
|
||||
const counts = new Map<string, Row>();
|
||||
for (const resource of resources) {
|
||||
const identity = resource[key];
|
||||
const id =
|
||||
@@ -95,10 +96,10 @@ const countBy = (
|
||||
? `id:${String(identity.id)}`
|
||||
: `name:${identity.utf16le_hex}`;
|
||||
const entry = counts.get(id);
|
||||
if (entry === undefined) counts.set(id, [summaryIdentity(identity), 1]);
|
||||
else entry[1] += 1;
|
||||
if (entry === undefined) counts.set(id, toRow(summaryIdentity(identity)));
|
||||
else entry.resource_count += 1;
|
||||
}
|
||||
return [...counts.values()];
|
||||
return byteBoundedList([...counts.values()], COUNT_LIST_BYTES);
|
||||
};
|
||||
|
||||
/** Summarize retained PE resource facts without reopening the image. */
|
||||
@@ -123,19 +124,14 @@ export const projectPeResourcesView = (
|
||||
directory_count: report.directories.length,
|
||||
resource_count: report.resources.length,
|
||||
icon_group_count: report.icon_groups.length,
|
||||
types: byteBoundedList(
|
||||
countBy(report.resources, "type").map(([type, resource_count]) => ({
|
||||
type,
|
||||
resource_count,
|
||||
})),
|
||||
COUNT_LIST_BYTES,
|
||||
),
|
||||
languages: byteBoundedList(
|
||||
countBy(report.resources, "language").map(
|
||||
([language, resource_count]) => ({ language, resource_count }),
|
||||
),
|
||||
COUNT_LIST_BYTES,
|
||||
),
|
||||
types: countBy(report.resources, "type", (type) => ({
|
||||
type,
|
||||
resource_count: 1,
|
||||
})),
|
||||
languages: countBy(report.resources, "language", (language) => ({
|
||||
language,
|
||||
resource_count: 1,
|
||||
})),
|
||||
limitation_count: report.limitations.length,
|
||||
};
|
||||
return ok({
|
||||
|
||||
Reference in new issue
Block a user