diff --git a/src/models/channel.js b/src/models/channel.js index 80bf3fafe..0d24bb259 100644 --- a/src/models/channel.js +++ b/src/models/channel.js @@ -41,4 +41,10 @@ export class Channel { this._guides = data.guides || [] this._blocklistRecords = data.blocklistRecords || [] } + + toObject() { + const { ...object } = this + + return object + } } diff --git a/src/pages/channels/[country]/[name]/+page.server.js b/src/pages/channels/[country]/[name]/+page.server.js index 41d783298..ac54fd7a0 100644 --- a/src/pages/channels/[country]/[name]/+page.server.js +++ b/src/pages/channels/[country]/[name]/+page.server.js @@ -1,4 +1,4 @@ -import { transformChannel } from '~/store' +import { createChannel } from '~/store' import _ from 'lodash' import channels from '~/data/channels.json' import countries from '~/data/countries.json' @@ -47,6 +47,6 @@ export function load({ params }) { let channel = data.channels[id] return { - channel: channel ? transformChannel(channel, data) : null + channel: channel ? createChannel(channel, data).toObject() : null } } diff --git a/src/store.js b/src/store.js index aca823d4c..199fa5023 100644 --- a/src/store.js +++ b/src/store.js @@ -85,6 +85,61 @@ export function setPageTitle(value) { } } +export function createChannel(data, api) { + let broadcastArea = [] + let regionCountries = [] + + data.broadcast_area.forEach(areaCode => { + const [type, code] = areaCode.split('/') + switch (type) { + case 'c': + const country = api.countries[code] + if (country) broadcastArea.push({ type, code: country.code, name: country.name }) + break + case 'r': + const region = api.regions[code] + if (region) { + broadcastArea.push({ type, code: region.code, name: region.name }) + regionCountries = [ + ...regionCountries, + ...region.countries.map(code => api.countries[code]).filter(Boolean) + ] + } + break + case 's': + const subdivision = api.subdivisions[code] + if (subdivision) + broadcastArea.push({ type, code: subdivision.code, name: subdivision.name }) + break + } + }) + + return new Channel({ + id: data.id, + name: data.name, + altNames: data.alt_names, + network: data.network, + owners: data.owners, + city: data.city, + country: api.countries[data.country], + subdivision: api.subdivisions[data.subdivision], + languages: data.languages.map(code => api.languages[code]).filter(Boolean), + categories: data.categories.map(id => api.categories[id]).filter(Boolean), + isNSFW: data.is_nsfw, + launched: data.launched, + closed: data.closed, + replacedBy: data.replaced_by, + website: data.website, + logo: data.logo, + streams: api.streams[data.id], + guides: api.guides[data.id], + blocklistRecords: api.blocklist[data.id], + hasUniqueName: api.nameIndex[data.name.toLowerCase()].length === 1, + broadcastArea, + regionCountries + }) +} + async function loadAPI() { const api = {} @@ -159,61 +214,6 @@ async function loadAPI() { return api } -function createChannel(data, api) { - let broadcastArea = [] - let regionCountries = [] - - data.broadcast_area.forEach(areaCode => { - const [type, code] = areaCode.split('/') - switch (type) { - case 'c': - const country = api.countries[code] - if (country) broadcastArea.push({ type, code: country.code, name: country.name }) - break - case 'r': - const region = api.regions[code] - if (region) { - broadcastArea.push({ type, code: region.code, name: region.name }) - regionCountries = [ - ...regionCountries, - ...region.countries.map(code => api.countries[code]).filter(Boolean) - ] - } - break - case 's': - const subdivision = api.subdivisions[code] - if (subdivision) - broadcastArea.push({ type, code: subdivision.code, name: subdivision.name }) - break - } - }) - - return new Channel({ - id: data.id, - name: data.name, - altNames: data.alt_names, - network: data.network, - owners: data.owners, - city: data.city, - country: api.countries[data.country], - subdivision: api.subdivisions[data.subdivision], - languages: data.languages.map(code => api.languages[code]).filter(Boolean), - categories: data.categories.map(id => api.categories[id]).filter(Boolean), - isNSFW: data.is_nsfw, - launched: data.launched, - closed: data.closed, - replacedBy: data.replaced_by, - website: data.website, - logo: data.logo, - streams: api.streams[data.id], - guides: api.guides[data.id], - blocklistRecords: api.blocklist[data.id], - hasUniqueName: api.nameIndex[data.name.toLowerCase()].length === 1, - broadcastArea, - regionCountries - }) -} - function getStreams() { let streams = [] get(selected).forEach(channel => {