« get me outta code hell

withResolvedArtworkReferenceList.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/withResolvedArtworkReferenceList.js
blob: e9c6a590f6a2f353ef907f864da916db532ca424 (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
import {input, templateCompositeFrom} from '#composite';
import {stitchArrays} from '#sugar';
import {is, isString, optional, validateArrayItems, validateProperties}
  from '#validators';

import {withFilteredList, withMappedList, withPropertiesFromList}
  from '#composite/data';

import inputWikiData from './inputWikiData.js';
import withResolvedReferenceList from './withResolvedReferenceList.js';

export default templateCompositeFrom({
  annotation: `withResolvedArtworkReferenceList`,

  inputs: {
    list: input({
      validate:
        validateArrayItems(
          validateProperties({
            reference: isString,
            annotation: optional(isString),
          })),

      acceptsNull: true,
    }),

    data: inputWikiData({allowMixedTypes: false}),
    find: input({type: 'function'}),

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

  steps: () => [
    withPropertiesFromList({
      list: input('list'),
      properties: input.value([
        'reference',
        'annotation',
      ]),
    }),

    withResolvedReferenceList({
      list: '#list.reference',
      data: input('data'),
      find: input('find'),
      notFoundMode: input.value('null'),
    }),

    {
      dependencies: [
        '#resolvedReferenceList',
        '#list.annotation',
      ],

      compute: (continuation, {
        ['#resolvedReferenceList']: thing,
        ['#list.annotation']: annotation,
      }) => continuation({
        ['#matches']:
          stitchArrays({
            thing,
            annotation,
          }),
      }),
    },

    {
      dependencies: ['#matches'],
      compute: (continuation, {'#matches': matches}) =>
        (matches.every(match => match)
          ? continuation.raiseOutput({
              ['#resolvedArtworkReferenceList']:
                matches,
            })
          : continuation()),
    },

    {
      dependencies: [input('notFoundMode')],
      compute: (continuation, {
        [input('notFoundMode')]: notFoundMode,
      }) =>
        (notFoundMode === 'exit'
          ? continuation.exit([])
          : continuation()),
    },

    {
      dependencies: ['#matches', input('notFoundMode')],
      compute: (continuation, {
        ['#matches']: matches,
        [input('notFoundMode')]: notFoundMode,
      }) =>
        (notFoundMode === 'null'
          ? continuation.raiseOutput({
              ['#resolvedArtworkReferenceList']:
                matches,
            })
          : continuation()),
    },

    withMappedList({
      list: '#resolvedReferenceList',
      map: input.value(thing => thing !== null),
    }),

    withFilteredList({
      list: '#matches',
      filter: '#mappedList',
    }),

    {
      dependencies: ['#filteredList'],
      compute: (continuation, {
        ['#filteredList']: filteredList,
      }) => continuation({
        ['#resolvedArtworkReferenceList']:
          filteredList,
      }),
    },
  ],
})