重构地址处理逻辑,使用 addressData 替代 address,增强状态缩写的准确性
This commit is contained in:
222
src/index.js
222
src/index.js
@@ -17,7 +17,7 @@ export default {
|
|||||||
async function handleRequest(request) {
|
async function handleRequest(request) {
|
||||||
const { searchParams } = new URL(request.url);
|
const { searchParams } = new URL(request.url);
|
||||||
const state = searchParams.get('state') || getRandomState();
|
const state = searchParams.get('state') || getRandomState();
|
||||||
let address, phone, country;
|
let phone, country, addressData;
|
||||||
|
|
||||||
const remoteProvinces = ['NL', 'NT', 'NU', 'YT']; // Remote Canadian Provinces/Territories
|
const remoteProvinces = ['NL', 'NT', 'NU', 'YT']; // Remote Canadian Provinces/Territories
|
||||||
|
|
||||||
@@ -35,20 +35,20 @@ async function handleRequest(request) {
|
|||||||
if (data.address.country_code === 'us') {
|
if (data.address.country_code === 'us') {
|
||||||
if (data.address.house_number && data.address.road && data.address.city) {
|
if (data.address.house_number && data.address.road && data.address.city) {
|
||||||
country = 'US';
|
country = 'US';
|
||||||
address = formatAddress(data.address, state, country);
|
addressData = data.address;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else if (data.address.country_code === 'ca') {
|
} else if (data.address.country_code === 'ca') {
|
||||||
if (remoteProvinces.includes(state)) {
|
if (remoteProvinces.includes(state)) {
|
||||||
// For remote provinces, allow partial addresses
|
// For remote provinces, allow partial addresses
|
||||||
country = 'CA';
|
country = 'CA';
|
||||||
address = formatAddress(data.address, state, country);
|
addressData = data.address;
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
// For other provinces, require detailed address
|
// For other provinces, require detailed address
|
||||||
if (data.address.house_number && data.address.road && data.address.city) {
|
if (data.address.house_number && data.address.road && data.address.city) {
|
||||||
country = 'CA';
|
country = 'CA';
|
||||||
address = formatAddress(data.address, state, country);
|
addressData = data.address;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -56,7 +56,7 @@ async function handleRequest(request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!address) {
|
if (!addressData) {
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({ error: 'Failed to retrieve detailed address' }),
|
JSON.stringify({ error: 'Failed to retrieve detailed address' }),
|
||||||
{
|
{
|
||||||
@@ -82,17 +82,86 @@ async function handleRequest(request) {
|
|||||||
phone = getRandomPhoneNumber(country, state);
|
phone = getRandomPhoneNumber(country, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse address components
|
// Ensure we have the correct state abbreviation
|
||||||
const addressParts = parseAddressComponents(address, state);
|
const stateAbbreviations = {
|
||||||
|
// US States
|
||||||
|
Alabama: 'AL',
|
||||||
|
Alaska: 'AK',
|
||||||
|
Arizona: 'AZ',
|
||||||
|
Arkansas: 'AR',
|
||||||
|
California: 'CA',
|
||||||
|
Colorado: 'CO',
|
||||||
|
Connecticut: 'CT',
|
||||||
|
Delaware: 'DE',
|
||||||
|
Florida: 'FL',
|
||||||
|
Georgia: 'GA',
|
||||||
|
Hawaii: 'HI',
|
||||||
|
Idaho: 'ID',
|
||||||
|
Illinois: 'IL',
|
||||||
|
Indiana: 'IN',
|
||||||
|
Iowa: 'IA',
|
||||||
|
Kansas: 'KS',
|
||||||
|
Kentucky: 'KY',
|
||||||
|
Louisiana: 'LA',
|
||||||
|
Maine: 'ME',
|
||||||
|
Maryland: 'MD',
|
||||||
|
Massachusetts: 'MA',
|
||||||
|
Michigan: 'MI',
|
||||||
|
Minnesota: 'MN',
|
||||||
|
Mississippi: 'MS',
|
||||||
|
Missouri: 'MO',
|
||||||
|
Montana: 'MT',
|
||||||
|
Nebraska: 'NE',
|
||||||
|
Nevada: 'NV',
|
||||||
|
'New Hampshire': 'NH',
|
||||||
|
'New Jersey': 'NJ',
|
||||||
|
'New Mexico': 'NM',
|
||||||
|
'New York': 'NY',
|
||||||
|
'North Carolina': 'NC',
|
||||||
|
'North Dakota': 'ND',
|
||||||
|
Ohio: 'OH',
|
||||||
|
Oklahoma: 'OK',
|
||||||
|
Oregon: 'OR',
|
||||||
|
Pennsylvania: 'PA',
|
||||||
|
'Rhode Island': 'RI',
|
||||||
|
'South Carolina': 'SC',
|
||||||
|
'South Dakota': 'SD',
|
||||||
|
Tennessee: 'TN',
|
||||||
|
Texas: 'TX',
|
||||||
|
Utah: 'UT',
|
||||||
|
Vermont: 'VT',
|
||||||
|
Virginia: 'VA',
|
||||||
|
Washington: 'WA',
|
||||||
|
'West Virginia': 'WV',
|
||||||
|
Wisconsin: 'WI',
|
||||||
|
Wyoming: 'WY',
|
||||||
|
// Canadian Provinces and Territories
|
||||||
|
Alberta: 'AB',
|
||||||
|
'British Columbia': 'BC',
|
||||||
|
Manitoba: 'MB',
|
||||||
|
'New Brunswick': 'NB',
|
||||||
|
'Newfoundland and Labrador': 'NL',
|
||||||
|
'Nova Scotia': 'NS',
|
||||||
|
Ontario: 'ON',
|
||||||
|
'Prince Edward Island': 'PE',
|
||||||
|
Quebec: 'QC',
|
||||||
|
Saskatchewan: 'SK',
|
||||||
|
'Northwest Territories': 'NT',
|
||||||
|
Nunavut: 'NU',
|
||||||
|
Yukon: 'YT',
|
||||||
|
};
|
||||||
|
|
||||||
|
// Get the correct state abbreviation - use addressData.state if available, otherwise use the original state parameter
|
||||||
|
const stateAbbr = stateAbbreviations[addressData.state] || state;
|
||||||
|
|
||||||
const jsonResponse = {
|
const jsonResponse = {
|
||||||
firstName: firstName,
|
firstName: firstName,
|
||||||
lastName: lastName,
|
lastName: lastName,
|
||||||
streetAddress: addressParts.streetAddress,
|
streetAddress: `${addressData.house_number || ''} ${addressData.road || ''}`.trim(),
|
||||||
aptSuiteBldg: addressParts.aptSuiteBldg,
|
aptSuiteBldg: '', // Could be enhanced if needed
|
||||||
city: addressParts.city,
|
city: addressData.city || addressData.town || addressData.village || '',
|
||||||
state: addressParts.state,
|
state: stateAbbr,
|
||||||
zipCode: addressParts.zipCode,
|
zipCode: addressData.postcode || '',
|
||||||
phoneNumber: phone
|
phoneNumber: phone
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -369,91 +438,6 @@ function getRandomLocationInState(state) {
|
|||||||
return { lat, lng };
|
return { lat, lng };
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatAddress(address, state, country) {
|
|
||||||
const stateAbbreviations = {
|
|
||||||
// US States
|
|
||||||
Alabama: 'AL',
|
|
||||||
Alaska: 'AK',
|
|
||||||
Arizona: 'AZ',
|
|
||||||
Arkansas: 'AR',
|
|
||||||
California: 'CA',
|
|
||||||
Colorado: 'CO',
|
|
||||||
Connecticut: 'CT',
|
|
||||||
Delaware: 'DE',
|
|
||||||
Florida: 'FL',
|
|
||||||
Georgia: 'GA',
|
|
||||||
Hawaii: 'HI',
|
|
||||||
Idaho: 'ID',
|
|
||||||
Illinois: 'IL',
|
|
||||||
Indiana: 'IN',
|
|
||||||
Iowa: 'IA',
|
|
||||||
Kansas: 'KS',
|
|
||||||
Kentucky: 'KY',
|
|
||||||
Louisiana: 'LA',
|
|
||||||
Maine: 'ME',
|
|
||||||
Maryland: 'MD',
|
|
||||||
Massachusetts: 'MA',
|
|
||||||
Michigan: 'MI',
|
|
||||||
Minnesota: 'MN',
|
|
||||||
Mississippi: 'MS',
|
|
||||||
Missouri: 'MO',
|
|
||||||
Montana: 'MT',
|
|
||||||
Nebraska: 'NE',
|
|
||||||
Nevada: 'NV',
|
|
||||||
'New Hampshire': 'NH',
|
|
||||||
'New Jersey': 'NJ',
|
|
||||||
'New Mexico': 'NM',
|
|
||||||
'New York': 'NY',
|
|
||||||
'North Carolina': 'NC',
|
|
||||||
'North Dakota': 'ND',
|
|
||||||
Ohio: 'OH',
|
|
||||||
Oklahoma: 'OK',
|
|
||||||
Oregon: 'OR',
|
|
||||||
Pennsylvania: 'PA',
|
|
||||||
'Rhode Island': 'RI',
|
|
||||||
'South Carolina': 'SC',
|
|
||||||
'South Dakota': 'SD',
|
|
||||||
Tennessee: 'TN',
|
|
||||||
Texas: 'TX',
|
|
||||||
Utah: 'UT',
|
|
||||||
Vermont: 'VT',
|
|
||||||
Virginia: 'VA',
|
|
||||||
Washington: 'WA',
|
|
||||||
'West Virginia': 'WV',
|
|
||||||
Wisconsin: 'WI',
|
|
||||||
Wyoming: 'WY',
|
|
||||||
// Canadian Provinces and Territories
|
|
||||||
Alberta: 'AB',
|
|
||||||
'British Columbia': 'BC',
|
|
||||||
Manitoba: 'MB',
|
|
||||||
'New Brunswick': 'NB',
|
|
||||||
'Newfoundland and Labrador': 'NL',
|
|
||||||
'Nova Scotia': 'NS',
|
|
||||||
Ontario: 'ON',
|
|
||||||
'Prince Edward Island': 'PE',
|
|
||||||
Quebec: 'QC',
|
|
||||||
Saskatchewan: 'SK',
|
|
||||||
'Northwest Territories': 'NT',
|
|
||||||
Nunavut: 'NU',
|
|
||||||
Yukon: 'YT',
|
|
||||||
};
|
|
||||||
const stateAbbr = stateAbbreviations[address.state] || state;
|
|
||||||
let formattedAddress = '';
|
|
||||||
|
|
||||||
if (address.house_number && address.road && address.city) {
|
|
||||||
if (country === 'US') {
|
|
||||||
formattedAddress = `${address.house_number} ${address.road}, ${address.city}, ${stateAbbr} ${address.postcode}, United States`;
|
|
||||||
} else if (country === 'CA') {
|
|
||||||
formattedAddress = `${address.house_number} ${address.road}, ${address.city}, ${stateAbbr} ${address.postcode}, Canada`;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// For partial addresses in remote provinces
|
|
||||||
formattedAddress = `${address.city}, ${stateAbbr} ${address.postcode}, ${country === 'US' ? 'United States' : 'Canada'}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return formattedAddress;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getRandomPhoneNumber(country, state) {
|
function getRandomPhoneNumber(country, state) {
|
||||||
const areaCodesUS = {
|
const areaCodesUS = {
|
||||||
AL: ['205', '251', '256', '334', '938'],
|
AL: ['205', '251', '256', '334', '938'],
|
||||||
@@ -540,50 +524,6 @@ function getRandomPhoneNumber(country, state) {
|
|||||||
return `(${areaCode}) ${exchangeCode}-${lineNumber}`;
|
return `(${areaCode}) ${exchangeCode}-${lineNumber}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseAddressComponents(fullAddress, state) {
|
|
||||||
// Parse the formatted address to extract components
|
|
||||||
const parts = fullAddress.split(', ');
|
|
||||||
let streetAddress = '';
|
|
||||||
let aptSuiteBldg = '';
|
|
||||||
let city = '';
|
|
||||||
let stateCode = '';
|
|
||||||
let zipCode = '';
|
|
||||||
|
|
||||||
if (parts.length >= 3) {
|
|
||||||
streetAddress = parts[0] || '';
|
|
||||||
city = parts[1] || '';
|
|
||||||
|
|
||||||
// Handle state and zip from the third part
|
|
||||||
if (parts[2]) {
|
|
||||||
const stateZipMatch = parts[2].match(/^([A-Z]{2})\s*([A-Z0-9\s-]*)/);
|
|
||||||
if (stateZipMatch) {
|
|
||||||
stateCode = stateZipMatch[1];
|
|
||||||
zipCode = stateZipMatch[2] ? stateZipMatch[2].trim() : '';
|
|
||||||
} else {
|
|
||||||
stateCode = state;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// For partial addresses
|
|
||||||
if (parts.length === 2) {
|
|
||||||
city = parts[0] || '';
|
|
||||||
const stateZipMatch = parts[1].match(/^([A-Z]{2})\s*([A-Z0-9\s-]*)/);
|
|
||||||
if (stateZipMatch) {
|
|
||||||
stateCode = stateZipMatch[1];
|
|
||||||
zipCode = stateZipMatch[2] ? stateZipMatch[2].trim() : '';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
streetAddress: streetAddress,
|
|
||||||
aptSuiteBldg: aptSuiteBldg, // Empty for now, could be enhanced
|
|
||||||
city: city,
|
|
||||||
state: stateCode || state,
|
|
||||||
zipCode: zipCode
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function getRandomState() {
|
function getRandomState() {
|
||||||
const states = [
|
const states = [
|
||||||
// US States
|
// US States
|
||||||
|
Reference in New Issue
Block a user