Spaces:
Running
Running
File size: 1,914 Bytes
618aeaa | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | export function uniqueLocationParts(parts) {
const seen = new Set();
return parts.map((part) => String(part || "").trim()).filter((part) => {
if (!part) return false;
const key = normalizeLocationName(part);
if (seen.has(key)) return false;
seen.add(key);
return true;
});
}
export function locationHeading(city, lang) {
const country = lang === "zh" ? city?.countryZh : city?.countryEn;
const place = lang === "zh" ? city?.cityZh : city?.cityEn;
return uniqueLocationParts([country, place]).join(" · ");
}
export function combinedLocationHeading(city) {
if (hasCountryCityEcho(city)) {
return uniqueLocationParts([
city?.countryZh || city?.cityZh,
city?.countryEn || city?.cityEn,
]).join(" | ");
}
return uniqueLocationParts([
locationHeading(city, "zh"),
locationHeading(city, "en"),
]).join(" | ");
}
export function bookshelfLocationLabel(city) {
return uniqueLocationParts([
city?.countryEn || city?.countryZh,
city?.cityEn || city?.cityZh,
]).join(" · ");
}
function hasCountryCityEcho(city) {
return sameLocationName(city?.countryZh, city?.cityZh)
|| sameLocationName(city?.countryEn, city?.cityEn)
|| cityNameExtendsCountry(city?.countryZh, city?.cityZh);
}
function sameLocationName(left, right) {
const normalizedLeft = normalizeLocationName(left);
const normalizedRight = normalizeLocationName(right);
return Boolean(normalizedLeft && normalizedRight && normalizedLeft === normalizedRight);
}
function normalizeLocationName(value) {
return String(value || "")
.trim()
.toLocaleLowerCase()
.replace(/\s+/g, " ");
}
function cityNameExtendsCountry(country, city) {
const normalizedCountry = normalizeLocationName(country);
const normalizedCity = normalizeLocationName(city);
return Boolean(normalizedCountry && normalizedCity && normalizedCity === `${normalizedCountry}城`);
}
|