« get me outta code hell

withPropertiesFromObject.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/withPropertiesFromObject.js
blob: 21726b586b018059c15923034fcbdc0857b1df6d (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
// Gets the listed properties from some object, providing each property's value
// as a dependency prefixed with the same name as the object (by default).
// If the object itself is null, all provided dependencies will be null;
// if it's missing only select properties, those will be provided as null.
//
// See also:
//  - withPropertiesFromList
//  - withPropertyFromObject
//

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

export default templateCompositeFrom({
  annotation: `withPropertiesFromObject`,

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

    properties: input({
      type: 'array',
      validate: validateArrayItems(isString),
    }),

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

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

  steps: () => [
    {
      dependencies: [input('object'), input('properties')],
      compute: (continuation, {
        [input('object')]: object,
        [input('properties')]: properties,
      }) => continuation({
        ['#entries']:
          (object === null
            ? properties.map(property => [property, null])
            : properties.map(property => [property, object[property]])),
      }),
    },

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

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