Skip to content

Commit fb19b01

Browse files
committed
Version 4.3.0
1 parent 9cbae0d commit fb19b01

14 files changed

+5412
-2
lines changed

bin/common.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.cardinals = exports.combined = void 0;
7+
var combined = {
8+
plurals: ["function(n, ord) {\n if (ord) return 'other';\n return 'other';\n}", "function(n, ord) {\n if (ord) return 'other';\n return (n == 1) ? 'one' : 'other';\n}", "function(n, ord) {\n if (ord) return 'other';\n return ((n == 0\n || n == 1)) ? 'one' : 'other';\n}", "function(n, ord) {\n var s = String(n).split('.'), v0 = !s[1];\n if (ord) return 'other';\n return (n == 1 && v0) ? 'one' : 'other';\n}"],
9+
categories: ['{cardinal:["other"],ordinal:["other"]}', '{cardinal:["one","other"],ordinal:["other"]}', '{cardinal:["one","other"],ordinal:["one","other"]}', '{cardinal:["one","two","other"],ordinal:["other"]}']
10+
};
11+
exports.combined = combined;
12+
var cardinals = {
13+
plurals: ["function(n) {\n return 'other';\n}", "function(n) {\n return (n == 1) ? 'one' : 'other';\n}", "function(n) {\n return ((n == 0\n || n == 1)) ? 'one' : 'other';\n}", "function(n) {\n var s = String(n).split('.'), v0 = !s[1];\n return (n == 1 && v0) ? 'one' : 'other';\n}"],
14+
categories: ['{cardinal:["other"],ordinal:[]}', '{cardinal:["one","other"],ordinal:[]}', '{cardinal:["one","other"],ordinal:[]}', '{cardinal:["one","two","other"],ordinal:[]}']
15+
};
16+
exports.cardinals = cardinals;
17+

bin/make-plural

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#!/usr/bin/env node
2+
3+
"use strict";
4+
5+
var common = _interopRequireWildcard(require("./common"));
6+
7+
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
8+
9+
/** A compiler for make-plural.js
10+
*
11+
* Usage:
12+
* ./bin/make-plural // checks all locale rules
13+
* ./bin/make-plural [lc] // prints the locale function for LC
14+
* ./bin/make-plural [lc] [n] [ord] // prints the (ORD ? ordinal : plural) category for N in locale LC
15+
*/
16+
var argv = require('minimist')(process.argv.slice(2), {
17+
default: {
18+
locale: null,
19+
value: null,
20+
ordinal: null,
21+
cardinal: null,
22+
categories: false,
23+
es6: false
24+
},
25+
alias: {
26+
locale: 'l',
27+
value: 'v',
28+
ordinal: 'o',
29+
cardinal: 'c',
30+
es6: 'e'
31+
},
32+
string: ['locale', 'value'],
33+
boolean: ['categories', 'es6']
34+
});
35+
36+
var MakePlural = require('../make-plural').load(require('../data/plurals.json'), require('../data/ordinals.json'));
37+
38+
var es6module = function es6module(value) {
39+
return "\nexport default {\n".concat(value, "\n}");
40+
}; // UMD pattern adapted from https://github.com/umdjs/umd/blob/master/returnExports.js
41+
42+
43+
var umd = function umd(global, value) {
44+
return "\n(function (root, ".concat(global, ") {\n if (typeof define === 'function' && define.amd) {\n define(").concat(global, ");\n } else if (typeof exports === 'object') {\n module.exports = ").concat(global, ";\n } else {\n root.").concat(global, " = ").concat(global, ";\n }\n}(this, {\n").concat(value, "\n}));");
45+
};
46+
47+
function mapForEachLanguage(cb, opt) {
48+
var style = opt && !opt.cardinals ? 'ordinal' : 'cardinal';
49+
var languages = [];
50+
51+
for (var lc in MakePlural.rules[style]) {
52+
var key = /^[A-Z_$][0-9A-Z_$]*$/i.test(lc) && lc !== 'in' ? lc : JSON.stringify(lc);
53+
var mp = new MakePlural(lc, opt).test();
54+
languages.push(key + ': ' + cb(mp));
55+
}
56+
57+
return languages;
58+
}
59+
60+
function printPluralsModule(es6) {
61+
var cp = common[MakePlural.ordinals ? 'combined' : 'cardinals'].plurals;
62+
var plurals = mapForEachLanguage(function (mp) {
63+
var fn = mp.toString();
64+
cp.forEach(function (p, i) {
65+
if (fn === p) fn = "_cp[".concat(i, "]");
66+
});
67+
return fn;
68+
});
69+
70+
if (es6) {
71+
console.log('const _cp = [\n' + cp.join(',\n') + '\n];');
72+
console.log(es6module(plurals.join(',\n\n')));
73+
} else {
74+
console.log('var _cp = [\n' + cp.join(',\n') + '\n];');
75+
console.log(umd('plurals', plurals.join(',\n\n')));
76+
}
77+
}
78+
79+
function printCategoriesModule(es6) {
80+
var cc = common[MakePlural.ordinals ? 'combined' : 'cardinals'].categories;
81+
var categories = mapForEachLanguage(function (mp) {
82+
var cat = JSON.stringify(mp.categories).replace(/"(\w+)":/g, '$1:');
83+
cc.forEach(function (c, i) {
84+
if (cat === c) cat = "_cc[".concat(i, "]");
85+
});
86+
return cat;
87+
});
88+
89+
if (es6) {
90+
console.log('const _cc = [\n ' + cc.join(',\n ') + '\n];');
91+
console.log(es6module(categories.join(',\n')));
92+
} else {
93+
console.log('var _cc = [\n ' + cc.join(',\n ') + '\n];');
94+
console.log(umd('pluralCategories', categories.join(',\n')));
95+
}
96+
}
97+
98+
function truthy(v) {
99+
if (v === '0' || v === 'false') return false;
100+
return !!v;
101+
}
102+
103+
argv._.forEach(function (a) {
104+
if (argv.locale === null) argv.locale = a;else if (argv.value === null) argv.value = a;else if (argv.ordinal === null) argv.ordinal = a;
105+
});
106+
107+
MakePlural.cardinals = argv.cardinal !== null ? truthy(argv.cardinal) : true;
108+
MakePlural.ordinals = argv.ordinal !== null ? truthy(argv.ordinal) : true;
109+
110+
if (argv.locale) {
111+
var mp = new MakePlural(argv.locale).test();
112+
113+
if (argv.categories) {
114+
var cats = mp.categories.cardinal.concat(mp.categories.ordinal).filter(function (v, i, self) {
115+
return self.indexOf(v) === i;
116+
});
117+
console.log(cats.join(', '));
118+
} else if (argv.value !== null) {
119+
console.log(mp(argv.value, truthy(argv.ordinal)));
120+
} else {
121+
console.log(mp.toString(argv.locale));
122+
}
123+
} else {
124+
if (argv.categories) {
125+
printCategoriesModule(argv.es6);
126+
} else {
127+
printPluralsModule(argv.es6);
128+
}
129+
}
130+

0 commit comments

Comments
 (0)