Skip to content
This repository has been archived by the owner on May 4, 2020. It is now read-only.

Commit

Permalink
fix(@formatjs/intl-unified-numberformat): validate numbering system (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pyrocat101 authored Apr 26, 2020
1 parent a071f01 commit c3ac544
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 15 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@
"url": "http://json.schemastore.org/lerna"
}
],
"typescript.tsdk": "node_modules/typescript/lib"
"typescript.tsdk": "node_modules/typescript/lib",
"npm.packageManager": "yarn",
"prettier.packageManager": "yarn",
"eslint.packageManager": "yarn"
}
16 changes: 16 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "dev:cjs",
"problemMatcher": ["$tsc-watch"],
"label": "yarn: dev:cjs",
"detail": "tsc -b packages/* --watch",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
9 changes: 8 additions & 1 deletion packages/formatjs-extract-cldr-data/src/extract-numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/
'use strict';
import * as NumbersData from 'cldr-numbers-full/main/ar/numbers.json';
import * as numberingSystems from 'cldr-core/supplemental/numberingSystems.json';
import {Locale} from './types';
import generateFieldExtractorFn, {
collapseSingleValuePluralRule,
Expand Down Expand Up @@ -148,6 +148,13 @@ export function generateDataForLocales(
}, {});
}

export function extractNumberingSystemNames() {
// Export an object instead of array to be more compatible with rollup.
return {
names: Object.keys(numberingSystems.supplemental.numberingSystems),
};
}

export default generateFieldExtractorFn<RawNumberData>(
loadNumbers,
hasNumbers,
Expand Down
5 changes: 4 additions & 1 deletion packages/formatjs-extract-cldr-data/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@ export {
generateDataForLocales as generateCurrencyDataForLocales,
} from './extract-currencies';
export {generateDataForLocales as generateUnitDataForLocales} from './extract-units';
export {generateDataForLocales as generateNumberDataForLocales} from './extract-numbers';
export {
extractNumberingSystemNames,
generateDataForLocales as generateNumberDataForLocales,
} from './extract-numbers';
export {getAllLocales as getAllDisplayNamesLocales} from './extract-displaynames';
7 changes: 7 additions & 0 deletions packages/intl-unified-numberformat/scripts/cldr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
generateNumberDataForLocales,
locales,
extractCurrencyDigits,
extractNumberingSystemNames,
} from 'formatjs-extract-cldr-data';
import {
SANCTIONED_UNITS,
Expand Down Expand Up @@ -146,6 +147,12 @@ outputJSONSync(
extractCurrencyDigits()
);

// Output numbering systems file
outputJSONSync(
resolve(__dirname, '../src/numbering-systems.json'),
extractNumberingSystemNames()
);

// For test262
// Only a subset of locales
outputFileSync(
Expand Down
40 changes: 31 additions & 9 deletions packages/intl-unified-numberformat/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ import {
import {extractILD, Patterns} from './data';
import * as currencyDigitsData from './currency-digits.json';
import * as ILND from './ilnd-numbers.json';
import {names as numberingSystemNames} from './numbering-systems.json';

const VALID_NUMBERING_SYSTEM_NAMES: Record<string, boolean> = Object.create(
null
);
for (const nu of numberingSystemNames) {
VALID_NUMBERING_SYSTEM_NAMES[nu] = true;
}

const RESOLVED_OPTIONS_KEYS = [
'locale',
Expand Down Expand Up @@ -140,6 +148,7 @@ export type UnifiedNumberFormatOptions = Intl.NumberFormatOptions &
signDisplay?: UnifiedNumberFormatOptionsSignDisplay;
unit?: Unit;
unitDisplay?: UnifiedNumberFormatOptionsUnitDisplay;
numberingSystem?: string;
};

export type ResolvedUnifiedNumberFormatOptions = Intl.ResolvedNumberFormatOptions &
Expand Down Expand Up @@ -203,7 +212,8 @@ function initializeNumberFormat(
opts?: UnifiedNumberFormatOptions
) {
const requestedLocales = getCanonicalLocales(locales);
const options = opts === undefined ? Object.create(null) : toObject(opts);
const options: UnifiedNumberFormatOptions =
opts === undefined ? Object.create(null) : toObject(opts);
const opt: any = Object.create(null);
const matcher = getOption(
options,
Expand All @@ -213,6 +223,24 @@ function initializeNumberFormat(
'best fit'
);
opt.localeMatcher = matcher;

const numberingSystem = getOption(
options,
'numberingSystem',
'string',
undefined,
undefined
);
if (
numberingSystem !== undefined &&
!VALID_NUMBERING_SYSTEM_NAMES[numberingSystem]
) {
// 8.a. If numberingSystem does not match the Unicode Locale Identifier type nonterminal,
// throw a RangeError exception.
throw RangeError(`Invalid numberingSystems: ${numberingSystem}`);
}
opt.nu = numberingSystem;

const {localeData} = UnifiedNumberFormat;
const r = createResolveLocale(UnifiedNumberFormat.getDefaultLocale)(
UnifiedNumberFormat.availableLocales,
Expand All @@ -223,17 +251,11 @@ function initializeNumberFormat(
localeData
);
const ildData = localeData[removeUnicodeExtensionFromLocale(r.locale)];
const numberingSystem = r.nu;
setMultiInternalSlots(__INTERNAL_SLOT_MAP__, nf, {
locale: r.locale,
dataLocale: r.dataLocale,
numberingSystem,
ild: extractILD(
ildData.units,
ildData.currencies,
ildData.numbers,
numberingSystem
),
numberingSystem: r.nu,
ild: extractILD(ildData.units, ildData.currencies, ildData.numbers, r.nu),
});

// https://tc39.es/proposal-unified-intl-numberformat/section11/numberformat_proposed_out.html#sec-setnumberformatunitoptions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"names":["adlm","ahom","arab","arabext","armn","armnlow","bali","beng","bhks","brah","cakm","cham","cyrl","deva","ethi","fullwide","geor","gong","gonm","grek","greklow","gujr","guru","hanidays","hanidec","hans","hansfin","hant","hantfin","hebr","hmng","hmnp","java","jpan","jpanfin","jpanyear","kali","khmr","knda","lana","lanatham","laoo","latn","lepc","limb","mathbold","mathdbl","mathmono","mathsanb","mathsans","mlym","modi","mong","mroo","mtei","mymr","mymrshan","mymrtlng","newa","nkoo","olck","orya","osma","rohg","roman","romanlow","saur","shrd","sind","sinh","sora","sund","takr","talu","taml","tamldec","telu","thai","tibt","tirh","vaii","wara","wcho"]}
2 changes: 1 addition & 1 deletion packages/intl-unified-numberformat/tests/runner.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {spawnSync} from 'child_process';
import {resolve} from 'path';
import {cpus} from 'os';
import chalk from 'chalk';
import * as chalk from 'chalk';

if (process.version.startsWith('v13')) {
console.log(
Expand Down
2 changes: 0 additions & 2 deletions packages/intl-unified-numberformat/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
"lib": ["dom", "es5", "esnext.intl", "es2017.intl", "es2018.intl"],
"outDir": "dist",
"rootDir": "src",
"allowSyntheticDefaultImports": true,
"noFallthroughCasesInSwitch": true,
"types": [
"babel__core",
"babel__generator",
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"noUnusedLocals": true,
"noUnusedParameters": true,
"preserveConstEnums": true,
"allowSyntheticDefaultImports": true,
"noFallthroughCasesInSwitch": true,
"declarationMap": true,
"sourceMap": true,
"composite": true,
Expand Down

1 comment on commit c3ac544

@vercel
Copy link

@vercel vercel bot commented on c3ac544 Apr 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.