Files
iptv/scripts/models/subdivision.ts

75 lines
1.8 KiB
TypeScript
Raw Normal View History

2025-04-16 20:54:55 +03:00
import { SubdivisionData, SubdivisionSerializedData } from '../types/subdivision'
2025-08-23 17:47:03 +03:00
import { Dictionary, Collection } from '@freearhey/core'
import { Country, Region } from '.'
2025-03-29 11:39:46 +03:00
2023-09-15 18:40:35 +03:00
export class Subdivision {
code: string
name: string
2025-03-29 11:39:46 +03:00
countryCode: string
country?: Country
2025-08-23 17:47:03 +03:00
parentCode?: string
regions?: Collection
cities?: Collection
2025-03-29 11:39:46 +03:00
2025-04-16 20:54:55 +03:00
constructor(data?: SubdivisionData) {
if (!data) return
2025-03-29 11:39:46 +03:00
this.code = data.code
this.name = data.name
this.countryCode = data.country
2025-08-23 17:47:03 +03:00
this.parentCode = data.parent || undefined
2025-03-29 11:39:46 +03:00
}
2025-04-16 20:54:55 +03:00
withCountry(countriesKeyByCode: Dictionary): this {
this.country = countriesKeyByCode.get(this.countryCode)
return this
}
2025-08-23 17:47:03 +03:00
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
}
2025-04-16 20:54:55 +03:00
serialize(): SubdivisionSerializedData {
return {
code: this.code,
name: this.name,
2025-08-23 17:47:03 +03:00
countryCode: this.countryCode,
country: this.country ? this.country.serialize() : undefined,
parentCode: this.parentCode || null
2025-04-16 20:54:55 +03:00
}
}
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
2025-08-23 17:47:03 +03:00
this.parentCode = data.parentCode || undefined
2023-09-15 18:40:35 +03:00
2025-03-29 11:39:46 +03:00
return this
2023-09-15 18:40:35 +03:00
}
}