Browse Source

fix city and country

master
mortezaei 2 days ago
parent
commit
4103f1c0ca
  1. 23
      src/components/Componentes/question-birthplace.test.ts
  2. 54
      src/components/Componentes/question-birthplace.tsx

23
src/components/Componentes/question-birthplace.test.ts

@ -13,6 +13,14 @@ describe("QuestionBirthplace parseValue", () => {
country: "Iran",
city: "Tehran",
});
expect(parseValue({ country: "Mashhad", city: "Iran" })).toEqual({
country: "Iran",
city: "Mashhad",
});
expect(parseValue({ country: "مشهد", city: "ایران" })).toEqual({
country: "ایران",
city: "مشهد",
});
});
it("parses standard 'Country, City' strings", () => {
@ -46,6 +54,21 @@ describe("QuestionBirthplace parseValue", () => {
});
});
it("correctly identifies country and city when formatted as 'City, Country'", () => {
expect(parseValue("Mashhad, Iran")).toEqual({
country: "Iran",
city: "Mashhad",
});
expect(parseValue("مشهد، ایران")).toEqual({
country: "ایران",
city: "مشهد",
});
expect(parseValue("Tehran, IR")).toEqual({
country: "IR",
city: "Tehran",
});
});
it("parses single country string", () => {
expect(parseValue("Iran")).toEqual({
country: "Iran",

54
src/components/Componentes/question-birthplace.tsx

@ -29,9 +29,34 @@ export function parseValue(rawValue: unknown): { country: string; city: string }
if (typeof rawValue === "object" && rawValue !== null) {
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 {
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)",
);
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(",")) {
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("،")) {
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(" - ")) {
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)) {

Loading…
Cancel
Save