« get me outta code hell

withClonedThings.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/withClonedThings.js
blob: 36c3ba540b52bb08cc0ea214637167773383eb30 (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
// Clones all the things in a list. If the 'assign' input is provided,
// all new things are assigned the same specified properties. If the
// 'assignEach' input is provided, each new thing is assigned the
// corresponding properties.

import {input, templateCompositeFrom} from '#composite';
import Thing from '#thing';
import {isObject, isThingClass, sparseArrayOf} from '#validators';

import {withMappedList} from '#composite/data';

export default templateCompositeFrom({
  annotation: `withClonedThings`,

  inputs: {
    things: input({type: 'array'}),

    reclass: input({
      validate: isThingClass,
      defaultValue: null,
    }),

    reclassUnder: input({
      validate: isThingClass,
      defaultValue: null,
    }),

    assign: input({
      type: 'object',
      defaultValue: null,
    }),

    assignEach: input({
      validate: sparseArrayOf(isObject),
      defaultValue: null,
    }),
  },

  outputs: ['#clonedThings'],

  steps: () => [
    {
      dependencies: [input('assign'), input('assignEach')],
      compute: (continuation, {
        [input('assign')]: assign,
        [input('assignEach')]: assignEach,
      }) => continuation({
        ['#assignmentMap']:
          (index) =>
            (assign && assignEach
              ? {...assignEach[index] ?? {}, ...assign}
           : assignEach
              ? assignEach[index] ?? {}
              : assign ?? {}),
      }),
    },

    {
      dependencies: [input('reclass'), input('reclassUnder')],
      compute: (continuation, {
        [input('reclass')]: reclass,
        [input('reclassUnder')]: reclassUnder,
      }) => continuation({
        ['#cloneOperation']:
          (reclassUnder && reclass
            ? source => reclassUnder.clone(source, {as: reclass})
         : reclass
            ? source => Thing.clone(source, {as: reclass})
            : source => Thing.clone(source)),
      }),
    },

    {
      dependencies: ['#assignmentMap', '#cloneOperation'],
      compute: (continuation, {
        ['#assignmentMap']: assignmentMap,
        ['#cloneOperation']: cloneOperation,
      }) => continuation({
        ['#cloningMap']:
          (thing, index) =>
            Object.assign(cloneOperation(thing), assignmentMap(index)),
      }),
    },

    withMappedList({
      list: input('things'),
      map: '#cloningMap',
    }).outputs({
      '#mappedList': '#clonedThings',
    }),
  ],
});