fix: satisfy strict null checks in uaq/fcna table lookups

This commit is contained in:
Aric Camarata 2026-05-30 18:49:21 -04:00
parent e1b761db7c
commit db575a3b41
2 changed files with 14 additions and 8 deletions

View file

@ -162,7 +162,8 @@ function uaqAnchorMs(hy: number, hm: number): number {
found = -1; found = -1;
while (lo <= hi) { while (lo <= hi) {
const mid = (lo + hi) >>> 1; const mid = (lo + hi) >>> 1;
const midHy = hDatesTable[mid].hy; // mid is always within [0, hDatesTable.length-1] by binary search invariant
const midHy = hDatesTable[mid]!.hy;
if (midHy === hy) { if (midHy === hy) {
found = mid; found = mid;
break; break;
@ -170,8 +171,9 @@ function uaqAnchorMs(hy: number, hm: number): number {
else hi = mid - 1; else hi = mid - 1;
} }
if (found !== -1 && hDatesTable[found].dpm !== 0) { // found is within [0, hDatesTable.length-1]; guard confirms it's valid before use.
const r = hDatesTable[found]; if (found !== -1 && hDatesTable[found]!.dpm !== 0) {
const r = hDatesTable[found]!;
let days = 0; let days = 0;
for (let i = 0; i < hm - 1; i++) { for (let i = 0; i < hm - 1; i++) {
days += (r.dpm >> i) & 1 ? 30 : 29; days += (r.dpm >> i) & 1 ? 30 : 29;

View file

@ -24,8 +24,10 @@ function findYearEntry(hy: number): HijriYearRecord | null {
while (lo <= hi) { while (lo <= hi) {
const mid = (lo + hi) >>> 1; const mid = (lo + hi) >>> 1;
const midHy = hDatesTable[mid].hy; // mid is always within [0, hDatesTable.length-1] by binary search invariant
if (midHy === hy) return hDatesTable[mid]; const row = hDatesTable[mid]!;
const midHy = row.hy;
if (midHy === hy) return row;
else if (midHy < hy) lo = mid + 1; else if (midHy < hy) lo = mid + 1;
else hi = mid - 1; else hi = mid - 1;
} }
@ -48,7 +50,8 @@ function uaqToHijri(date: Date): HijriDate | null {
while (lo <= hi) { while (lo <= hi) {
const mid = (lo + hi) >>> 1; const mid = (lo + hi) >>> 1;
const entry = hDatesTable[mid]; // mid is always within [0, hDatesTable.length-1] by binary search invariant
const entry = hDatesTable[mid]!;
const entryUtc = Date.UTC(entry.gy, entry.gm - 1, entry.gd); const entryUtc = Date.UTC(entry.gy, entry.gm - 1, entry.gd);
if (entryUtc <= inputUtc) { if (entryUtc <= inputUtc) {
@ -60,9 +63,10 @@ function uaqToHijri(date: Date): HijriDate | null {
} }
// dpm === 0 is the sentinel entry (hy 1501) marking the upper bound. // dpm === 0 is the sentinel entry (hy 1501) marking the upper bound.
if (found === -1 || hDatesTable[found].dpm === 0) return null; // found is within [0, hDatesTable.length-1]; guard above confirms it's valid.
if (found === -1 || hDatesTable[found]!.dpm === 0) return null;
const record = hDatesTable[found]; const record = hDatesTable[found]!;
const startUtc = Date.UTC(record.gy, record.gm - 1, record.gd); const startUtc = Date.UTC(record.gy, record.gm - 1, record.gd);
let remaining = Math.round((inputUtc - startUtc) / MS_PER_DAY); let remaining = Math.round((inputUtc - startUtc) / MS_PER_DAY);
let hijriMonth = 0; let hijriMonth = 0;