« get me outta code hell

withReverseAnnotatedReferenceList.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/withReverseAnnotatedReferenceList.js
blob: debc68e4aa24b7f26f94ed0f5ad7ca040e6088e9 (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
// Analogous implementation for withReverseReferenceList, for annotated
// references.
//
// Unlike withReverseContributionList, this composition is responsible for
// "flipping" the directionality of references: in a forward reference list,
// `thing` points to the thing being referenced, while here, it points to the
// referencing thing.
//
// This behavior can be customized to respect reference lists which are shaped
// differently than the default and/or to customize the reversed property and
// provide a less generic label than just "thing".

import withReverseList_template from './helpers/withReverseList-template.js';

import {input} from '#composite';
import {stitchArrays} from '#sugar';

import {
  withFlattenedList,
  withMappedList,
  withPropertyFromList,
  withStretchedList,
} from '#composite/data';

export default withReverseList_template({
  annotation: `withReverseAnnotatedReferenceList`,

  propertyInputName: 'list',
  outputName: '#reverseAnnotatedReferenceList',

  additionalInputs: {
    forward: input({type: 'string', defaultValue: 'thing'}),
    backward: input({type: 'string', defaultValue: 'thing'}),
    annotation: input({type: 'string', defaultValue: 'annotation'}),
  },

  customCompositionSteps: () => [
    withPropertyFromList({
      list: input('data'),
      property: input('list'),
    }).outputs({
      '#values': '#referenceLists',
    }),

    withPropertyFromList({
      list: '#referenceLists',
      property: input.value('length'),
    }),

    withFlattenedList({
      list: '#referenceLists',
    }).outputs({
      '#flattenedList': '#references',
    }),

    withStretchedList({
      list: input('data'),
      lengths: '#referenceLists.length',
    }).outputs({
      '#stretchedList': '#things',
    }),

    withPropertyFromList({
      list: '#references',
      property: input('annotation'),
    }).outputs({
      '#values': '#annotations',
    }),

    {
      dependencies: [
        input('backward'),
        input('annotation'),
        '#things',
        '#annotations',
      ],

      compute: (continuation, {
        [input('backward')]: thingProperty,
        [input('annotation')]: annotationProperty,
        ['#things']: things,
        ['#annotations']: annotations,
      }) => continuation({
        '#referencingThings':
          stitchArrays({
            [thingProperty]: things,
            [annotationProperty]: annotations,
          }),
      }),
    },

    withPropertyFromList({
      list: '#references',
      property: input('forward'),
    }).outputs({
      '#values': '#individualReferencedThings',
    }),

    withMappedList({
      list: '#individualReferencedThings',
      map: input.value(thing => [thing]),
    }).outputs({
      '#mappedList': '#referencedThings',
    }),
  ],
});