Skip to content

Instantly share code, notes, and snippets.

@fatuk
Last active August 29, 2015 14:02
Show Gist options
  • Save fatuk/a3f025606adcd876ef73 to your computer and use it in GitHub Desktop.
Save fatuk/a3f025606adcd876ef73 to your computer and use it in GitHub Desktop.
Declining function
<?php
/**
* Возвращает единицу измерения с правильным окончанием
*
* @param {Number} num Число
* @param {Object} cases Варианты слова {nom: 'час', gen: 'часа', plu: 'часов'}
* @return {String}
*/
function units(num, cases) {
num = Math.abs(num);
var word = '';
if (num.toString().indexOf('.') > -1) {
word = cases.gen;
} else {
word = (
num % 10 == 1 && num % 100 != 11
? cases.nom
: num % 10 >= 2 && num % 10 <= 4 && (num % 100 < 10 || num % 100 >= 20)
? cases.gen
: cases.plu
);
}
return word;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment