« get me outta code hell

groupTracksByGroup.js « util « content « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/content/util/groupTracksByGroup.js
blob: 559967bc3140ca78d2622dbc2707c11e0d5aa500 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import {empty} from '../../util/sugar.js';

export default function groupTracksByGroup(tracks, groups) {
  const lists = new Map(groups.map(group => [group, []]));
  lists.set('other', []);

  for (const track of tracks) {
    const group = groups.find(group => group.albums.includes(track.album));
    if (group) {
      lists.get(group).push(track);
    } else {
      lists.get('other').push(track);
    }
  }

  for (const [key, tracks] of lists.entries()) {
    if (empty(tracks)) {
      lists.delete(key);
    }
  }

  return lists;
}