From 6c3b186c9b5df38b35a82e0454b648600a690a4f Mon Sep 17 00:00:00 2001 From: harukin-OneMix4 Date: Sat, 23 Dec 2023 04:43:45 +0900 Subject: [PATCH] =?UTF-8?q?=E5=BF=B5=E3=81=AE=E3=81=9F=E3=82=81trycatch?= =?UTF-8?q?=E8=A8=AD=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ActionSheetComponents/EachTrainInfo.js | 192 +++++++++--------- 1 file changed, 98 insertions(+), 94 deletions(-) diff --git a/components/ActionSheetComponents/EachTrainInfo.js b/components/ActionSheetComponents/EachTrainInfo.js index b007311..380c9d2 100644 --- a/components/ActionSheetComponents/EachTrainInfo.js +++ b/components/ActionSheetComponents/EachTrainInfo.js @@ -96,26 +96,15 @@ export const EachTrainInfo = ({ return StationNumbers[0]; }); function findReversalPoints(array) { - // 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 { + 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") @@ -124,84 +113,99 @@ export const EachTrainInfo = ({ .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; - } - } - } + })); + 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(""), }; - 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]; - } - + }); + // 完全一致 + 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 []; + // 通過駅の場合 + } + + return []; + } + } catch (e) { + console.log(e); } } // 使用例