Add linked Chinese postal code input and lookup data
This commit is contained in:
1 parent
3f685ca068
commit
1e43d910c0
8 files changed
+141
-4
No files matched your search
@@ -0,0 +1,5 @@
|
||||
// 由 tombcato/china-zipcode-data 的 china_zipcode_adcode.json 生成。
|
||||
// 上游项目采用 MIT License:https://github.com/tombcato/china-zipcode-data
|
||||
export type ChinaPostcodeRow = readonly [districtCode: string, postcode: string]
|
||||
|
||||
export const chinaPostcodeRows: readonly ChinaPostcodeRow[] = [["110101","100010"],["110102","100032"],["110105","100020"],["110106","100071"],["110107","100043"],["110108","100089"],["110109","102300"],["110111","102488"],["110112","101149"],["110113","101300"],["110114","102200"],["110115","102600"],["110116","101400"],["110117","101200"],["110118","101500"],["110119","102100"],["120101","300041"],["120102","300171"],["120103","300202"],["120104","300110"],["120105","300143"],["120106","300131"],["120110","300300"],["120111","300380"],["120112","300350"],["120113","300400"],["120114","301700"],["120115","301800"],["120116","300450"],["120117","301500"],["120118","301600"],["120119","301900"],["130102","050000"],["130104","050051"],["130105","050000"],["130107","050100"],["130108","050011"],["130109","052160"],["130110","050200"],["130111","051430"],["130121","050300"],["130123","050800"],["130125","050600"],["130126","050500"],["130127","051330"],["130128","052560"],["130129","051230"],["130130","052460"],["130131","050400"],["130132","051130"],["130133","051530"],["130181","052360"],["130183","052260"],["130184","050700"],["130202","063017"],["130203","063000"],["130204","063100"],["130205","063021"],["130207","063300"],["130208","064000"],["130209","063200"],["130224","063500"],["130225","063600"],["130227","064300"],["130229","064100"],["130281","064200"],["130283","064400"],["130284","063700"],["130302","066000"],["130303","066200"],["130304","066100"],["130306","066300"],["130321","066500"],["130322","066600"],["130324","066400"],["130402","056001"],["130403","056004"],["130404","056002"],["130406","056200"],["130407","057550"],["130408","057150"],["130423","056600"],["130424","056700"],["130425","056900"],["130426","056400"],["130427","056500"],["130430","057450"],["130431","057350"],["130432","057650"],["130433","057750"],["130434","056800"],["130435","057250"],["130481","056300"],["130502","054000"],["130503","054000"],["130505","054400"],["130506","054400"],["130522","054300"],["130523","054200"],["130524","055450"],["130525","055350"],["130528","055550"],["130529","055250"],["130530","055650"],["130531","054600"],["130532","054500"],["130533","054700"],["130534","054800"],["130535","054900"],["130581","055750"],["130582","054100"],["130602","071052"],["130606","071000"],["130607","072150"],["130608","071100"],["130609","072550"],["130623","074100"],["130624","073200"],["130626","072650"],["130627","072350"],["130628","071500"],["130629","071700"],["130630","074300"],["130631","072450"],["130632","071600"],["130633","074200"],["130634","073100"],["130635","071400"],["130636","072250"],["130637","071300"],["130638","071800"],["130681","072750"],["130682","073000"],["130683","071200"],["130684","074000"],["130702","075000"],["130703","075061"],["130705","075100"],["130706","075300"],["130708","076250"],["130709","076350"],["130722","076450"],["130723","076650"],["130724","076550"],["130725","076750"],["130726","075700"],["130727","075800"],["130728","076150"],["130730","075400"],["130731","075600"],["130732","075500"],["130802","067000"],["130803","067000"],["130804","067200"],["130821","067400"],["130822","067300"],["130824","068250"],["130825","068150"],["130826","068350"],["130827","067600"],["130828","068450"],["130881","067500"],["130902","061000"],["130903","061000"],["130921","061000"],["130922","062650"],["130923","061600"],["130924","061200"],["130925","061300"],["130926","062350"],["130927","061500"],["130928","061800"],["130929","062250"],["130930","061400"],["130981","062150"],["130982","062550"],["130983","061100"],["130984","062450"],["131002","065000"],["131003","065000"],["131022","065500"],["131023","065600"],["131024","065400"],["131025","065900"],["131026","065800"],["131028","065300"],["131081","065700"],["131082","065200"],["131102","053000"],["131103","053200"],["131121","053100"],["131122","053400"],["131123","053300"],["131124","053900"],["131125","053600"],["131126","253800"],["131127","053500"],["131128","053700"],["131182","053800"],["140105","030000"],["140106","030000"],["140107","030000"],["140108","030000"],["140109","030000"],["140110","030000"],["140121","030400"],["140122","030100"],["140123","030300"],["140181","030200"],["140212","037002"],["140213","037500"],["140214","037000"],["140215","038300"],["140221","038100"],["140222","038200"],["140223","037500"],["140224","034400"],["140225","037400"],["140226","037100"],["140302","045000"],["140303","045000"],["140311","045000"],["140321","045200"],["140322","045100"],["140403","047500"],["140404","046000"],["140405","046100"],["140406","047500"],["140423","046200"],["140425","047400"],["140426","047600"],["140427","047300"],["140428","046600"],["140429","046300"],["140430","046400"],["140431","046500"],["140502","048000"],["140521","048200"],["140522","048100"],["140524","048300"],["140525","048012"],["140581","048400"],["140602","036002"],["140603","036800"],["140621","036900"],["140622","037600"],["140623","03720Line truncated
|
||||
@@ -0,0 +1,47 @@
|
||||
import { nativePlaceRows } from './nativePlaces.generated'
|
||||
import { chinaPostcodeRows } from './chinaPostcodes.generated'
|
||||
|
||||
type NativePlace = { id: number, pid: number, deep: number, name: string }
|
||||
|
||||
const places = nativePlaceRows.map(([id, pid, deep, name]) => ({ id, pid, deep, name }))
|
||||
const placesById = new Map(places.map((place) => [place.id, place]))
|
||||
const postcodeByDistrictCode = new Map(chinaPostcodeRows)
|
||||
|
||||
function pathFor(place: NativePlace) {
|
||||
const path = [place]
|
||||
let parent = placesById.get(place.pid)
|
||||
while (parent) {
|
||||
path.unshift(parent)
|
||||
parent = placesById.get(parent.pid)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
export function districtCodeForNativePlace(value: string) {
|
||||
const normalized = value.trim()
|
||||
const place = places.find((item) =>
|
||||
item.deep >= 2 && pathFor(item).map((entry) => entry.name).join(' / ') === normalized,
|
||||
)
|
||||
const district = place && pathFor(place)[2]
|
||||
return district && /^\d{6}$/.test(String(district.id)) ? String(district.id) : undefined
|
||||
}
|
||||
|
||||
export function nativePlaceForDistrictCode(districtCode: string) {
|
||||
const district = placesById.get(Number(districtCode))
|
||||
return district?.deep === 2 ? pathFor(district).map((place) => place.name).join(' / ') : undefined
|
||||
}
|
||||
|
||||
export function postcodeForNativePlace(value: string) {
|
||||
const districtCode = districtCodeForNativePlace(value)
|
||||
return districtCode ? postcodeByDistrictCode.get(districtCode) : undefined
|
||||
}
|
||||
|
||||
export function nativePlaceCandidatesForPostcode(postcode: string) {
|
||||
const query = postcode.replace(/\D/g, '').slice(0, 6)
|
||||
if (query.length !== 4 && query.length !== 6) return []
|
||||
const candidates = chinaPostcodeRows
|
||||
.filter(([, value]) => query.length === 6 ? value === query : value.startsWith(query))
|
||||
.map(([districtCode]) => nativePlaceForDistrictCode(districtCode))
|
||||
.filter((value): value is string => Boolean(value))
|
||||
return [...new Set(candidates)]
|
||||
}
|
||||
Reference in new issue
Block a user