UNPKG

@ipdb/cac

Version:

IPIP.net database patches (China Administrative divisions Code)

33 lines (29 loc) 757 B
const list = require('./data') const query = (country, region, city) => { if (country === '中国') { if (list[region]) { const result = list[region][city] || list[region][region] return result.toString() } // 尝试前缀匹配 for (const key in list) { if (region.indexOf(key) === 0) { const cityList = list[key] for (const cityKey in cityList) { if (city.indexOf(cityKey) === 0) { return cityList[cityKey].toString() } } } } return '100000' } return '' } const patch = data => { if (!data.china_admin_code) { data.china_admin_code = query(data.country_name, data.region_name, data.city_name) } return data } module.exports = patch