Update scripts

This commit is contained in:
freearhey
2025-04-16 20:54:55 +03:00
parent d39af32f18
commit d42b102cdf
39 changed files with 1256 additions and 508 deletions

View File

@@ -1,27 +1,26 @@
import { Collection, Dictionary } from '@freearhey/core'
import { Subdivision } from '.'
type RegionData = {
code: string
name: string
countries: string[]
}
import { Country, Subdivision } from '.'
import type { RegionData, RegionSerializedData } from '../types/region'
import { CountrySerializedData } from '../types/country'
import { SubdivisionSerializedData } from '../types/subdivision'
export class Region {
code: string
name: string
countryCodes: Collection
countries?: Collection
subdivisions?: Collection
countries: Collection = new Collection()
subdivisions: Collection = new Collection()
constructor(data?: RegionData) {
if (!data) return
constructor(data: RegionData) {
this.code = data.code
this.name = data.name
this.countryCodes = new Collection(data.countries)
}
withCountries(countriesGroupedByCode: Dictionary): this {
this.countries = this.countryCodes.map((code: string) => countriesGroupedByCode.get(code))
withCountries(countriesKeyByCode: Dictionary): this {
this.countries = this.countryCodes.map((code: string) => countriesKeyByCode.get(code))
return this
}
@@ -35,11 +34,11 @@ export class Region {
}
getSubdivisions(): Collection {
return this.subdivisions || new Collection()
return this.subdivisions
}
getCountries(): Collection {
return this.countries || new Collection()
return this.countries
}
includesCountryCode(code: string): boolean {
@@ -49,4 +48,30 @@ export class Region {
isWorldwide(): boolean {
return this.code === 'INT'
}
serialize(): RegionSerializedData {
return {
code: this.code,
name: this.name,
countryCodes: this.countryCodes.all(),
countries: this.countries.map((country: Country) => country.serialize()).all(),
subdivisions: this.subdivisions
.map((subdivision: Subdivision) => subdivision.serialize())
.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)
)
return this
}
}