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