-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
64 lines (50 loc) · 1.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict'
var slice = Array.prototype.slice;
function getPluralNoun(num) {
var forms = slice.call(arguments, 1);
var str;
switch (forms.length) {
case 1:
throw new Error('Not enough forms');
break;
case 2:
str = num > 1 ? forms[1] : forms[0];
break;
default:
str = forms[getNounPluralForm(num)];
break;
}
return str.replace(/%d/g, num);
}
function getPluralVerb(num) {
var forms = slice.call(arguments, 1);
var str = forms[getVerbPluralForm(num)];
return str.replace(/%d/g, num);
}
function getVerbPluralForm(a) {
if (a > 1000000) {
return 2;
}
if (a > 1000 && a < 1000000 && /000$/.test(a)) {
a /= 1000;
}
if (a % 10 === 1 && a % 100 !== 11 || /1000$/.test((a).toString())) {
return 0;
} else if (a % 10 >= 2 && a % 10 <= 4 && (a % 100 < 10 || a % 100 >= 20)) {
return 1;
} else {
return 2;
}
}
function getNounPluralForm(a) {
if (a % 10 === 1 && a % 100 !== 11) {
return 0;
} else if (a % 10 >= 2 && a % 10 <= 4 && (a % 100 < 10 || a % 100 >= 20)) {
return 1;
} else {
return 2;
}
}
module.exports = getPluralNoun;
module.exports.noun = getPluralNoun;
module.exports.verb = getPluralVerb;