Created
November 6, 2022 20:14
-
-
Save Renik07/276a54c8c40bbdad841208a1d6571b44 to your computer and use it in GitHub Desktop.
Word declension (склонение слов)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const wordDecl = (number:number, titles:[string, string, string]):string => { | |
const cases = [2, 0, 1, 1, 1, 2]; | |
return titles[ (number % 100 > 4 && number % 100 < 20) ? 2 : cases[(number % 10 < 5) ? number % 10 : 5] ]; | |
} | |
wordDecl(product.reviewCount, ["отзыв", "отзыва", "отзывов"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Перечисляем варианты ответов от 0 до 5 отзывов | |
const cases = [2, 0, 1, 1, 1, 2]; | |
// Если 5 (105) отзывов... 19 (119) отзывов, возвращаем третий вариант | |
number % 100 > 4 && number % 100 < 20 | |
// Для покрытия кейса типа 24, 34 ... | |
number % 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment