Reference · Parsing, formatting and shared types
parseLocationfunction
Parses a US, Canadian or Japanese address into its parts. The country is detected from the text (a state, a
province, a postal code, Japanese script or romaji designators) unless options.country names it. A Japanese
address fills its own fields (prefecture, municipality, town, chome, ban, go) and the shared ones that
stand for them. Australia and the United Kingdom are read too when their modules are passed in options.countries
(australia from /au, unitedKingdom from /gb): options.country picks one, or each module's own detection
decides, before the US, Canada and Japan are tried.
Import it
import { parseLocation } from "@johnmorrisdotca/address-plus";
Definition
function parseLocation(address: string, options?: ParseOptions): ParsedAddress | null
Parameters
| Name | Type | Description |
|---|---|---|
address | string | The address as one string; commas, line breaks and full-width characters are all read. |
options (optional) | ParseOptions | How to parse: the country, strict postal codes, snake_case keys and the rest (see |
Returns
ParsedAddress | null
The parts found, or null when the text is empty or holds nothing that can be read as an address. A part
that is not in the address is absent from the result, never an empty string.
Examples
import { parseLocation } from "@johnmorrisdotca/address-plus";
console.log(parseLocation("1600 Pennsylvania Ave NW, Washington, DC 20500"));
// → {"number":"1600","street":"Pennsylvania","type":"Ave","suffix":"NW","city":"Washington","state":"DC","zip":"20500","zipValid":true,"country":"US"}{
number: '1600',
street: 'Pennsylvania',
type: 'Ave',
suffix: 'NW',
city: 'Washington',
state: 'DC',
zip: '20500',
zipValid: true,
country: 'US'
}What it prints (run when this page was built)