JavaScript - Formater un nombre avec PluralRules

<!DOCTYPE html>
<html lang="fr">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input id="nb" type="number" oninput="changeNumber()" min="1" value="1" />
    <p><span id="number"></span></p>
    <script>
        const nbArticlesEl = document.getElementById("nb");
        const basketEl = document.getElementById("number");

        const enOrdinalRules = new Intl.PluralRules("fr-FR");

        const suffixes = new Map([
            ["one", "er"],
            ["other", "ème"]
        ]);
        const formatOrdinals = (n) => {
            const rule = enOrdinalRules.select(n);
            const suffix = suffixes.get(rule);
            return `${n}${suffix}`;
        };

        function changeNumber() {
            number = formatOrdinals(nbArticlesEl.value);
            basketEl.innerHTML = number;
        }
    </script>
</body>

</html>

Commentaires