« get me outta code hell

excludeFromList.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/excludeFromList.js
blob: d798dcdcff566ab012a9940316deb6b6eb0f3cf6 (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
// Filters particular values out of a list. Note that this will always
// completely skip over null, but can be used to filter out any other
// primitive or object value.
//
// See also:
//  - fillMissingListItems
//
// More list utilities:
//  - withFilteredList, withMappedList, withSortedList
//  - withFlattenedList, withUnflattenedList
//  - withPropertyFromList, withPropertiesFromList
//

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

export default templateCompositeFrom({
  annotation: `excludeFromList`,

  inputs: {
    list: input(),

    item: input({defaultValue: null}),
    items: input({type: 'array', defaultValue: null}),
  },

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

  steps: () => [
    {
      dependencies: [
        input.staticDependency('list'),
        input('list'),
        input('item'),
        input('items'),
      ],

      compute: (continuation, {
        [input.staticDependency('list')]: listName,
        [input('list')]: listContents,
        [input('item')]: excludeItem,
        [input('items')]: excludeItems,
      }) => continuation({
        [listName ?? '#list']:
          listContents.filter(item => {
            if (excludeItem !== null && item === excludeItem) return false;
            if (!empty(excludeItems) && excludeItems.includes(item)) return false;
            return true;
          }),
      }),
    },
  ],
});