Intl.js icon indicating copy to clipboard operation
Intl.js copied to clipboard

formatToParts outdated from spec?

Open erin-doyle opened this issue 4 years ago • 1 comments

I'm putting some pieces together so please forgive me if I'm just misreading things. I have the following polyfill code:

const fillNumberFormatFormatToParts = () => new Promise((resolve) => {
        if (!Intl.NumberFormat.prototype.formatToParts) {
            require.ensure(['intl'], (require) => {
                const IntlPolyfill = require('intl');
                Intl.NumberFormat.prototype.formatToParts = IntlPolyfill.NumberFormat.prototype.formatToParts;
                return resolve();
            }, 'Intl');
        } else {
            return resolve();
        }
    });

In iOS Safari 11 and 12 (where this is clearly being polyfilled) I get the following error:

"TypeError: this value for formatToParts() is not an initialized Intl.NumberFormat object."

It looks as if here: https://github.com/tc39/ecma402/issues/122 the prototype for NumberFormat was changed from being an instance of NumberFormat to being a plain object. And that is documented here: https://tc39.es/proposal-unified-intl-numberformat/section11/numberformat_proposed_out.html#sec-properties-of-intl-numberformat-prototype-object.

But based on the implementation it seems to still be requiring NumberFormat to be an instance:

function formatToParts(value = undefined) {
  let internal = this !== null && typeof this === 'object' && getInternalProperties(this);
  if (!internal || !internal['[[initializedNumberFormat]]'])
      throw new TypeError('`this` value for formatToParts() is not an initialized Intl.NumberFormat object.');

  let x = Number(value);
  return FormatNumberToParts(this, x);
}

And this is the spec being referenced for the Intl.NumberFormat.prototype from your documentation: http://www.ecma-international.org/ecma-402/1.0/#sec-11.3.

Or am I just doing something wrong here?

erin-doyle avatar Feb 05 '20 19:02 erin-doyle

I haven't dug into the issue yet, but I can attest to the fact that all users visiting my sites on Mobile Safari 12.1 and 12.1.2 are failing to have formatToParts correctly polyfilled.

cipater avatar Mar 06 '20 01:03 cipater