feat : merge mat
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user