« get me outta code hell

withResolvedContribs.js « wiki-data « composite « data « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/composite/wiki-data/withResolvedContribs.js
blob: fd51c81e82547d5959f41f7d6fe5976b55e34a77 (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
// Resolves the contribsByRef contained in the provided dependency,
// providing (named by the second argument) the result. "Resolving"
// means mapping the artist reference of each contribution to an artist
// object, and filtering out those whose artist reference doesn't match
// any artist.

import {input, templateCompositeFrom} from '#composite';
import find from '#find';
import {filterMultipleArrays, stitchArrays} from '#sugar';
import thingConstructors from '#things';
import {is, isContributionList, isStringNonEmpty} from '#validators';

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

export default templateCompositeFrom({
  annotation: `withResolvedContribs`,

  inputs: {
    from: input({
      validate: isContributionList,
      acceptsNull: true,
    }),

    notFoundMode: input({
      validate: is('exit', 'filter', 'null'),
      defaultValue: 'null',
    }),

    thingProperty: input({
      validate: isStringNonEmpty,
      defaultValue: null,
    }),
  },

  outputs: ['#resolvedContribs'],

  steps: () => [
    raiseOutputWithoutDependency({
      dependency: input('from'),
      mode: input.value('empty'),
      output: input.value({
        ['#resolvedContribs']: [],
      }),
    }),

    {
      dependencies: [
        input('thingProperty'),
        input.staticDependency('from'),
      ],

      compute: (continuation, {
        [input('thingProperty')]: thingProperty,
        [input.staticDependency('from')]: fromDependency,
      }) => continuation({
        ['#thingProperty']:
          (thingProperty
            ? thingProperty
         : !fromDependency?.startsWith('#')
            ? fromDependency
            : null),
      }),
    },

    withPropertiesFromList({
      list: input('from'),
      properties: input.value(['artist', 'annotation']),
      prefix: input.value('#contribs'),
    }),

    {
      dependencies: ['#contribs.artist', '#contribs.annotation'],

      compute(continuation, {
        ['#contribs.artist']: artist,
        ['#contribs.annotation']: annotation,
      }) {
        filterMultipleArrays(artist, annotation, (artist, _annotation) => artist);

        return continuation({
          ['#details']:
            stitchArrays({artist, annotation}),
        });
      },
    },

    {
      dependencies: [
        '#details',
        '#thingProperty',
        input.myself(),
      ],

      compute: (continuation, {
        ['#details']: details,
        ['#thingProperty']: thingProperty,
        [input.myself()]: myself,
      }) => continuation({
        ['#contributions']:
          details.map(details => {
            const contrib = new thingConstructors.Contribution();

            Object.assign(contrib, {
              ...details,
              thing: myself,
              thingProperty: thingProperty,
            });

            return contrib;
          }),
      }),
    },

    {
      dependencies: ['#contributions'],

      compute: (continuation, {
        ['#contributions']: contributions,
      }) => continuation({
        ['#resolvedContribs']:
          contributions
            .filter(contrib => contrib.artist),
      }),
    },
  ],
});