Skip to content

Commit 3b9a399

Browse files
committed
Re-expose Intl.NumberFormat.formatToParts
`Intl.NumberFormat.formatToParts` was first propsed in #30. The spec for it was created in #79 and merged in #100. Due to browser implemantations not being ready at the time, it was moved back to Stage 3 in #101. The internal refactorings 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: '%' } ]
1 parent 03b1008 commit 3b9a399

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

spec/numberformat.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,23 @@ <h1>get Intl.NumberFormat.prototype.format</h1>
618618
</emu-alg>
619619
</emu-clause>
620620

621+
<emu-clause id="sec-intl.numberformat.prototype.formattoparts">
622+
<h1>Intl.NumberFormat.prototype.formatToParts ( [ _value_ ] )</h1>
623+
624+
<p>
625+
When the *Intl.NumberFormat.prototype.formatToParts* is called with an optional argument _value_, the following steps are taken:
626+
</p>
627+
628+
<emu-alg>
629+
1. Let _nf_ be *this* value.
630+
1. If Type(_nf_) is not Object, throw a *TypeError* exception.
631+
1. If _nf_ does not have an [[initializedNumberFormat]] internal slot, throw a *TypeError* exception.
632+
1. If _value_ is not provided, let _value_ be *undefined*.
633+
1. Let _x_ be ? ToNumber(_value_).
634+
1. Return ? FormatNumberToParts(_nf_, _x_).
635+
</emu-alg>
636+
</emu-clause>
637+
621638
<emu-clause id="sec-intl.numberformat.prototype.resolvedoptions">
622639
<h1>Intl.NumberFormat.prototype.resolvedOptions ()</h1>
623640

0 commit comments

Comments
 (0)