-
Notifications
You must be signed in to change notification settings - Fork 9
/
plurals-en.js
44 lines (39 loc) · 1.05 KB
/
plurals-en.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
(function pluralsEn() {
// See plurals-ru.js for Russian
'use strict';
/**
*
* @param {number} amount
* @param {string[]} cases Third case is optional and is used for zero ['apples', 'apples', 'no apples']
* @returns {string}
*/
function pluralize(amount, cases) {
if (amount === 1) {
return cases[0];
} else if (cases.length === 3 && amount === 0) {
return cases[2];
} else {
return cases[1];
}
}
const amountRe = /\${amount}/mg;
const pluralRe = /\${plural}/mg;
function pluralizeFmt(cases, tpl) {
function fmt(amount, plural) {
return tpl
.replace(amountRe, amount)
.replace(pluralRe, plural);
}
return (amount) => {
const plural = pluralize(amount, cases);
return fmt(amount, plural);
};
}
window.scUtils = Object.assign(
window.scUtils || {},
{
pluralize,
pluralizeFmt,
}
);
}());