-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
2,726 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:[]}'] | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#!/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; } } | ||
|
||
/** 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 }, | ||
alias: { locale: 'l', value: 'v', ordinal: 'o', cardinal: 'c' }, | ||
string: ['locale', 'value'], | ||
boolean: ['categories'] | ||
}), | ||
MakePlural = require('../make-plural').load(require('../data/plurals.json'), require('../data/ordinals.json')); | ||
|
||
// UMD pattern adapted from https://github.com/umdjs/umd/blob/master/returnExports.js | ||
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), | ||
mp = new MakePlural(lc, opt).test(); | ||
languages.push(key + ': ' + cb(mp)); | ||
} | ||
return languages; | ||
} | ||
|
||
function printPluralsModule() { | ||
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; | ||
}); | ||
console.log('var _cp = [\n' + cp.join(',\n') + '\n];'); | ||
console.log(umd('plurals', plurals.join(',\n\n'))); | ||
} | ||
|
||
function printCategoriesModule() { | ||
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; | ||
}); | ||
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(); | ||
} else { | ||
printPluralsModule(); | ||
} | ||
} | ||
|
Oops, something went wrong.