deckhearth/lib/deck-detail-grouping.js

43 lines
986 B
JavaScript
Raw Permalink Normal View History

/**
* Group deck card rows for the deck detail view.
* @param {Array<object>|undefined} cards
* @param {'type'|'cmc'|'color'|'rarity'|string} groupBy
*/
export function groupDeckCards(cards, groupBy) {
if (!cards) {
return {};
}
return cards.reduce((groups, card) => {
let key;
switch (groupBy) {
case 'type':
key = card.card_type ? card.card_type.split(' — ')[0] : 'Unknown';
break;
case 'cmc':
key = `${card.cmc || 0} Mana`;
break;
case 'color':
try {
const colors = card.colors ? JSON.parse(card.colors) : [];
key = colors.length === 0 ? 'Colorless' : colors.join('');
} catch {
key = 'Colorless';
}
break;
case 'rarity':
key = card.rarity || 'Unknown';
break;
default:
key = 'All Cards';
}
if (!groups[key]) {
groups[key] = [];
}
groups[key].push(card);
return groups;
}, {});
}