« get me outta code hell

withIndexInList.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/withIndexInList.js
blob: b1af2033374c6785eaa0426ee385f59bd7077fe1 (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
// Gets the index of the provided item in the provided list. Note that this
// will output -1 if the item is not found, and this may be detected using
// any availability check with type: 'index'. If the list includes the item
// twice, the output index will be of the first match.
//
// Both the list and item must be provided.
//
// See also:
//  - withNearbyItemFromList
//  - exitWithoutDependency
//  - raiseOutputWithoutDependency
//

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

export default templateCompositeFrom({
  annotation: `withIndexInList`,

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

  outputs: ['#index'],

  steps: () => [
    {
      dependencies: [input('list'), input('item')],
      compute: (continuation, {
        [input('list')]: list,
        [input('item')]: item,
      }) => continuation({
        ['#index']:
          list.indexOf(item),
      }),
    },
  ],
});