wayfarer-weather/lib/weather/providers/openweather/shared.ts
Cayden Schwechten 6250d23297 init
2026-10-31 21:20:16 -05:00

32 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}