Skip to content

Commit 62dd4ed

Browse files
committed
Version 4.1.1
1 parent 63e14de commit 62dd4ed

14 files changed

+4029
-2
lines changed

Diff for: bin/common.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
var combined = exports.combined = {
7+
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}'],
8+
categories: ['{cardinal:["other"],ordinal:["other"]}', '{cardinal:["one","other"],ordinal:["other"]}', '{cardinal:["one","other"],ordinal:["one","other"]}', '{cardinal:["one","two","other"],ordinal:["other"]}']
9+
};
10+
11+
var cardinals = exports.cardinals = {
12+
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}'],
13+
categories: ['{cardinal:["other"],ordinal:[]}', '{cardinal:["one","other"],ordinal:[]}', '{cardinal:["one","other"],ordinal:[]}', '{cardinal:["one","two","other"],ordinal:[]}']
14+
};
15+

Diff for: bin/make-plural

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

0 commit comments

Comments
 (0)