« get me outta code hell

withNearbyItemFromList.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/withNearbyItemFromList.js
blob: 83a8cc21e7f6ab7bc5ab06385faeb03205859eed (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
// Gets a nearby (typically adjacent) item in a list, meaning the item which is
// placed at a particular offset compared to the provided item. This is null if
// the provided list doesn't include the provided item at all, and also if the
// offset would read past either end of the list - except if configured:
//
//  - If the 'wrap' input is provided (as true), the offset will loop around
//    and continue from the opposing end.
//
//  - If the 'valuePastEdge' input is provided, that value will be output
//    instead of null.
//
// Both the list and item must be provided.
//
// See also:
//  - withIndexInList
//

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

import {raiseOutputWithoutDependency} from '#composite/control-flow';

import withIndexInList from './withIndexInList.js';

export default templateCompositeFrom({
  annotation: `withNearbyItemFromList`,

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

    offset: input({type: 'number'}),
    wrap: input({type: 'boolean', defaultValue: false}),
  },

  outputs: ['#nearbyItem'],

  steps: () => [
    withIndexInList({
      list: input('list'),
      item: input('item'),
    }),

    raiseOutputWithoutDependency({
      dependency: '#index',
      mode: input.value('index'),

      output: input.value({
        ['#nearbyItem']:
          null,
      }),
    }),

    {
      dependencies: [
        input('list'),
        input('offset'),
        input('wrap'),
        '#index',
      ],

      compute: (continuation, {
        [input('list')]: list,
        [input('offset')]: offset,
        [input('wrap')]: wrap,
        ['#index']: index,
      }) => continuation({
        ['#nearbyItem']:
          atOffset(list, index, offset, {wrap}),
      }),
    },
  ],
});