import { Num } from '@/helper/util' ; import { CourseScoreRecord } from './types' ; param { number } num param { number } fractionDigits returns function reserveDigits ( num , fractionDigits = 3 ) { return Num ( num . toFixed ( fractionDigits ) ) ; } param { Array < { value : number ; weight : number } > } arr returns function getWeightedAverage ( arr ) { return arr . reduce ( ( acc , cur ) => [ acc [ 0 ] + cur . value * cur . weight , acc [ 1 ] + cur . weight ] , [ 0 , 0 ] ) . reduce ( ( valueSum , weightSum ) => ( weightSum ? valueSum / weightSum : 0 ) ) ; } param { CourseScoreRecord [ ] } arr returns function mapGPA ( arr ) { return arr . map ( ( v ) => ( { value : v . gradePoint || 0 , weight : v . credit } ) ) ; } param { CourseScoreRecord [ ] } arr returns function mapScore ( arr ) { return arr . map ( ( v ) => ( { value : v . courseScore || 0 , weight : v . credit } ) ) ; } param { CourseScoreRecord [ ] } arr returns function removeIllegalScoreCourses ( arr ) { return arr . filter ( ( v ) => v . courseScore && v . courseScore > 0 ) ; } param { CourseScoreRecord [ ] } arr returns export function removeMinorCourses ( arr ) { return arr . filter ( ( v ) => v . coursePropertyName !== '辅修' ) ; } param { CourseScoreRecord [ ] } arr returns export function reserveMinorCourses ( arr ) { return arr . filter ( ( v ) => v . coursePropertyName === '辅修' ) ; } export function getTotalCourseCredits ( courses ) { return courses . reduce ( ( acc , cur ) => acc + cur . credit , 0 ) ; } param { CourseScoreRecord [ ] } arr returns function getCompulsoryCourses ( arr ) { return arr . filter ( ( v ) => v . coursePropertyName === '必修' ) ; } param { CourseScoreRecord [ ] } arr returns function getSelectedCourses ( arr ) { return arr . filter ( ( v ) => v . selected ) ; } param { CourseScoreRecord [ ] } arr returns function getAllCoursesGPA ( arr ) { return reserveDigits ( getWeightedAverage ( mapGPA ( removeIllegalScoreCourses ( arr ) ) ) ) ; } param { CourseScoreRecord [ ] } arr returns function getAllCoursesScore ( arr ) { return reserveDigits ( getWeightedAverage ( mapScore ( removeIllegalScoreCourses ( arr ) ) ) ) ; } param { CourseScoreRecord [ ] } arr returns { CourseScoreRecord [ ] } export function reserveHigherCoursesForRetakenCourses ( arr ) { return Object . values ( Object . entries ( arr ) . sort ( ( a , b ) => Num ( a [ 1 ] . courseScore ) - Num ( b [ 1 ] . courseScore ) ) . reduceRight ( ( acc , cur ) => acc [ cur [ 1 ] . courseNumber ] ? acc : { ... acc , [ cur [ 1 ] . courseNumber ] : cur } , { } as Record < string , [ string , CourseScoreRecord ] > ) ) . sort ( ( a , b ) => Num ( a [ 0 ] ) - Num ( b [ 0 ] ) ) . map ( ( v ) => v [ 1 ] ) ; } param { CourseScoreRecord [ ] } arr returns { CourseScoreRecord [ ] } function reserveNewerCoursesForRetakenCourses ( arr ) { return Object . values ( arr . sort ( ( a , b ) => Num ( a . examTime ) - Num ( b . examTime ) ) . reduceRight ( ( acc , cur ) => acc [ cur . courseNumber ] ? acc : { ... acc , [ cur . courseNumber ] : cur } , { } as Record < string , CourseScoreRecord > ) ) ; } param { CourseScoreRecord [ ] } arr returns { CourseScoreRecord [ ] } function reserveOlderCoursesForRetakenCourses ( arr ) { return Object . values ( arr . sort ( ( a , b ) => Num ( a . examTime ) - Num ( b . examTime ) ) . reduce ( ( acc , cur ) => acc [ cur . courseNumber ] ? acc : { ... acc , [ cur . courseNumber ] : cur } , { } as Record < string , CourseScoreRecord > ) ) ; } param { number } score param { string } semester returns function getPointByScore ( score , semester ) { if ( ! score ) { return undefined ; } const r = semester . match ( /^\d+/ ) ; if ( ! r ) { return undefined ; } const enrollmentYear = Num ( r [ 0 ] ) ; if ( enrollmentYear >= 2017 ) { if ( score >= 90 ) { return 4 ; } else if ( score >= 85 ) { return 3.7 ; } else if ( score >= 80 ) { return 3.3 ; } else if ( score >= 76 ) { return 3 ; } else if ( score >= 73 ) { return 2.7 ; } else if ( score >= 70 ) { return 2.3 ; } else if ( score >= 66 ) { return 2 ; } else if ( score >= 63 ) { return 1.7 ; } else if ( score >= 61 ) { return 1.3 ; } else if ( score >= 60 ) { return 1 ; } else { return 0 ; } } else { if ( score >= 95 ) { return 4 ; } else if ( score >= 90 ) { return 3.8 ; } else if ( score >= 85 ) { return 3.6 ; } else if ( score >= 80 ) { return 3.2 ; } else if ( score >= 75 ) { return 2.7 ; } else if ( score >= 70 ) { return 2.2 ; } else if ( score >= 65 ) { return 1.7 ; } else if ( score >= 60 ) { return 1 ; } else { return 0 ; } } } type LevelName = 'A' | 'A-' | 'B+' | 'B' | 'B-' | 'C+' | 'C' | 'C-' | 'D+' | 'D' | 'F' ; param { number } score param { string } semester returns { LevelName | undefined } export function getLevelNameByScore ( score , semester ) { if ( ! score ) { return undefined ; } const r = semester . match ( /^\d+/ ) ; if ( ! r ) { return undefined ; } const enrollmentYear = Num ( r [ 0 ] ) ; if ( enrollmentYear >= 2017 ) { if ( score >= 90 ) { return 'A' ; } else if ( score >= 85 ) { return 'A-' ; } else if ( score >= 80 ) { return 'B+' ; } else if ( score >= 76 ) { return 'B' ; } else if ( score >= 73 ) { return 'B-' ; } else if ( score >= 70 ) { return 'C+' ; } else if ( score >= 66 ) { return 'C' ; } else if ( score >= 63 ) { return 'C-' ; } else if ( score >= 61 ) { return 'D+' ; } else if ( score >= 60 ) { return 'D' ; } else { return 'F' ; } } else { return undefined ; } } type LevelCode = '-20' | '-21' | '-22' | '-23' | '-24' | '-25' | '-26' | '-27' | '-28' | '-29' | '-30' ; param { number } score param { string } semester returns { LevelName | undefined } export function getLevelCodeByScore ( score , semester ) { if ( ! score ) { return undefined ; } const r = semester . match ( /^\d+/ ) ; if ( ! r ) { return undefined ; } const enrollmentYear = Num ( r [ 0 ] ) ; if ( enrollmentYear >= 2017 ) { if ( score >= 90 ) { return '-20' ; } else if ( score >= 85 ) { return '-21' ; } else if ( score >= 80 ) { return '-22' ; } else if ( score >= 76 ) { return '-23' ; } else if ( score >= 73 ) { return '-24' ; } else if ( score >= 70 ) { return '-25' ; } else if ( score >= 66 ) { return '-26' ; } else if ( score >= 63 ) { return '-27' ; } else if ( score >= 61 ) { return '-28' ; } else if ( score >= 60 ) { return '-29' ; } else { return '-30' ; } } else { return undefined ; } } export { getAllCoursesGPA , getAllCoursesScore , getCompulsoryCourses , getSelectedCourses , getPointByScore , reserveNewerCoursesForRetakenCourses , reserveOlderCoursesForRetakenCourses } ;