Skip to content

Commit

Permalink
feat(@formatjs/intl-numberformat): reduce asset size since we use Int…
Browse files Browse the repository at this point in the history
…l.getCanonicalLocales
  • Loading branch information
longlho committed May 24, 2020
1 parent c376d66 commit 3ae64b2
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 182 deletions.
10 changes: 10 additions & 0 deletions packages/intl-numberformat/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

* **@formatjs/intl-numberformat:** Use native Intl.getCanonicalLocales ([54cea38](https://github.com/formatjs/formatjs/commit/54cea38396bdfbbc24b1fa12996a5347f44a61f7))

This is a rewrite of Intl.NumberFormat polyfill:
- Feature compatible with ES2021 spec.
- Pass most of Intl.NumberFormat test262 cases.
- New version is 3 to 4 times faster in the benchmark of instantiating NumberFormat object + formatting numbers in currency or unit styles. It is almost 2 times faster in the benchmark variant that reuses the NumberFormat object.
- Slightly reduce locale data JSON size.
- Support compound unit like `kilometer-per-second`, where `kilometer` and `second` can be any simple unit whitelisted in [IsSanctionedSimpleUnitIdentifier][].
- Polyfill for `NumberFormat.prototype.toLocaleString`.
- Drop `Unified` prefix from the type definitions.
- Reorganized source module structure.


### BREAKING CHANGES

Expand Down
12 changes: 6 additions & 6 deletions packages/intl-numberformat/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {uglify} from 'rollup-plugin-uglify';
import resolve from 'rollup-plugin-node-resolve';
import json from '@rollup/plugin-json';
import commonjs from 'rollup-plugin-commonjs';
import {generateTestConfig} from '../../rollup.config';
import * as path from 'path';
// import {generateTestConfig} from '../../rollup.config';
// import * as path from 'path';
const resolveConfig = resolve({
mainFields: ['module', 'main'],
});
Expand All @@ -12,9 +12,9 @@ const jsonConfig = json();
const commonjsConfig = commonjs();

// This is taking a really long time
const browserFriendlyTests = [
path.resolve(__dirname, 'tests/unit-zh-smoke.test.ts'),
];
// const browserFriendlyTests = [
// path.resolve(__dirname, 'tests/unit-zh-smoke.test.ts'),
// ];

export default [
{
Expand Down Expand Up @@ -76,5 +76,5 @@ export default [
jsonConfig,
],
},
...browserFriendlyTests.map(generateTestConfig),
// ...browserFriendlyTests.map(generateTestConfig),
];
7 changes: 0 additions & 7 deletions packages/intl-numberformat/scripts/cldr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {
} from 'formatjs-extract-cldr-data';
import {
SANCTIONED_UNITS,
getAliasesByLang,
getParentLocalesByLang,
removeUnitNamespace,
RawNumberLocaleData,
} from '@formatjs/intl-utils';
Expand All @@ -25,9 +23,6 @@ const unitsData = generateUnitDataForLocales();

const allData = locales.reduce(
(all: Record<string, RawNumberLocaleData>, locale) => {
const lang = locale.split('-')[0];
const aliases = getAliasesByLang(lang);
const parentLocales = getParentLocalesByLang(lang);
if (!all[locale]) {
all[locale] = {
data: {
Expand All @@ -39,8 +34,6 @@ const allData = locales.reduce(
},
},
availableLocales: [locale],
aliases,
parentLocales,
};
}

Expand Down
11 changes: 1 addition & 10 deletions packages/intl-numberformat/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,16 +385,7 @@ NumberFormat.__addLocaleData = function __addLocaleData(
...data: RawNumberLocaleData[]
) {
for (const datum of data) {
const availableLocales: string[] = Object.keys(
[
...datum.availableLocales,
...Object.keys(datum.aliases),
...Object.keys(datum.parentLocales),
].reduce((all: Record<string, true>, k) => {
all[k] = true;
return all;
}, {})
);
const availableLocales: string[] = datum.availableLocales;
for (const locale of availableLocales) {
try {
NumberFormat.localeData[locale] = unpackData(locale, datum);
Expand Down
1 change: 1 addition & 0 deletions packages/intl-numberformat/tests/currency-code.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '@formatjs/intl-getcanonicallocales/polyfill';
import {NumberFormat} from '../src';
import * as en from '../src/locale-data/en.json';
NumberFormat.__addLocaleData(en as any);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '@formatjs/intl-getcanonicallocales/polyfill';
import '@formatjs/intl-pluralrules/polyfill-locales';
import {NumberFormat} from '../src';
import * as zh from '../src/locale-data/zh.json';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '@formatjs/intl-getcanonicallocales/polyfill';
import '@formatjs/intl-pluralrules/polyfill-locales';
import {NumberFormat} from '../src';
import * as zh from '../src/locale-data/zh.json';
Expand Down
1 change: 1 addition & 0 deletions packages/intl-numberformat/tests/signDisplay-zh-TW.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '@formatjs/intl-getcanonicallocales/polyfill';
import '@formatjs/intl-pluralrules/polyfill-locales';
import {NumberFormat} from '../src';
import * as zh from '../src/locale-data/zh.json';
Expand Down
3 changes: 2 additions & 1 deletion packages/intl-numberformat/tests/unit-zh-TW.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '@formatjs/intl-getcanonicallocales/polyfill';
import '@formatjs/intl-pluralrules/polyfill-locales';
import {NumberFormat} from '../src';
import * as zh from '../src/locale-data/zh.json';
Expand Down Expand Up @@ -147,7 +148,7 @@ describe('unit-zh-TW', function () {
for (const [number, expectedData] of tests) {
for (const unitDisplay in expectedData) {
const expected = expectedData[unitDisplay];
it(unitDisplay, function () {
it(`${number} - ${unitDisplay}`, function () {
const nf = new NumberFormat('zh-TW', {
style: 'unit',
unit: 'meter',
Expand Down
158 changes: 0 additions & 158 deletions packages/intl-numberformat/tests/unit-zh-smoke.test.ts

This file was deleted.

0 comments on commit 3ae64b2

Please sign in to comment.