36 lines
1.6 KiB
JavaScript
36 lines
1.6 KiB
JavaScript
/**
|
||
* 生成全国县级行政区划快照:node scripts/gen-china-districts.js
|
||
* 数据源:devDependency @province-city-china/data(GB/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} 条`);
|