Files

50 lines
1.2 KiB
TypeScript
Raw Permalink Normal View History

2026-07-28 21:55:39 -04:00
function extractEpisodeNumber(value?: string): number {
if (!value) return 0
const match = value.match(/(\d+)(?:\/\d+)?\s*$/)
if (!match) {
const anyNumber = value.match(/\d+/)
return anyNumber ? Number(anyNumber[0]) : 0
}
return Number(match[1])
}
export function getEpisodeDisplay(
episode?: string,
episodeTotal?: string,
2026-07-30 21:38:01 -04:00
prefix = 'Tập',
2026-07-28 21:55:39 -04:00
): string | undefined {
if (!episode) return undefined
2026-07-30 21:38:01 -04:00
const trimmed = episode.trim()
// "Hoàn tất" status
if (/hoan.t|hoàn.t/.test(trimmed)) {
return 'Hoàn tất'
}
// Extract "X/Y" pattern anywhere in the string (with or without spaces)
const slashMatch = trimmed.match(/(\d+)\s*\/\s*(\d+)/)
if (slashMatch) {
const epNum = Number(slashMatch[1])
const totalNum = Number(slashMatch[2])
if (epNum > 0 && totalNum > 0 && epNum < totalNum) {
return `${prefix} ${epNum}/${totalNum}`
}
return `${prefix} ${totalNum}`
}
// Single number
2026-07-28 21:55:39 -04:00
const totalNum = extractEpisodeNumber(episodeTotal)
const epNum = extractEpisodeNumber(episode)
if (totalNum > 0 && epNum > 0 && epNum !== totalNum) {
2026-07-30 21:38:01 -04:00
return `${prefix} ${epNum}/${totalNum}`
2026-07-28 21:55:39 -04:00
}
2026-07-30 21:38:01 -04:00
if (epNum > 0) {
return `${prefix} ${epNum}`
}
return trimmed
2026-07-28 21:55:39 -04:00
}