« get me outta code hell

fillMissingListItems.js « 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/data/fillMissingListItems.js
blob: 4f818a791e36f67e7dffb8108e696d628da1a7d7 (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
// Replaces items of a list, which are null or undefined, with some fallback
// value. By default, this replaces the passed dependency.
//
// See also:
//  - excludeFromList
//
// More list utilities:
//  - withFilteredList, withMappedList, withSortedList
//  - withFlattenedList, withUnflattenedList
//  - withPropertyFromList, withPropertiesFromList
//

import {input, templateCompositeFrom} from '#composite';

export default templateCompositeFrom({
  annotation: `fillMissingListItems`,

  inputs: {
    list: input({type: 'array'}),
    fill: input({acceptsNull: true}),
  },

  outputs: ({
    [input.staticDependency('list')]: list,
  }) => [list ?? '#list'],

  steps: () => [
    {
      dependencies: [input('list'), input('fill')],
      compute: (continuation, {
        [input('list')]: list,
        [input('fill')]: fill,
      }) => continuation({
        ['#filled']:
          list.map(item => item ?? fill),
      }),
    },

    {
      dependencies: [input.staticDependency('list'), '#filled'],
      compute: (continuation, {
        [input.staticDependency('list')]: list,
        ['#filled']: filled,
      }) => continuation({
        [list ?? '#list']:
          filled,
      }),
    },
  ],
});