« get me outta code hell

withMainRelease.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/withMainRelease.js
blob: 67a312aeec44dfc30f665d5220bdeee6e6799131 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Resolves this track's `mainRelease` reference, using weird-ass atypical
// machinery that operates on soupyFind and does not operate on findMixed,
// let alone a prim and proper standalone find spec.
//
// Raises null only if there is no `mainRelease` reference provided at all.
// This will early exit (with notFoundValue) if the reference doesn't resolve.
//

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

import {raiseOutputWithoutDependency} from '#composite/control-flow';
import {withPropertyFromObject} from '#composite/data';
import {withResolvedReference} from '#composite/wiki-data';
import {soupyFind} from '#composite/wiki-properties';

export default templateCompositeFrom({
  annotation: `withMainRelease`,

  inputs: {
    from: input({
      defaultDependency: 'mainRelease',
      acceptsNull: true,
    }),

    notFoundValue: input({defaultValue: null}),
  },

  outputs: ['#mainRelease'],

  steps: () => [
    raiseOutputWithoutDependency({
      dependency: input('from'),
      output: input.value({'#mainRelease': null}),
    }),

    {
      dependencies: [input('from'), 'name'],
      compute: (continuation, {
        [input('from')]: ref,
        ['name']: ownName,
      }) =>
        (ref === 'same name single'
          ? continuation({
              ['#albumOrTrackReference']: null,
              ['#sameNameSingleReference']: ownName,
            })
          : continuation({
              ['#albumOrTrackReference']: ref,
              ['#sameNameSingleReference']: null,
            })),
    },

    withResolvedReference({
      ref: '#albumOrTrackReference',
      find: soupyFind.input('trackMainReleasesOnly'),
    }).outputs({
      '#resolvedReference': '#matchingTrack',
    }),

    withResolvedReference({
      ref: '#albumOrTrackReference',
      find: soupyFind.input('album'),
    }).outputs({
      '#resolvedReference': '#matchingAlbum',
    }),

    withResolvedReference({
      ref: '#sameNameSingleReference',
      find: soupyFind.input('albumSinglesOnly'),
      findOptions: input.value({
        fuzz: {
          capitalization: true,
          kebab: true,
        },
      }),
    }).outputs({
      '#resolvedReference': '#sameNameSingle',
    }),

    {
      dependencies: ['#sameNameSingle'],
      compute: (continuation, {
        ['#sameNameSingle']: sameNameSingle,
      }) =>
        (sameNameSingle
          ? continuation.raiseOutput({
              ['#mainRelease']:
                sameNameSingle,
            })
          : continuation()),
    },

    {
      dependencies: [
        '#matchingTrack',
        '#matchingAlbum',
        input('notFoundValue'),
      ],

      compute: (continuation, {
        ['#matchingTrack']: matchingTrack,
        ['#matchingAlbum']: matchingAlbum,
        [input('notFoundValue')]: notFoundValue,
      }) =>
        (matchingTrack && matchingAlbum
          ? continuation()
       : matchingTrack ?? matchingAlbum
          ? continuation.raiseOutput({
              ['#mainRelease']:
                matchingTrack ?? matchingAlbum,
            })
          : continuation.exit(notFoundValue)),
    },

    withPropertyFromObject({
      object: '#matchingAlbum',
      property: input.value('tracks'),
    }),

    {
      dependencies: [
        '#matchingAlbum.tracks',
        '#matchingTrack',
        input('notFoundValue'),
      ],

      compute: (continuation, {
        ['#matchingAlbum.tracks']: matchingAlbumTracks,
        ['#matchingTrack']: matchingTrack,
        [input('notFoundValue')]: notFoundValue,
      }) =>
        (matchingAlbumTracks.includes(matchingTrack)
          ? continuation.raiseOutput({'#mainRelease': matchingTrack})
          : continuation.exit(notFoundValue)),
    },
  ],
});