sendou.ink/app/features/tier-list-maker/tier-list-maker-utils.ts
Kalle 187e1aa105
Some checks are pending
E2E Tests / e2e (push) Waiting to run
Tests and checks on push / run-checks-and-tests (push) Waiting to run
Updates translation progress / update-translation-progress-issue (push) Waiting to run
Tier list maker feature (#2634)
2025-11-16 16:30:24 +02:00

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
);
}