« get me outta code hell

withPropertiesFromList.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/withPropertiesFromList.js
blob: 08907babdda4436885ad1d30f258c93d8ed54ed6 (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
// Gets the listed properties from each of a list of objects, providing lists
// of property values each into a dependency prefixed with the same name as the
// list (by default).
//
// Like withPropertyFromList, this doesn't alter indices.
//
// See also:
//  - withPropertiesFromObject
//  - withPropertyFromList
//
// More list utilities:
//  - excludeFromList
//  - fillMissingListItems
//  - withFilteredList, withMappedList, withSortedList
//  - withFlattenedList, withUnflattenedList
//

import {input, templateCompositeFrom} from '#composite';
import {isString, validateArrayItems} from '#validators';

export default templateCompositeFrom({
  annotation: `withPropertiesFromList`,

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

    properties: input({
      validate: validateArrayItems(isString),
    }),

    prefix: input.staticValue({type: 'string', defaultValue: null}),
  },

  outputs: ({
    [input.staticDependency('list')]: list,
    [input.staticValue('properties')]: properties,
    [input.staticValue('prefix')]: prefix,
  }) =>
    (properties
      ? properties.map(property =>
          (prefix
            ? `${prefix}.${property}`
         : list
            ? `${list}.${property}`
            : `#list.${property}`))
      : ['#lists']),

  steps: () => [
    {
      dependencies: [input('list'), input('properties')],
      compute: (continuation, {
        [input('list')]: list,
        [input('properties')]: properties,
      }) => continuation({
        ['#lists']:
          Object.fromEntries(
            properties.map(property => [
              property,
              list.map(item => item[property] ?? null),
            ])),
      }),
    },

    {
      dependencies: [
        input.staticDependency('list'),
        input.staticValue('properties'),
        input.staticValue('prefix'),
        '#lists',
      ],

      compute: (continuation, {
        [input.staticDependency('list')]: list,
        [input.staticValue('properties')]: properties,
        [input.staticValue('prefix')]: prefix,
        ['#lists']: lists,
      }) =>
        (properties
          ? continuation(
              Object.fromEntries(
                properties.map(property => [
                  (prefix
                    ? `${prefix}.${property}`
                 : list
                    ? `${list}.${property}`
                    : `#list.${property}`),
                  lists[property],
                ])))
          : continuation({'#lists': lists})),
    },
  ],
});