« get me outta code hell

withSum.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/withSum.js
blob: 484e99060fe84206e7b804354f5d5b9120bebca6 (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
// Gets the numeric total of adding all the values in a list together.
// Values that are false, null, or undefined are skipped over.

import {input, templateCompositeFrom} from '#composite';
import {isNumber, sparseArrayOf} from '#validators';

export default templateCompositeFrom({
  annotation: `withSum`,

  inputs: {
    values: input({
      validate: sparseArrayOf(isNumber),
    }),
  },

  outputs: ['#sum'],

  steps: () => [
    {
      dependencies: [input('values')],
      compute: (continuation, {
        [input('values')]: values,
      }) => continuation({
        ['#sum']:
          values
            .filter(item => typeof item === 'number')
            .reduce(
              (accumulator, value) => accumulator + value,
              0),
      }),
    },
  ],
});