Show light drizzle icon on 7-day forecast when rain/storm chance is low
This commit is contained in:
parent
d870a8e668
commit
0b8e012569
@ -29,7 +29,7 @@
|
|||||||
<div class="daily-list">
|
<div class="daily-list">
|
||||||
{#each daily.time as day, i}
|
{#each daily.time as day, i}
|
||||||
{#if i < 7}
|
{#if i < 7}
|
||||||
{@const info = getWeatherInfo(daily.weather_code[i])}
|
{@const info = getWeatherInfo(daily.weather_code[i], precip)}
|
||||||
{@const tempMax = Math.round(daily.temperature_2m_max[i])}
|
{@const tempMax = Math.round(daily.temperature_2m_max[i])}
|
||||||
{@const tempMin = Math.round(daily.temperature_2m_min[i])}
|
{@const tempMin = Math.round(daily.temperature_2m_min[i])}
|
||||||
{@const precip = daily.precipitation_probability_max?.[i] || 0}
|
{@const precip = daily.precipitation_probability_max?.[i] || 0}
|
||||||
|
|||||||
@ -39,8 +39,25 @@ const CODE_MAP = {
|
|||||||
* @param {number} code - WMO weather code (0-99)
|
* @param {number} code - WMO weather code (0-99)
|
||||||
* @returns {{ icon: string, label: string, desc: string }}
|
* @returns {{ icon: string, label: string, desc: string }}
|
||||||
*/
|
*/
|
||||||
export function getWeatherInfo(code) {
|
// Codes that render as a rain/storm icon but are downgraded to light drizzle
|
||||||
return CODE_MAP[code] || { icon: '❓', label: 'Unknown', desc: 'Unknown conditions' }
|
// when the forecast's precipitation chance is low — a 17% "chance of rain"
|
||||||
|
// shouldn't display as a full rain icon.
|
||||||
|
const RAIN_CODES = new Set([56, 57, 61, 63, 65, 66, 67, 80, 81, 82, 95, 96, 99])
|
||||||
|
const LOW_CHANCE_PRECIP = 35
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get weather display info for a WMO code.
|
||||||
|
* @param {number} code - WMO weather code (0-99)
|
||||||
|
* @param {number|null} prob - precipitation chance (%) for the period. When a
|
||||||
|
* rain/storm code has only a low chance, show light drizzle instead.
|
||||||
|
* @returns {{ icon: string, label: string, desc: string }}
|
||||||
|
*/
|
||||||
|
export function getWeatherInfo(code, prob = null) {
|
||||||
|
const info = CODE_MAP[code] || { icon: '❓', label: 'Unknown', desc: 'Unknown conditions' }
|
||||||
|
if (prob != null && prob < LOW_CHANCE_PRECIP && RAIN_CODES.has(code)) {
|
||||||
|
return { icon: CODE_MAP[51].icon, label: 'Light Drizzle', desc: 'Chance of light rain' }
|
||||||
|
}
|
||||||
|
return info
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -56,6 +56,23 @@ describe('weather-codes', () => {
|
|||||||
expect(getWeatherInfo(81).icon).toBe('🌧️')
|
expect(getWeatherInfo(81).icon).toBe('🌧️')
|
||||||
expect(getWeatherInfo(82).icon).toBe('⛈️')
|
expect(getWeatherInfo(82).icon).toBe('⛈️')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('downgrades rain to light drizzle when precip chance is low', () => {
|
||||||
|
const info = getWeatherInfo(63, 17)
|
||||||
|
expect(info.icon).toBe('🌦️')
|
||||||
|
expect(info.label).toBe('Light Drizzle')
|
||||||
|
expect(info.desc).toBe('Chance of light rain')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('keeps full rain/storm icon when precip chance is high', () => {
|
||||||
|
expect(getWeatherInfo(63, 60).icon).toBe('🌧️')
|
||||||
|
expect(getWeatherInfo(95, 70).icon).toBe('⛈️')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('does not downgrade when no probability is provided', () => {
|
||||||
|
expect(getWeatherInfo(63).icon).toBe('🌧️')
|
||||||
|
expect(getWeatherInfo(82).icon).toBe('⛈️')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('getWeatherSeverity', () => {
|
describe('getWeatherSeverity', () => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user