-
-
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.
Merge pull request #24 from eemeli/add-examples
Add 'make-plural/examples'
- Loading branch information
Showing
12 changed files
with
801 additions
and
78 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
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,47 @@ | ||
import { identifier } from 'safe-identifier' | ||
import getCompiler from './get-compiler.js' | ||
import printUMD from './print-umd.js' | ||
|
||
export default function printExamplesModule(args) { | ||
const MakePlural = getCompiler(args) | ||
const { locale, dts, maxRepeat, umd } = args | ||
const locales = | ||
locale.length === 0 ? Object.keys(MakePlural.rules.cardinal) : locale.sort() | ||
|
||
const localesByExample = {} | ||
for (const lc of locales) { | ||
const mpc = new MakePlural(lc) | ||
mpc.compile() | ||
mpc.test() | ||
const ex = JSON.stringify(mpc.examples) | ||
const id = identifier(lc) | ||
const prev = localesByExample[ex] | ||
if (prev) prev.push(id) | ||
else localesByExample[ex] = [id] | ||
} | ||
|
||
let str = '' | ||
let commonId = 'a' | ||
const examples = [] | ||
for (const [ex, locales] of Object.entries(localesByExample)) { | ||
if (!dts && locales.length > maxRepeat && commonId <= 'z') { | ||
str += `const ${commonId} = ${ex};\n` | ||
for (const lc of locales) examples.push({ lc, ex: commonId }) | ||
commonId = String.fromCharCode(commonId.charCodeAt(0) + 1) | ||
} else { | ||
for (const lc of locales) examples.push({ lc, ex }) | ||
} | ||
} | ||
examples.sort((a, b) => (a.lc < b.lc ? -1 : 1)) | ||
if (str) str += '\n' | ||
|
||
if (dts) { | ||
for (const { lc, ex } of examples) str += `export const ${lc}: ${ex};\n` | ||
} else if (umd) { | ||
const cm = examples.map(({ lc, ex }) => `${lc}: ${ex}`) | ||
str += printUMD('pluralCategories', cm.join(',\n')) + '\n' | ||
} else { | ||
for (const { lc, ex } of examples) str += `export const ${lc} = ${ex};\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
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 |
---|---|---|
@@ -1,57 +1,25 @@ | ||
export class Tests { | ||
constructor(obj) { | ||
this.lc = obj.lc | ||
this.ordinal = {} | ||
this.cardinal = {} | ||
} | ||
|
||
add(type, cat, src) { | ||
this[type][cat] = { src, values: null } | ||
} | ||
|
||
error(n, type, msg) { | ||
const lc = JSON.stringify(this.lc) | ||
const val = JSON.stringify(n) | ||
return new Error( | ||
`Locale ${lc} ${type} rule self-test failed for ${val} (${msg})` | ||
) | ||
} | ||
function errorMsg(lc, n, type, msg) { | ||
const val = JSON.stringify(n) | ||
return `Locale ${lc} ${type} rule self-test failed for ${val} (${msg})` | ||
} | ||
|
||
testCond(n, type, expResult, fn) { | ||
try { | ||
var r = fn(n, type === 'ordinal') | ||
} catch (error) { | ||
/* istanbul ignore next: should not happen unless CLDR data is broken */ | ||
throw this.error(n, type, error) | ||
} | ||
if (r !== expResult) { | ||
const res = JSON.stringify(r) | ||
const exp = JSON.stringify(expResult) | ||
throw this.error(n, type, `was ${res}, expected ${exp}`) | ||
} | ||
return true | ||
function testCond(lc, n, type, expResult, fn) { | ||
try { | ||
var r = fn(n, type === 'ordinal') | ||
} catch (error) { | ||
/* istanbul ignore next: should not happen unless CLDR data is broken */ | ||
throw new Error(errorMsg(lc, n, type, error)) | ||
} | ||
|
||
testCat(type, cat, fn) { | ||
const data = this[type][cat] | ||
if (!data.values) { | ||
data.values = data.src | ||
.join(' ') | ||
.replace(/^[ ,]+|[ ,…]+$/g, '') | ||
.replace(/(0\.[0-9])~(1\.[1-9])/g, '$1 1.0 $2') | ||
.split(/[ ,~…]+/) | ||
.filter(n => !n.includes('c')) | ||
} | ||
data.values.forEach(n => { | ||
this.testCond(n, type, cat, fn) | ||
if (!/\.0+$/.test(n)) this.testCond(Number(n), type, cat, fn) | ||
}) | ||
return true | ||
if (r !== expResult) { | ||
const res = JSON.stringify(r) | ||
const exp = JSON.stringify(expResult) | ||
throw new Error(errorMsg(lc, n, type, `was ${res}, expected ${exp}`)) | ||
} | ||
} | ||
|
||
testAll(fn) { | ||
for (let cat in this.cardinal) this.testCat('cardinal', cat, fn) | ||
for (let cat in this.ordinal) this.testCat('ordinal', cat, fn) | ||
return true | ||
export function testCat(lc, type, cat, values, fn) { | ||
for (const n of values) { | ||
testCond(lc, n, type, cat, fn) | ||
if (!/\.0+$/.test(n)) testCond(lc, Number(n), type, cat, fn) | ||
} | ||
} |
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
Oops, something went wrong.