course-assessment.mjs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. /** v3 course scores live in an existing report table, separately from 0–100 peer reviews. */
  2. export const COURSE_ASSESSMENT_ID = 'course-priority';
  3. export const COURSE_ASSESSMENT_COLUMNS = Object.freeze(['机会', '证据强度', '可做性', '商业价值', '依据一句话', '排序']);
  4. const invalid = () => { throw Object.assign(Error('COURSE_ASSESSMENT_INVALID'), { code: 'COURSE_ASSESSMENT_INVALID' }); };
  5. const key = value => typeof value === 'string' ? value.replace(/\s/gu, '') : '';
  6. const text = value => typeof value === 'string' && Boolean(value.trim());
  7. const score = value => {
  8. if (value === null || value === '' || ['待确认', '未知', '未评分', '—'].includes(value)) return null;
  9. if (typeof value === 'string' && /^[123]$/.test(value.trim())) value = Number(value.trim());
  10. if (!Number.isInteger(value) || value < 1 || value > 3) invalid();
  11. return value;
  12. };
  13. export function courseAssessmentRows(section) {
  14. if (section?.id !== COURSE_ASSESSMENT_ID) return null;
  15. if (section.kind !== 'table' || !Array.isArray(section.data?.cols)
  16. || section.data.cols.length !== COURSE_ASSESSMENT_COLUMNS.length
  17. || section.data.cols.some((column, i) => key(column) !== key(COURSE_ASSESSMENT_COLUMNS[i]))
  18. || !Array.isArray(section.data.rows) || !section.data.rows.length) invalid();
  19. return section.data.rows.map(row => {
  20. if (!Array.isArray(row) || row.length !== 6) invalid();
  21. const [candidate, evidence, feasibility, business, basis, order] = row;
  22. if (!text(candidate) || !text(basis) || typeof order !== 'string') invalid();
  23. const scores = [evidence, feasibility, business].map(score);
  24. const unknown = scores.some(value => value === null);
  25. const low = scores.includes(1);
  26. if ((unknown || low) && /^(?:第\s*)?[123一二三]\s*(?:名|位)?$/.test(order.trim())) invalid();
  27. return { candidate, scores, basis, order, unknown, low,
  28. status: low ? '不进入前三' : unknown ? '未知项待补' : '可参与排序' };
  29. });
  30. }
  31. export function validateCourseAssessment(report) {
  32. for (const chapter of report?.chapters || []) for (const section of chapter.sections || []) courseAssessmentRows(section);
  33. }