forked from moiskillnadne/ml-base-encryption
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatResponses.js
47 lines (38 loc) · 1.33 KB
/
formatResponses.js
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
import { ItemResponseType } from './constants.js'
import { joinWihComma } from './joinWithComma.js'
import { createArrayFromMinToMax } from './createArrayFromMinToMax.js'
/**
* @module index
* @description Method to format responses based on item type
* @param {string} type
* @param {object} responseValues
* @returns {string} The formatted responses
*/
export function formatResponses(type, responseValues) {
const forbiddenTypes = [
ItemResponseType.SingleSelectionPerRow,
ItemResponseType.MultipleSelectionPerRow,
ItemResponseType.SliderRows,
]
if (forbiddenTypes.includes(type)) return ''
if(type === ItemResponseType.Slider) {
const min = Number(responseValues?.minValue)
const max = Number(responseValues?.maxValue)
const scores = responseValues?.scores
const options = createArrayFromMinToMax(min, max)
return joinWihComma(
options?.map(
(item, i) => `${item}: ${item}${scores?.length ? ` (score: ${scores[i]})` : ''}`,
) || [],
)
}
if (!responseValues?.options?.length) return ''
return joinWihComma(
responseValues.options.map(({ text, value, score }) => {
const stringifiedValue = `${value ?? ''}`;
return `${text}${stringifiedValue ? `: ${stringifiedValue}` : ''}${
typeof score === 'number' ? ` (score: ${score})` : ''
}`;
}),
)
}