Drop 11 non-null assertions in api.ts, DayGroupedFlightList, FlightCard
Regex capture groups and array boundary accesses replaced with nullish fallbacks / explicit guards. Warning count: 41 → 30.
This commit is contained in:
@@ -165,10 +165,10 @@ function bitmaskToDates(bitmask: string, baseDate: string): string[] {
|
||||
// Parse baseDate — could be "yyyy-MM-ddT00:00:00" or "yyyyMMdd"
|
||||
let year: number, month: number, day: number;
|
||||
if (baseDate.includes("-")) {
|
||||
const parts = baseDate.split("T")[0]!.split("-");
|
||||
year = parseInt(parts[0]!, 10);
|
||||
month = parseInt(parts[1]!, 10) - 1;
|
||||
day = parseInt(parts[2]!, 10);
|
||||
const parts = (baseDate.split("T")[0] ?? baseDate).split("-");
|
||||
year = parseInt(parts[0] ?? "0", 10);
|
||||
month = parseInt(parts[1] ?? "1", 10) - 1;
|
||||
day = parseInt(parts[2] ?? "1", 10);
|
||||
} else {
|
||||
year = parseInt(baseDate.slice(0, 4), 10);
|
||||
month = parseInt(baseDate.slice(4, 6), 10) - 1;
|
||||
|
||||
@@ -45,7 +45,9 @@ interface DayGroup {
|
||||
|
||||
function getPrimaryLeg(flight: ISimpleFlight): IFlightLeg {
|
||||
if (flight.routeType === "Direct") return flight.leg;
|
||||
return flight.legs[0]!;
|
||||
const first = flight.legs[0];
|
||||
if (!first) throw new Error("MultiLeg flight has no legs");
|
||||
return first;
|
||||
}
|
||||
|
||||
/** Pull the local-time wall-clock date (yyyy-MM-dd) from the first leg. */
|
||||
@@ -70,7 +72,7 @@ function groupFlightsByDay(flights: ISimpleFlight[]): DayGroup[] {
|
||||
const [y, m, d] = date.split("-").map((s) => parseInt(s, 10));
|
||||
return {
|
||||
date,
|
||||
parsed: new Date(y!, (m ?? 1) - 1, d ?? 1),
|
||||
parsed: new Date(y ?? 1970, (m ?? 1) - 1, d ?? 1),
|
||||
flights: list,
|
||||
};
|
||||
});
|
||||
@@ -85,7 +87,14 @@ function depMinutes(f: ISimpleFlight): number {
|
||||
return (h ?? 0) * 60 + (m ?? 0);
|
||||
}
|
||||
function arrMinutes(f: ISimpleFlight): number {
|
||||
const last = f.routeType === "Direct" ? f.leg : f.legs[f.legs.length - 1]!;
|
||||
let last: IFlightLeg;
|
||||
if (f.routeType === "Direct") {
|
||||
last = f.leg;
|
||||
} else {
|
||||
const tail = f.legs[f.legs.length - 1];
|
||||
if (!tail) return 0;
|
||||
last = tail;
|
||||
}
|
||||
const iso = last.arrival.times.scheduledArrival.local ?? "";
|
||||
const t = iso.slice(11, 16);
|
||||
if (!t) return 0;
|
||||
@@ -96,7 +105,7 @@ function flyingMinutes(f: ISimpleFlight): number {
|
||||
const ft = f.flyingTime ?? "";
|
||||
const m = /^(\d+):(\d+)/.exec(ft);
|
||||
if (!m) return 0;
|
||||
return parseInt(m[1]!, 10) * 60 + parseInt(m[2]!, 10);
|
||||
return parseInt(m[1] ?? "0", 10) * 60 + parseInt(m[2] ?? "0", 10);
|
||||
}
|
||||
|
||||
function sortFlights(flights: ISimpleFlight[], mode: SortMode): ISimpleFlight[] {
|
||||
|
||||
@@ -83,8 +83,8 @@ function formatFlyingTime(value: string, language: string): string {
|
||||
let m = 0;
|
||||
const hms = /^(\d+):(\d+):(\d+)$/.exec(value);
|
||||
if (hms) {
|
||||
h = parseInt(hms[1]!, 10);
|
||||
m = parseInt(hms[2]!, 10);
|
||||
h = parseInt(hms[1] ?? "0", 10);
|
||||
m = parseInt(hms[2] ?? "0", 10);
|
||||
} else {
|
||||
const iso = /^PT(?:(\d+)H)?(?:(\d+)M)?/.exec(value);
|
||||
if (iso) {
|
||||
|
||||
Reference in New Issue
Block a user