-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSortPairwiseResults.ts
51 lines (46 loc) · 1.37 KB
/
SortPairwiseResults.ts
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
import * as noflo from 'noflo'
import { ProcessHandler, ProcessInput, ProcessOutput, NofloComponent } from '../libs/noflo-types'
import { PairwiseVote, Statement } from 'rsf-types'
const process: ProcessHandler = (input: ProcessInput, output: ProcessOutput) => {
if (!input.hasData('statements', 'rankings')) {
return
}
const statements: Statement[] = input.getData('statements')
const rankings: PairwiseVote[] = input.getData('rankings')
const withCounts = statements.map(statement => {
return {
...statement,
count: rankings.filter(vote => vote.choices[vote.choice].text === statement.text).length
}
})
const sorted = withCounts.sort((a, b) => {
if (a.count > b.count) return -1
else if (a.count === b.count) return 0
else if (a.count < b.count) return 1
})
output.send({
sorted: sorted
})
output.done()
}
const getComponent = (): NofloComponent => {
const c: NofloComponent = new noflo.Component()
c.description = ''
c.icon = 'handshake-o'
c.inPorts.add('statements', {
datatype: 'array', // rsf-types/Statement[]
description: 'The list of statements'
})
c.inPorts.add('rankings', {
datatype: 'array', // rsf-types/PairwiseVote[]
description: 'The list of votes'
})
c.outPorts.add('sorted', {
datatype: 'array' // rsf-types/Statement[]
})
c.process(process)
return c
}
export {
getComponent
}