import * as React from "react";
import type { CountryContext } from "react-svg-worldmap";
import WorldMap from "react-svg-worldmap";
import { populationData } from "../data/CountryData";
const localizedCountryDictionary = new Map<string, string>([
["br", "Brasil"],
["cn", "China"],
["id", "Indonesia"],
["in", "India"],
["mx", "México"],
["ng", "Nigeria"],
["ru", "Rusia"],
["us", "Estados Unidos"],
]);
function localizeNumber(num: number | undefined, digits: number) {
if (typeof num === "undefined") return "";
const magnitude = [
{ value: 1e9, text: " mil millónes " },
{ value: 1e6, text: " millónes " },
{ value: 1e3, text: " miles " },
{ value: 1, text: "" },
].find((magnitude) => num >= magnitude.value);
if (magnitude) {
return (
(num / magnitude.value)
.toFixed(digits)
.replace(/\.0+$|(?<number>\.[0-9]*[1-9])0+$/, "$1") + magnitude.text
);
}
return "";
}
const getLocalizedText = ({
countryCode,
countryValue,
prefix,
suffix,
}: CountryContext) =>
`${
localizedCountryDictionary.get(countryCode.toLocaleLowerCase()) ?? "Unknown"
}: ${prefix}${localizeNumber(countryValue, 2)}${suffix}`;
export default function App(): JSX.Element {
return (
<WorldMap
title="Los diez países principales por población"
data={populationData}
valueSuffix="personas"
tooltipTextFunction={getLocalizedText}
/>
);
}