« get me outta code hell

withTrackArtDate.js « track « things « composite « data « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/composite/things/track/withTrackArtDate.js
blob: e2c4d8bc593d47e749959bdef1f245c0db75cb76 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Gets the date of cover art release. This represents only the track's own
// unique cover artwork, if any.
//
// If the 'fallback' option is false (the default), this will only output
// the track's own coverArtDate or its album's trackArtDate. If 'fallback'
// is set, and neither of these is available, it'll output the track's own
// date instead.

import {input, templateCompositeFrom} from '#composite';
import {isDate} from '#validators';

import {raiseOutputWithoutDependency} from '#composite/control-flow';

import withDate from './withDate.js';
import withHasUniqueCoverArt from './withHasUniqueCoverArt.js';
import withPropertyFromAlbum from './withPropertyFromAlbum.js';

export default templateCompositeFrom({
  annotation: `withTrackArtDate`,

  inputs: {
    from: input({
      validate: isDate,
      defaultDependency: 'coverArtDate',
      acceptsNull: true,
    }),

    fallback: input({
      type: 'boolean',
      defaultValue: false,
    }),
  },

  outputs: ['#trackArtDate'],

  steps: () => [
    withHasUniqueCoverArt(),

    raiseOutputWithoutDependency({
      dependency: '#hasUniqueCoverArt',
      mode: input.value('falsy'),
      output: input.value({'#trackArtDate': null}),
    }),

    {
      dependencies: [input('from')],
      compute: (continuation, {
        [input('from')]: from,
      }) =>
        (from
          ? continuation.raiseOutput({'#trackArtDate': from})
          : continuation()),
    },

    withPropertyFromAlbum({
      property: input.value('trackArtDate'),
    }),

    {
      dependencies: [
        '#album.trackArtDate',
        input('fallback'),
      ],

      compute: (continuation, {
        ['#album.trackArtDate']: albumTrackArtDate,
        [input('fallback')]: fallback,
      }) =>
        (albumTrackArtDate
          ? continuation.raiseOutput({'#trackArtDate': albumTrackArtDate})
       : fallback
          ? continuation()
          : continuation.raiseOutput({'#trackArtDate': null})),
    },

    withDate().outputs({
      '#date': '#trackArtDate',
    }),
  ],
});