Files
movie-advisor/lib/utils.ts
T
nerandClaude Opus 4.8 284458f783 feat(torrents): pass tmdb_id and season to m3u stream for series
Add seasonFromEpisodes (single season only, skips ranges) and include
tmdb_id plus the resolved season in the m3u/cast stream params.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-07 19:14:59 +03:00

91 lines
2.8 KiB
TypeScript

import { type ClassValue, clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';
export const cn = (...inputs: ClassValue[]) => twMerge(clsx(inputs));
export const slugify = (value: string) =>
value
.toLowerCase()
.normalize('NFKD')
.replace(/[̀-ͯ]/g, '')
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '')
.slice(0, 60)
.replace(/-+$/, '');
// Keyword-carrying show URL (/movie/27205-inception); the slug is optional decoration —
// routing only relies on the leading numeric id.
export const showPath = ({ type, id, title }: Pick<Show, 'type' | 'id' | 'title'>) => {
const slug = slugify(title || '');
return `/${type}/${id}${slug ? `-${slug}` : ''}`;
};
export const formatDate = (date: string) =>
new Intl.DateTimeFormat('en-GB', { day: '2-digit', month: '2-digit', year: 'numeric', timeZone: 'UTC' })
.format(new Date(date))
.replace(/\//g, '.');
export const createUniqueRandomGenerator = (max: number) => {
const numbers = Array.from({ length: max }, (_, i) => i + 1);
for (let i = numbers.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[numbers[i], numbers[j]] = [numbers[j], numbers[i]];
}
let index = 0;
return () => {
if (index >= numbers.length) {
index = 0;
}
return numbers[index++];
};
};
export const getMagnetHash = (link: string) => {
const [, hash] = link.match(/btih:([A-Fa-f0-9]{40}|[A-Za-z0-9]{32})/) || [];
return hash?.toUpperCase();
};
// Stable identity for a torrent that survives the lazy empty -> fetched magnet transition:
// Toloka has a permanent download URL, the rest always carry a magnet infohash.
export const torrentKey = (torrent: Torrent) =>
torrent.download || getMagnetHash(torrent.magnet) || torrent.hash || torrent.id;
export const seasonFromEpisodes = (episodes?: string): number | undefined => {
if (!episodes) return undefined;
if (/^S\d+\s*[-–—]\s*\d+/i.test(episodes)) return undefined;
const [, season] = episodes.match(/^S(\d+)/i) || [];
return season ? Number(season) : undefined;
};
export const formatBytes = (bytes?: number) => {
if (bytes == null || !isFinite(bytes)) return '';
const units = ['B', 'KB', 'MB', 'GB', 'TB'] as const;
let i = 0;
let v = bytes;
while (v >= 1024 && i < units.length - 1) {
v /= 1024;
i++;
}
return `${v.toFixed(v < 10 && i > 0 ? 1 : 0)} ${units[i]}`;
};
export const detectSafari = () => {
const ua = navigator.userAgent;
const isSafari = ua.includes('Safari') && !ua.includes('Chrome') && !ua.includes('CriOS') && !ua.includes('FxiOS');
const isIOS = /iPad|iPhone|iPod/.test(ua) || (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1);
return { isSafari, isIOS };
};
export const isStandaloneApp = () => window.matchMedia('screen and (display-mode: standalone)').matches;