-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a5fb32
commit 3a8d994
Showing
5 changed files
with
48 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,49 @@ | ||
import { LineCode, StopCode, lines } from 'mtr-kit' | ||
import { LineCode, StopCode, lineMap, lines } from 'mtr-kit' | ||
|
||
import { scheduleMap } from '../worker.js' | ||
import { Schedule, scheduleMap } from '../worker.js' | ||
|
||
const getStopSchedules = (line: LineCode, stop: StopCode) => | ||
const getLineStopSchedule = (line: LineCode, stop: StopCode) => | ||
scheduleMap.get(`${line}-${stop}`) | ||
|
||
const getLineSchedule = (code: LineCode) => { | ||
const stops = lines.find(item => item.code === code)?.stops || [] | ||
return stops | ||
const getStopSchedules = (stop: StopCode): ({ line: LineCode } & Schedule)[] => | ||
lines | ||
.filter(line => line.stops.includes(stop)) | ||
.map(line => { | ||
const schedule = getLineStopSchedule(line.code, stop) | ||
return schedule | ||
? { | ||
line: line.code, | ||
...schedule, | ||
} | ||
: null | ||
}) | ||
.filter((v): v is NonNullable<typeof v> => Boolean(v)) | ||
|
||
const getLineSchedule = (code: LineCode): ({ stop: StopCode } & Schedule)[] => | ||
lineMap[code].stops | ||
.map(stop => { | ||
const schedule = getStopSchedules(code, stop.code) | ||
const schedule = getLineStopSchedule(code, stop) | ||
return schedule | ||
? { | ||
code: stop.code, | ||
stop, | ||
...schedule, | ||
} | ||
: null | ||
}) | ||
.filter((v): v is NonNullable<typeof v> => Boolean(v)) | ||
} | ||
|
||
const getSchedule = async () => | ||
lines.map(line => { | ||
const schedule = getLineSchedule(line.code) | ||
return { code: line.code, stops: schedule } | ||
}) | ||
lines.reduce<({ line: LineCode; stop: StopCode } & Schedule)[]>( | ||
(acc, line) => [ | ||
...acc, | ||
...getLineSchedule(line.code).map(item => ({ line: line.code, ...item })), | ||
], | ||
[] | ||
) | ||
|
||
export const scheduleService = { | ||
getLineSchedule, | ||
getSchedule, | ||
getLineStopSchedule, | ||
getStopSchedules, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters