|
|
@ -29,9 +29,34 @@ export function parseValue(rawValue: unknown): { country: string; city: string } |
|
|
|
|
|
|
|
|
if (typeof rawValue === "object" && rawValue !== null) { |
|
|
if (typeof rawValue === "object" && rawValue !== null) { |
|
|
const obj = rawValue as BirthplaceValue; |
|
|
const obj = rawValue as BirthplaceValue; |
|
|
|
|
|
const rawCountry = typeof obj.country === "string" ? obj.country.trim() : ""; |
|
|
|
|
|
const rawCity = typeof obj.city === "string" ? obj.city.trim() : ""; |
|
|
|
|
|
|
|
|
|
|
|
// If obj has country and city inverted (e.g. { country: "Mashhad", city: "Iran" })
|
|
|
|
|
|
if (rawCountry && rawCity && !isKnownCountry(rawCountry) && isKnownCountry(rawCity)) { |
|
|
|
|
|
return { |
|
|
|
|
|
country: rawCity, |
|
|
|
|
|
city: rawCountry, |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
// If only country is provided but it is actually a city
|
|
|
|
|
|
if (rawCountry && !rawCity && !isKnownCountry(rawCountry)) { |
|
|
|
|
|
return { |
|
|
|
|
|
country: "", |
|
|
|
|
|
city: rawCountry, |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
// If only city is provided but it is actually a country
|
|
|
|
|
|
if (!rawCountry && rawCity && isKnownCountry(rawCity)) { |
|
|
|
|
|
return { |
|
|
|
|
|
country: rawCity, |
|
|
|
|
|
city: "", |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return { |
|
|
return { |
|
|
country: typeof obj.country === "string" ? obj.country.trim() : "", |
|
|
|
|
|
city: typeof obj.city === "string" ? obj.city.trim() : "", |
|
|
|
|
|
|
|
|
country: rawCountry, |
|
|
|
|
|
city: rawCity, |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -65,23 +90,30 @@ export function parseValue(rawValue: unknown): { country: string; city: string } |
|
|
"ایالات متحده آمریکا (US)", |
|
|
"ایالات متحده آمریکا (US)", |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const splitLocation = (part1: string, part2: string) => { |
|
|
|
|
|
// 1. If part1 is a known country and part2 is not (or both), part1 is country, part2 is city
|
|
|
|
|
|
if (isKnownCountry(part1)) { |
|
|
|
|
|
return { country: part1, city: part2 }; |
|
|
|
|
|
} |
|
|
|
|
|
// 2. If part1 is not a known country, but part2 is a known country (e.g. "Mashhad, Iran" or "مشهد، ایران")
|
|
|
|
|
|
if (isKnownCountry(part2)) { |
|
|
|
|
|
return { country: part2, city: part1 }; |
|
|
|
|
|
} |
|
|
|
|
|
// 3. Fallback: assume first part is country
|
|
|
|
|
|
return { country: part1, city: part2 }; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
if (str.includes(",")) { |
|
|
if (str.includes(",")) { |
|
|
const idx = str.indexOf(","); |
|
|
const idx = str.indexOf(","); |
|
|
const countryPart = str.slice(0, idx).trim(); |
|
|
|
|
|
const cityPart = str.slice(idx + 1).trim(); |
|
|
|
|
|
return { country: countryPart, city: cityPart }; |
|
|
|
|
|
|
|
|
return splitLocation(str.slice(0, idx).trim(), str.slice(idx + 1).trim()); |
|
|
} |
|
|
} |
|
|
if (str.includes("،")) { |
|
|
if (str.includes("،")) { |
|
|
const idx = str.indexOf("،"); |
|
|
const idx = str.indexOf("،"); |
|
|
const countryPart = str.slice(0, idx).trim(); |
|
|
|
|
|
const cityPart = str.slice(idx + 1).trim(); |
|
|
|
|
|
return { country: countryPart, city: cityPart }; |
|
|
|
|
|
|
|
|
return splitLocation(str.slice(0, idx).trim(), str.slice(idx + 1).trim()); |
|
|
} |
|
|
} |
|
|
if (str.includes(" - ")) { |
|
|
if (str.includes(" - ")) { |
|
|
const idx = str.indexOf(" - "); |
|
|
const idx = str.indexOf(" - "); |
|
|
const countryPart = str.slice(0, idx).trim(); |
|
|
|
|
|
const cityPart = str.slice(idx + 3).trim(); |
|
|
|
|
|
return { country: countryPart, city: cityPart }; |
|
|
|
|
|
|
|
|
return splitLocation(str.slice(0, idx).trim(), str.slice(idx + 3).trim()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (isKnownCountry(str)) { |
|
|
if (isKnownCountry(str)) { |
|
|
|