Merge branch 'trunk' of github.com:UniversityOfHelsinkiCS/oodikone into trunk

This commit is contained in:
Matti Luukkainen
2021-03-12 01:13:10 +02:00
2 changed files with 74 additions and 0 deletions
@@ -0,0 +1,50 @@
// Idea here is to find exactly one matching course or fail otherwise.
const { mayhemifiedDatesMatch } = require('../utils')
const filterByDate = (date, courses) => courses.filter(course => mayhemifiedDatesMatch(course.date, date))
const filterByCode = (code, courses) => courses.filter(({ course }) => course.code === code)
const filterByCredits = (credits, courses) => courses.filter(course => course.credits === credits)
const coursesMatch = (a, b) =>
a.course.code === b.course.code && a.credits === b.credits && mayhemifiedDatesMatch(a.date, b.date)
const matchExactlyOneCourse = (courseToPair, courses) => {
const { code } = courseToPair.course
const codeMatches = filterByCode(code, courses)
if (codeMatches.length === 0) {
console.log('ERROR! Could not match courses.')
throw new Error()
}
if (codeMatches.length === 1 && coursesMatch(courseToPair, codeMatches[0])) {
return codeMatches[0]
}
// Need to filter by date.
const { date, credits } = courseToPair
const dateMatches = filterByDate(date, codeMatches)
if (dateMatches.length === 0) {
console.log('ERROR! Could not match courses.')
throw new Error()
}
if (dateMatches.length === 1 && coursesMatch(courseToPair, dateMatches[0])) {
return dateMatches[0]
}
// Need to filter by credit.
const creditsMatches = filterByCredits(credits, dateMatches)
if (creditsMatches.length !== 1) {
console.log('ERROR! Could not match courses.')
throw new Error()
}
return creditsMatches[0]
}
module.exports = matchExactlyOneCourse
@@ -0,0 +1,24 @@
const moment = require('moment')
// Because seriously omfg
const mayhemifiedDatesMatch = (a, b) => {
// Figure out which one is from oodi.
let oodi, sis
if (moment(a).hour() === 0) {
sis = a
oodi = b
} else {
oodi = a
sis = b
}
// Then figure out if it's DST and fix the "timezone".
const dst = moment(oodi).hour() === 21
const tzFix = dst ? 3 : 2
const oodiDate = moment(oodi).add(tzFix, 'hours')
return oodiDate.isSame(sis)
}
module.exports = {
mayhemifiedDatesMatch
}