Created
April 28, 2020 13:51
-
-
Save Defite/1d710ae3eac06f5eeed7f8a4f9e47ff8 to your computer and use it in GitHub Desktop.
Plural.js
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
/** | |
* Get plural by number | |
* @param {Number} value - number | |
* @param {Array} cases - array of cases | |
* @returns {String} case | |
*/ | |
const plural = (value, cases) => { | |
let n = Math.abs(value); | |
n %= 100; | |
if (n >= 5 && n <= 20) { | |
return cases[2]; | |
} | |
n %= 10; | |
if (n === 1) { | |
return cases[0]; | |
} | |
if (n >= 2 && n <= 4) { | |
return cases[1]; | |
} | |
return cases[2]; | |
}; | |
//Usage | |
console.log(20, plural(20, ['минута', 'минуты', 'минут'])); // 20 минут |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment