Skip to content

Commit 018dd7b

Browse files
committed
Version 3.0.6
1 parent a7ec25f commit 018dd7b

File tree

11 files changed

+2726
-1
lines changed

11 files changed

+2726
-1
lines changed

bin/common.js

Lines changed: 15 additions & 0 deletions
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+

bin/make-plural

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

0 commit comments

Comments
 (0)