« get me outta code hell

generateAlbumSocialEmbedDescription.js « dependencies « content « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/content/dependencies/generateAlbumSocialEmbedDescription.js
blob: 7099616a79111ebf726d3f51c6a99cf257c64821 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import {accumulateSum} from '#sugar';

export default {
  extraDependencies: ['language'],

  data(album) {
    const data = {};

    const duration = accumulateSum(album.tracks, track => track.duration);

    data.hasDuration = duration > 0;
    data.hasTracks = album.tracks.length > 0;
    data.hasDate = !!album.date;
    data.hasAny = (data.hasDuration || data.hasTracks || data.hasDuration);

    if (!data.hasAny)
      return data;

    if (data.hasDuration)
      data.duration = duration;

    if (data.hasTracks)
      data.tracks = album.tracks.length;

    if (data.hasDate)
      data.date = album.date;

    return data;
  },

  generate(data, {language}) {
    return language.formatString(
      'albumPage.socialEmbed.body' + [
        data.hasDuration && '.withDuration',
        data.hasTracks && '.withTracks',
        data.hasDate && '.withReleaseDate',
      ].filter(Boolean).join(''),

      Object.fromEntries([
        data.hasDuration &&
          ['duration', language.formatDuration(data.duration)],
        data.hasTracks &&
          ['tracks', language.countTracks(data.tracks, {unit: true})],
        data.hasDate &&
          ['date', language.formatDate(data.date)],
      ].filter(Boolean)));
  },
};