Created
November 5, 2021 13:21
-
-
Save ShepetaAndrey/d3507a1ac72ff3544cdb734fd1a80178 to your computer and use it in GitHub Desktop.
Word declension in JS
This file contains hidden or 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
/** | |
* Defines the right word form suitable for number provided | |
* @param {Number} number | |
* @param {[String]} declensions | |
* @param {[Number]} cases | |
* @returns {String} | |
* @example | |
* const result = declension(10, ['монета', 'монеты', 'монет']) | |
* console.log(result) // 'монет' | |
*/ | |
export function declension(number, declensions, cases = [2, 0, 1, 1, 1, 2]) { | |
return declensions[ | |
number % 100 > 4 && number % 100 < 20 | |
? 2 | |
: cases[number % 10 < 5 ? number % 10 : 5] | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment