33 lines
1.1 KiB
TypeScript
33 lines
1.1 KiB
TypeScript
|
|
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;
|
|||
|
|
}
|