Skip to content

Commit

Permalink
feat(@formatjs/intl-getcanonicallocales): expose shouldPolyfill to de…
Browse files Browse the repository at this point in the history
…tect if platform needs our polyfill
  • Loading branch information
longlho committed Aug 18, 2020
1 parent 8820290 commit ba0aac6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
8 changes: 4 additions & 4 deletions packages/intl-getcanonicallocales/BUILD
Expand Up @@ -27,10 +27,10 @@ alias(
actual = "%s.pack" % PACKAGE_NAME,
)

SRCS = glob(["src/**/*.ts"]) + [
"index.ts",
"polyfill.ts",
]
SRCS = glob([
"src/**/*.ts",
"*.ts",
])

TESTS = glob([
"tests/*.test.ts",
Expand Down
12 changes: 3 additions & 9 deletions packages/intl-getcanonicallocales/polyfill.ts
@@ -1,4 +1,5 @@
import {getCanonicalLocales} from './';
import {getCanonicalLocales} from '.';
import {shouldPolyfill} from './should-polyfill';
if (typeof Intl === 'undefined') {
if (typeof window !== 'undefined') {
Object.defineProperty(window, 'Intl', {
Expand All @@ -10,14 +11,7 @@ if (typeof Intl === 'undefined') {
});
}
}
if (
!('getCanonicalLocales' in Intl) ||
// Native Intl.getCanonicalLocales is just buggy
// https://bugs.chromium.org/p/v8/issues/detail?id=10682
((Intl as any).getCanonicalLocales as typeof getCanonicalLocales)(
'und-x-private'
)[0] === 'x-private'
) {
if (shouldPolyfill()) {
Object.defineProperty(Intl, 'getCanonicalLocales', {
value: getCanonicalLocales,
writable: true,
Expand Down
10 changes: 10 additions & 0 deletions packages/intl-getcanonicallocales/should-polyfill.ts
@@ -0,0 +1,10 @@
export function shouldPolyfill() {
return (
typeof Intl === 'undefined' ||
!('getCanonicalLocales' in Intl) ||
// Native Intl.getCanonicalLocales is just buggy
// https://bugs.chromium.org/p/v8/issues/detail?id=10682
((Intl as any).getCanonicalLocales as any)('und-x-private')[0] ===
'x-private'
);
}

0 comments on commit ba0aac6

Please sign in to comment.