Skip to content

Instantly share code, notes, and snippets.

@vladislav805
Last active June 14, 2020 20:52
Show Gist options
  • Save vladislav805/c627586d915da64aa2ad9e069492f14d to your computer and use it in GitHub Desktop.
Save vladislav805/c627586d915da64aa2ad9e069492f14d to your computer and use it in GitHub Desktop.
Pluralize: 1 item, 2 items, 5 items / 1 элемент, 2 элемента, 5 элементов
export const pluralize = (number, cases) => {
number = Math.abs(number);
return cases[number % 100 > 4 && number % 100 < 20 ? 2 : [2, 0, 1, 1, 1, 2][number % 10 < 5 ? number % 10 : 5]];
};
export const pluralize = (n: number, cases: [string, string, string]): string => {
n = Math.abs(n);
return cases[n % 100 > 4 && n % 100 < 20 ? 2 : [2, 0, 1, 1, 1, 2][n % 10 < 5 ? n % 10 : 5]];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment