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