### Install cn-division Package Source: https://github.com/kk-418/cn-division/blob/main/README.md Install the package using npm, pnpm, or yarn. ```bash npm install cn-division # 或 pnpm add cn-division # 或 yarn add cn-division ``` -------------------------------- ### Install cn-division Package Source: https://context7.com/kk-418/cn-division/llms.txt Install the cn-division package using npm or pnpm. ```bash npm install cn-division # 或 pnpm add cn-division ``` -------------------------------- ### Example Output of Data Fetching Source: https://github.com/kk-418/cn-division/blob/main/README.md Illustrates the expected console output after running the 'npm run fetch' command, showing the progress of data acquisition, including sub-querying for specific cities and a summary of the parsed data. ```bash npm run fetch 执行后会看到类似输出: ``` 开始从民政部获取最新行政区划数据... 数据源: https://dmfw.mca.gov.cn/9095/xzqh/getList?code=&maxLevel=3 正在获取接口数据... 接口数据获取成功 开始解析接口数据... 下钻查询: 东莞市 (441900000000) 下钻查询: 中山市 (442000000000) 下钻查询: 儋州市 (460400000000) 解析完成: 省份 33 个, 城市 342 个, 区县 2923 个 数据验证完成 原始数据已保存到: temp/raw-data.json === 数据统计 === 省份数量: 31 城市数量: 342 区县数量: 2923 总计: 3296 ``` -------------------------------- ### Query Tool: Get Province by Code Source: https://context7.com/kk-418/cn-division/llms.txt Retrieves province information using either a 2-digit short code or a 6-digit full code. ```APIDOC ## Query Tool: Get Province by Code Retrieves province information using either a 2-digit short code or a 6-digit full code. ### Import ```js import { getProvinceByCode } from 'cn-division'; ``` ### Usage ```js getProvinceByCode('110000'); // Returns { c: '11', n: '北京市' } getProvinceByCode('11'); // Returns { c: '11', n: '北京市' } getProvinceByCode('44'); // Returns { c: '44', n: '广东省' } getProvinceByCode('99'); // Returns undefined ``` ``` -------------------------------- ### Get Name by Code (any length) Source: https://context7.com/kk-418/cn-division/llms.txt Automatically identifies code length to match province, city, or county and returns the corresponding name. Returns undefined for invalid codes. ```javascript import { getNameByCode } from 'cn-division'; getNameByCode('110000'); // '北京市' getNameByCode('1101'); // '北京市' getNameByCode('110101'); // '东城区' getNameByCode('441900401000'); // '松山湖' getNameByCode('232761'); // '加格达奇区' getNameByCode('000000'); // undefined ``` -------------------------------- ### Import Cascader Data (with codes) Source: https://github.com/kk-418/cn-division/blob/main/README.md Import the function to get cascader data including codes. This is useful for scenarios where codes are essential for data processing or display. ```javascript import { getCascaderDataWithCode } from 'cn-division'; // 导入: ~105K (pca-code.json) ``` -------------------------------- ### Get Address Value from Codes Source: https://github.com/kk-418/cn-division/blob/main/README.md Construct a unified AddressValue object by providing province, city, and county codes. ```javascript import { getAddressValueFromCodes } from 'cn-division'; const addr = getAddressValueFromCodes('11', '1101', '110101'); // { province: { code: '11', name: '北京市' }, city: { code: '1101', name: '北京市' }, county: { code: '110101', name: '东城区' } } ``` -------------------------------- ### Import Cascader Data (no codes) Source: https://github.com/kk-418/cn-division/blob/main/README.md Import the function to get cascader data without codes. This is recommended for general use cases where only names are needed, resulting in a smaller bundle size. ```javascript import { getCascaderData } from 'cn-division'; // 导入: ~43K (pca.json) ``` -------------------------------- ### WeChat Mini Program: Code Lookup Module Source: https://context7.com/kk-418/cn-division/llms.txt Imports a self-contained module for code lookup utilities (getting names, full paths, province by code), optimized for WeChat Mini Programs. ```javascript // Only import code lookup tools - ~143 KB import { getNameByCode, formatFullPath, getProvinceByCode } from 'cn-division/lookup'; ``` -------------------------------- ### Get City by Code (4 or 6 digits) Source: https://context7.com/kk-418/cn-division/llms.txt Retrieves city information using a 4-digit short code or a 6-digit standard code. Returns undefined if the code is not found. ```javascript import { getCityByCode } from 'cn-division'; getCityByCode('1101'); // { c: '1101', n: '北京市', p: '11' } getCityByCode('440300'); // { c: '4403', n: '深圳市', p: '44' } getCityByCode('9999'); // undefined ``` -------------------------------- ### getCityByCode() - Get City by Code Source: https://context7.com/kk-418/cn-division/llms.txt Retrieves city information using either a 4-digit short code or a 6-digit standard code. Returns city details or undefined if the code is invalid. ```APIDOC ## getCityByCode(code: string): object | undefined ### Description Retrieves city information based on a provided administrative code. It supports both 4-digit short codes (e.g., '4403') and 6-digit standard codes (e.g., '440300'). ### Parameters #### Path Parameters - **code** (string) - Required - The 4-digit short code or 6-digit standard code for the city. ### Request Example ```javascript import { getCityByCode } from 'cn-division'; getCityByCode('1101'); // Returns { c: '1101', n: '北京市', p: '11' } getCityByCode('440300'); // Returns { c: '4403', n: '深圳市', p: '44' } getCityByCode('9999'); // Returns undefined ``` ### Response #### Success Response (200) - **c** (string) - The city code. - **n** (string) - The city name. - **p** (string) - The province code. #### Error Response - Returns `undefined` if the provided code is invalid or not found. ``` -------------------------------- ### getCitiesByProvince() - Get Cities by Province Source: https://context7.com/kk-418/cn-division/llms.txt Fetches a list of all cities within a specified province, using either a 2-digit province code or a full 6-digit province code. ```APIDOC ## getCitiesByProvince(provinceCode: string): Array ### Description Retrieves a list of all cities belonging to a specified province. The province can be identified by its 2-digit code or its full 6-digit code. ### Parameters #### Path Parameters - **provinceCode** (string) - Required - The 2-digit province code or 6-digit province code. ### Request Example ```javascript import { getCitiesByProvince } from 'cn-division'; const jsCities = getCitiesByProvince('32'); // jsCities will be an array like [{ c: '3201', n: '南京市', p: '32' }, { c: '3202', n: '无锡市', p: '32' }, ...] const gdCities = getCitiesByProvince('440000'); console.log(gdCities.length); // Outputs the number of cities in Guangdong province (e.g., 21) ``` ### Response #### Success Response (200) - Returns an array of city objects. Each object contains: - **c** (string) - The city code. - **n** (string) - The city name. - **p** (string) - The province code. ``` -------------------------------- ### Get Cascader Data (with codes) Source: https://github.com/kk-418/cn-division/blob/main/README.md Retrieve province, city, and county data formatted for cascader components, where values are administrative codes. This is suitable for scenarios requiring precise identification. ```javascript import { getCascaderDataWithCode } from 'cn-division'; const options = getCascaderDataWithCode(); // [{ label: '北京市', value: '11', children: [{ label: '北京市', value: '1101', children: [...] }] }] ``` -------------------------------- ### getNameByCode() - Get Name by Code Source: https://context7.com/kk-418/cn-division/llms.txt Automatically identifies the code length and matches it against province, city, or county codes to return the corresponding name string. Returns undefined for unrecognized codes. ```APIDOC ## getNameByCode(code: string): string | undefined ### Description Retrieves the administrative division name (province, city, or county) by its code. The function automatically detects the code's length and matches it against the hierarchy. ### Parameters #### Path Parameters - **code** (string) - Required - The administrative code (can be for province, city, or county). ### Request Example ```javascript import { getNameByCode } from 'cn-division'; getNameByCode('110000'); // Returns '北京市' getNameByCode('1101'); // Returns '北京市' getNameByCode('110101'); // Returns '东城区' getNameByCode('441900401000'); // Returns '松山湖' getNameByCode('232761'); // Returns '加格达奇区' getNameByCode('000000'); // Returns undefined ``` ### Response #### Success Response (200) - Returns the name string of the administrative division. #### Error Response - Returns `undefined` if the code is invalid or not found. ``` -------------------------------- ### Get Cities by Province Code Source: https://context7.com/kk-418/cn-division/llms.txt Retrieves all cities within a given province. Accepts a 2-digit province code or a 6-digit full province code. ```javascript import { getCitiesByProvince } from 'cn-division'; const jsCities = getCitiesByProvince('32'); // [{ c: '3201', n: '南京市', p: '32' }, { c: '3202', n: '无锡市', p: '32' }, ...] // Also accepts full code format const gdCities = getCitiesByProvince('440000'); console.log(gdCities.length); // 21 ``` -------------------------------- ### getCountiesByCity() - Get Counties by City Source: https://context7.com/kk-418/cn-division/llms.txt Retrieves all districts and counties within a specified city, using either a 4-digit city code or a full 6-digit city code. ```APIDOC ## getCountiesByCity(cityCode: string): Array ### Description Retrieves a list of all districts and counties belonging to a specified city. The city can be identified by its 4-digit code or its full 6-digit code. ### Parameters #### Path Parameters - **cityCode** (string) - Required - The 4-digit city code or 6-digit city code. ### Request Example ```javascript import { getCountiesByCity } from 'cn-division'; const njCounties = getCountiesByCity('3201'); // njCounties will be an array like [{ c: '320102', n: '玄武区', cc: '3201' }, { c: '320104', n: '秦淮区', cc: '3201' }, ...] const szCounties = getCountiesByCity('4403'); console.log(szCounties.map(c => c.n)); // Outputs an array of county names like ['罗湖区', '福田区', '南山区', '宝安区', '龙岗区', '盐田区', '龙华区', '坪山区', '光明区'] ``` ### Response #### Success Response (200) - Returns an array of county objects. Each object contains: - **c** (string) - The county code. - **n** (string) - The county name. - **cc** (string) - The city code. ``` -------------------------------- ### Get Cascader Data (province-city, with codes) Source: https://github.com/kk-418/cn-division/blob/main/README.md Retrieve province and city data formatted for cascader components, where values are administrative codes. This is useful for applications like price lookups. ```javascript import { getCascaderPCWithCode } from 'cn-division'; const pcOptions = getCascaderPCWithCode(); // [{ label: '北京市', value: '11', children: [{ label: '北京市', value: '1101' }] }] ``` -------------------------------- ### getCountyByCode() - Get County by Code Source: https://context7.com/kk-418/cn-division/llms.txt Fetches county information using standard 6-digit codes or special 12-digit codes for specific functional areas. Returns county details or undefined for invalid codes. ```APIDOC ## getCountyByCode(code: string): object | undefined ### Description Retrieves county (district/county-level city) information using administrative codes. It supports standard 6-digit codes and special 12-digit codes used for certain functional areas (like Dongguan). ### Parameters #### Path Parameters - **code** (string) - Required - The 6-digit standard code or 12-digit special code for the county. ### Request Example ```javascript import { getCountyByCode } from 'cn-division'; getCountyByCode('110101'); // Returns { c: '110101', n: '东城区', cc: '1101' } getCountyByCode('441900401000'); // Returns { c: '441900401000', n: '松山湖', cc: '4419' } getCountyByCode('999999'); // Returns undefined ``` ### Response #### Success Response (200) - **c** (string) - The county code. - **n** (string) - The county name. - **cc** (string) - The city code. #### Error Response - Returns `undefined` if the provided code is invalid or not found. ``` -------------------------------- ### Query Functions for Administrative Data Source: https://github.com/kk-418/cn-division/blob/main/README.md Provides functions to retrieve detailed information about provinces, cities, and counties using their respective codes. Also includes functions to get names by code, check for municipality status, and retrieve lists of cities or counties within a given province or city. ```typescript // Get province information by code function getProvinceByCode(code: string): Province | undefined // Get city information by code function getCityByCode(code: string): City | undefined // Get county information by code function getCountyByCode(code: string): County | undefined // Get name by code function getNameByCode(code: string): string | undefined // Check if it's a municipality code function isMunicipalityCode(code: string): boolean // Get all cities within a province function getCitiesByProvince(provinceCode: string): City[] // Get all counties within a city function getCountiesByCity(cityCode: string): County[] // Validate code format function validateCode(code: string): boolean ``` -------------------------------- ### Get County by Code (6 or 12 digits) Source: https://context7.com/kk-418/cn-division/llms.txt Retrieves county information using a 6-digit standard code or a 12-digit special code for areas like Dongguan. Returns undefined if the code is not found. ```javascript import { getCountyByCode } from 'cn-division'; getCountyByCode('110101'); // { c: '110101', n: '东城区', cc: '1101' } getCountyByCode('441900401000'); // { c: '441900401000', n: '松山湖', cc: '4419' } getCountyByCode('999999'); // undefined ``` -------------------------------- ### Get Counties by City Code Source: https://context7.com/kk-418/cn-division/llms.txt Retrieves all counties within a given city. Accepts a 4-digit city code. ```javascript import { getCountiesByCity } from 'cn-division'; const njCounties = getCountiesByCity('3201'); // [{ c: '320102', n: '玄武区', cc: '3201' }, { c: '320104', n: '秦淮区', cc: '3201' }, ...] const szCounties = getCountiesByCity('4403'); console.log(szCounties.map(c => c.n)); // ['罗湖区', '福田区', '南山区', '宝安区', '龙岗区', '盐田区', '龙华区', '坪山区', '光明区'] ``` -------------------------------- ### Importing Specific Modules (Recommended for WeChat Mini Programs) Source: https://github.com/kk-418/cn-division/blob/main/README.md Demonstrates how to import only the necessary modules to minimize package size in WeChat Mini Programs, which do not support tree-shaking. Each module is self-contained and only the imported files contribute to the total package size. ```APIDOC ## Importing Specific Modules (Recommended for WeChat Mini Programs) ### Description WeChat Mini Programs do not support tree-shaking. Importing the entire `cn-division` package can increase the main package size by approximately 800KB. This package provides modules in the `miniprogram_dist/` directory, where each function is a separate, self-contained module. Only the imported module files will be included in the package size. ### Code Examples ```javascript // Import only the province-city level selector (with code) - approx. 106 KB import { getCascaderPCWithCode } from 'cn-division/cascader-pc-code'; // Import only address parsing - approx. 42 KB import { parseAddress } from 'cn-division/parse'; // Import only code lookup - approx. 143 KB import { getNameByCode, formatFullPath } from 'cn-division/lookup'; ``` ### Available Modules | Module Path | Exported Functions | Size | |---------------------------------|----------------------------------------------------------|--------| | `cn-division/cascader-pca-code` | `getCascaderDataWithCode()` | ~106 KB | | `cn-division/cascader-pca` | `getCascaderData()` | ~43 KB | | `cn-division/cascader-pc-code` | `getCascaderPCWithCode()` | ~106 KB | | `cn-division/cascader-pc` | `getCascaderPC()` | ~6 KB | | `cn-division/lookup` | `getNameByCode()`, `getProvinceByCode()`, `getCityByCode()`, `getCountyByCode()` etc. | ~143 KB | | `cn-division/parse` | `parseAddress()` | ~42 KB | Each module includes `.d.ts` type declarations for full TypeScript support. ``` -------------------------------- ### Import VERSION and MUNICIPALITY_CODES Constants Source: https://context7.com/kk-418/cn-division/llms.txt Imports and logs the library version and the list of municipality codes. ```javascript import { VERSION, MUNICIPALITY_CODES } from 'cn-division'; console.log(VERSION); // '2026.0.0' console.log(MUNICIPALITY_CODES); // ['11', '12', '31', '50'] ``` -------------------------------- ### Importing Constants from cn-division Source: https://github.com/kk-418/cn-division/blob/main/README.md Demonstrates how to import and use constants like MUNICIPALITY_CODES and VERSION from the 'cn-division' library. MUNICIPALITY_CODES lists codes for municipalities, and VERSION indicates the data version. ```javascript import { MUNICIPALITY_CODES, VERSION } from 'cn-division'; console.log(MUNICIPALITY_CODES); // ['11', '12', '31', '50'] console.log(VERSION); // '2026.0.0' ``` -------------------------------- ### Building Data from Raw Files Source: https://github.com/kk-418/cn-division/blob/main/README.md Converts the fetched raw data into various distributable formats, including coded and non-coded versions, and structured JSON files for provinces, cities, counties, and PCA/PC hierarchies. This command uses already fetched data. ```bash npm run build ``` -------------------------------- ### Miniprogram Specific Imports Source: https://context7.com/kk-418/cn-division/llms.txt Provides guidance on how to import specific modules for WeChat Mini Programs to manage bundle size, as they do not support tree-shaking. ```APIDOC ## WeChat Mini Program Specific Imports Due to the lack of tree-shaking support in WeChat Mini Programs, it's recommended to import functionality from specific modules within the `miniprogram_dist/` directory to control bundle size. ### Cascader Data (Province, City, Area) - **`getCascaderData`**: Imports data for a three-level cascader without codes (approx. 43 KB). ```javascript import { getCascaderData } from 'cn-division/cascader-pca'; ``` - **`getCascaderDataWithCode`**: Imports data for a three-level cascader including codes (approx. 106 KB). ```javascript import { getCascaderDataWithCode } from 'cn-division/cascader-pca-code'; ``` ### Cascader Data (Province, City) - **`getCascaderPCWithCode`**: Imports data for a two-level cascader (province and city) with codes (approx. 106 KB). ```javascript import { getCascaderPCWithCode } from 'cn-division/cascader-pc-code'; ``` ### Address Parsing - **`parseAddress`**: Imports only the address parsing functionality (approx. 42 KB). ```javascript import { parseAddress } from 'cn-division/parse'; ``` ### Code Lookup Tools - **`getNameByCode`, `formatFullPath`, `getProvinceByCode`**: Imports a bundle of code lookup utilities (approx. 143 KB). ```javascript import { getNameByCode, formatFullPath, getProvinceByCode } from 'cn-division/lookup'; ``` ``` -------------------------------- ### Import Specific Data Files (JSON) Source: https://github.com/kk-418/cn-division/blob/main/README.md Import specific JSON data files directly for maximum tree-shaking efficiency. This is recommended when only a subset of data is needed. ```javascript import pca from 'cn-division/dist/no-code/pca.json'; // 仅导入需要的 JSON: ~43K ``` -------------------------------- ### Import Specific Modules (e.g., provinces, cities) Source: https://github.com/kk-418/cn-division/blob/main/README.md Import only the necessary modules like provinces and cities to minimize bundle size. This approach is highly efficient for specific use cases. ```javascript import { provinces, cities } from 'cn-division'; // 仅导入省份和城市: ~5K ``` -------------------------------- ### Import Full Data Sets Source: https://github.com/kk-418/cn-division/blob/main/README.md Import various pre-defined data sets including provinces, cities, counties, and their combinations, with and without codes. ```javascript import { provinces, cities, counties, provincesNoCode, citiesNoCode, countiesNoCode, pca, pc, pcaCode, pcCode } from 'cn-division'; console.log(provinces); // [{ c: '11', n: '北京市' }, { c: '12', n: '天津市' }, ...] console.log(provincesNoCode); // ['北京市', '天津市', '河北省', ...] ``` -------------------------------- ### formatFullPath() - Format Full Path Source: https://context7.com/kk-418/cn-division/llms.txt Constructs a full administrative path string in the format 'Province / City / County' based on a county code. It automatically handles deduplication for municipalities where province and city share the same name. ```APIDOC ## formatFullPath(code: string): string ### Description Formats the full administrative path string (e.g., 'Province / City / County') using a given county code. It intelligently handles deduplication for municipalities where the province and city names are identical. ### Parameters #### Path Parameters - **code** (string) - Required - The county code (can be 6-digit or 12-digit). ### Request Example ```javascript import { formatFullPath } from 'cn-division'; formatFullPath('110101'); // Returns '北京市 / 东城区' (Municipality deduplication) formatFullPath('320505'); // Returns '江苏省 / 苏州市 / 虎丘区' formatFullPath('440305'); // Returns '广东省 / 深圳市 / 南山区' formatFullPath('441900401000'); // Returns '广东省 / 东莞市 / 松山湖' (12-digit code) formatFullPath('abc'); // Returns '' (Empty string for invalid code) ``` ### Response #### Success Response (200) - Returns the formatted full administrative path string. #### Error Response - Returns an empty string (`''`) if the provided code is invalid. ``` -------------------------------- ### Format Full Administrative Path Source: https://context7.com/kk-418/cn-division/llms.txt Constructs a 'Province / City / County' path string from a county code. Automatically deduplicates province and city names for municipalities. ```javascript import { formatFullPath } from 'cn-division'; formatFullPath('110101'); // '北京市 / 东城区' (直辖市自动去重) formatFullPath('320505'); // '江苏省 / 苏州市 / 虎丘区' formatFullPath('440305'); // '广东省 / 深圳市 / 南山区' formatFullPath('441900401000'); // '广东省 / 东莞市 / 松山湖' (12位编码) formatFullPath('abc'); // '' (无效编码返回空字符串) ``` -------------------------------- ### Running Full Data Update Source: https://github.com/kk-418/cn-division/blob/main/README.md Executes a full update process including fetching raw data and building it into distributable formats. This is the primary command for refreshing the administrative division data. ```bash npm run update ``` -------------------------------- ### WeChat Mini Program: Address Parsing Module Source: https://context7.com/kk-418/cn-division/llms.txt Imports a self-contained module specifically for address string parsing, optimized for WeChat Mini Programs to manage bundle size. ```javascript // Only import address parsing - ~42 KB import { parseAddress } from 'cn-division/parse'; ``` -------------------------------- ### WeChat Mini Program: Province-City Cascader (With Code) Source: https://context7.com/kk-418/cn-division/llms.txt Imports a self-contained module for province and city cascader data with codes, optimized for WeChat Mini Programs. ```javascript // Only import province and city two-level selector (with codes) - ~106 KB import { getCascaderPCWithCode } from 'cn-division/cascader-pc-code'; ``` -------------------------------- ### Utility Functions for Address Parsing and Path Formatting Source: https://github.com/kk-418/cn-division/blob/main/README.md Offers utility functions for parsing a given address string into its constituent parts (province, city, county, rest) and for formatting a full administrative path string from a given code. ```typescript // Address parsing function parseAddress(address: string): ParseResult | null // Format full path function formatFullPath(code: string): string ``` -------------------------------- ### WeChat Mini Program: Cascader Data (With Code) Source: https://context7.com/kk-418/cn-division/llms.txt Imports a self-contained module for province, city, and county cascader data including codes, optimized for WeChat Mini Programs. ```javascript // Only import province, city, and county three-level selector (with codes) - ~106 KB import { getCascaderDataWithCode } from 'cn-division/cascader-pca-code'; ``` -------------------------------- ### Constants: VERSION and MUNICIPALITY_CODES Source: https://context7.com/kk-418/cn-division/llms.txt Provides access to the library's version number and a list of codes representing the direct-controlled municipalities. ```APIDOC ## Constants ### VERSION and MUNICIPALITY_CODES These constants provide essential information about the library and administrative divisions. #### `VERSION` - Type: `string` - Description: The current version of the `cn-division` library. #### `MUNICIPALITY_CODES` - Type: `Array` - Description: An array containing the 2-digit codes for China's direct-controlled municipalities: Beijing ('11'), Tianjin ('12'), Shanghai ('31'), and Chongqing ('50'). ### Request Example ```javascript import { VERSION, MUNICIPALITY_CODES } from 'cn-division'; console.log(VERSION); // Outputs the library version, e.g., '2026.0.0' console.log(MUNICIPALITY_CODES); // Outputs ['11', '12', '31', '50'] ``` ``` -------------------------------- ### WeChat Mini Program: Cascader Data (No Code) Source: https://context7.com/kk-418/cn-division/llms.txt Imports a self-contained module for province, city, and county cascader data without codes, optimized for WeChat Mini Programs to manage bundle size. ```javascript // Only import province, city, and county three-level selector (without codes) - ~43 KB import { getCascaderData } from 'cn-division/cascader-pca'; ``` -------------------------------- ### Fetching Raw Data Source: https://github.com/kk-418/cn-division/blob/main/README.md Initiates the data fetching process from the Ministry of Civil Affairs REST API. This command downloads the latest administrative division data into a temporary file. ```bash npm run fetch ``` -------------------------------- ### On-Demand Module Imports for WeChat Mini Programs Source: https://github.com/kk-418/cn-division/blob/main/README.md Import only the necessary modules to reduce the main package size in WeChat Mini Programs. This approach avoids tree-shaking limitations and includes only referenced module files. ```javascript // Only import province-city level selector (with code) — only ~106 KB import { getCascaderPCWithCode } from 'cn-division/cascader-pc-code'; // Only import address parsing — only ~42 KB import { parseAddress } from 'cn-division/parse'; // Only import code lookup — only ~143 KB import { getNameByCode, formatFullPath } from 'cn-division/lookup'; ``` -------------------------------- ### Configuring Data Fetching Year Source: https://github.com/kk-418/cn-division/blob/main/README.md Shows how to configure the data fetching process to use data from a specific year by modifying the 'year' property in the CONFIG object within 'lib/fetch.js'. Setting it to 'undefined' defaults to the latest version. ```javascript const CONFIG = { year: 2025, // 固定为 2025 年数据;undefined 表示始终使用最新年版 }; ``` -------------------------------- ### Utility Functions for Address and Code Manipulation Source: https://github.com/kk-418/cn-division/blob/main/README.md Import and use various utility functions for retrieving province, city, and county information by code, parsing addresses, formatting full paths, and validating codes. These functions are part of the main 'cn-division' export. ```javascript import { getProvinceByCode, getCityByCode, getCountyByCode, getNameByCode, parseAddress, formatFullPath, isMunicipalityCode, getCitiesByProvince, getCountiesByCity, validateCode } from 'cn-division'; // Get information by code getProvinceByCode('110000'); // { c: '11', n: '北京市' } getCityByCode('110100'); // { c: '1101', n: '北京市', p: '11' } getCountyByCode('110101'); // { c: '110101', n: '东城区', cc: '1101' } // Get name by code getNameByCode('110000'); // '北京市' getNameByCode('110101'); // '东城区' // Format full path formatFullPath('110101'); // '北京市 / 东城区' formatFullPath('320505'); // '江苏省 / 苏州市 / 虎丘区' // Address parsing parseAddress('北京市朝阳区建国路88号'); // { // province: '北京市', // city: undefined, // county: '朝阳区', // rest: '建国路88号' // } parseAddress('广东省深圳市南山区科技园'); // { // province: '广东省', // city: '深圳市', // county: '南山区', // rest: '科技园' // } // Check if it's a municipality code isMunicipalityCode('110000'); // true isMunicipalityCode('320000'); // false // Get cities in a province getCitiesByProvince('32'); // All cities in Jiangsu Province // Get counties in a city getCountiesByCity('3201'); // All counties in Nanjing City // Validate code format validateCode('110101'); // true validateCode('441900401000'); // true validateCode('123'); // false ``` -------------------------------- ### Data Update Commands Source: https://context7.com/kk-418/cn-division/llms.txt Provides npm scripts for fetching the latest administrative division data from the Ministry of Civil Affairs and building the library's data files. ```APIDOC ## Data Update Commands These npm scripts allow you to manage and update the administrative division data used by the library. ### `npm run fetch` - **Description**: Fetches the latest administrative division data directly from the Ministry of Civil Affairs interface. ### `npm run build` - **Description**: Processes the raw fetched data and builds it into various JSON formats required by the library. ### `npm run update` - **Description**: Executes a complete update process, including both fetching the latest data and building the necessary files (`fetch` followed by `build`). ``` -------------------------------- ### Address Object Construction: From Codes Source: https://context7.com/kk-418/cn-division/llms.txt Constructs a standardized `AddressValue` object containing full names and codes for province, city, and county based on provided codes. Useful for form pre-filling and address data standardization. ```APIDOC ## Address Object Construction: From Codes Constructs a standardized `AddressValue` object containing full names and codes for province, city, and county based on provided codes. Useful for form pre-filling and address data standardization. ### Import ```js import { getAddressValueFromCodes } from 'cn-division'; ``` ### Usage ```js const addr = getAddressValueFromCodes('11', '1101', '110101'); // Expected output: // { // province: { code: '11', name: '北京市' }, // city: { code: '1101', name: '北京市' }, // county: { code: '110101', name: '东城区' } // } const addr2 = getAddressValueFromCodes('44', '4403', '440305'); // Expected output: // { // province: { code: '44', name: '广东省' }, // city: { code: '4403', name: '深圳市' }, // county: { code: '440305', name: '南山区' } // } // Returns null if province code is invalid const invalid = getAddressValueFromCodes('99', '9901', '990101'); // Expected output: // null ``` ``` -------------------------------- ### Build Unified Address Object from Codes Source: https://context7.com/kk-418/cn-division/llms.txt Constructs an `AddressValue` object containing full names and codes for province, city, and county based on provided three-level codes. Useful for form pre-filling and address data standardization. ```javascript import { getAddressValueFromCodes } from 'cn-division'; const addr = getAddressValueFromCodes('11', '1101', '110101'); // { // province: { code: '11', name: '北京市' }, // city: { code: '1101', name: '北京市' }, // county: { code: '110101', name: '东城区' } // } const addr2 = getAddressValueFromCodes('44', '4403', '440305'); // { // province: { code: '44', name: '广东省' }, // city: { code: '4403', name: '深圳市' }, // county: { code: '440305', name: '南山区' } // } // 省份编码无效时返回 null const invalid = getAddressValueFromCodes('99', '9901', '990101'); // null ``` -------------------------------- ### Export Original Array Data with Codes Source: https://context7.com/kk-418/cn-division/llms.txt Export flat arrays of provinces, cities, and counties with their respective codes. Cities include province codes, and counties include city codes. ```javascript import { provincesCode, citiesCode, countiesCode } from 'cn-division'; console.log(provincesCode.slice(0, 2)); // [{ c: '11', n: '北京市' }, { c: '12', n: '天津市' }] console.log(citiesCode.slice(0, 2)); // [{ c: '1101', n: '北京市', p: '11' }, { c: '1201', n: '天津市', p: '12' }] console.log(countiesCode.slice(0, 2)); // [{ c: '110101', n: '东城区', cc: '1101' }, { c: '110102', n: '西城区', cc: '1101' }] ``` -------------------------------- ### Data Export: Province-City Hierarchy (PC) Source: https://context7.com/kk-418/cn-division/llms.txt Provides a nested object structure for province-city hierarchy. ```APIDOC ## Data Export: Province-City Hierarchy (PC) Provides a nested object structure for province-city hierarchy. ### Import ```js import { pc } from 'cn-division'; ``` ### Structure `pc`: `{ [Province Name]: { [City Name]: 1 } }` ### Usage ```js console.log(Object.keys(pc['广东省']).slice(0, 3)); // Expected output: // ['广州市', '韶关市', '深圳市'] ``` ``` -------------------------------- ### Data Update Commands Source: https://context7.com/kk-418/cn-division/llms.txt Shell commands to fetch the latest data from the Ministry of Civil Affairs, build it into various JSON formats, or perform a complete update. ```bash # Fetch latest data from the Ministry of Civil Affairs interface npm run fetch # Build raw data into multi-format JSON npm run build # Complete update process (fetch + build) npm run update ``` -------------------------------- ### Use with TDesign Mini Program Cascader Source: https://github.com/kk-418/cn-division/blob/main/README.md Integrate province-city cascader data with TDesign's Mini Program Cascader component. Data is loaded in the component's attached lifecycle. ```javascript import { getCascaderPCWithCode } from 'cn-division'; Component({ data: { areaOptions: [] }, lifetimes: { attached() { this.setData({ areaOptions: getCascaderPCWithCode() }); } } }); ``` -------------------------------- ### Data Export: Province-City-County Hierarchy (With Codes) Source: https://context7.com/kk-418/cn-division/llms.txt Provides a nested array structure for province-city-county hierarchy with codes. ```APIDOC ## Data Export: Province-City-County Hierarchy (With Codes) Provides a nested array structure for province-city-county hierarchy with codes. ### Import ```js import { pcaCode } from 'cn-division'; ``` ### Structure `pcaCode`: Array of objects, each with `c` (code), `n` (name), and `ch` (children). ### Usage ```js console.log(pcaCode[0]); // Expected output: // { c: '11', n: '北京市', ch: [{ c: '1101', n: '北京市', ch: [...] }] } ``` ``` -------------------------------- ### Parse Address String Source: https://context7.com/kk-418/cn-division/llms.txt Extracts province, city, and county information from a natural language address string, returning the remaining address fragment. Returns null if no administrative divisions are recognized. ```javascript import { parseAddress } from 'cn-division'; parseAddress('北京市朝阳区建国路88号'); // { province: '北京市', city: undefined, county: '朝阳区', rest: '建国路88号' } parseAddress('广东省深圳市南山区科技园南区'); // { province: '广东省', city: '深圳市', county: '南山区', rest: '科技园南区' } parseAddress('上海市浦东新区陆家嘴环路1000号'); // { province: '上海市', city: undefined, county: '浦东新区', rest: '陆家嘴环路1000号' } parseAddress('无效地址XYZ'); // null ``` -------------------------------- ### parseAddress() - Parse Address String Source: https://context7.com/kk-418/cn-division/llms.txt Parses a natural language address string to extract province, city, and county information, returning the remaining address segment. Returns null if no administrative divisions are recognized. ```APIDOC ## parseAddress(address: string): object | null ### Description Parses a natural language address string to extract administrative division information (province, city, county) and returns the rest of the address. If no administrative divisions can be identified, it returns `null`. ### Parameters #### Path Parameters - **address** (string) - Required - The natural language address string to parse. ### Request Example ```javascript import { parseAddress } from 'cn-division'; parseAddress('北京市朝阳区建国路88号'); // Returns { province: '北京市', city: undefined, county: '朝阳区', rest: '建国路88号' } parseAddress('广东省深圳市南山区科技园南区'); // Returns { province: '广东省', city: '深圳市', county: '南山区', rest: '科技园南区' } parseAddress('上海市浦东新区陆家嘴环路1000号'); // Returns { province: '上海市', city: undefined, county: '浦东新区', rest: '陆家嘴环路1000号' } parseAddress('无效地址XYZ'); // Returns null ``` ### Response #### Success Response (200) - Returns an object containing: - **province** (string | undefined) - The identified province name. - **city** (string | undefined) - The identified city name. - **county** (string | undefined) - The identified county name. - **rest** (string) - The remaining part of the address string after administrative divisions are extracted. #### Error Response - Returns `null` if no administrative divisions are recognized in the address string. ``` -------------------------------- ### Data Export: Province-City-County Hierarchy (No Codes) Source: https://context7.com/kk-418/cn-division/llms.txt Provides a nested object structure for province-city-county hierarchy without codes. ```APIDOC ## Data Export: Province-City-County Hierarchy (No Codes) Provides a nested object structure for province-city-county hierarchy without codes. ### Import ```js import { pca } from 'cn-division'; ``` ### Structure `pca`: `{ [Province Name]: { [City Name]: [Array of County Names] } }` ### Usage ```js console.log(Object.keys(pca).slice(0, 2)); // Expected output: // ['北京市', '天津市'] console.log(pca['北京市']['北京市'].slice(0, 3)); // Expected output: // ['东城区', '西城区', '朝阳区'] ``` ``` -------------------------------- ### Query Province by Code Source: https://context7.com/kk-418/cn-division/llms.txt Retrieves province information using either a 2-digit short code (e.g., '11') or a 6-digit full code (e.g., '110000'). Returns undefined if the code is not found. ```javascript import { getProvinceByCode } from 'cn-division'; getProvinceByCode('110000'); // { c: '11', n: '北京市' } getProvinceByCode('11'); // { c: '11', n: '北京市' } getProvinceByCode('44'); // { c: '44', n: '广东省' } getProvinceByCode('99'); // undefined ``` -------------------------------- ### Export Three-Level Hierarchical Structure (pca / pcaCode) Source: https://context7.com/kk-418/cn-division/llms.txt Provides nested data structures for province-city-county relationships. `pca` is without codes, and `pcaCode` includes codes with 'c', 'n', and 'ch' fields. ```javascript import { pca, pcaCode } from 'cn-division'; // pca: { [省份名]: { [城市名]: [区县名数组] } } console.log(Object.keys(pca).slice(0, 2)); // ['北京市', '天津市'] console.log(pca['北京市']['北京市'].slice(0, 3)); // ['东城区', '西城区', '朝阳区'] // pcaCode: 带 c/n/ch 字段的嵌套数组 console.log(pcaCode[0]); // { c: '11', n: '北京市', ch: [{ c: '1101', n: '北京市', ch: [...] }] } ``` -------------------------------- ### Data Export: Cities with Codes Source: https://context7.com/kk-418/cn-division/llms.txt Exports a flat array of cities with their codes, names, and province codes. ```APIDOC ## Data Export: Cities with Codes Exports a flat array of cities with their codes, names, and province codes. ### Import ```js import { citiesCode } from 'cn-division'; ``` ### Usage ```js console.log(citiesCode.slice(0, 2)); // Expected output: // [{ c: '1101', n: '北京市', p: '11' }, { c: '1201', n: '天津市', p: '12' }] ``` ``` -------------------------------- ### API Reference - Data Exports Source: https://github.com/kk-418/cn-division/blob/main/README.md Lists the data structures exported by the 'cn-division' package, including detailed information about provinces, cities, counties, and hierarchical structures, with and without codes. ```APIDOC ## API Reference - Data Exports ### Description This section details the various data structures that are exported directly from the `cn-division` package. These exports provide access to the underlying administrative division data in different formats. ### Exported Data | Export Name | Type | Description | |---------------------|------------------|---------------------------------------------------| | `provinces` | `Province[]` | Province data (with codes) | | `cities` | `City[]` | City data (with codes) | | `counties` | `County[]` | County data (with codes) | | `provincesNoCode` | `string[]` | Array of province names | | `citiesNoCode` | `string[]` | Array of city names | | `countiesNoCode` | `string[]` | Array of county names | | `pca` | `PCAData` | Province-City-County structure (without codes) | | `pc` | `PCData` | Province-City structure (without codes) | | `pcaCode` | `PCACodeData[]` | Province-City-County structure (with codes) | | `pcCode` | `PCCodeData[]` | Province-City structure (with codes) | ```