Files
buzz-sheet/lib/material-poster.ts
T
gunshiz 6dc8e3afaa
CI / Verify (push) Successful in 1m31s
CI / Build immutable images and deploy (push) Successful in 4m0s
feat : merge mat
2026-09-06 18:05:48 +07:00

34 lines
801 B
TypeScript

export type PosterMaterial = {
key: string;
amount: number;
name: string;
imageUrl: string | null;
};
export function mergePosterMaterials(materials: PosterMaterial[]): PosterMaterial[] {
const merged: PosterMaterial[] = [];
const indexByImageUrl = new Map<string, number>();
for (const material of materials) {
if (!material.imageUrl) {
merged.push(material);
continue;
}
const existingIndex = indexByImageUrl.get(material.imageUrl);
if (existingIndex === undefined) {
indexByImageUrl.set(material.imageUrl, merged.length);
merged.push(material);
continue;
}
const existing = merged[existingIndex];
merged[existingIndex] = {
...existing,
amount: existing.amount + material.amount,
};
}
return merged;
}