mh-two-thousand-and-two
2024-04-12 7fc6dbf547b8899d949b67cdec36b96a7d1701c7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env node
 
"use strict";
 
var common = _interopRequireWildcard(require("./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)) { 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; } }
 
/** 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 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']
});
 
var MakePlural = require('../make-plural').load(require('../data/plurals.json'), require('../data/ordinals.json'));
 
var es6module = function es6module(value) {
  return "\nexport default {\n".concat(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, ".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}));");
};
 
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[".concat(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[".concat(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);
  }
}