wayfarer-weather/lib/weather/providers/openweather/shared.ts

33 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

2026-10-31 21:20:16 -05:00
import type { Condition } from "../../types";
/** Shared by both OpenWeather plans. */
export const OWM_LABEL = "OpenWeather";
export interface OwmWeatherTag {
id: number;
description: string;
icon: string; // ends in "d" (day) or "n" (night)
}
/** Condition codes: https://openweathermap.org/weather-conditions */
export function toCondition(code: number): Condition {
if (code >= 200 && code < 300) return "storm";
if (code >= 300 && code < 600) return "rain"; // drizzle + rain
if (code >= 600 && code < 700) return "snow";
if (code >= 700 && code < 800) return "fog"; // mist, haze, smoke, dust…
if (code === 800) return "clear";
return "clouds";
}
/** OpenWeather gives probability of precipitation as 0–1; we use 0–100. */
export function popToPercent(pop: number | undefined): number | null {
return pop === undefined ? null : Math.round(pop * 100);
}
/** Rain + snow volume, either of which may be missing. */
export function precipitation(rain?: number, snow?: number): number | null {
if (rain === undefined && snow === undefined) return null;
return Math.round(((rain ?? 0) + (snow ?? 0)) * 10) / 10;
}