@sroussey/parse-address
Version:
US Street Address Parser
144 lines • 4.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IntlAddressParser = exports.AddressParser = void 0;
const parser_1 = require("./maps/us/parser");
const parser_2 = require("./maps/ca/parser");
const states_1 = require("./maps/us/states");
const provinces_1 = require("./maps/ca/provinces");
class AddressParser {
constructor(country = "us") {
Object.defineProperty(this, "parser", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
switch (country) {
case "us":
this.parser = new parser_1.AddressParserUS();
break;
case "ca":
this.parser = new parser_2.AddressParserCA();
break;
}
}
normalizeAddress(parts) {
return this.parser.normalizeAddress(parts);
}
parseAddress(address) {
return this.parser.parseAddress(address);
}
parseStreet(address) {
return this.parser.parseStreet(address);
}
parseInformalAddress(address) {
return this.parser.parseInformalAddress(address);
}
parsePoAddress(address) {
return this.parser.parsePoAddress(address);
}
parseLocation(address) {
return this.parser.parseLocation(address);
}
parseIntersection(address) {
return this.parser.parseIntersection(address);
}
findStreetTypeShortCode(streetType) {
return this.parser.findStreetTypeShortCode(streetType);
}
}
exports.AddressParser = AddressParser;
function detectCountry(address) {
// Canadian postal code pattern: A1A 1A1 or A1A1A1
const canadianPostalCode = /[A-Za-z]\d[A-Za-z]\s*\d[A-Za-z]\d/;
// US ZIP code pattern: 12345 or 12345-1234
const usZipCode = /\b\d{5}(?:-?\d{4})?\b/;
// Check for explicit country indicators
if (/\b(Canada)\b/i.test(address)) {
return "ca";
}
if (/\b(US|USA|United States)\b/i.test(address)) {
return "us";
}
// Check postal code formats first (more reliable than province/state codes)
if (canadianPostalCode.test(address)) {
return "ca";
}
if (usZipCode.test(address)) {
return "us";
}
// Check for Canadian provinces vs US states
const addressLower = address.toLowerCase();
const canadianProvinces = Object.keys(provinces_1.provinceCodesMap);
const canadianProvinceCodes = Object.values(provinces_1.provinceCodesMap);
const usStates = Object.keys(states_1.stateCodesMap);
const usStateCodes = Object.values(states_1.stateCodesMap);
// Check for Canadian provinces (full names first)
for (const province of canadianProvinces) {
if (addressLower.includes(province.toLowerCase())) {
return "ca";
}
}
// Check for US states (full names)
for (const state of usStates) {
if (addressLower.includes(state.toLowerCase())) {
return "us";
}
}
// Check for state/province codes - be more specific to avoid conflicts
// Check US state codes first (more common)
for (const stateCode of usStateCodes) {
if (new RegExp(`\\b${stateCode}\\b`).test(address)) {
return "us";
}
}
// Check for Canadian province codes (only if not already matched as US)
for (const provinceCode of canadianProvinceCodes) {
if (new RegExp(`\\b${provinceCode}\\b`).test(address)) {
return "ca";
}
}
// Check for French street types (strong indicator of Canada)
const frenchStreetTypes = ['rue', 'chemin', 'boulevard', 'avenue', 'allée'];
for (const frenchType of frenchStreetTypes) {
if (addressLower.includes(frenchType)) {
return "ca";
}
}
// Default to US
return "us";
}
class IntlAddressParser {
constructor() {
Object.defineProperty(this, "parsers", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.parsers = {
us: new AddressParser("us"),
ca: new AddressParser("ca")
};
}
// must end in country name or country code
parseLocation(address) {
const country = detectCountry(address);
return this.parsers[country].parseLocation(address);
}
// must end in country name or country code
parseAddress(address) {
const country = detectCountry(address);
return this.parsers[country].parseAddress(address);
}
// must end in country name or country code
parseInformalAddress(address) {
const country = detectCountry(address);
return this.parsers[country].parseInformalAddress(address);
}
parseStreet(address, country) {
return this.parsers[country].parseStreet(address);
}
}
exports.IntlAddressParser = IntlAddressParser;
//# sourceMappingURL=parser.js.map