« 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: 4e1890075eefeaf4fbcb33e417013945175a8a7c (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 '#sugar';

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