« 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: 4f2405068b1d32f460147ba7a13baa73d011e5ae (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
// 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.
//
// If the `internal` input is true, this reads the CacheableObject update value
// of the object rather than its exposed value.
//
// See also:
//  - withPropertiesFromObject
//  - withPropertyFromList
//

import CacheableObject from '#cacheable-object';
import {input, templateCompositeFrom} from '#composite';

export default templateCompositeFrom({
  annotation: `withPropertyFromObject`,

  inputs: {
    object: input({type: 'object', acceptsNull: true}),
    property: input({type: 'string'}),
    internal: input({type: 'boolean', defaultValue: false}),
  },

  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: [
        input('object'),
        input('property'),
        input('internal'),
      ],

      compute: (continuation, {
        [input('object')]: object,
        [input('property')]: property,
        [input('internal')]: internal,
      }) => continuation({
        '#value':
          (object === null
            ? null
         : internal
            ? CacheableObject.getUpdateValue(object, property)
                ?? null
            : object[property]
                ?? null),
      }),
    },

    {
      dependencies: ['#output', '#value'],

      compute: (continuation, {
        ['#output']: output,
        ['#value']: value,
      }) => continuation({
        [output]: value,
      }),
    },
  ],
});