Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Intl.NumberFormat ES2020 polyfill #642

Merged
merged 11 commits into from
Jun 10, 2020

Conversation

longlho
Copy link
Contributor

@longlho longlho commented May 21, 2020

fixes #553

@longlho longlho requested a review from a team as a code owner May 21, 2020 03:13
@JakeChampion
Copy link
Owner

Thanks for this pull request @longlho :-)
I ran the tests on our CI and they look to fail on chrome 29 up to chrome 76, specifically it is the feature detect which is not working correctly when the polyfill is applied.

    -> Intl.NumberFormat passes the feature detect
       http://bs-local.com:9876/test?includePolyfills=yes&always=no&feature=Intl.NumberFormat
       false == true

Copy link
Owner

@JakeChampion JakeChampion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this pull request @longlho :-)
I ran the tests on our CI and they look to fail on chrome 29 up to chrome 76, specifically it is the feature detect which is not working correctly when the polyfill is applied.

    -> Intl.NumberFormat passes the feature detect
       http://bs-local.com:9876/test?includePolyfills=yes&always=no&feature=Intl.NumberFormat
       false == true

@JakeChampion JakeChampion added this to incoming in Origami ✨ May 21, 2020
@longlho
Copy link
Contributor Author

longlho commented May 21, 2020

any suggestion on how to repro (other than asking you to re-run in your pipeline)?

@longlho
Copy link
Contributor Author

longlho commented May 22, 2020

depends on formatjs/formatjs#1684

@JakeChampion
Copy link
Owner

@longlho now that formatjs/formatjs#1684 is merged, do you know how we bring the update into this pull-request?

@longlho
Copy link
Contributor Author

longlho commented Jun 9, 2020

yup ill tackle that today

@longlho
Copy link
Contributor Author

longlho commented Jun 10, 2020

fixed :)

@JakeChampion
Copy link
Owner

@longlho it looks like the feature detect in detect.js does not return true once the polyfill has been applied in Chrome.

Here is the test failure we saw for this in CI:

Failures:
- chrome 29.0 up to and including 76.0:
  -> Intl.NumberFormat passes the feature detect
      http://bs-local.com:9876/test?includePolyfills=yes&always=no&feature=Intl.NumberFormat
      false == true

@longlho
Copy link
Contributor Author

longlho commented Jun 10, 2020

Hmm how is the polyfill loaded btw? It needs to load the en locale for detect to pass. I don't wanna add en locale data to polyfill.js since it'll load that for every locale.

@JakeChampion
Copy link
Owner

Hmm how is the polyfill loaded btw? It needs to load the en locale for detect to pass. I don't wanna add en locale data to polyfill.js since it'll load that for every locale.

That is a good point, we should not load in the locale for en if it is not requested.

Perhaps we can have a less thorough feature detect like the other Intl polyfills we have? So instead of this:

'Intl' in self && 'NumberFormat' in self.Intl && (function () {
  try {
    new Intl.NumberFormat(undefined, {
      style: 'unit',
      unit: 'byte',
    });
  } catch (e) {
    return false;
  }
  return true;
})()

We have this:

'Intl' in self && 'NumberFormat' in self.Intl

Would that detect be enough or do we need to instantiate Intl.NumberFormat with a style?

@JakeChampion
Copy link
Owner

@longlho How about this feature detect instead?

'Intl' in self && 'NumberFormat' in self.Intl && (function () {
  try {
    var options = {
      compactDisplay: 'long'
    };
    var notationSupport = false;
    Object.defineProperty(options, 'notation', {
      get: function() {
        notationSupport = true;
        return 'compact';
      }
    });

    var compactDisplaySupport = false;
    Object.defineProperty(options, 'compactDisplay', {
      get: function() {
        compactDisplay = true;
        return 'long';
      }
    });

    new Intl.NumberFormat(undefined, options).format(1_807_687.733);

    return notationSupport && compactDisplaySupport;
    } catch (e) {
      return false;
    }
  return true;
})()

@longlho
Copy link
Contributor Author

longlho commented Jun 10, 2020

Let me double check our implementation but that might work. The issue might be that due to https://tc39.es/ecma402/#sec-initializenumberformat step 11 we try to resolve the locale 1st, and since there's no locale it might crap out.

@longlho
Copy link
Contributor Author

longlho commented Jun 10, 2020

Yeah it's technically not spec-compliant to not check for a locale, since it expects at least 1 locale (DefaultLocale) to be loaded...

@JakeChampion
Copy link
Owner

Let me double check our implementation but that might work. The issue might be that due to tc39.es/ecma402/#sec-initializenumberformat step 11 we try to resolve the locale 1st, and since there's no locale it might crap out.

Ah yes, I think I understand, for the detect to pass we must first insert some locale data into it. Let me see if I need to rework our test-runner to make that possible.

@JakeChampion
Copy link
Owner

Our test-runner creates a describe block which contains an it block for the feature detect tests and then it embeds the contents of the tests.js into the describe block as well.
https://github.com/Financial-Times/polyfill-library/blob/2e2fe842881e9b7b1359005c35db09c64666cc36/test/polyfills/server.js#L153-L162

This means we can write a beforeEach or before block which adds the locale data before the feature detect test is called. I'll update this PR with the change required.

@JakeChampion JakeChampion merged commit 2de0546 into JakeChampion:master Jun 10, 2020
Origami ✨ automation moved this from incoming to complete Jun 10, 2020
@JakeChampion
Copy link
Owner

@longlho
Copy link
Contributor Author

longlho commented Jun 10, 2020

awesome thanks a lot @JakeChampion !

@longlho longlho deleted the numberformat branch June 10, 2020 17:29
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 11, 2021
@robertboulton robertboulton removed this from Done in Origami ✨ Jul 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Intl.NumberFormat with compact notation, Firefox
2 participants