Skip to content

Addresses

import { parseAddress, formatAddress } from 'pakdatakit/address'

Splits a Pakistani mailing address typed as one line into its parts, matching the city, district and province against the library’s own lists, and puts parts back together as a single mailing line.

Loading address data…

Street
—
Area
—
City
—
District
—
Province
—
Postal code
—
formatAddress()
—
import { parseAddress, formatAddress } from 'pakdatakit';
parseAddress('12 Model Town Lahore');
// → {
// "raw": "12 Model Town Lahore",
// "street": "12 Model Town",
// "area": null,
// "city": "Lahore",
// "district": null,
// "province": "Punjab",
// "postalCode": null,
// "warnings": []
// }
parseAddress('Saddar, Karachi, Punjab');
// → {
// "raw": "Saddar, Karachi, Punjab",
// "street": "Saddar",
// "area": null,
// "city": "Karachi",
// "district": null,
// "province": "Punjab",
// "postalCode": null,
// "warnings": [
// "Karachi is in Sindh, but the address says Punjab"
// ]
// }
formatAddress({ street: 'House 12, Street 5', area: 'F-7/2', city: 'Islamabad', postalCode: '44000' });
// → 'House 12, Street 5, F-7/2, Islamabad 44000'
function parseAddress(address: string): ParsedAddress;
interface ParsedAddress {
raw: string;
street: string | null;
area: string | null;
city: string | null;
district: string | null;
province: string | null;
postalCode: string | null;
warnings: string[];
}

Parameters. address: the raw address. It works with or without commas between components.

Returns the structured components. warnings lists inconsistencies found while parsing, such as a city in a different province from the one the address names.

Caveats. The postal code is taken from the last standalone 5-digit number that is not a house, plot, box or street number. Known cities, districts and provinces are matched from the datasets, and the remaining segments are assigned to street and area.

Related: formatAddress(), getProvince(), postal codes.

function formatAddress(
parts: Partial<ParsedAddress> & { raw?: string },
): string;

Parameters. parts: any of the components of a ParsedAddress; a parsed address can be passed back in as it is.

Returns a single mailing line: street, area, city (or the district when there is no city) and province, separated by commas, then the postal code. When no component is given it returns raw, or "".

Caveats. Only the components provided are included; no postal code is invented.

Related: parseAddress().