Reference · Parsing, formatting and shared types
parsePostalCodefunction
Takes a ZIP code, ZIP+4 or Canadian postal code off the end of a text.
Import it
import { parsePostalCode } from "@johnmorrisdotca/address-plus";
Definition
function parsePostalCode(text: string): { zip: string | undefined; plus4: string | undefined; remaining: string; detectedCountry?: "US" | "CA"; detectedProvince?: string; }
Parameters
| Name | Type | Description |
|---|---|---|
text | string | The text, such as |
Returns
{ zip: string | undefined; plus4: string | undefined; remaining: string; detectedCountry?: "US" | "CA"; detectedProvince?: string; }
The code and its ZIP+4 (undefined when there is none), the text that remains, and the country and
province the code points to.
Examples
import { parsePostalCode } from "@johnmorrisdotca/address-plus";
console.log(parsePostalCode("Toronto ON M5H 2N2"));
// → {"zip":"M5H 2N2","remaining":"Toronto ON","detectedCountry":"CA","detectedProvince":"ON"}{
zip: 'M5H 2N2',
plus4: undefined,
remaining: 'Toronto ON',
detectedCountry: 'CA',
detectedProvince: 'ON'
}What it prints (run when this page was built)