-
-
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.
fix(cli): Refactor cli.js, splitting out print-categories, print-plur…
…als & print-umd
- Loading branch information
Showing
4 changed files
with
150 additions
and
133 deletions.
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
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,32 @@ | ||
import { identifier } from 'safe-identifier' | ||
import * as common from './common' | ||
import umd from './print-umd' | ||
|
||
export default function printCategoriesModule(MakePlural, { es6 }) { | ||
const { categories: cc } = MakePlural.ordinals | ||
? common.combined | ||
: common.cardinals | ||
const categories = Object.keys(MakePlural.rules.cardinal).map(lc => { | ||
const mpc = new MakePlural(lc) | ||
mpc.compile() | ||
mpc.test() | ||
const cat = JSON.stringify(mpc.categories).replace(/"(\w+)":/g, '$1:') | ||
const i = cc.indexOf(cat) | ||
return [lc, i === -1 ? cat : `_${i}`] | ||
}) | ||
|
||
let str | ||
if (es6) { | ||
str = cc.map((c, i) => `const _${i} = ${c};`).join('\n') + '\n\n' | ||
for (const [lc, cat] of categories) { | ||
const id = identifier(lc) | ||
str += `export const ${id} = ${cat};\n` | ||
} | ||
} else { | ||
str = cc.map((c, i) => `var _${i} = ${c};`).join('\n') + '\n\n' | ||
const cm = categories.map(([lc, cat]) => `${identifier(lc)}: ${cat}`) | ||
str += umd('pluralCategories', cm.join(',\n')) + '\n' | ||
} | ||
return str | ||
} | ||
|
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,67 @@ | ||
import aliases from 'cldr-core/supplemental/aliases.json' | ||
import { identifier } from 'safe-identifier' | ||
import * as common from './common' | ||
import umd from './print-umd' | ||
|
||
function getAlias(MakePlural, lc) { | ||
const alias = aliases.supplemental.metadata.alias.languageAlias[lc] | ||
if (!alias) return null | ||
const r = alias._replacement | ||
return MakePlural.rules.cardinal[r] ? r : null // https://unicode-org.atlassian.net/browse/CLDR-13227 | ||
} | ||
|
||
export default function printPluralsModule(MakePlural, { es6 }) { | ||
const { plurals: cp } = MakePlural.ordinals | ||
? common.combined | ||
: common.cardinals | ||
const aliased = [] | ||
const plurals = Object.keys(MakePlural.rules.cardinal).map(lc => { | ||
const alias = getAlias(MakePlural, lc) | ||
if (alias) { | ||
aliased.push([lc, alias]) | ||
return [lc, alias] | ||
} | ||
const mpc = new MakePlural(lc) | ||
const fn = mpc.compile().toString() | ||
mpc.test() | ||
const i = cp.indexOf(fn) | ||
return [ | ||
lc, | ||
i === -1 | ||
? fn.replace(/^function\b/, `function ${identifier(lc)}`) | ||
: `_${i}` | ||
] | ||
}) | ||
|
||
let str = '' | ||
for (let i = 0; i < cp.length; ++i) { | ||
str += cp[i].replace(/^function\b/, `function _${i}`) + '\n' | ||
} | ||
for (const [src, tgt] of aliased) { | ||
const idx = plurals.findIndex(pl => pl[0] === tgt) | ||
process.stderr.write(JSON.stringify({ tgt, idx }) + '\n') | ||
const [lc, fn] = plurals[idx] | ||
if (fn.startsWith('_')) { | ||
const jdx = plurals.findIndex(pl => pl[0] === src) | ||
plurals[jdx][1] = fn | ||
} else if (!es6) { | ||
str += `${fn}\n` | ||
plurals[idx][1] = identifier(lc) | ||
} | ||
} | ||
str += '\n' | ||
if (es6) { | ||
for (const [lc, fn] of plurals) { | ||
if (fn.startsWith('function')) { | ||
str += `export ${fn}\n` | ||
} else { | ||
const id = identifier(lc) | ||
str += `export const ${id} = ${fn};\n` | ||
} | ||
} | ||
} else { | ||
const pm = plurals.map(([lc, fn]) => `${identifier(lc)}: ${fn}`) | ||
str += umd('plurals', pm.join(',\n\n')) + '\n' | ||
} | ||
return str | ||
} |
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,18 @@ | ||
import { source } from 'common-tags' | ||
|
||
// UMD pattern adapted from https://github.com/umdjs/umd/blob/master/returnExports.js | ||
export default function umd(global, value) { | ||
return source` | ||
(function (root, ${global}) { | ||
if (typeof define === 'function' && define.amd) { | ||
define(${global}); | ||
} else if (typeof exports === 'object') { | ||
module.exports = ${global}; | ||
} else { | ||
root.${global} = ${global}; | ||
} | ||
}(this, { | ||
${value} | ||
})); | ||
` | ||
} |