« get me outta code hell

withPropertyFromList.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/withPropertyFromList.js
blob: 3ce05fdfdda1ca505bc31863a132c4aab80a3bb8 (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
// Gets a property from each of a list of objects (in a dependency) and
// provides the results.
//
// This doesn't alter any list indices, so positions which were null in the
// original list are kept null here. Objects which don't have the specified
// property are retained in-place as null.
//
// See also:
//  - withPropertiesFromList
//  - withPropertyFromObject
//
// More list utilities:
//  - excludeFromList
//  - fillMissingListItems
//  - withFlattenedList
//  - withUnflattenedList
//

import {empty} from '#sugar';

// todo: OUHHH THIS ONE'S NOT UPDATED YET LOL
export default function({
  list,
  property,
  into = null,
}) {
  into ??=
    (list.startsWith('#')
      ? `${list}.${property}`
      : `#${list}.${property}`);

  return {
    annotation: `withPropertyFromList`,
    flags: {expose: true, compose: true},

    expose: {
      mapDependencies: {list},
      mapContinuation: {into},
      options: {property},

      compute(continuation, {list, '#options': {property}}) {
        if (list === undefined || empty(list)) {
          return continuation({into: []});
        }

        return continuation({
          into:
            list.map(item =>
              (item === null || item === undefined
                ? null
                : item[property] ?? null)),
        });
      },
    },
  };
}