« get me outta code hell

withPropertyFromObject.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/withPropertyFromObject.js
blob: b31bab157fd13634b0281b604815dc928c54dfe0 (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
// Gets a property of some object (in a dependency) and provides that value.
// If the object itself is null, or the object doesn't have the listed property,
// the provided dependency will also be null.
//
// See also:
//  - withPropertiesFromObject
//  - withPropertyFromList
//

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

export default templateCompositeFrom({
  annotation: `withPropertyFromObject`,

  inputs: {
    object: input({type: 'object', acceptsNull: true}),
    property: input({type: 'string'}),
  },

  outputs: ({
    [input.staticDependency('object')]: object,
    [input.staticValue('property')]: property,
  }) =>
    (object && property
      ? (object.startsWith('#')
          ? [`${object}.${property}`]
          : [`#${object}.${property}`])
      : ['#value']),

  steps: () => [
    {
      dependencies: [
        input.staticDependency('object'),
        input.staticValue('property'),
      ],

      compute: (continuation, {
        [input.staticDependency('object')]: object,
        [input.staticValue('property')]: property,
      }) => continuation({
        '#output':
          (object && property
            ? (object.startsWith('#')
                ? `${object}.${property}`
                : `#${object}.${property}`)
            : '#value'),
      }),
    },

    {
      dependencies: [
        '#output',
        input('object'),
        input('property'),
      ],

      compute: (continuation, {
        ['#output']: output,
        [input('object')]: object,
        [input('property')]: property,
      }) => continuation({
        [output]:
          (object === null
            ? null
            : object[property] ?? null),
      }),
    },
  ],
});