Skip to content

Commit 1e13963

Browse files
committed
Auto merge of #3160 - Turbo87:intl, r=pichfl
Replace `Intl.NumberFormat` usage This PR should hopefully fix #3158. I've not been able to reproduce the issue locally, but it looks like it's related to `Intl.NumberFormat` and the polyfill that #3145 introduced. Since we only need this code for number formatting, and we usually want to format in the user's browser locale we can instead use `.toLocaleString()`. I've extracted an `intl` service for this, which makes it easier to test the `format-num` helper, and brings the implementation closer to the ember-intl addon. r? `@pichfl`
2 parents eac5020 + 3775157 commit 1e13963

File tree

7 files changed

+40
-117
lines changed

7 files changed

+40
-117
lines changed

app/helpers/format-num.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
import { helper } from '@ember/component/helper';
1+
import Helper from '@ember/component/helper';
2+
import { inject as service } from '@ember/service';
23

3-
import window from 'ember-window-mock';
4+
export default class FormatNumHelper extends Helper {
5+
@service intl;
46

5-
function newNumberFormat() {
6-
try {
7-
return new Intl.NumberFormat(window.navigator.languages || window.navigator.language);
8-
} catch {
9-
return new Intl.NumberFormat('en');
7+
compute([value]) {
8+
return this.intl.formatNumber(value);
109
}
1110
}
12-
13-
export function formatNum(value) {
14-
return newNumberFormat().format(value);
15-
}
16-
17-
export default helper(params => formatNum(params[0]));

app/routes/application.js

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,11 @@ import { action } from '@ember/object';
22
import Route from '@ember/routing/route';
33
import { inject as service } from '@ember/service';
44

5-
import { shouldPolyfill as shouldPolyfillGetCanonicalLocales } from '@formatjs/intl-getcanonicallocales/should-polyfill';
6-
import { shouldPolyfill as shouldPolyfillLocale } from '@formatjs/intl-locale/should-polyfill';
7-
import { shouldPolyfill as shouldPolyfillNumberFormat } from '@formatjs/intl-numberformat/should-polyfill';
8-
import { shouldPolyfill as shouldPolyfillPluralRules } from '@formatjs/intl-pluralrules/should-polyfill';
9-
105
export default class ApplicationRoute extends Route {
11-
@service notifications;
126
@service progress;
137
@service session;
148

15-
async beforeModel() {
9+
beforeModel() {
1610
// trigger the task, but don't wait for the result here
1711
//
1812
// we don't need a `catch()` block here because network
@@ -21,35 +15,6 @@ export default class ApplicationRoute extends Route {
2115
//
2216
// eslint-disable-next-line ember-concurrency/no-perform-without-catch
2317
this.session.loadUserTask.perform();
24-
25-
// load `Intl` polyfills if necessary
26-
let polyfillImports = [];
27-
if (shouldPolyfillGetCanonicalLocales()) {
28-
console.debug('Loading Intl.getCanonicalLocales() polyfill…');
29-
polyfillImports.push(import('@formatjs/intl-getcanonicallocales/polyfill'));
30-
}
31-
if (shouldPolyfillLocale()) {
32-
console.debug('Loading Intl.Locale polyfill…');
33-
polyfillImports.push(import('@formatjs/intl-locale/polyfill'));
34-
}
35-
if (shouldPolyfillPluralRules()) {
36-
console.debug('Loading Intl.PluralRules polyfill…');
37-
polyfillImports.push(import('@formatjs/intl-pluralrules/polyfill'));
38-
polyfillImports.push(import('@formatjs/intl-pluralrules/locale-data/en'));
39-
}
40-
if (shouldPolyfillNumberFormat()) {
41-
console.debug('Loading Intl.NumberFormat polyfill…');
42-
polyfillImports.push(import('@formatjs/intl-numberformat/polyfill'));
43-
polyfillImports.push(import('@formatjs/intl-numberformat/locale-data/en'));
44-
}
45-
46-
try {
47-
await Promise.all(polyfillImports);
48-
} catch {
49-
let message =
50-
'We tried to load some polyfill code for your browser, but network issues caused the request to fail. If you notice any issues please try to reload the page.';
51-
this.notifications.warning(message);
52-
}
5318
}
5419

5520
@action loading(transition) {

app/services/intl.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Service from '@ember/service';
2+
3+
export default class IntlService extends Service {
4+
// `undefined` means "use the default language of the browser"
5+
locale = undefined;
6+
7+
formatNumber(value) {
8+
return Number(value).toLocaleString(this.locale);
9+
}
10+
}

package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@
3737
"trailingComma": "all"
3838
},
3939
"dependencies": {
40-
"@formatjs/intl-getcanonicallocales": "1.5.3",
41-
"@formatjs/intl-locale": "2.4.12",
42-
"@formatjs/intl-numberformat": "6.1.2",
43-
"@formatjs/intl-pluralrules": "4.0.2",
4440
"@sentry/browser": "5.29.2",
4541
"@sentry/integrations": "5.29.2",
4642
"chart.js": "2.9.4",

tests/acceptance/categories-test.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@ import { module, test } from 'qunit';
44

55
import percySnapshot from '@percy/ember';
66
import a11yAudit from 'ember-a11y-testing/test-support/audit';
7-
import window from 'ember-window-mock';
8-
import { setupWindowMock } from 'ember-window-mock/test-support';
97

108
import axeConfig from '../axe-config';
119
import setupMirage from '../helpers/setup-mirage';
1210

1311
module('Acceptance | categories', function (hooks) {
1412
setupApplicationTest(hooks);
15-
setupWindowMock(hooks);
1613
setupMirage(hooks);
1714

1815
test('listing categories', async function (assert) {
19-
window.navigator = { languages: ['en'] };
16+
this.owner.lookup('service:intl').locale = 'en';
2017

2118
this.server.create('category', { category: 'API bindings' });
2219
this.server.create('category', { category: 'Algorithms' });
@@ -37,7 +34,7 @@ module('Acceptance | categories', function (hooks) {
3734
});
3835

3936
test('listing categories (locale: de)', async function (assert) {
40-
window.navigator = { languages: ['de'] };
37+
this.owner.lookup('service:intl').locale = 'de';
4138

4239
this.server.create('category', { category: 'Everything', crates_cnt: 1234 });
4340

tests/unit/helpers/format-num-test.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
1+
import { render } from '@ember/test-helpers';
2+
import { setupRenderingTest } from 'ember-qunit';
13
import { module, test } from 'qunit';
24

3-
import window from 'ember-window-mock';
4-
import { setupWindowMock } from 'ember-window-mock/test-support';
5-
6-
import { formatNum } from '../../../helpers/format-num';
5+
import { hbs } from 'ember-cli-htmlbars';
76

87
module('Unit | Helper | format-num', function (hooks) {
9-
setupWindowMock(hooks);
8+
setupRenderingTest(hooks);
9+
10+
test('it works', async function (assert) {
11+
this.owner.lookup('service:intl').locale = 'en';
12+
13+
await render(hbs`{{format-num 42}}`);
14+
assert.dom().hasText('42');
15+
16+
await render(hbs`{{format-num 0}}`);
17+
assert.dom().hasText('0');
18+
19+
await render(hbs`{{format-num 0.2}}`);
20+
assert.dom().hasText('0.2');
1021

11-
test('it works', function (assert) {
12-
window.navigator = { language: 'en' };
22+
await render(hbs`{{format-num 1000}}`);
23+
assert.dom().hasText('1,000');
1324

14-
assert.equal(formatNum(42), '42');
15-
assert.equal(formatNum(0), '0');
16-
assert.equal(formatNum(0.2), '0.2');
17-
assert.equal(formatNum(1000), '1,000');
18-
assert.equal(formatNum(1000000), '1,000,000');
25+
await render(hbs`{{format-num 1000000}}`);
26+
assert.dom().hasText('1,000,000');
1927
});
2028
});

yarn.lock

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,47 +1356,6 @@
13561356
minimatch "^3.0.4"
13571357
strip-json-comments "^3.1.1"
13581358

1359-
"@formatjs/ecma402-abstract@1.5.0":
1360-
version "1.5.0"
1361-
resolved "https://registry.yarnpkg.com/@formatjs/ecma402-abstract/-/ecma402-abstract-1.5.0.tgz#759c8f11ff45e96f8fb58741e7fbdb41096d5ddd"
1362-
integrity sha512-wXv36yo+mfWllweN0Fq7sUs7PUiNopn7I0JpLTe3hGu6ZMR4CV7LqK1llhB18pndwpKoafQKb1et2DCJAOW20Q==
1363-
dependencies:
1364-
tslib "^2.0.1"
1365-
1366-
"@formatjs/intl-getcanonicallocales@1.5.3":
1367-
version "1.5.3"
1368-
resolved "https://registry.yarnpkg.com/@formatjs/intl-getcanonicallocales/-/intl-getcanonicallocales-1.5.3.tgz#b5978462340da1502502c3fde1c4abccff8f3b8e"
1369-
integrity sha512-QVBnSPZ32Y80wkXbf36hP9VbyklbOb8edppxFcgO9Lbd47zagllw65Y81QOHEn/j11JcTn2OhW0vea95LHvQmA==
1370-
dependencies:
1371-
cldr-core "38"
1372-
tslib "^2.0.1"
1373-
1374-
"@formatjs/intl-locale@2.4.12":
1375-
version "2.4.12"
1376-
resolved "https://registry.yarnpkg.com/@formatjs/intl-locale/-/intl-locale-2.4.12.tgz#0a320864f84e87876149bdee88357bdc02919a5d"
1377-
integrity sha512-40khHPYtZRdqjQs1fItNj94eMoiGwJJOAKN//9qUhAsHrXvGxseFVBSg+mItV0Orwb3wqItUj3hu6evrouhstw==
1378-
dependencies:
1379-
"@formatjs/ecma402-abstract" "1.5.0"
1380-
"@formatjs/intl-getcanonicallocales" "1.5.3"
1381-
cldr-core "38"
1382-
tslib "^2.0.1"
1383-
1384-
"@formatjs/intl-numberformat@6.1.2":
1385-
version "6.1.2"
1386-
resolved "https://registry.yarnpkg.com/@formatjs/intl-numberformat/-/intl-numberformat-6.1.2.tgz#2fd8d67a3bbb09993114be5c3c721e56a41595d0"
1387-
integrity sha512-td0XggQKu8dlPUlP2f322NW0WY58zWLlOoiUVh4eolcWqOs4maKL8tjmFBPXSQt55JiKy14ZrDw1dljyNHLB0g==
1388-
dependencies:
1389-
"@formatjs/ecma402-abstract" "1.5.0"
1390-
tslib "^2.0.1"
1391-
1392-
"@formatjs/intl-pluralrules@4.0.2":
1393-
version "4.0.2"
1394-
resolved "https://registry.yarnpkg.com/@formatjs/intl-pluralrules/-/intl-pluralrules-4.0.2.tgz#73dafc7f3872b50faec65fce15822f210f334386"
1395-
integrity sha512-Oh4zmA3odkXBCPAGVhZuAnP3IfCPy8DyizA9s4QkYptVICX38RbUvXwzr2a9DVpKNiZ7i2TnV4Qkeo06N/6b7Q==
1396-
dependencies:
1397-
"@formatjs/ecma402-abstract" "1.5.0"
1398-
tslib "^2.0.1"
1399-
14001359
"@glimmer/component@1.0.3":
14011360
version "1.0.3"
14021361
resolved "https://registry.yarnpkg.com/@glimmer/component/-/component-1.0.3.tgz#38c26fc4855fd7ad0e0816d18d80d32c578e5140"
@@ -4907,11 +4866,6 @@ class-utils@^0.3.5:
49074866
isobject "^3.0.0"
49084867
static-extend "^0.1.1"
49094868

4910-
cldr-core@38:
4911-
version "38.0.0"
4912-
resolved "https://registry.yarnpkg.com/cldr-core/-/cldr-core-38.0.0.tgz#f54a5dfd222c79a050f1fe8e87ad9764f16fb840"
4913-
integrity sha512-WkjA4zo5rLT/BWTZAxHJ0lJXwI33gCYucEz1+CpoI8Wu+rr5IrC57wyNXyrNNMdxfDE5RsWi/JCQ9qnG8sHTiw==
4914-
49154869
clean-base-url@^1.0.0:
49164870
version "1.0.0"
49174871
resolved "https://registry.yarnpkg.com/clean-base-url/-/clean-base-url-1.0.0.tgz#c901cf0a20b972435b0eccd52d056824a4351b7b"
@@ -14252,7 +14206,7 @@ tslib@^1, tslib@^1.10.0, tslib@^1.9.0, tslib@^1.9.3:
1425214206
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
1425314207
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==
1425414208

14255-
tslib@^2.0.0, tslib@^2.0.1:
14209+
tslib@^2.0.0:
1425614210
version "2.0.3"
1425714211
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.0.3.tgz#8e0741ac45fc0c226e58a17bfc3e64b9bc6ca61c"
1425814212
integrity sha512-uZtkfKblCEQtZKBF6EBXVZeQNl82yqtDQdv+eck8u7tdPxjLu2/lp5/uPW+um2tpuxINHWy3GhiccY7QgEaVHQ==

0 commit comments

Comments
 (0)