Files
Academic-Affairs-System/web/scripts/generate-china-postcode-data.mjs
T

18 lines
930 B
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { readFile, writeFile } from 'node:fs/promises'
import { resolve } from 'node:path'
const [inputFile, outputFile] = process.argv.slice(2)
if (!inputFile || !outputFile) {
throw new Error('用法:node scripts/generate-china-postcode-data.mjs <china_zipcode_adcode.json> <输出文件>')
}
const sourceData = JSON.parse(await readFile(resolve(inputFile), 'utf8'))
const rows = sourceData
.filter((row) => /^\d{6}$/.test(row.code) && /^\d{6}$/.test(row.zipCode))
.map((row) => [row.code, row.zipCode])
const source = `// 由 tombcato/china-zipcode-data 的 china_zipcode_adcode.json 生成。\n// 上游项目采用 MIT License:https://github.com/tombcato/china-zipcode-data\nexport type ChinaPostcodeRow = readonly [districtCode: string, postcode: string]\n\nexport const chinaPostcodeRows: readonly ChinaPostcodeRow[] = ${JSON.stringify(rows)} as const\n`
await writeFile(resolve(outputFile), source, 'utf8')