safety-eval-service-frontend/scripts/gen-china-districts.js

36 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 生成全国县级行政区划快照node scripts/gen-china-districts.js
* 数据源devDependency @province-city-china/dataGB/T 2260省市区四级全量
* 输出src/enumerate/chinaDistricts.json仅保留县级约 3300 条([code, 名称, 省+市]160KB 左右)
* 行政区划调整后重新执行本脚本即可刷新快照
*/
const fs = require("fs");
const path = require("path");
const all = require("@province-city-china/data");
/** 省级名称表code 形如 XX0000 */
const provinceNameByCode = {};
/** 地级名称表code 形如 XXYY00 */
const cityNameByCode = {};
all.forEach((item) => {
const isProvince = item.city === 0 && item.area === 0 && item.town === 0;
const isCity = item.city !== 0 && item.area === 0 && item.town === 0;
if (isProvince) provinceNameByCode[item.province] = item.name;
if (isCity) cityNameByCode[item.province + item.city] = item.name;
});
const districts = all
.filter((item) => item.city !== 0 && item.area !== 0 && item.town === 0)
.map((item) => {
const province = provinceNameByCode[item.province] || "";
const city = cityNameByCode[item.province + item.city] || "";
// 直辖市等省市同名时不重复拼接
const region = city && city !== province ? `${province}${city}` : province;
return [item.code, item.name, region];
});
const outFile = path.join(__dirname, "../src/enumerate/chinaDistricts.json");
fs.writeFileSync(outFile, `${JSON.stringify(districts)}\n`, "utf8");
console.log(`已生成 ${path.relative(process.cwd(), outFile)}${districts.length}`);