From 4103f1c0ca59f933b07919eca0e9a862816e20fa Mon Sep 17 00:00:00 2001 From: mortezaei Date: Sat, 22 Aug 2026 02:33:14 +0330 Subject: [PATCH] fix city and country --- .../Componentes/question-birthplace.test.ts | 23 ++++++++ .../Componentes/question-birthplace.tsx | 54 +++++++++++++++---- 2 files changed, 66 insertions(+), 11 deletions(-) diff --git a/src/components/Componentes/question-birthplace.test.ts b/src/components/Componentes/question-birthplace.test.ts index 0d6b2eb..c2edf60 100644 --- a/src/components/Componentes/question-birthplace.test.ts +++ b/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", diff --git a/src/components/Componentes/question-birthplace.tsx b/src/components/Componentes/question-birthplace.tsx index 1f08acf..268747f 100644 --- a/src/components/Componentes/question-birthplace.tsx +++ b/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)) {