Skip to content

Commit

Permalink
fix(@formatjs/intl-numberformat): add polyfill check for ES2023, fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
longlho committed Jan 26, 2024
1 parent d22ebf9 commit 9ea1e94
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/intl-numberformat/should-polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 38,29 @@ function supportsES2020() {
return true
}

/**
* Check if Intl.NumberFormat is ES2020 compatible.
* Caveat: we are not checking `toLocaleString`.
*
* @public
* @param unit unit to check
*/
function supportsES2023() {
try {
const s = new Intl.NumberFormat('en', {
notation: 'compact',
minimumSignificantDigits: 3,
maximumSignificantDigits: 3,
minimumFractionDigits: 2,
maximumFractionDigits: 2,
roundingPriority: 'morePrecision',
}).format(1e8)
return s === '100.00M'
} catch (e) {
return false
}
}

function supportedLocalesOf(locale?: string | string[]) {
if (!locale) {
return true
Expand All @@ -51,6 74,7 @@ export function shouldPolyfill(locale = 'en') {
typeof Intl === 'undefined' ||
!('NumberFormat' in Intl) ||
!supportsES2020() ||
!supportsES2023() ||
onlySupportsEn() ||
!supportedLocalesOf(locale)
) {
Expand Down

0 comments on commit 9ea1e94

Please sign in to comment.