Skip to content

Instantly share code, notes, and snippets.

@mistadikay
Created March 25, 2015 08:18
Show Gist options
  • Save mistadikay/6a68e1429c5944bc867c to your computer and use it in GitHub Desktop.
Save mistadikay/6a68e1429c5944bc867c to your computer and use it in GitHub Desktop.
/**
* @param {object} NS namespace (window by default)
*/
(function(NS){
NS = NS || window;
/**
* Returns option from cases with the correct ending according to passed number
*
* @param {Number} num number
* @param {Object} cases {nom: 'час', gen: 'часа', plu: 'часов'}
* nom — Nominativ, именительный падеж;
* gen — Genetiv, родительный падеж;
* plu — Plural, множественное число.
* @return {String}
*/
NS.textUnits = function(num, cases){
num = Math.abs(num);
if(cases === u){
cases = {nom: '1', gen: '2', plu: ''};
}
if (num.toString().indexOf('.') !== -1) {
return cases.gen;
} else {
return (
num % 10 == 1 && num % 100 != 11
? cases.nom
: num % 10 >= 2 && num % 10 <= 4 && (num % 100 < 10 || num % 100 >= 20)
? cases.gen
: cases.plu
);
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment