UNPKG

827 BJavaScriptView Raw
1'use strict';
2
3function declineWord(n, body, one = null, two_till_four = null, zero_or_five_till_nine = null) {
4 n = Math.abs(n % 100);
5 const n1 = Math.floor(n / 10);
6 const n2 = n % 10;
7
8 if(!(body || one).match(/[a-z]/i)) {
9 one = one || '';
10 two_till_four = two_till_four || '';
11 zero_or_five_till_nine = zero_or_five_till_nine || '';
12 } else {
13 one = one != null ? one : '';
14 two_till_four = two_till_four != null ? two_till_four : 's';
15 zero_or_five_till_nine = zero_or_five_till_nine != null ? zero_or_five_till_nine : two_till_four;
16 }
17
18 if(n1 !== 1) {
19 if(n2 === 1) {
20 return `${body}${one}`;
21 }
22 if(2 <= n2 && n2 <= 4) {
23 return `${body}${two_till_four}`;
24 }
25 }
26 return `${body}${zero_or_five_till_nine}`;
27}
28
29module.exports = declineWord;