Skip to content

Commit

Permalink
Re-expose Intl.NumberFormat.formatToParts
Browse files Browse the repository at this point in the history
`Intl.NumberFormat.formatToParts` was first propsed in #30. The spec for
it was created in #79 and merged in #100 (with follow-ups). Due to
browser implementations not being ready at the time, it was moved back
to Stage 3 in #101.  The internal refactoring were kept in master and
the user-facing method `formatToParts` was removed from the spec in #102.

As of August 1st, 2017, `Intl.NumberFormat.prototype.formatToParts` has
shipped in two engines (behind a flag): SpiderMonkey and V8.  This PR
brings `Intl.NumberFormat.formatToParts` back as Stage 4 proposal.

    > const usd = Intl.NumberFormat('en', { style: 'currency', currency: 'USD' });
    > usd.format(123456.789)
    '$123,456.79'
    > usd.formatToParts(123456.789)
    [ { type: 'currency', value: '$' },
      { type: 'integer', value: '123' },
      { type: 'group', value: ',' },
      { type: 'integer', value: '456' },
      { type: 'decimal', value: '.' },
      { type: 'fraction', value: '79' } ]

    > const pc = Intl.NumberFormat('en', { style: 'percent', minimumFractionDigits: 2 })
    > pc.format(-0.123456)
    '-12.35%'
    > pc.formatToParts(-0.123456)
    [ { type: 'minusSign', value: '-' },
      { type: 'integer', value: '12' },
      { type: 'decimal', value: '.' },
      { type: 'fraction', value: '35' },
      { type: 'literal', value: '%' } ]
  • Loading branch information
stasm committed Aug 8, 2017
1 parent 03b1008 commit 58eaae9
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions spec/numberformat.html
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,23 @@ <h1>get Intl.NumberFormat.prototype.format</h1>
</emu-alg>
</emu-clause>

<emu-clause id="sec-intl.numberformat.prototype.formattoparts">
<h1>Intl.NumberFormat.prototype.formatToParts ( [ _value_ ] )</h1>

<p>
When the *Intl.NumberFormat.prototype.formatToParts* is called with an optional argument _value_, the following steps are taken:
</p>

<emu-alg>
1. Let _nf_ be *this* value.
1. If Type(_nf_) is not Object, throw a *TypeError* exception.
1. If _nf_ does not have an [[initializedNumberFormat]] internal slot, throw a *TypeError* exception.
1. If _value_ is not provided, let _value_ be *undefined*.
1. Let _x_ be ? ToNumber(_value_).
1. Return ? FormatNumberToParts(_nf_, _x_).
</emu-alg>
</emu-clause>

<emu-clause id="sec-intl.numberformat.prototype.resolvedoptions">
<h1>Intl.NumberFormat.prototype.resolvedOptions ()</h1>

Expand Down

0 comments on commit 58eaae9

Please sign in to comment.