(rawQuestions)
| 48 | * 规范化问题列表 |
| 49 | */ |
| 50 | export const normalizeQuestions = (rawQuestions) => { |
| 51 | if (!Array.isArray(rawQuestions)) return [] |
| 52 | |
| 53 | return rawQuestions |
| 54 | .map((item, index) => { |
| 55 | if (!item || typeof item !== 'object') return null |
| 56 | |
| 57 | const question = String(item.question || '').trim() |
| 58 | if (!question) return null |
| 59 | |
| 60 | const questionId = |
| 61 | String(item.questionId || item.question_id || '').trim() || `q-${index + 1}` |
| 62 | const operation = String(item.operation || '').trim() |
| 63 | const allowOther = Boolean(item.allowOther ?? item.allow_other ?? true) |
| 64 | const baseOptions = normalizeOptions(item.options || []) |
| 65 | const hasOtherOption = baseOptions.some((option) => isOtherOption(option)) |
| 66 | const options = |
| 67 | allowOther && !hasOtherOption |
| 68 | ? [...baseOptions, { label: '其他', value: DEFAULT_OTHER_OPTION_VALUE }] |
| 69 | : baseOptions |
| 70 | |
| 71 | return { |
| 72 | questionId, |
| 73 | question, |
| 74 | options, |
| 75 | multiSelect: Boolean(item.multiSelect ?? item.multi_select ?? false), |
| 76 | allowOther, |
| 77 | operation |
| 78 | } |
| 79 | }) |
| 80 | .filter(Boolean) |
| 81 | } |
| 82 | |
| 83 | export { DEFAULT_OTHER_OPTION_VALUE } |
no test coverage detected