« 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: 9af6aa84571000dbf18af3bc2190a39351892e59 (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
// 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 CacheableObject from '#cacheable-object';
import {input, templateCompositeFrom} from '#composite';
import {isObject, sparseArrayOf} from '#validators';

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

export default templateCompositeFrom({
  annotation: `withClonedThings`,

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

    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: ['#assignmentMap'],
      compute: (continuation, {
        ['#assignmentMap']: assignmentMap,
      }) => continuation({
        ['#cloningMap']:
          (thing, index) =>
            Object.assign(
              CacheableObject.clone(thing),
              assignmentMap(index)),
      }),
    },

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