From 039dff5d8f78cc46d00cc99d569e7e124001240a Mon Sep 17 00:00:00 2001 From: harukin-OneMix4 Date: Wed, 14 Feb 2024 00:48:34 +0900 Subject: [PATCH] =?UTF-8?q?=E4=B8=80=E9=83=A8function=E3=81=AE=E5=88=86?= =?UTF-8?q?=E5=B2=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EachTrainInfoCore.js | 266 +++--------------- .../findReversalPoints.js | 113 ++++++++ lib/eachTrainInfoCoreLib/getStationData.js | 15 + lib/eachTrainInfoCoreLib/getType.js | 11 + lib/eachTrainInfoCoreLib/migrateTrainName.js | 7 + lib/eachTrainInfoCoreLib/openBackTrainInfo.js | 68 +++++ .../searchSpecialTrain.js | 13 + 7 files changed, 265 insertions(+), 228 deletions(-) create mode 100644 lib/eachTrainInfoCoreLib/findReversalPoints.js create mode 100644 lib/eachTrainInfoCoreLib/getStationData.js create mode 100644 lib/eachTrainInfoCoreLib/getType.js create mode 100644 lib/eachTrainInfoCoreLib/migrateTrainName.js create mode 100644 lib/eachTrainInfoCoreLib/openBackTrainInfo.js create mode 100644 lib/eachTrainInfoCoreLib/searchSpecialTrain.js diff --git a/components/ActionSheetComponents/EachTrainInfoCore.js b/components/ActionSheetComponents/EachTrainInfoCore.js index 6971081..13ae3b2 100644 --- a/components/ActionSheetComponents/EachTrainInfoCore.js +++ b/components/ActionSheetComponents/EachTrainInfoCore.js @@ -29,6 +29,13 @@ import { LongHeader } from "./EachTrainInfo/LongHeader"; import { ShortHeader } from "./EachTrainInfo/ShortHeader"; import { ScrollStickyContent } from "./EachTrainInfo/ScrollStickyContent"; import { LandscapeTrainInfo } from "./EachTrainInfo/LandscapeTrainInfo"; +import { getStationData } from "../../lib/eachTrainInfoCoreLib/getStationData"; +import { findReversalPoints } from "../../lib/eachTrainInfoCoreLib/findReversalPoints"; +import { migrateTrainName } from "../../lib/eachTrainInfoCoreLib/migrateTrainName"; +import { getType } from "../../lib/eachTrainInfoCoreLib/getType"; +import { searchSpecialTrain } from "../../lib/eachTrainInfoCoreLib/searchSpecialTrain"; +import { openBackTrainInfo } from "../../lib/eachTrainInfoCoreLib/openBackTrainInfo"; + export const EachTrainInfoCore = ({ actionSheetRef, data, @@ -74,18 +81,6 @@ export const EachTrainInfoCore = ({ })) ); - const getStationData = (stationName) => { - const Stations = stationList.map((a) => - a.filter((d) => d.StationName == stationName) - ); - const Station = - Stations && - Stations.reduce((newArray, e) => { - return newArray.concat(e); - }, []); - if (!Station[0]) return []; - return Station.map((d) => d.StationNumber)[0]; - }; const stopStationIDList = trainData.map((i, index) => { const [station, se, time] = i.split(","); const Stations = stationList.map((a) => @@ -100,119 +95,6 @@ export const EachTrainInfoCore = ({ .map((d) => d.StationNumber); return StationNumbers[0]; }); - function findReversalPoints(array) { - try { - // arrayは現在位置の駅ID(駅在宅の場合は1つの配列、駅間の場合は2つの配列) - // stopStationIDListは停車駅の駅IDの配列 - if (!stopStationIDList.length) return []; - // arrayが二次元配列だったら早期リターン - if (!array instanceof Array) return []; - if (!array.length) return []; - if (array[0] instanceof Array) return []; - const arrayNumber = array.map((d) => ({ - line: d - .split("") - .filter((s) => "A" < s && s < "Z") - .join(""), - ID: d - .split("") - .filter((s) => "0" <= s && s <= "9") - .join(""), - })); - const stopStationIDListNumber = stopStationIDList.map((d) => { - if (!d) return { line: [], ID: [] }; - return { - line: d - .split("") - .filter((s) => "A" < s && s < "Z") - .join(""), - ID: d - .split("") - .filter((s) => "0" <= s && s <= "9") - .join(""), - }; - }); - // 完全一致 - if (array.length == 1) { - const index = stopStationIDList.indexOf(array[0]); - if (index != -1) return [index]; - // 通過駅の場合 - for (let i = 0; i < stopStationIDListNumber.length - 1; i++) { - if (stopStationIDListNumber[i].ID < arrayNumber[0].ID) { - if (stopStationIDListNumber[i + 1].ID > arrayNumber[0].ID) { - return [i + 1]; - } - } - if (stopStationIDListNumber[i].ID > arrayNumber[0].ID) { - if (stopStationIDListNumber[i + 1].ID < arrayNumber[0].ID) { - return [i + 1]; - } - } - } - } - // 駅間の場合 - if (array.length == 2) { - const index1 = stopStationIDList.indexOf(array[0]); - const index2 = stopStationIDList.indexOf(array[1]); - if (index1 != -1 && index2 != -1) { - // 駅間で通過駅も無い場合 - if (index1 < index2) { - if (index1 + 1 == index2) { - return [index2]; - } else { - const returnArray = []; - for (let i = index1 + 1; i <= index2; i++) { - returnArray.push(i); - } - return returnArray; - } - } - if (index1 > index2) { - if (index2 + 1 == index1) return [index1]; - else { - const returnArray = []; - for (let i = index2 + 1; i <= index1; i++) { - returnArray.push(i); - } - return returnArray; - } - } - } else { - const getNearStationID = (stationID) => { - for (let i = 0; i <= stopStationIDListNumber.length; i++) { - if (stopStationIDListNumber[i].ID < stationID) { - if (stopStationIDListNumber[i + 1].ID > stationID) { - return i + 1; - } - } - if (stopStationIDListNumber[i].ID > stationID) { - if (stopStationIDListNumber[i + 1].ID < stationID) { - return i + 1; - } - } - } - }; - let newIndex1 = index1; - let newIndex2 = index2; - if (index1 == -1) { - newIndex1 = getNearStationID(arrayNumber[0].ID); - } - if (index2 == -1) { - newIndex2 = getNearStationID(arrayNumber[1].ID); - } - if (newIndex1 && newIndex2) { - return [newIndex1, newIndex2]; - } - - // 通過駅の場合 - } - - return []; - } - } catch (e) { - console.log(e); - } - } useEffect(() => { if (trainData.length == 0) return; if (showNearTrain.length == 0) return; @@ -235,25 +117,12 @@ export const EachTrainInfoCore = ({ const scrollHandlers = actionSheetRef ? useScrollHandlers("scrollview-1", actionSheetRef) : null; - const migrateTrainName = (string) => { - return string - .replace("マリン", "マリンライナー") - .replace("ライナーライナー", "ライナー"); - }; - const getType = (string) => { - switch (string) { - case "express": - return "特急"; - case "rapid": - return "快速"; - default: - return ""; - } - }; // 使用例 const points = - trainPositionSwitch == "true" ? findReversalPoints(currentPosition) : []; + trainPositionSwitch == "true" + ? findReversalPoints(currentPosition, stopStationIDList) + : []; const { height, width } = useWindowDimensions(); const [isLandscape, setIsLandscape] = useState(false); useEffect(() => { @@ -264,9 +133,12 @@ export const EachTrainInfoCore = ({ const pos = currentTrainData?.Pos.replace("(下り)", "") .replace("(上り)", "") .split("~"); - setCurrentPosition([getStationData(pos[0]), getStationData(pos[1])]); + setCurrentPosition([ + getStationData(pos[0], stationList), + getStationData(pos[1], stationList), + ]); } else { - setCurrentPosition([getStationData(currentTrainData?.Pos)]); + setCurrentPosition([getStationData(currentTrainData?.Pos, stationList)]); } }, [currentTrainData]); useEffect(() => { @@ -282,24 +154,13 @@ export const EachTrainInfoCore = ({ if (!data.trainNum) return; const TD = trainList[data.trainNum]; if (!TD) { - searchSpecialTrain(data.trainNum); + const specialTrainActualID = searchSpecialTrain(data.trainNum, trainList); + if (specialTrainActualID) setTrueTrainID(specialTrainActualID); setTrainData([]); return; } setTrainData(TD.split("#").filter((d) => d != "")); }, [data]); - const searchSpecialTrain = (trainNum) => { - const searchBase = trainNum.replace("S", "").replace("X", ""); - const search = (text) => { - const TD = trainList[searchBase + text]; - if (TD) { - return true; - } - return false; - }; - if (search("D")) setTrueTrainID(searchBase + "D"); - if (search("M")) setTrueTrainID(searchBase + "M"); - }; const replaceSpecialTrainDetail = (trainNum) => { let TD = trainList[trainNum]; @@ -321,76 +182,6 @@ export const EachTrainInfoCore = ({ .catch((d) => AS.setItem("trainPositionSwitch", "false")); }, []); - const openBackTrainInfo = (stationInfo, currentTrainIndex) => { - const migrationArray = (stationInfo) => { - const mainTrainStationPosition = trainData.findIndex( - (d) => d.split(",")[0] == stationInfo - ); - - const relationMain = (() => { - if (mainTrainStationPosition == 0) return "head"; - if (mainTrainStationPosition == trainData.length - 1) return "tail"; - return "middle"; - })(); - - const subTrainStationPosition = showNearTrain.findIndex( - (d) => d.split(",")[0] == stationInfo - ); - const relationSub = (() => { - if (subTrainStationPosition == 0) return "head"; - if (subTrainStationPosition == showNearTrain.length - 1) return "tail"; - return "middle"; - })(); - - switch (relationMain) { - case "head": - if (relationSub == "head") { - return; - } else if (relationSub == "tail") { - return [ - ...showNearTrain.slice(0, subTrainStationPosition), - ...trainData, - ]; - } else if (relationSub == "middle") { - return [ - ...showNearTrain.slice(0, subTrainStationPosition), - ...trainData, - ]; - } else return; - case "tail": - if (relationSub == "head") { - return [ - ...trainData.slice(0, mainTrainStationPosition), - ...showNearTrain, - ]; - } else if (relationSub == "tail") { - return; - } else if (relationSub == "middle") { - return [ - ...trainData.slice(0, mainTrainStationPosition), - ...showNearTrain.slice(subTrainStationPosition), - ]; - } else return; - case "middle": - if (relationSub == "head") { - return [ - ...trainData.slice(0, mainTrainStationPosition), - ...showNearTrain, - ]; - } else if (relationSub == "tail") { - return [ - ...showNearTrain.slice(0, subTrainStationPosition), - ...trainData.slice(mainTrainStationPosition), - ]; - } else return; - } - }; - const array = migrationArray(stationInfo); - if (!array) return; - setTrainData(array); - setIsConcatNear(true); - }; - const openTrainInfo = (d) => { const train = customTrainDataDetector(d); let TrainNumber = ""; @@ -517,7 +308,16 @@ export const EachTrainInfoCore = ({ > {headStation && !isConcatNear && ( openBackTrainInfo(headStation)} + onPress={() => { + const array = openBackTrainInfo( + headStation, + trainData, + showNearTrain + ); + if (!array) return; + setTrainData(array); + setIsConcatNear(true); + }} style={{ padding: 10, flexDirection: "row", @@ -574,7 +374,17 @@ export const EachTrainInfoCore = ({ )} {tailStation && !isConcatNear && ( openBackTrainInfo(tailStation)} + onPress={() => { + const array = openBackTrainInfo( + tailStation, + trainData, + showNearTrain + ); + + if (!array) return; + setTrainData(array); + setIsConcatNear(true); + }} style={{ padding: 10, flexDirection: "row", diff --git a/lib/eachTrainInfoCoreLib/findReversalPoints.js b/lib/eachTrainInfoCoreLib/findReversalPoints.js new file mode 100644 index 0000000..7a84fd6 --- /dev/null +++ b/lib/eachTrainInfoCoreLib/findReversalPoints.js @@ -0,0 +1,113 @@ +// arrayは現在位置の駅ID(駅在宅の場合は1つの配列、駅間の場合は2つの配列) +// stopStationIDListは停車駅の駅IDの配列 [Y01,Y02,Y05,...] +export const findReversalPoints = (array, stopStationIDList) => { + try { + if (!stopStationIDList.length) return []; + // arrayが二次元配列だったら早期リターン + if (!array instanceof Array) return []; + if (!array.length) return []; + if (array[0] instanceof Array) return []; + const arrayNumber = array.map((d) => ({ + line: d + .split("") + .filter((s) => "A" < s && s < "Z") + .join(""), + ID: d + .split("") + .filter((s) => "0" <= s && s <= "9") + .join(""), + })); + const stopStationIDListNumber = stopStationIDList.map((d) => { + if (!d) return { line: [], ID: [] }; + return { + line: d + .split("") + .filter((s) => "A" < s && s < "Z") + .join(""), + ID: d + .split("") + .filter((s) => "0" <= s && s <= "9") + .join(""), + }; + }); + // 完全一致 + if (array.length == 1) { + const index = stopStationIDList.indexOf(array[0]); + if (index != -1) return [index]; + // 通過駅の場合 + for (let i = 0; i < stopStationIDListNumber.length - 1; i++) { + if (stopStationIDListNumber[i].ID < arrayNumber[0].ID) { + if (stopStationIDListNumber[i + 1].ID > arrayNumber[0].ID) { + return [i + 1]; + } + } + if (stopStationIDListNumber[i].ID > arrayNumber[0].ID) { + if (stopStationIDListNumber[i + 1].ID < arrayNumber[0].ID) { + return [i + 1]; + } + } + } + } + // 駅間の場合 + if (array.length == 2) { + const index1 = stopStationIDList.indexOf(array[0]); + const index2 = stopStationIDList.indexOf(array[1]); + if (index1 != -1 && index2 != -1) { + // 駅間で通過駅も無い場合 + if (index1 < index2) { + if (index1 + 1 == index2) { + return [index2]; + } else { + const returnArray = []; + for (let i = index1 + 1; i <= index2; i++) { + returnArray.push(i); + } + return returnArray; + } + } + if (index1 > index2) { + if (index2 + 1 == index1) return [index1]; + else { + const returnArray = []; + for (let i = index2 + 1; i <= index1; i++) { + returnArray.push(i); + } + return returnArray; + } + } + } else { + const getNearStationID = (stationID) => { + for (let i = 0; i <= stopStationIDListNumber.length; i++) { + if (stopStationIDListNumber[i].ID < stationID) { + if (stopStationIDListNumber[i + 1].ID > stationID) { + return i + 1; + } + } + if (stopStationIDListNumber[i].ID > stationID) { + if (stopStationIDListNumber[i + 1].ID < stationID) { + return i + 1; + } + } + } + }; + let newIndex1 = index1; + let newIndex2 = index2; + if (index1 == -1) { + newIndex1 = getNearStationID(arrayNumber[0].ID); + } + if (index2 == -1) { + newIndex2 = getNearStationID(arrayNumber[1].ID); + } + if (newIndex1 && newIndex2) { + return [newIndex1, newIndex2]; + } + + // 通過駅の場合 + } + + return []; + } + } catch (e) { + console.log(e); + } +}; diff --git a/lib/eachTrainInfoCoreLib/getStationData.js b/lib/eachTrainInfoCoreLib/getStationData.js new file mode 100644 index 0000000..20b386a --- /dev/null +++ b/lib/eachTrainInfoCoreLib/getStationData.js @@ -0,0 +1,15 @@ +// 駅名から駅情報を取得する +//stationName: 駅名 +//stationList: 駅情報リスト +export const getStationData = (stationName, stationList) => { + const Stations = stationList.map((a) => + a.filter((d) => d.StationName == stationName) + ); + const Station = + Stations && + Stations.reduce((newArray, e) => { + return newArray.concat(e); + }, []); + if (!Station[0]) return []; + return Station.map((d) => d.StationNumber)[0]; +}; diff --git a/lib/eachTrainInfoCoreLib/getType.js b/lib/eachTrainInfoCoreLib/getType.js new file mode 100644 index 0000000..d989461 --- /dev/null +++ b/lib/eachTrainInfoCoreLib/getType.js @@ -0,0 +1,11 @@ +// 種別判定 +export const getType = (string) => { + switch (string) { + case "express": + return "特急"; + case "rapid": + return "快速"; + default: + return ""; + } +}; diff --git a/lib/eachTrainInfoCoreLib/migrateTrainName.js b/lib/eachTrainInfoCoreLib/migrateTrainName.js new file mode 100644 index 0000000..88e0a26 --- /dev/null +++ b/lib/eachTrainInfoCoreLib/migrateTrainName.js @@ -0,0 +1,7 @@ +// Description: 電車名の変換を行う。 +// マリンライナーやマリン表記をマリンライナーに変換する。 +export const migrateTrainName = (string) => { + return string + .replace("マリン", "マリンライナー") + .replace("ライナーライナー", "ライナー"); +}; diff --git a/lib/eachTrainInfoCoreLib/openBackTrainInfo.js b/lib/eachTrainInfoCoreLib/openBackTrainInfo.js new file mode 100644 index 0000000..8b5944b --- /dev/null +++ b/lib/eachTrainInfoCoreLib/openBackTrainInfo.js @@ -0,0 +1,68 @@ +export const openBackTrainInfo = (stationInfo, trainData, showNearTrain) => { + const migrationArray = (stationInfo) => { + const mainTrainStationPosition = trainData.findIndex( + (d) => d.split(",")[0] == stationInfo + ); + + const relationMain = (() => { + if (mainTrainStationPosition == 0) return "head"; + if (mainTrainStationPosition == trainData.length - 1) return "tail"; + return "middle"; + })(); + + const subTrainStationPosition = showNearTrain.findIndex( + (d) => d.split(",")[0] == stationInfo + ); + const relationSub = (() => { + if (subTrainStationPosition == 0) return "head"; + if (subTrainStationPosition == showNearTrain.length - 1) return "tail"; + return "middle"; + })(); + + switch (relationMain) { + case "head": + if (relationSub == "head") { + return; + } else if (relationSub == "tail") { + return [ + ...showNearTrain.slice(0, subTrainStationPosition), + ...trainData, + ]; + } else if (relationSub == "middle") { + return [ + ...showNearTrain.slice(0, subTrainStationPosition), + ...trainData, + ]; + } else return; + case "tail": + if (relationSub == "head") { + return [ + ...trainData.slice(0, mainTrainStationPosition), + ...showNearTrain, + ]; + } else if (relationSub == "tail") { + return; + } else if (relationSub == "middle") { + return [ + ...trainData.slice(0, mainTrainStationPosition), + ...showNearTrain.slice(subTrainStationPosition), + ]; + } else return; + case "middle": + if (relationSub == "head") { + return [ + ...trainData.slice(0, mainTrainStationPosition), + ...showNearTrain, + ]; + } else if (relationSub == "tail") { + return [ + ...showNearTrain.slice(0, subTrainStationPosition), + ...trainData.slice(mainTrainStationPosition), + ]; + } else return; + } + }; + const array = migrationArray(stationInfo); + if (!array) return null; + return array; +}; diff --git a/lib/eachTrainInfoCoreLib/searchSpecialTrain.js b/lib/eachTrainInfoCoreLib/searchSpecialTrain.js new file mode 100644 index 0000000..ec0e53d --- /dev/null +++ b/lib/eachTrainInfoCoreLib/searchSpecialTrain.js @@ -0,0 +1,13 @@ +// S列番の列車からDやMの列車を検索する +export const searchSpecialTrain = (trainNum, trainList) => { + const searchBase = trainNum.replace("S", "").replace("X", ""); + const search = (text) => { + const TD = trainList[searchBase + text]; + if (TD) { + return true; + } + return false; + }; + if (search("D")) return searchBase + "D"; + if (search("M")) return searchBase + "M"; +};