From dc7345fb1ddbb45e05d2f5b75b6f578b3fa9355b Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sat, 14 Feb 2026 18:09:47 -0700 Subject: [PATCH] feat: Use file line number as stable METAR ID in URL Co-authored-by: aider (gemini/gemini-2.5-pro) --- metar.html | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/metar.html b/metar.html index 9d14c1a..a276588 100644 --- a/metar.html +++ b/metar.html @@ -819,16 +819,19 @@ return sections; } - function displayMetar(index) { + function displayMetar(id) { if (metars.length === 0) { metarDisplay.textContent = 'No METARs loaded.'; return; } - let metarIndex; - if (index !== undefined && index >= 0 && index < metars.length) { - metarIndex = index; - } else { + let metarIndex = -1; + + if (id !== undefined) { + metarIndex = metars.findIndex(m => m.line === id); + } + + if (metarIndex === -1) { // Bias random selection towards the more interesting (higher-scored) METARs. // Math.random() * Math.random() skews distribution towards 0. const randomSortedIndex = Math.floor(Math.random() * Math.random() * sortedMetars.length); @@ -841,10 +844,10 @@ } currentMetarIndex = metarIndex; - const metarString = metars[metarIndex].metar; - history.replaceState(null, '', '#' + metarIndex); + const metarData = metars[metarIndex]; + history.replaceState(null, '', '#' + metarData.line); - currentMetarSections = generateMetarSections(metarString); + currentMetarSections = generateMetarSections(metarData.metar); currentSectionIndex = 0; updateDisplay(); } @@ -854,11 +857,11 @@ .then(data => { const lines = data.split('\n'); const rawMetars = []; - lines.forEach(line => { + lines.forEach((line, index) => { if (line.includes('METAR')) { const metarString = line.substring(line.indexOf('METAR') + 6).replace('=', '').trim(); if (metarString) { - rawMetars.push(metarString); + rawMetars.push({ metar: metarString, line: index + 1 }); } } }); @@ -867,17 +870,17 @@ // 1. Calculate component frequencies for rarity scoring const componentFrequencies = {}; - rawMetars.forEach(metarString => { - const parts = getMetarParts(metarString); + rawMetars.forEach(metarObj => { + const parts = getMetarParts(metarObj.metar); parts.forEach(part => { componentFrequencies[part] = (componentFrequencies[part] || 0) + 1; }); }); // 2. Score and store each METAR - rawMetars.forEach(metarString => { - const score = calculateMetarScore(metarString, componentFrequencies, rawMetars.length); - metars.push({ metar: metarString, score: score }); + rawMetars.forEach(metarObj => { + const score = calculateMetarScore(metarObj.metar, componentFrequencies, rawMetars.length); + metars.push({ metar: metarObj.metar, score: score, line: metarObj.line }); }); // 3. Create a sorted copy for biased random selection, but don't sort the main array @@ -891,10 +894,10 @@ } const hash = window.location.hash.substring(1); - const initialIndex = parseInt(hash, 10); + const initialId = parseInt(hash, 10); - if (!isNaN(initialIndex) && initialIndex >= 0 && initialIndex < metars.length) { - displayMetar(initialIndex); + if (!isNaN(initialId)) { + displayMetar(initialId); } else { displayMetar(); // Display initial random METAR } @@ -908,9 +911,9 @@ decodeAllCheckbox.addEventListener('change', updateDisplay); window.addEventListener('hashchange', () => { const hash = window.location.hash.substring(1); - const index = parseInt(hash, 10); - if (!isNaN(index) && index >= 0 && index < metars.length) { - displayMetar(index); + const id = parseInt(hash, 10); + if (!isNaN(id)) { + displayMetar(id); } });