You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iptv/scripts/models/region.ts

119 lines
3.1 KiB
TypeScript

8 months ago
import { Collection, Dictionary } from '@freearhey/core'
3 months ago
import { City, Country, Subdivision } from '.'
7 months ago
import type { RegionData, RegionSerializedData } from '../types/region'
import { CountrySerializedData } from '../types/country'
import { SubdivisionSerializedData } from '../types/subdivision'
3 months ago
import { CitySerializedData } from '../types/city'
2 years ago
export class Region {
code: string
name: string
8 months ago
countryCodes: Collection
3 months ago
countries?: Collection
subdivisions?: Collection
cities?: Collection
regions?: Collection
7 months ago
constructor(data?: RegionData) {
if (!data) return
2 years ago
8 months ago
this.code = data.code
this.name = data.name
this.countryCodes = new Collection(data.countries)
}
7 months ago
withCountries(countriesKeyByCode: Dictionary): this {
this.countries = this.countryCodes.map((code: string) => countriesKeyByCode.get(code))
8 months ago
return this
}
withSubdivisions(subdivisions: Collection): this {
this.subdivisions = subdivisions.filter(
(subdivision: Subdivision) => this.countryCodes.indexOf(subdivision.countryCode) > -1
)
return this
}
3 months ago
withCities(cities: Collection): this {
this.cities = cities.filter((city: City) => this.countryCodes.indexOf(city.countryCode) > -1)
return this
}
withRegions(regions: Collection): this {
this.regions = regions.filter(
(region: Region) => !region.countryCodes.intersects(this.countryCodes).isEmpty()
)
return this
}
8 months ago
getSubdivisions(): Collection {
3 months ago
if (!this.subdivisions) return new Collection()
7 months ago
return this.subdivisions
8 months ago
}
getCountries(): Collection {
3 months ago
if (!this.countries) return new Collection()
7 months ago
return this.countries
8 months ago
}
3 months ago
getCities(): Collection {
if (!this.cities) return new Collection()
return this.cities
}
getRegions(): Collection {
if (!this.regions) return new Collection()
return this.regions
8 months ago
}
3 months ago
includesCountryCode(code: string): boolean {
return this.countryCodes.includes((countryCode: string) => countryCode === code)
2 years ago
}
7 months ago
3 months ago
isWorldwide(): boolean {
return ['INT', 'WW'].includes(this.code)
}
7 months ago
serialize(): RegionSerializedData {
return {
code: this.code,
name: this.name,
countryCodes: this.countryCodes.all(),
3 months ago
countries: this.getCountries()
.map((country: Country) => country.serialize())
.all(),
subdivisions: this.getSubdivisions()
7 months ago
.map((subdivision: Subdivision) => subdivision.serialize())
3 months ago
.all(),
cities: this.getCities()
.map((city: City) => city.serialize())
7 months ago
.all()
}
}
deserialize(data: RegionSerializedData): this {
this.code = data.code
this.name = data.name
this.countryCodes = new Collection(data.countryCodes)
this.countries = new Collection(data.countries).map((data: CountrySerializedData) =>
new Country().deserialize(data)
)
this.subdivisions = new Collection(data.subdivisions).map((data: SubdivisionSerializedData) =>
new Subdivision().deserialize(data)
)
3 months ago
this.cities = new Collection(data.cities).map((data: CitySerializedData) =>
new City().deserialize(data)
)
7 months ago
return this
}
2 years ago
}