Files
lunch/lib/timer.ts
T
Kai BöhmandClaude Opus 5.5 2c6dbd06be
ci / olares chart · sanity (pull_request) Successful in 3s
ci / app · types + tests + build (pull_request) Successful in 2m57s
markt / eintrag (pull_request) Successful in 20s
ci / oberfläche · Browser am iPhone (pull_request) Successful in 6m38s
Cooking mode with timers and wake lock, ratings and thank-yous (26.11.13)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HUc2nv7rJVU2gp3Bgtha3q
2026-11-07 04:58:40 +00:00

19 lines
962 B
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.
// Zeit aus einem Schritttext lesen („30 Minuten köcheln“, „bake for 1 hour“),
// damit der Kochmodus auch ohne hinterlegten Timer einen anbietet. Bei einer
// Spanne („10–15 Minuten“) gilt die kürzere Zeit: lieber einmal früher nachsehen.
const UNITS: [RegExp, number][] = [
[/^(min|min\.|minute|minuten|minutes|mins)$/i, 60],
[/^(std|std\.|stunde|stunden|h|hour|hours|heure|heures|hora|horas)$/i, 3600],
[/^(sek|sek\.|sekunden|sec|secs|seconds|secondes|segundos)$/i, 1],
];
export function timerFromText(text: string): number | null {
const m = text.match(/(\d+(?:[.,]\d+)?)(?:\s*(?:-|–|bis|to|à|a)\s*\d+(?:[.,]\d+)?)?\s*([A-Za-zäöüÄÖÜ]+\.?)/);
if (!m) return null;
const word = m[2]!.replace(/\.$/, "");
const unit = UNITS.find(([re]) => re.test(word));
if (!unit) return null;
const seconds = Math.round(Number(m[1]!.replace(",", ".")) * unit[1]);
return seconds > 0 && seconds <= 24 * 3600 ? seconds : null;
}