mirror of https://github.com/iptv-org/iptv
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.
75 lines
1.8 KiB
TypeScript
75 lines
1.8 KiB
TypeScript
import { SubdivisionData, SubdivisionSerializedData } from '../types/subdivision'
|
|
import { Dictionary, Collection } from '@freearhey/core'
|
|
import { Country, Region } from '.'
|
|
|
|
export class Subdivision {
|
|
code: string
|
|
name: string
|
|
countryCode: string
|
|
country?: Country
|
|
parentCode?: string
|
|
regions?: Collection
|
|
cities?: Collection
|
|
|
|
constructor(data?: SubdivisionData) {
|
|
if (!data) return
|
|
|
|
this.code = data.code
|
|
this.name = data.name
|
|
this.countryCode = data.country
|
|
this.parentCode = data.parent || undefined
|
|
}
|
|
|
|
withCountry(countriesKeyByCode: Dictionary): this {
|
|
this.country = countriesKeyByCode.get(this.countryCode)
|
|
|
|
return this
|
|
}
|
|
|
|
withRegions(regions: Collection): this {
|
|
this.regions = regions.filter((region: Region) =>
|
|
region.countryCodes.includes(this.countryCode)
|
|
)
|
|
|
|
return this
|
|
}
|
|
|
|
withCities(citiesGroupedBySubdivisionCode: Dictionary): this {
|
|
this.cities = new Collection(citiesGroupedBySubdivisionCode.get(this.code))
|
|
|
|
return this
|
|
}
|
|
|
|
getRegions(): Collection {
|
|
if (!this.regions) return new Collection()
|
|
|
|
return this.regions
|
|
}
|
|
|
|
getCities(): Collection {
|
|
if (!this.cities) return new Collection()
|
|
|
|
return this.cities
|
|
}
|
|
|
|
serialize(): SubdivisionSerializedData {
|
|
return {
|
|
code: this.code,
|
|
name: this.name,
|
|
countryCode: this.countryCode,
|
|
country: this.country ? this.country.serialize() : undefined,
|
|
parentCode: this.parentCode || null
|
|
}
|
|
}
|
|
|
|
deserialize(data: SubdivisionSerializedData): this {
|
|
this.code = data.code
|
|
this.name = data.name
|
|
this.countryCode = data.countryCode
|
|
this.country = data.country ? new Country().deserialize(data.country) : undefined
|
|
this.parentCode = data.parentCode || undefined
|
|
|
|
return this
|
|
}
|
|
}
|