34 lines
801 B
TypeScript
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;
|
|
}
|