mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-05-05 04:36:10 -05:00
29 lines
801 B
TypeScript
29 lines
801 B
TypeScript
import type { TierListItem, TierListState } from "./tier-list-maker-schemas";
|
|
|
|
export function tierListItemId(item: TierListItem) {
|
|
return `${item.type}:${item.id}${item.nth ? `:${item.nth}` : ""}`;
|
|
}
|
|
|
|
/**
|
|
* Finds the next nth value for a duplicate item in the tier list.
|
|
* Searches through all tiers to find the maximum nth value for items
|
|
* with the same id and type, then returns max + 1.
|
|
*/
|
|
export function getNextNthForItem(
|
|
item: TierListItem,
|
|
tiers: TierListState,
|
|
): number {
|
|
return (
|
|
Array.from(tiers.tierItems.values()).reduce((maxNth, items) => {
|
|
const matchingItems = items.filter(
|
|
(i) => i.id === item.id && i.type === item.type,
|
|
);
|
|
const currentMax = Math.max(
|
|
...matchingItems.map((i) => i.nth ?? 0),
|
|
maxNth,
|
|
);
|
|
return currentMax;
|
|
}, 0) + 1
|
|
);
|
|
}
|