UNPKG

china-location

Version:

Chinese Administrative Division information, 中国行政区划信息

1 lines 8.85 kB
{"version":3,"file":"ChinaLocation.modern.mjs","sources":["../lib/ChinaLocation.ts"],"sourcesContent":["'use strict';\n\ninterface LocationItem {\n code: string;\n name?: string;\n}\ninterface ProvinceLocationItem extends LocationItem {\n cities: object;\n}\ninterface CityLocationItem extends LocationItem {\n districts: object;\n}\ninterface Address {\n province: LocationItem;\n city: LocationItem;\n district: LocationItem;\n}\n\nclass ChinaLocation {\n locationData: object;\n currentProvinces: Array<LocationItem>;\n currentCities: Array<LocationItem>;\n currentDistricts: Array<LocationItem>;\n activeProvince: string;\n activeCity: string;\n activeDistrict: string;\n\n constructor(locationData) {\n\n this.locationData = locationData;\n\n this.currentProvinces = this.getProvinces();\n const defaultProvince: LocationItem = this.currentProvinces[0];\n this.currentCities = this.getCities(defaultProvince);\n const defaultCity = this.currentCities[0];\n this.currentDistricts = this.getDistricts(defaultCity, defaultProvince);\n\n this.activeProvince = defaultProvince.code; //province code\n this.activeCity = defaultCity.code; //city code\n this.activeDistrict = this.currentDistricts[0].code; //district code\n }\n\n getProvinces(): Array<LocationItem> {\n const provinceKeys = Object.keys(this.locationData);\n const provinces = [];\n provinceKeys.forEach(provinceKey => {\n const provinceData = this.locationData[provinceKey];\n provinces.push({\n code: provinceData.code,\n name: provinceData.name,\n });\n });\n return provinces;\n }\n\n getCities(currentProvince: LocationItem): Array<LocationItem> {\n const citiesObject = this.locationData[currentProvince.code].cities;\n const cityKeys = Object.keys(citiesObject);\n const cities = [];\n cityKeys.forEach(cityKey => {\n const cityData = citiesObject[cityKey];\n cities.push({\n code: cityData.code,\n name: cityData.name,\n });\n });\n return cities;\n }\n\n getDistricts(\n currentCity: LocationItem,\n currentProvince: LocationItem\n ): Array<LocationItem> {\n const provincesObject = this.locationData[currentProvince.code];\n const currentCityObject = provincesObject.cities[currentCity.code];\n const districtsObject = currentCityObject.districts;\n const districtKeys = Object.keys(districtsObject);\n const districts = [];\n districtKeys.forEach(districtKey => {\n districts.push({\n code: districtKey,\n name: districtsObject[districtKey],\n });\n });\n return districts;\n }\n\n getProvinceByCode(code: string): ProvinceLocationItem {\n return this.locationData[code];\n }\n\n getCityByCode(cityCode: string, provinceCode: string): CityLocationItem {\n return this.getProvinceByCode(provinceCode).cities[cityCode];\n }\n\n getDistrictByCode(\n districtCode: string,\n cityCode: string,\n provinceCode: string\n ): string {\n return this.getCityByCode(cityCode, provinceCode).districts[districtCode];\n }\n\n getCity(code: string, cities: object): CityLocationItem {\n return cities[code];\n }\n\n getDistrict(code: string, districts: object): string {\n return districts[code];\n }\n\n changeLocation(\n provinceCode: string,\n cityCode?: string,\n districtCode?: string\n ): this {\n this.changeProvince(provinceCode, cityCode, districtCode);\n return this;\n }\n\n changeProvince(\n provinceCode: string,\n cityCode?: string,\n districtCode?: string\n ): void {\n const cities = this.getCities({ code: provinceCode } as LocationItem);\n let city = cities[0];\n if (cityCode) {\n let i = 0;\n for (; i < cities.length; i++) {\n const tempCity = cities[i];\n if (tempCity.code == cityCode) {\n city = tempCity;\n break;\n }\n }\n }\n\n this.currentCities = cities;\n this.activeProvince = provinceCode;\n this.changeCity(city ? city.code : '', districtCode);\n }\n\n changeCity(cityCode: string, districtCode?: string): void {\n const districts = this.getDistricts(\n {\n code: cityCode,\n },\n {\n code: this.activeProvince,\n }\n );\n\n this.currentDistricts = districts;\n this.activeCity = cityCode;\n\n let district = districts[0];\n if (districtCode) {\n let i = 0;\n for (; i < districts.length; i++) {\n const tempDistrict = districts[i];\n if (tempDistrict.code == districtCode) {\n district = tempDistrict;\n break;\n }\n }\n }\n\n this.changeDistrict(district ? district.code : '');\n }\n\n changeDistrict(districtCode: string): void {\n this.activeDistrict = districtCode;\n }\n\n getCurrentAddress(): Address {\n const originalProvince = this.getProvinceByCode(this.activeProvince);\n const originalCity = this.getCity(this.activeCity, originalProvince.cities);\n const originalDistrict = this.getDistrict(\n this.activeDistrict,\n originalCity.districts\n );\n\n return {\n province: {\n code: originalProvince.code,\n name: originalProvince.name,\n },\n city: {\n code: originalCity.code,\n name: originalCity.name,\n },\n district: {\n code: this.activeDistrict,\n name: originalDistrict || '',\n },\n };\n }\n\n getCurrentProvinces(): Array<LocationItem> {\n return this.currentProvinces;\n }\n\n getCurrentCities(): Array<LocationItem> {\n return this.currentCities;\n }\n\n getCurrentDistricts(): Array<LocationItem> {\n return this.currentDistricts;\n }\n}\n\nexport default ChinaLocation;\n"],"names":["constructor","locationData","this","currentProvinces","getProvinces","defaultProvince","currentCities","getCities","defaultCity","currentDistricts","getDistricts","activeProvince","code","activeCity","activeDistrict","provinceKeys","Object","keys","provinces","forEach","provinceKey","provinceData","push","name","currentProvince","citiesObject","cities","cityKeys","cityKey","cityData","currentCity","districtsObject","districts","districtKeys","districtKey","getProvinceByCode","getCityByCode","cityCode","provinceCode","getDistrictByCode","districtCode","getCity","getDistrict","changeLocation","changeProvince","city","i","length","tempCity","changeCity","district","tempDistrict","changeDistrict","getCurrentAddress","originalProvince","originalCity","originalDistrict","province","getCurrentProvinces","getCurrentCities","getCurrentDistricts"],"mappings":"eAkBA,MASEA,YAAYC,GAEVC,KAAKD,aAAeA,EAEpBC,KAAKC,iBAAmBD,KAAKE,eAC7B,IAAMC,EAAgCH,KAAKC,iBAAiB,GAC5DD,KAAKI,cAAgBJ,KAAKK,UAAUF,GACpC,IAAMG,EAAcN,KAAKI,cAAc,GACvCJ,KAAKO,iBAAmBP,KAAKQ,aAAaF,EAAaH,GAEvDH,KAAKS,eAAiBN,EAAgBO,KACtCV,KAAKW,WAAaL,EAAYI,KAC9BV,KAAKY,eAAiBZ,KAAKO,iBAAiB,GAAGG,KAGjDR,eACE,IAAMW,EAAeC,OAAOC,KAAKf,KAAKD,cAChCiB,EAAY,GAQlB,OAPAH,EAAaI,QAAQC,IACnB,IAAMC,EAAenB,KAAKD,aAAamB,GACvCF,EAAUI,KAAK,CACbV,KAAMS,EAAaT,KACnBW,KAAMF,EAAaE,SAGhBL,EAGTX,UAAUiB,G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