« get me outta code hell

withFilteredList.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/withFilteredList.js
blob: 82e56903300e0113b5ca7e2c096b994aba9cc185 (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
// Applies a filter - an array of truthy and falsy values - to the index-
// corresponding items in a list. Items which correspond to a truthy value
// are kept, and the rest are excluded from the output list.
//
// TODO: It would be neat to apply an availability check here, e.g. to allow
// not providing a filter at all and performing the check on the contents of
// the list (though on the filter, if present, is fine too). But that's best
// done by some shmancy-fancy mapping support in composite.js, so a bit out
// of reach for now (apart from proving uses built on top of a more boring
// implementation).
//
// TODO: There should be two outputs - one for the items included according to
// the filter, and one for the items excluded.
//
// See also:
//  - withMappedList
//  - withSortedList
//
// More list utilities:
//  - excludeFromList
//  - fillMissingListItems
//  - withFlattenedList, withUnflattenedList
//  - withPropertyFromList, withPropertiesFromList
//

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

export default templateCompositeFrom({
  annotation: `withFilteredList`,

  inputs: {
    list: input({type: 'array'}),
    filter: input({type: 'array'}),
  },

  outputs: ['#filteredList'],

  steps: () => [
    {
      dependencies: [input('list'), input('filter')],
      compute: (continuation, {
        [input('list')]: list,
        [input('filter')]: filter,
      }) => continuation({
        '#filteredList':
          list.filter((item, index) => filter[index]),
      }),
    },
  ],
});