Skip to content

Instantly share code, notes, and snippets.

@maradondt
Created June 16, 2022 10:40
Show Gist options
  • Save maradondt/6e0a1ecbc4b51f8eeb39f7e29213a950 to your computer and use it in GitHub Desktop.
Save maradondt/6e0a1ecbc4b51f8eeb39f7e29213a950 to your computer and use it in GitHub Desktop.
склонение
/**
* @description
* https://realadmin.ru/coding/sklonenie-na-javascript.html
* @param n number
* @param text_forms string[]
* @returns string
*
* @example
* ```js
* declOfNum(1, ['минута', 'минуты', 'минут']); // вернёт — минута
* declOfNum(2, ['минута', 'минуты', 'минут']); // вернёт — минуты
* declOfNum(5, ['минута', 'минуты', 'минут']); // вернёт — минут
* ```
*/
export function declOfNum(n: number, text_forms: string[]) {
n = Math.abs(n) % 100;
const n1 = n % 10;
if (n > 10 && n < 20) {
return text_forms[2];
}
if (n1 > 1 && n1 < 5) {
return text_forms[1];
}
if (n1 == 1) {
return text_forms[0];
}
return text_forms[2];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment