Skip to content

Commit

Permalink
fix: merge daily data and original data properly
Browse files Browse the repository at this point in the history
  • Loading branch information
star0202 committed Jul 16, 2024
1 parent 348189e commit 1b57aad
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
35 changes: 18 additions & 17 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import School from './models/School'
import type { Timetable } from './models/Timetable'
import { encodeBase64, encodeEUCKR } from './utils/encode'
import { parseResponse } from './utils/parse'
import { mergeMap } from './utils/array'

export default class Comcigan {
private readonly rest = axios.create({
Expand Down Expand Up @@ -56,29 +57,29 @@ export default class Comcigan {
const teachersLen = Math.floor(Math.log10(teachers.length - 1)) + 1
const subjects = data[`자료${subjectCode}`] as string[]

const original = data[`자료${originalCode}`] as (number | undefined)[][][][]
const day = data[`자료${dayCode}`] as number[][][][]
const original = data[`자료${originalCode}`] as number[][][][]
const now = data[`자료${dayCode}`] as number[][][][]

const getSubject = (code: number) =>
subjects[Number(code.toString().slice(0, -teachersLen - 1))]
const getTeacher = (code: number) =>
teachers[Number(code.toString().slice(-teachersLen))]

return day.slice(1).map((grade, gIdx) =>
grade.slice(1).map((cls, cIdx) =>
cls.slice(1).map((day, dIdx) =>
day.slice(1).map((period, pIdx) => {
const origin = original[gIdx + 1][cIdx + 1][dIdx + 1][pIdx + 1]
const changed = period !== origin
const getSubject = (code?: number) =>
code
? subjects[Number(code.toString().slice(0, -teachersLen - 1))]
: '없음'
const getTeacher = (code?: number) =>
code ? teachers[Number(code.toString().slice(-teachersLen))] : '없음'

return mergeMap(now.slice(1), original.slice(1), (gNow, gOrigin) =>
mergeMap(gNow.slice(1), gOrigin.slice(1), (cNow, cOrigin) =>
mergeMap(cNow.slice(1), cOrigin.slice(1), (dNow, dOrigin) =>
mergeMap(dNow.slice(1), dOrigin.slice(1), (pNow, pOrigin) => {
const changed = pNow !== pOrigin
return {
subject: getSubject(period),
teacher: getTeacher(period),
subject: getSubject(pNow),
teacher: getTeacher(pNow),
changed,
...(changed
? {
originalSubject: origin ? getSubject(origin) : '없음',
originalTeacher: origin ? getTeacher(origin) : '없음',
originalSubject: getSubject(pOrigin),
originalTeacher: getTeacher(pOrigin),
}
: {}),
} as Timetable
Expand Down
11 changes: 11 additions & 0 deletions src/utils/array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const mergeMap = <T, F>(
a: T[],
b: T[],
callbackFn: (a: T, b: T) => F,
): F[] => {
const result = []
for (let i = 0; i < Math.max(a.length, b.length); i++)
result.push(callbackFn(a[i], b[i]))

return result
}

0 comments on commit 1b57aad

Please sign in to comment.