timeFilteringをリファクタリング

This commit is contained in:
harukin-expo-dev-env
2025-09-13 14:02:06 +00:00
parent a8cf24e745
commit 60e1dcd1a5

View File

@@ -8,7 +8,7 @@ type trainDataProps = {
}; };
export const trainTimeFiltering: (x: trainDataProps) => boolean = (props) => { export const trainTimeFiltering: (x: trainDataProps) => boolean = (props) => {
const { d, currentTrain, station } = props; const { d, currentTrain, station } = props;
const baseTime = 2; const baseTime = 2; // 何時間以内の列車を表示するか
if (currentTrain.filter((t) => t.num == d.train).length == 0) { if (currentTrain.filter((t) => t.num == d.train).length == 0) {
const date = dayjs(); const date = dayjs();
const trainTime = date const trainTime = date
@@ -23,36 +23,35 @@ export const trainTimeFiltering: (x: trainDataProps) => boolean = (props) => {
return false; return false;
} else { } else {
const Pos = trainPosition(currentTrain.filter((t) => t.num == d.train)[0]); const Pos = trainPosition(currentTrain.filter((t) => t.num == d.train)[0]);
const nextPos = Pos.isBetween ? Pos.Pos.to : Pos.Pos.Pos; let nextPos = "";
const PrePos = Pos.isBetween ? Pos.Pos.from : ""; let PrePos = "";
if (station[0].Station_JP == nextPos) { //
if (d.lastStation != station[0].Station_JP) return true; if (Pos.isBetween == true) {
} else if (station[0].Station_JP == PrePos) { nextPos = Pos.Pos.to;
return false; PrePos = Pos.Pos.from;
} else {
nextPos = Pos.Pos.Pos;
} }
let [h, m] = d.time.split(":"); const stationData = station[0].Station_JP;
let delay = isNaN(currentTrain.filter((t) => t.num == d.train)[0].delay) switch (stationData) {
? 0 case nextPos:
: currentTrain.filter((t) => t.num == d.train)[0].delay; if (d.lastStation != stationData) return true;
break;
case PrePos:
return false;
default:
break;
}
const [h, m] = d.time.split(":");
const delayData = currentTrain.filter((t) => t.num == d.train)[0].delay;
let delay = delayData === "入線" ? 0 : delayData;
const date = dayjs(); const date = dayjs();
let targetHour = parseInt(h); const IntH = parseInt(h) < 4 ? parseInt(h) + 24 : parseInt(h);
const IntM = parseInt(m);
// 4時を日付変更線として処理 const targetDate = date.hour(IntH).minute(IntM + delay);
if (targetHour < 4) { if (date.isAfter(targetDate)) return false;
targetHour += 24; if (targetDate.diff(date) < baseTime * 60 * 60 * 1000) return true;
} return false;
let currentHour = date.hour();
if (currentHour < 4) {
currentHour += 24;
}
const db = dayjs()
.hour(targetHour)
.minute(parseInt(m) + parseInt(delay));
const currentTime = dayjs().hour(currentHour);
return !currentTime.isAfter(db);
} }
}; };
type getTimeProps = ( type getTimeProps = (