Skip to content

Commit

Permalink
Version 4.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Apr 4, 2018
1 parent 2462c62 commit 82b3733
Show file tree
Hide file tree
Showing 14 changed files with 4,053 additions and 2 deletions.
15 changes: 15 additions & 0 deletions bin/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
var combined = exports.combined = {
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}'],
categories: ['{cardinal:["other"],ordinal:["other"]}', '{cardinal:["one","other"],ordinal:["other"]}', '{cardinal:["one","other"],ordinal:["one","other"]}', '{cardinal:["one","two","other"],ordinal:["other"]}']
};

var cardinals = exports.cardinals = {
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}'],
categories: ['{cardinal:["other"],ordinal:[]}', '{cardinal:["one","other"],ordinal:[]}', '{cardinal:["one","other"],ordinal:[]}', '{cardinal:["one","two","other"],ordinal:[]}']
};

113 changes: 113 additions & 0 deletions bin/make-plural
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/usr/bin/env node

'use strict';

var _common = require('./common');

var common = _interopRequireWildcard(_common);

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; } }

var argv = require('minimist')(process.argv.slice(2), {
default: { locale: null, value: null, ordinal: null, cardinal: null, categories: false, es6: false },
alias: { locale: 'l', value: 'v', ordinal: 'o', cardinal: 'c', es6: 'e' },
string: ['locale', 'value'],
boolean: ['categories', 'es6']
}); /** A compiler for make-plural.js
*
* Usage:
* ./bin/make-plural // checks all locale rules
* ./bin/make-plural [lc] // prints the locale function for LC
* ./bin/make-plural [lc] [n] [ord] // prints the (ORD ? ordinal : plural) category for N in locale LC
*/

var MakePlural = require('../make-plural').load(require('../data/plurals.json'), require('../data/ordinals.json'));

var es6module = function es6module(value) {
return '\nexport default {\n' + value + '\n}';
};

// UMD pattern adapted from https://github.com/umdjs/umd/blob/master/returnExports.js
var umd = function umd(global, value) {
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}));';
};

function mapForEachLanguage(cb, opt) {
var style = opt && !opt.cardinals ? 'ordinal' : 'cardinal';
var languages = [];
for (var lc in MakePlural.rules[style]) {
var key = /^[A-Z_$][0-9A-Z_$]*$/i.test(lc) && lc !== 'in' ? lc : JSON.stringify(lc);
var mp = new MakePlural(lc, opt).test();
languages.push(key + ': ' + cb(mp));
}
return languages;
}

function printPluralsModule(es6) {
var cp = common[MakePlural.ordinals ? 'combined' : 'cardinals'].plurals;
var plurals = mapForEachLanguage(function (mp) {
var fn = mp.toString();
cp.forEach(function (p, i) {
if (fn === p) fn = '_cp[' + i + ']';
});
return fn;
});
if (es6) {
console.log('const _cp = [\n' + cp.join(',\n') + '\n];');
console.log(es6module(plurals.join(',\n\n')));
} else {
console.log('var _cp = [\n' + cp.join(',\n') + '\n];');
console.log(umd('plurals', plurals.join(',\n\n')));
}
}

function printCategoriesModule(es6) {
var cc = common[MakePlural.ordinals ? 'combined' : 'cardinals'].categories;
var categories = mapForEachLanguage(function (mp) {
var cat = JSON.stringify(mp.categories).replace(/"(\w+)":/g, '$1:');
cc.forEach(function (c, i) {
if (cat === c) cat = '_cc[' + i + ']';
});
return cat;
});
if (es6) {
console.log('const _cc = [\n ' + cc.join(',\n ') + '\n];');
console.log(es6module(categories.join(',\n')));
} else {
console.log('var _cc = [\n ' + cc.join(',\n ') + '\n];');
console.log(umd('pluralCategories', categories.join(',\n')));
}
}

function truthy(v) {
if (v === '0' || v === 'false') return false;
return !!v;
}

argv._.forEach(function (a) {
if (argv.locale === null) argv.locale = a;else if (argv.value === null) argv.value = a;else if (argv.ordinal === null) argv.ordinal = a;
});

MakePlural.cardinals = argv.cardinal !== null ? truthy(argv.cardinal) : true;
MakePlural.ordinals = argv.ordinal !== null ? truthy(argv.ordinal) : true;

if (argv.locale) {
var mp = new MakePlural(argv.locale).test();
if (argv.categories) {
var cats = mp.categories.cardinal.concat(mp.categories.ordinal).filter(function (v, i, self) {
return self.indexOf(v) === i;
});
console.log(cats.join(', '));
} else if (argv.value !== null) {
console.log(mp(argv.value, truthy(argv.ordinal)));
} else {
console.log(mp.toString(argv.locale));
}
} else {
if (argv.categories) {
printCategoriesModule(argv.es6);
} else {
printPluralsModule(argv.es6);
}
}

Loading

0 comments on commit 82b3733

Please sign in to comment.