stationList作成関数の分離化

This commit is contained in:
harukin-DeskMini 2022-10-08 23:41:23 +09:00
parent 10b37d61db
commit 7b2b2eb630
2 changed files with 170 additions and 153 deletions

167
lib/getStationList.js Normal file
View File

@ -0,0 +1,167 @@
import 予讃線 from "../assets/四国旅客鉄道予讃線.json";
import 土讃線 from "../assets/四国旅客鉄道土讃線.json";
import 高徳線 from "../assets/四国旅客鉄道高徳線.json";
import 内子線 from "../assets/四国旅客鉄道内子線.json";
import 徳島線 from "../assets/四国旅客鉄道徳島線.json";
import 鳴門線 from "../assets/四国旅客鉄道鳴門線.json";
let status = undefined;
export const getStationList = async (props) => {
if (status) return status;
//駅リストイニシャライズ
const HeaderConfig = {
headers: {
referer: "https://train.jr-shikoku.co.jp/sp.html",
},
};
return await Promise.all([
fetch(
"https://train.jr-shikoku.co.jp/g?arg1=station&arg2=yosan",
HeaderConfig
).then((response) => response.json()),
fetch(
"https://train.jr-shikoku.co.jp/g?arg1=station&arg2=uwajima",
HeaderConfig
).then((response) => response.json()),
fetch(
"https://train.jr-shikoku.co.jp/g?arg1=station&arg2=uwajima2",
HeaderConfig
).then((response) => response.json()),
fetch(
"https://train.jr-shikoku.co.jp/g?arg1=station&arg2=dosan",
HeaderConfig
).then((response) => response.json()),
fetch(
"https://train.jr-shikoku.co.jp/g?arg1=station&arg2=dosan2",
HeaderConfig
).then((response) => response.json()),
fetch(
"https://train.jr-shikoku.co.jp/g?arg1=station&arg2=koutoku",
HeaderConfig
).then((response) => response.json()),
fetch(
"https://train.jr-shikoku.co.jp/g?arg1=station&arg2=tokushima",
HeaderConfig
).then((response) => response.json()),
fetch(
"https://train.jr-shikoku.co.jp/g?arg1=station&arg2=naruto",
HeaderConfig
).then((response) => response.json()),
fetch(
"https://train.jr-shikoku.co.jp/g?arg1=station&arg2=between",
HeaderConfig
).then((response) => response.json()),
fetch(
"https://train.jr-shikoku.co.jp/g?arg1=line&arg2=train_lang",
HeaderConfig
).then((response) => response.json()),
]).then((values) => {
let stationList = {};
[
stationList.予讃線,
stationList.松宇線,
stationList.伊予灘線,
stationList.土讃線,
stationList.窪川線,
stationList.高徳線,
stationList.徳島線,
stationList.鳴門線,
stationList.駅間リスト,
stationList.日英対応表,
] = values;
const concatBetweenStations = (eachRouteData) => {
let additional = [];
eachRouteData.forEach((routeData, routeIndex) => {
try {
const currentStationID = parseInt(
routeData.StationNumber.replace(/[A-Z]/g, "")
);
const nextStationID = parseInt(
eachRouteData[routeIndex + 1].StationNumber.replace(/[A-Z]/g, "")
);
if (nextStationID - currentStationID != 1) {
stationList.駅間リスト.forEach((betweenList) => {
if (
betweenList.BetweenStation ==
routeData.Station_JP +
"" +
eachRouteData[routeIndex + 1].Station_JP
) {
additional = additional.concat(betweenList.Datas);
}
});
}
} catch (e) {}
});
return eachRouteData
.concat(additional)
.sort((a, b) => (a.StationNumber > b.StationNumber ? 1 : -1));
};
const addStationPosition = (setDataBase, geoJson, EnJpList) => {
return setDataBase.map((data) => {
let stationName;
if (data.hasOwnProperty("Station_JP")) stationName = data.Station_JP;
else if (data.hasOwnProperty("StationName")) {
stationName = data.StationName;
data.Station_JP = data.StationName;
data.Station_EN = EnJpList.find(
(d) => d.Station_JP == data.Station_JP
).Station_EN;
}
geoJson.features
.filter((d) => d.geometry.type == "Point")
.forEach((element) => {
if (element.properties.name == stationName) {
data.lat = element.geometry.coordinates[1];
data.lng = element.geometry.coordinates[0];
}
});
return data;
});
};
console.log(stationList.予讃線);
stationList.予讃線 = addStationPosition(
concatBetweenStations(stationList.予讃線),
予讃線,
stationList.日英対応表
);
stationList.松宇線 = addStationPosition(
concatBetweenStations(stationList.松宇線),
予讃線,
stationList.日英対応表
);
stationList.伊予灘線 = addStationPosition(
concatBetweenStations(stationList.伊予灘線),
予讃線,
stationList.日英対応表
);
stationList.土讃線 = addStationPosition(
concatBetweenStations(stationList.土讃線),
土讃線,
stationList.日英対応表
);
stationList.窪川線 = addStationPosition(
concatBetweenStations(stationList.窪川線),
土讃線,
stationList.日英対応表
);
stationList.高徳線 = addStationPosition(
concatBetweenStations(stationList.高徳線),
高徳線,
stationList.日英対応表
);
stationList.徳島線 = addStationPosition(
concatBetweenStations(stationList.徳島線),
徳島線,
stationList.日英対応表
);
stationList.鳴門線 = addStationPosition(
concatBetweenStations(stationList.鳴門線),
鳴門線,
stationList.日英対応表
);
status = stationList;
return stationList;
});
};

156
menu.js
View File

@ -47,6 +47,7 @@ import Sign from "./components/駅名表/Sign";
import { UsefulBox } from "./components/atom/UsefulBox"; import { UsefulBox } from "./components/atom/UsefulBox";
import { TicketBox } from "./components/atom/TicketBox"; import { TicketBox } from "./components/atom/TicketBox";
import { TextBox } from "./components/atom/TextBox"; import { TextBox } from "./components/atom/TextBox";
import { getStationList } from "./lib/getStationList";
export default function Menu(props) { export default function Menu(props) {
const { const {
@ -78,159 +79,8 @@ export default function Menu(props) {
const [originalStationList, setOriginalStationList] = useState(); const [originalStationList, setOriginalStationList] = useState();
useEffect(() => { useEffect(() => {
//駅リストイニシャライズ getStationList().then((stationList) => {
const HeaderConfig = { console.log(stationList);
headers: {
referer: "https://train.jr-shikoku.co.jp/sp.html",
},
};
Promise.all([
fetch(
"https://train.jr-shikoku.co.jp/g?arg1=station&arg2=yosan",
HeaderConfig
).then((response) => response.json()),
fetch(
"https://train.jr-shikoku.co.jp/g?arg1=station&arg2=uwajima",
HeaderConfig
).then((response) => response.json()),
fetch(
"https://train.jr-shikoku.co.jp/g?arg1=station&arg2=uwajima2",
HeaderConfig
).then((response) => response.json()),
fetch(
"https://train.jr-shikoku.co.jp/g?arg1=station&arg2=dosan",
HeaderConfig
).then((response) => response.json()),
fetch(
"https://train.jr-shikoku.co.jp/g?arg1=station&arg2=dosan2",
HeaderConfig
).then((response) => response.json()),
fetch(
"https://train.jr-shikoku.co.jp/g?arg1=station&arg2=koutoku",
HeaderConfig
).then((response) => response.json()),
fetch(
"https://train.jr-shikoku.co.jp/g?arg1=station&arg2=tokushima",
HeaderConfig
).then((response) => response.json()),
fetch(
"https://train.jr-shikoku.co.jp/g?arg1=station&arg2=naruto",
HeaderConfig
).then((response) => response.json()),
fetch(
"https://train.jr-shikoku.co.jp/g?arg1=station&arg2=between",
HeaderConfig
).then((response) => response.json()),
fetch(
"https://train.jr-shikoku.co.jp/g?arg1=line&arg2=train_lang",
HeaderConfig
).then((response) => response.json()),
]).then((values) => {
let stationList = {};
[
stationList.予讃線,
stationList.松宇線,
stationList.伊予灘線,
stationList.土讃線,
stationList.窪川線,
stationList.高徳線,
stationList.徳島線,
stationList.鳴門線,
stationList.駅間リスト,
stationList.日英対応表,
] = values;
const concatBetweenStations = (eachRouteData) => {
let additional = [];
eachRouteData.forEach((routeData, routeIndex) => {
try {
const currentStationID = parseInt(
routeData.StationNumber.replace(/[A-Z]/g, "")
);
const nextStationID = parseInt(
eachRouteData[routeIndex + 1].StationNumber.replace(/[A-Z]/g, "")
);
if (nextStationID - currentStationID != 1) {
stationList.駅間リスト.forEach((betweenList) => {
if (
betweenList.BetweenStation ==
routeData.Station_JP +
"" +
eachRouteData[routeIndex + 1].Station_JP
) {
additional = additional.concat(betweenList.Datas);
}
});
}
} catch (e) {}
});
return eachRouteData
.concat(additional)
.sort((a, b) => (a.StationNumber > b.StationNumber ? 1 : -1));
};
const addStationPosition = (setDataBase, geoJson, EnJpList) => {
return setDataBase.map((data) => {
let stationName;
if (data.hasOwnProperty("Station_JP")) stationName = data.Station_JP;
else if (data.hasOwnProperty("StationName")) {
stationName = data.StationName;
data.Station_JP = data.StationName;
data.Station_EN = EnJpList.find(
(d) => d.Station_JP == data.Station_JP
).Station_EN;
}
geoJson.features
.filter((d) => d.geometry.type == "Point")
.forEach((element) => {
if (element.properties.name == stationName) {
data.lat = element.geometry.coordinates[1];
data.lng = element.geometry.coordinates[0];
}
});
return data;
});
};
stationList.予讃線 = addStationPosition(
concatBetweenStations(stationList.予讃線),
予讃線,
stationList.日英対応表
);
stationList.松宇線 = addStationPosition(
concatBetweenStations(stationList.松宇線),
予讃線,
stationList.日英対応表
);
stationList.伊予灘線 = addStationPosition(
concatBetweenStations(stationList.伊予灘線),
予讃線,
stationList.日英対応表
);
stationList.土讃線 = addStationPosition(
concatBetweenStations(stationList.土讃線),
土讃線,
stationList.日英対応表
);
stationList.窪川線 = addStationPosition(
concatBetweenStations(stationList.窪川線),
土讃線,
stationList.日英対応表
);
stationList.高徳線 = addStationPosition(
concatBetweenStations(stationList.高徳線),
高徳線,
stationList.日英対応表
);
stationList.徳島線 = addStationPosition(
concatBetweenStations(stationList.徳島線),
徳島線,
stationList.日英対応表
);
stationList.鳴門線 = addStationPosition(
concatBetweenStations(stationList.鳴門線),
鳴門線,
stationList.日英対応表
);
setOriginalStationList(stationList); setOriginalStationList(stationList);
}); });
}, []); }, []);