export type COURSE_TYPE = { name : string ; title : string ; chapters : number ; } ; export const _courses = [ { name : "Ocean 101" , title : "ocean101" , chapters : 24 } , { name : "Intro to Data Defi" , title : "introToDataDefi" , chapters : 6 } , { name : "Compute To Data" , title : "ComputeToData" , chapters : 7 } ] ; export const COURSES = { OCEAN_101 : _courses [ 0 ] , INTRO_TO_DATA_DEFI : _courses [ 1 ] , COMPUTE_TO_DATA : _courses [ 2 ] } ; export const coursesTitles = _courses . map ( x => x . title ) ; param course param progress returns export const getNbProgressForCourse = ( course , progress ) : number => getProgressForCourse ( course , progress ) . length ; param course param progress returns export const getProgressForCourse = ( course , progress ) : string [ ] => progress . filter ( chapter => chapter . split ( "/" ) [ 1 ] === course . title ) ; param progress param totalChapters returns export const getRemainingChaters = ( course , progress ) : number [ ] => { const completedChapters = progress . map ( x => parseInt ( x . split ( "/" ) [ 2 ] . split ( "-" ) [ 1 ] ) ) . sort ( ( a , b ) => b - a ) ; const allChapters = [ ] ; for ( let i = 1 ; i <= course . chapters ; i ++ ) { allChapters . push ( i ) ; } return allChapters . filter ( chapter => ! completedChapters . includes ( chapter ) ) ; } ; export const isCourseCompletedFromTitle = ( courseTitle , progress ) : boolean => { const course = getCourseByTitle ( courseTitle ) ; if ( course ) return isCourseCompleted ( course , progress ) ; else throw Error ( `Course with title ${ courseTitle } not found.` ) ; } ; export const isCourseCompleted = ( course , progress ) : boolean => { return getNbProgressForCourse ( course , progress ) >= course . chapters ; } ; export const getCourseByTitle = ( courseTitle ) : COURSE_TYPE | undefined => _courses . find ( course => course . title === courseTitle ) ; export const courseTitleExists = ( courseTitle ) : boolean => getCourseByTitle ( courseTitle ) ? true : false ; export const getCourseFromIdx = ( courseIdx ) : COURSE_TYPE => _courses [ courseIdx - 1 ] ; export const toTokenId = ( userId , course ) : string => { const courseIndex = _courses . indexOf ( course ) + 1 ; const courseIndexPadded = courseIndex . toString ( ) . padStart ( 3 , '0' ) ; const userIdPadded = userId . toString ( ) . padStart ( 8 , '0' ) ; return courseIndexPadded + userIdPadded ; } ;