UNPKG

lcn

Version:

中华人民共和国行政区划,省市区数据

215 lines (214 loc) 7.02 kB
import data from './data'; import { isAreaCode, isCityCode, isProvinceCode, isInland, getProvinceCode, getCityCode, isCrownCountryCityCode } from './util'; export { data, isAreaCode, isCityCode, isProvinceCode, isInland, getProvinceCode, getCityCode, isCrownCountryCityCode }; export type DataType = typeof data; export type CascaderOption = { /** * 仅包含中国大陆内地数据。默认为 `false`。 * * 如果为 `true`,将不包含香港/澳门/台湾。 */ inland?: boolean; /** * 自定义字段名。 */ fieldNames?: { code?: string; name?: string; children?: string; }; /** * 自定义数据源。默认 `data`。 */ dataSource?: DataType; /** * 子级为空时的值,默认 `array`。 * * `array` 表示为`[]``null` 表示为 `null``none` 表示删除该子级。 */ emptyChildrenValue?: 'none' | 'null' | 'array'; /** * 忽略直辖市或省直辖县的市级。默认 `false`。 */ ignoreCrownCountryCity?: boolean; }; export type CascadeData = { code?: string; name?: string; children?: CascadeData[]; [key: string]: any; }; export type CascadeDataWithNull = Omit<CascadeData, 'children'> & { children?: CascadeDataWithNull[] | null; }; /** * 将数据拆分成省市区数据。 * * @param opts * @returns */ export declare function splitPCA(opts?: { dataSource?: DataType; inland?: boolean; province?: boolean; city?: boolean; area?: boolean; }): { provinces: { code: string; name: string; }[]; cities: { code: string; name: string; }[]; areas: { code: string; name: string; }[]; }; /** * 获取省市级联数据。 * * @param {Object} [options] 配置项。 * @param {boolean} [options.inland=false] 仅包含中国大陆内地数据。默认为 `false`。如果为 `true`,将排除香港/澳门/台湾。 * @param {boolean} [options.fieldNames] 自定义字段名。 * @param {boolean} [options.dataSource=data] 自定义数据源。默认 `data`。 * @param {boolean} [options.emptyChildrenValue='array'] 子级为空时的值,默认 `array``array` 表示为`[]``null` 表示为 `null``none` 表示删除该子级。 * @returns {Array} 省市级联数据。 * @example * // 全部省市级联数据 * const pc = getPC(); * // [ * // { * // code: "410000", * // name: "河南省", * // children: [ * // { code: "410100", name: "郑州市" }, * // // ... * // ] * // }, * // // ... * // ] * * // 中国大陆内地省市级联数据 * const pc2 = getPC({ inland: true }); * // 数据结构同上,不包含香港/澳门/台湾。 * * // 自定义字段名 * const pc3 = getPC({ inland: true, fieldNames: { code: "value", name: "label" } }); * // [ * // { * // value: "410000", * // label: "河南省", * // children: [ * // { value: "410100", label: "郑州市" }, * // // ... * // ] * // }, * // // ... * // ] */ export declare function getPC(options?: CascaderOption & { emptyChildrenValue: 'null'; }): CascadeDataWithNull[]; export declare function getPC(options?: CascaderOption): CascadeData[]; /** * 获取省市区级联数据 * * @param {Object} [options] 配置项。 * @param {boolean} [options.inland=false] 仅包含中国大陆内地数据。默认为 `false`。如果为 `true`,将排除香港/澳门/台湾。 * @param {boolean} [options.fieldNames] 自定义字段名。 * @param {boolean} [options.dataSource=data] 自定义数据源。默认 `data`。 * @param {boolean} [options.emptyChildrenValue='array'] 子级为空时的值,默认 `array``array` 表示为`[]``null` 表示为 `null``none` 表示删除该子级。 * @returns {Array} 省市区级联数据。 * @example * // 全部省市区级联数据 * const pca = getPCA(); * // [ * // { * // code: "410000", * // name: "河南省", * // children: [ * // { code: "410100", name: "郑州市", children: [...] }, * // // ... * // ] * // }, * // // ... * // ] * * // 中国大陆内地省市区级联数据 * const pca2 = getPCA({ inland: true }); * // 数据结构同上,不包含香港/澳门/台湾。 * * // 自定义字段名 * const pca3 = getPCA({ inland: true, fieldNames: { code: "value", name: "label" } }); * // [ * // { * // value: "410000", * // label: "河南省", * // children: [ * // { value: "410100", label: "郑州市", children: [...] }, * // // ... * // ] * // }, * // // ... * // ] */ export declare function getPCA(options?: Omit<CascaderOption, 'ignoreCrownCountryCity'> & { emptyChildrenValue: 'null'; }): CascadeDataWithNull[]; export declare function getPCA(options?: Omit<CascaderOption, 'ignoreCrownCountryCity'>): CascadeData[]; type ParseItem = null | { code: string; name: string; }; /** * 解析省市区编码数据。 * * @param {string} code 省市区编码。 * @param {Object} [options] 配置项。 * @param {boolean} [options.dataSource=data] 自定义数据源。默认 `data`。 * @param {boolean} [options.ignoreCrownCountryCityName=false] 忽略直辖市或省直辖县的市级名称。默认 `false`。如果为 `true`,则直辖市或省直辖县的市级名称返回`''`。 * @returns {Array} 返回一个元组数组,对应省市区(某个没找到的返回 null)。 * @example * parseCode('410102'); * // [{ code: '410000', name: '河南省' }, { code: '410100', name: '郑州市' }, { code: '410102', name: '中原区' }]; * * parseCode('410100'); * // [{ code: '410000', name: '河南省' }, { code: '410100', name: '郑州市' }, null]; * * parseCode('410000'); * // [{ code: '410000', name: '河南省' }, null, null]; * * parseCode('000102'); * // [null, null, null] */ export declare function parseCode(code?: string, options?: { /** * 自定义数据源。默认 `data`。 */ dataSource?: DataType; /** * 忽略直辖市或省直辖县的市级名称。默认 `false`。如果为 `true`,则直辖市或省直辖县的市级名称返回`''`。 */ ignoreCrownCountryCityName?: boolean; }): [ParseItem, ParseItem, ParseItem]; /** * @deprecated 即将废弃,请使用 `parseCode`。 */ export declare const parseAreaCode: typeof parseCode; /** * 通过区县名称和市编码反查区县编码。 * * @param {string} areaName 区县名称。 * @param {string} cityCode 市级编码。 * @returns {string | undefined} 如果找到区县数据,返回区县编码,否则返回 `undefined`。 * @example * getAreaCodeByNameAndCityCode('海沧区', '350200'); * // '350205' * * getAreaCodeByNameAndCityCode('海沧区123', '350200'); * // undefined */ export declare const getAreaCodeByNameAndCityCode: (areaName: string, cityCode: string) => string | undefined;