Skip to content

Commit

Permalink
Merge pull request #38565 from callstack-internal/audit/app-startup/r…
Browse files Browse the repository at this point in the history
…emove-intl-polyfills
  • Loading branch information
luacmartins authored Mar 26, 2024
2 parents 03567be + 05cc46b commit 886be54
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 14 deletions.
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
"@expensify/react-native-live-markdown": "0.1.5",
"@expo/metro-runtime": "~3.1.1",
"@formatjs/intl-datetimeformat": "^6.10.0",
"@formatjs/intl-getcanonicallocales": "^2.2.0",
"@formatjs/intl-listformat": "^7.2.2",
"@formatjs/intl-locale": "^3.3.0",
"@formatjs/intl-numberformat": "^8.5.0",
Expand Down Expand Up @@ -155,8 +154,8 @@
"react-native-plaid-link-sdk": "10.8.0",
"react-native-qrcode-svg": "^6.2.0",
"react-native-quick-sqlite": "^8.0.0-beta.2",
"react-native-release-profiler": "^0.1.6",
"react-native-reanimated": "^3.7.2",
"react-native-release-profiler": "^0.1.6",
"react-native-render-html": "6.3.1",
"react-native-safe-area-context": "4.8.2",
"react-native-screens": "3.29.0",
Expand Down
2 changes: 1 addition & 1 deletion src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2974,7 +2974,7 @@ const CONST = {
CURRENCY: 'XAF',
FORMAT: 'symbol',
SAMPLE_INPUT: '123456.789',
EXPECTED_OUTPUT: 'FCFA 123,457',
EXPECTED_OUTPUT: 'FCFA 123,457',
},

PATHS_TO_TREAT_AS_EXTERNAL: ['NewExpensify.dmg', 'docs/index.html'],
Expand Down
17 changes: 17 additions & 0 deletions src/libs/IntlPolyfill/index.android.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import polyfillListFormat from './polyfillListFormat';
import type IntlPolyfill from './types';

/**
* Polyfill the Intl API, always performed for native devices.
*/
const intlPolyfill: IntlPolyfill = () => {
// Native devices require extra polyfills which are
// not yet implemented in hermes.
// see support: https://hermesengine.dev/docs/intl/

require('@formatjs/intl-locale/polyfill');

polyfillListFormat();
};

export default intlPolyfill;
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@ import type IntlPolyfill from './types';
* Polyfill the Intl API, always performed for native devices.
*/
const intlPolyfill: IntlPolyfill = () => {
// Native devices require extra polyfills
require('@formatjs/intl-getcanonicallocales/polyfill');
// Native devices require extra polyfills which are
// not yet implemented in hermes.
// see support: https://hermesengine.dev/docs/intl/

require('@formatjs/intl-locale/polyfill');

// Required to polyfill NumberFormat on iOS
// see: https://github.com/facebook/hermes/issues/1172#issuecomment-1776156538
require('@formatjs/intl-pluralrules/polyfill');
polyfillNumberFormat();

// Required to polyfill DateTimeFormat on iOS
// see: https://github.com/facebook/hermes/issues/1172#issuecomment-1776156538
polyfillDateTimeFormat();

polyfillListFormat();
};

Expand Down
24 changes: 16 additions & 8 deletions src/libs/IntlPolyfill/polyfillNumberFormat.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import CONST from '@src/CONST';

const numberFormat = new Intl.NumberFormat(CONST.LOCALES.DEFAULT, {
style: CONST.POLYFILL_TEST.STYLE,
currency: CONST.POLYFILL_TEST.CURRENCY,
currencyDisplay: CONST.POLYFILL_TEST.FORMAT,
});

/**
* Check if the locale data is as expected on the device.
* Ensures that the currency data is consistent across devices.
*/
function hasOldCurrencyData(): boolean {
return (
new Intl.NumberFormat(CONST.LOCALES.DEFAULT, {
style: CONST.POLYFILL_TEST.STYLE,
currency: CONST.POLYFILL_TEST.CURRENCY,
currencyDisplay: CONST.POLYFILL_TEST.FORMAT,
}).format(Number(CONST.POLYFILL_TEST.SAMPLE_INPUT)) !== CONST.POLYFILL_TEST.EXPECTED_OUTPUT
);
return numberFormat.format(Number(CONST.POLYFILL_TEST.SAMPLE_INPUT)) !== CONST.POLYFILL_TEST.EXPECTED_OUTPUT;
}

/**
* Checks if the formatToParts function is available on the
* Intl.NumberFormat object.
*/
function hasFormatToParts(): boolean {
return typeof numberFormat.formatToParts === 'function';
}

export default function () {
if (Intl && 'NumberFormat' in Intl && !hasOldCurrencyData()) {
if (Intl && 'NumberFormat' in Intl && !hasOldCurrencyData() && hasFormatToParts()) {
return;
}

Expand Down

0 comments on commit 886be54

Please sign in to comment.