zh-address-parse

repository·master·Indexed 21 days ago

https://github.com/ldwonday/zh-address-parse

A JavaScript/TypeScript library for parsing Chinese delivery addresses into structured components including province, city, area, name, detail, phone, and postal code. Supports both regular expression and tree-based lookup parsing methods.

Tokens
1.2K
Snippets
6
Records
6
Agent score
24%

What's inside zh-address-parse

  1. Include zh-address-parse via script tag

    master

    For direct browser usage, include the minified script. The parser is exposed on the global window.ZhAddressParse object.

    <script async defer src="./zh-address-parse.min.js"></script>
    <script>
        const address = "some address";
        const parseResult = window.ZhAddressParse(address, { type: 0, textFilter: ['电話', '電話', '聯系人'] });
        console.log(parseResult);
    </script>
  2. Use AddressParse in JavaScript/TypeScript

    master

    Import AddressParse and call it with an address string and an optional configuration object.

    By default, the parser uses regular expressions (type: 0). You can also use tree-based lookup (type: 1).

    The function returns an object containing the following keys:

    • province: Province name
    • city: City name
    • area: District/County name
    • name: Specific location name
    • detail: Detailed address information
    • phone: Extracted phone number
    • postalCode: Extracted postal code
    import AddressParse from 'zh-address-parse'
    
    const options = {
      type: 0, // 0: Regex (default), 1: Tree lookup
      textFilter: [], // Fields to pre-filter/clean
      nameMaxLength: 4, // Maximum length for Chinese names
      extraGovData: {
        city: [], 
        province: [], 
        area: [] 
      }
    }
    
    const parseResult = AddressParse('your address', options)
    // parseResult: { province: '', name: '', city: '', area: '', detail: '', phone: '', postalCode: '' }
  3. Configure AddressParse options

    master

    The AddressParse function accepts an optional options object with the following properties:

    ParameterTypeDescription
    typeNumberParsing method. 0 for Regex (default), 1 for Tree lookup.
    textFilterArrayArray of strings used for pre-filtering/cleaning the address text.
    nameMaxLengthNumberThe maximum length of the Chinese name to search for. Default is 4.
    extraGovDataObjectCustom administrative division data.
    type GovData = {
        code: string;
        provinceCode?: string;
        cityCode?: string;
        name: string;
    }
    
    // extraGovData structure:
    // {
    //   city?: GovData[];
    //   area: GovData[];
    //   province: GovData[];
    // }
  4. Use the AddressParse function to parse Chinese addresses

    master

    The AddressParse function is the primary API for extracting structured data from a Chinese delivery address string. It accepts two arguments:

    1. address (string): The raw address text to be parsed.
    2. options (object): Configuration for the parsing process.

    Options

    • type (number): Determines the parsing mode (e.g., 0 for default behavior).
    • textFilter (string[]): An array of strings used to filter out specific terms from the parsed result. In the example below, terms like '电話', '電話', or '联系人' are filtered out.

    The function returns an object where keys represent the address components (e.g., province, city, district) and values represent the extracted text.

    import AddressParse from './lib/address-parse';
    
    const address = '北京市朝阳区建国门外大街1号';
    const options = {
        type: 0,
        textFilter: ['电話', '電話', '联系人']
    };
    
    const parseResult = AddressParse(address, options);
    console.log(parseResult);
    // Example output structure:
    // { province: '北京市', city: '北京市', district: '朝阳区', ... }
  5. Import zh-address-parse

    master

    You can use the zh-address-parse package by requiring it in your Node.js environment. The package exports the minified distribution build, providing the core address parsing functionality.

    const zhAddressParse = require('zh-address-parse');