Skip to content

Instantly share code, notes, and snippets.

@Renik07
Created November 6, 2022 20:14
Show Gist options
  • Save Renik07/276a54c8c40bbdad841208a1d6571b44 to your computer and use it in GitHub Desktop.
Save Renik07/276a54c8c40bbdad841208a1d6571b44 to your computer and use it in GitHub Desktop.
Word declension (склонение слов)
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, ["отзыв", "отзыва", "отзывов"])
// Перечисляем варианты ответов от 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