Last active
August 29, 2015 14:02
-
-
Save fatuk/a3f025606adcd876ef73 to your computer and use it in GitHub Desktop.
Declining function
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
<?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