Files
meditation-admin/lib/utils.ts
T

41 lines
1.6 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.
// Tiny className combiner (avoids an extra dependency).
export function cn(...parts: Array<string | false | null | undefined>): string {
return parts.filter(Boolean).join(" ");
}
// Convert ASCII digits to Persian digits for display.
const FA_DIGITS = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"];
export function toFa(value: string | number | null | undefined): string {
if (value === null || value === undefined) return "";
return String(value).replace(/\d/g, (d) => FA_DIGITS[Number(d)]);
}
// Seconds -> "م:ث" style label (e.g. 90 -> ۱:۳۰). Used by the timer presets,
// whose backend field is `duration_seconds`.
export function formatDuration(seconds?: number | null): string {
if (!seconds && seconds !== 0) return "—";
const m = Math.floor(seconds / 60);
const s = Math.floor(seconds % 60);
return toFa(`${m}:${String(s).padStart(2, "0")}`);
}
// Derive a clean title from an uploaded file name: drop the extension and turn
// underscores/dashes into spaces (e.g. "morning_calm-01.mp3" -> "morning calm 01").
export function fileNameToTitle(name: string): string {
return name
.replace(/\.[^./\\]+$/, "")
.replace(/[_-]+/g, " ")
.replace(/\s+/g, " ")
.trim();
}
// Minutes -> Persian label. Media and music store `duration` in minutes.
export function formatMinutes(minutes?: number | null): string {
if (minutes === null || minutes === undefined) return "—";
const m = Math.floor(minutes);
if (m < 60) return toFa(`${m} دقیقه`);
const h = Math.floor(m / 60);
const rem = m % 60;
return rem ? toFa(`${h} ساعت و ${rem} دقیقه`) : toFa(`${h} ساعت`);
}