Skip to content

Commit

Permalink
Prettified Code!
Browse files Browse the repository at this point in the history
  • Loading branch information
LOOHP authored and actions-user committed May 15, 2024
1 parent 9147c13 commit ba90d9f
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 58 deletions.
156 changes: 100 additions & 56 deletions src/components/route-board/RouteTerminus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface RouteTerminus {
const RouteTerminus = ({ terminus }: RouteTerminus) => {
const { t, i18n } = useTranslation();
const {
db: { routeList, stopList }
db: { routeList, stopList },
} = useContext(DbContext);
const { route, co, orig, dest, stops, bound, gtfsId } = terminus;

Expand All @@ -21,83 +21,127 @@ const RouteTerminus = ({ terminus }: RouteTerminus) => {
return [arr[0], arr[arr.length - 1]];
};
const diffConsecutive = (array, sequence) => {

Check failure on line 23 in src/components/route-board/RouteTerminus.tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

Parameter 'array' implicitly has an 'any' type.

Check failure on line 23 in src/components/route-board/RouteTerminus.tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

Parameter 'sequence' implicitly has an 'any' type.

Check failure on line 23 in src/components/route-board/RouteTerminus.tsx

View workflow job for this annotation

GitHub Actions / build (21.x)

Parameter 'array' implicitly has an 'any' type.

Check failure on line 23 in src/components/route-board/RouteTerminus.tsx

View workflow job for this annotation

GitHub Actions / build (21.x)

Parameter 'sequence' implicitly has an 'any' type.
for (let i = 0; i <= array.length - sequence.length; i++) {
for (let i = 0; i <= array.length - sequence.length; i++) {
let j;
for (j = 0; j < sequence.length; j++) {
if (array[i + j] !== sequence[j]) {
break;
}
for (j = 0; j < sequence.length; j++) {
if (array[i + j] !== sequence[j]) {
break;
}
}
if (j === sequence.length) {
return true;
if (j === sequence.length) {
return true;
}
}
return false;
};

let routeRemark = useMemo(
() => {
let remark = "";
if (terminus.serviceType >= 2) {
for (let [, data] of Object.entries(routeList)) {
if (Number(data.serviceType) === 1 &&
route === data.route &&
JSON.stringify(bound) === JSON.stringify(data.bound) &&
(co[0] === "gmb" ? gtfsId === data.gtfsId : JSON.stringify(co) === JSON.stringify(data.co)))
{
if (data.stops[co[0]][data.stops[co[0]].length - 1].zh !== stops[co[0]][stops[co[0]].length - 1].zh) {
remark = t("開往") + dest[i18n.language];
} else if (data.stops[co[0]][0].zh !== stops[co[0]][0].zh) {
let routeRemark = useMemo(() => {
let remark = "";
if (terminus.serviceType >= 2) {

Check failure on line 40 in src/components/route-board/RouteTerminus.tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

Operator '>=' cannot be applied to types 'string' and 'number'.

Check failure on line 40 in src/components/route-board/RouteTerminus.tsx

View workflow job for this annotation

GitHub Actions / build (21.x)

Operator '>=' cannot be applied to types 'string' and 'number'.
for (let [, data] of Object.entries(routeList)) {
if (
Number(data.serviceType) === 1 &&
route === data.route &&
JSON.stringify(bound) === JSON.stringify(data.bound) &&
(co[0] === "gmb"
? gtfsId === data.gtfsId
: JSON.stringify(co) === JSON.stringify(data.co))
) {
if (
data.stops[co[0]][data.stops[co[0]].length - 1].zh !==

Check failure on line 51 in src/components/route-board/RouteTerminus.tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

Property 'zh' does not exist on type 'string'.

Check failure on line 51 in src/components/route-board/RouteTerminus.tsx

View workflow job for this annotation

GitHub Actions / build (21.x)

Property 'zh' does not exist on type 'string'.
stops[co[0]][stops[co[0]].length - 1].zh

Check failure on line 52 in src/components/route-board/RouteTerminus.tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

Property 'zh' does not exist on type 'string'.

Check failure on line 52 in src/components/route-board/RouteTerminus.tsx

View workflow job for this annotation

GitHub Actions / build (21.x)

Property 'zh' does not exist on type 'string'.
) {
remark = t("開往") + dest[i18n.language];

Check failure on line 54 in src/components/route-board/RouteTerminus.tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Terminal'.

Check failure on line 54 in src/components/route-board/RouteTerminus.tsx

View workflow job for this annotation

GitHub Actions / build (21.x)

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Terminal'.
} else if (data.stops[co[0]][0].zh !== stops[co[0]][0].zh) {

Check failure on line 55 in src/components/route-board/RouteTerminus.tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

Property 'zh' does not exist on type 'string'.

Check failure on line 55 in src/components/route-board/RouteTerminus.tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

Property 'zh' does not exist on type 'string'.

Check failure on line 55 in src/components/route-board/RouteTerminus.tsx

View workflow job for this annotation

GitHub Actions / build (21.x)

Property 'zh' does not exist on type 'string'.

Check failure on line 55 in src/components/route-board/RouteTerminus.tsx

View workflow job for this annotation

GitHub Actions / build (21.x)

Property 'zh' does not exist on type 'string'.
if (!terminus.nlbId) {
remark = t("從") + orig[i18n.language] + t("開出");
}
} else {
let mainRouteFirstStop = stopList[data.stops[co][0]].name;
let mainRouteLastStop =
stopList[data.stops[co][data.stops[co].length - 1]].name;
let routeFirstStop = stopList[stops[co][0]].name;
let routeLastStop = stopList[stops[co][stops[co].length - 1]].name;

if (mainRouteLastStop.zh !== routeLastStop.zh) {
remark = t("開往") + routeLastStop[i18n.language];
} else if (mainRouteFirstStop.zh !== routeFirstStop.zh) {
if (!terminus.nlbId) {
remark = t("從") + orig[i18n.language] + t("開出");
remark = t("從") + routeFirstStop[i18n.language] + t("開出");
}
} else {
let mainRouteFirstStop = stopList[data.stops[co][0]].name;
let mainRouteLastStop = stopList[data.stops[co][data.stops[co].length - 1]].name;
let routeFirstStop = stopList[stops[co][0]].name;
let routeLastStop = stopList[stops[co][stops[co].length - 1]].name;

if (mainRouteLastStop.zh !== routeLastStop.zh) {
remark = t("開往") + routeLastStop[i18n.language];
} else if (mainRouteFirstStop.zh !== routeFirstStop.zh) {
if (!terminus.nlbId) {
remark = t("從") + routeFirstStop[i18n.language] + t("開出");
}
let difference = stops[co].filter(
(x) => !data.stops[co].includes(x)
);
let diffName = difference.map(
(x) => stopList[x].name[i18n.language]
);
if (difference.length > 0) {
remark =
t("經") +
firstLastDiff(diffName).join(
t(diffConsecutive(data.stops[co], difference) ? "至" : "及")
);
} else {
let difference = stops[co]
.filter(x => !data.stops[co].includes(x));
let diffName = difference
.map(x => stopList[x].name[i18n.language]);
let difference = data.stops[co].filter(
(x) => !stops[co].includes(x)
);
let diffName = difference.map(
(x) => stopList[x].name[i18n.language]
);
if (difference.length > 0) {
remark = t("經") + firstLastDiff(diffName).join(t(diffConsecutive(data.stops[co], difference) ? "至" : "及"));
} else {
let difference = data.stops[co]
.filter(x => !stops[co].includes(x));
let diffName = difference
.map(x => stopList[x].name[i18n.language]);
if (difference.length > 0) {
remark = t("不經") + firstLastDiff(diffName).join(t(diffConsecutive(data.stops[co], difference) ? "至" : "及"));
}
remark =
t("不經") +
firstLastDiff(diffName).join(
t(
diffConsecutive(data.stops[co], difference)
? "至"
: "及"
)
);
}
}
}
break;
}
}
break;
}
}
if (terminus.nlbId) {
remark = t("從") + toProperCase(terminus.orig[i18n.language]) + t("開出") + " " + remark;
}
return remark;
},
[terminus, routeList, i18n.language, bound, co, dest, gtfsId, orig, route, stopList, stops, t]
);
}
if (terminus.nlbId) {
remark =
t("從") +
toProperCase(terminus.orig[i18n.language]) +
t("開出") +
" " +
remark;
}
return remark;
}, [
terminus,
routeList,
i18n.language,
bound,
co,
dest,
gtfsId,
orig,
route,
stopList,
stops,
t,
]);

return (
<Box sx={rootSx}>
<Box sx={fromToWrapperSx}>
<span>{`${t("往")} `}</span>
<Typography component="h3" variant="h6" sx={destinationSx} textOverflow="ellipsis" overflow="hidden">
<Typography
component="h3"
variant="h6"
sx={destinationSx}
textOverflow="ellipsis"
overflow="hidden"
>
{toProperCase(terminus.dest[i18n.language])}
</Typography>
</Box>
Expand Down
4 changes: 3 additions & 1 deletion src/components/route-eta/RouteHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ const RouteHeader = ({ routeId, stopId }: RouteHeaderProps) => {
<Paper id="route-eta-header" sx={PaperSx} elevation={0}>
<RouteNo routeNo={t(route)} component="h1" align="center" />
<Typography component="h2" variant="caption" align="center">
{t("往")}{langSpace(i18n)}{toProperCase(dest[i18n.language])}{" "}
{t("往")}
{langSpace(i18n)}
{toProperCase(dest[i18n.language])}{" "}
{nlbId ? t("從") + toProperCase(orig[i18n.language]) + t("開出") : ""}
</Typography>
<ReverseButton routeId={routeId} stopId={stopId} />
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { TFunction } from "i18next";

export const langSpace = (i18n) => {
return i18n.language === "en" ? " " : "";
}
};

export const getDistance = (a: GeoLocation, b: GeoLocation) => {
const R = 6371e3; // metres
Expand Down

0 comments on commit ba90d9f

Please sign in to comment.