|
8 | 8 | export let showEpisodeImage: boolean = false; |
9 | 9 |
|
10 | 10 | const dispatch = createEventDispatcher(); |
| 11 | + const dateFormatter = new Intl.DateTimeFormat("en-GB", { |
| 12 | + day: "2-digit", |
| 13 | + month: "long", |
| 14 | + year: "numeric" |
| 15 | + }); |
| 16 | + const formattedDateCache = new Map<string, string>(); |
11 | 17 |
|
12 | 18 | function onClickEpisode() { |
13 | 19 | dispatch("clickEpisode", { episode }); |
|
17 | 23 | dispatch("contextMenu", { episode, event }); |
18 | 24 | } |
19 | 25 |
|
20 | | - let _date: Date; |
21 | | - let date: string; |
| 26 | + function parseEpisodeDate(rawDate?: Date): Date | null { |
| 27 | + if (!rawDate) return null; |
| 28 | + const parsedDate = new Date(rawDate); |
| 29 | + return Number.isNaN(parsedDate.getTime()) ? null : parsedDate; |
| 30 | + } |
22 | 31 |
|
23 | | - $: { |
24 | | - _date = new Date(episode.episodeDate || ""); |
25 | | - date = window.moment(_date).format("DD MMMM YYYY"); |
| 32 | + function getCacheKey(ep: Episode, parsedDate: Date): string { |
| 33 | + const identifier = ep.url ?? ep.streamUrl ?? ep.title ?? "episode"; |
| 34 | + return `${identifier}|${parsedDate.getTime()}`; |
26 | 35 | } |
| 36 | +
|
| 37 | + function formatEpisodeDate(ep: Episode): string { |
| 38 | + const parsedDate = parseEpisodeDate(ep?.episodeDate); |
| 39 | + if (!parsedDate) return ""; |
| 40 | +
|
| 41 | + const cacheKey = getCacheKey(ep, parsedDate); |
| 42 | + const cachedDate = formattedDateCache.get(cacheKey); |
| 43 | + if (cachedDate) return cachedDate; |
| 44 | +
|
| 45 | + const formattedDate = dateFormatter.format(parsedDate); |
| 46 | + formattedDateCache.set(cacheKey, formattedDate); |
| 47 | + return formattedDate; |
| 48 | + } |
| 49 | +
|
| 50 | + let date: string = ""; |
| 51 | +
|
| 52 | + $: date = formatEpisodeDate(episode); |
27 | 53 | </script> |
28 | 54 |
|
29 | 55 | <button |
|
0 commit comments