Skip to content

Commit

Permalink
[Normative] Consolidate option reading into SetNumberFormatDigitOptio…
Browse files Browse the repository at this point in the history
…ns so it can be leveraged in PluralRules (#130)
  • Loading branch information
sffc committed Jan 30, 2023
1 parent b755dd4 commit 29acfc6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 26 deletions.
26 changes: 14 additions & 12 deletions numberformat/diff.emu
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,9 @@
1. Let _mxfdDefault_ be 0.
1. Else,
1. Let _mxfdDefault_ be 3.
1. <ins class="block">Let _roundingIncrement_ be ? GetNumberOption(_options_, *"roundingIncrement"*, 1, 5000, 1).</ins>
1. <ins class="block">If _roundingIncrement_ is not in « 1, 2, 5, 10, 20, 25, 50, 100, 200, 250, 500, 1000, 2000, 2500, 5000 », throw a *RangeError* exception.</ins>
1. <ins class="block">If _roundingIncrement_ is not 1, set _mxfdDefault_ to _mnfdDefault_.</ins>
1. Let _notation_ be ? GetOption(_options_, *"notation"*, ~string~, &laquo; *"standard"*, *"scientific"*, *"engineering"*, *"compact"* &raquo;, *"standard"*).
1. Set _numberFormat_.[[Notation]] to _notation_.
1. Perform ? SetNumberFormatDigitOptions(_numberFormat_, _options_, _mnfdDefault_, _mxfdDefault_, _notation_).
1. <ins class="block">If _roundingIncrement_ is not 1, then</ins>
1. <ins class="block">If _numberFormat_.[[RoundingType]] is not ~fractionDigits~, throw a *TypeError* exception.</ins>
1. <ins class="block">If _numberFormat_.[[MaximumFractionDigits]] is not equal to _numberFormat_.[[MinimumFractionDigits]], throw a *RangeError* exception.</ins>
1. <ins class="block">Set _numberFormat_.[[RoundingIncrement]] to _roundingIncrement_.</ins>
1. <ins class="block">Let _trailingZeroDisplay_ be ? GetOption(_options_, *"trailingZeroDisplay"*, ~string~, &laquo; *"auto"*, *"stripIfInteger"* &raquo;, *"auto"*).</ins>
1. <ins class="block">Set _numberFormat_.[[TrailingZeroDisplay]] to _trailingZeroDisplay_.</ins>
1. Let _compactDisplay_ be ? GetOption(_options_, *"compactDisplay"*, ~string~, &laquo; *"short"*, *"long"* &raquo;, *"short"*).
1. <ins class="block">Let _defaultUseGrouping_ be *"auto"*.</ins>
1. If _notation_ is *"compact"*, then
Expand All @@ -98,8 +89,6 @@
1. Set _numberFormat_.[[UseGrouping]] to _useGrouping_.
1. Let _signDisplay_ be ? GetOption(_options_, *"signDisplay"*, ~string~, &laquo; *"auto"*, *"never"*, *"always"*, *"exceptZero"*<ins>, *"negative"*</ins> &raquo;, *"auto"*).
1. Set _numberFormat_.[[SignDisplay]] to _signDisplay_.
1. <ins class="block">Let _roundingMode_ be ? GetOption(_options_, *"roundingMode"*, ~string~, &laquo; *"ceil"*, *"floor"*, *"expand"*, *"trunc"*, *"halfCeil"*, *"halfFloor"*, *"halfExpand"*, *"halfTrunc"*, *"halfEven"* &raquo;, *"halfExpand"*).</ins>
1. <ins class="block">Set _numberFormat_.[[RoundingMode]] to _roundingMode_.</ins>
1. Return _numberFormat_.
</emu-alg>
</emu-clause>
Expand All @@ -110,6 +99,7 @@
<p>
The abstract operation SetNumberFormatDigitOptions applies digit
options used for number formatting onto the intl object.
<ins>It is used by both Intl.NumberFormat and Intl.PluralRules.</ins>
</p>
<emu-alg>
1. Let _mnid_ be ? GetNumberOption(_options_, *"minimumIntegerDigits,"*, 1, 21, 1).
Expand All @@ -119,6 +109,15 @@
1. Let _mxsd_ be ? Get(_options_, *"maximumSignificantDigits"*).
1. Set _intlObj_.[[MinimumIntegerDigits]] to _mnid_.
1. <ins class="block">Let _roundingPriority_ be ? GetOption(_options_, *"roundingPriority"*, ~string~, &laquo; *"auto"*, *"morePrecision"*, *"lessPrecision"* &raquo;, *"auto"*).</ins>
1. <ins class="block">Let _roundingIncrement_ be ? GetNumberOption(_options_, *"roundingIncrement"*, 1, 5000, 1).</ins>
1. <ins class="block">If _roundingIncrement_ is not in « 1, 2, 5, 10, 20, 25, 50, 100, 200, 250, 500, 1000, 2000, 2500, 5000 », throw a *RangeError* exception.</ins>
1. <ins class="block">Let _roundingMode_ be ? GetOption(_options_, *"roundingMode"*, ~string~, &laquo; *"ceil"*, *"floor"*, *"expand"*, *"trunc"*, *"halfCeil"*, *"halfFloor"*, *"halfExpand"*, *"halfTrunc"*, *"halfEven"* &raquo;, *"halfExpand"*).</ins>
1. <ins class="block">Let _trailingZeroDisplay_ be ? GetOption(_options_, *"trailingZeroDisplay"*, ~string~, &laquo; *"auto"*, *"stripIfInteger"* &raquo;, *"auto"*).</ins>
1. <ins class="block">NOTE: All fields required by SetNumberFormatDigitOptions have now been read from _options_. The remainder of this AO interprets the options and may throw exceptions.</ins>
1. <ins class="block">If _roundingIncrement_ is not 1, set _mxfdDefault_ to _mnfdDefault_.</ins>
1. <ins class="block">Set _intlObj_.[[RoundingIncrement]] to _roundingIncrement_.</ins>
1. <ins class="block">Set _intlObj_.[[RoundingMode]] to _roundingMode_.</ins>
1. <ins class="block">Set _intlObj_.[[TrailingZeroDisplay]] to _trailingZeroDisplay_.</ins>
1. If _mnsd_ is not *undefined* or _mxsd_ is not *undefined*, then
1. Let _hasSd_ be *true*.
1. Else,
Expand Down Expand Up @@ -181,6 +180,9 @@
1. <ins class="block">Set _intlObj_.[[MaximumFractionDigits]] to 0.</ins>
1. <ins class="block">Set _intlObj_.[[MinimumSignificantDigits]] to 1.</ins>
1. <ins class="block">Set _intlObj_.[[MaximumSignificantDigits]] to 2.</ins>
1. <ins class="block">If _roundingIncrement_ is not 1, then</ins>
1. <ins class="block">If _intlObj_.[[RoundingType]] is not ~fractionDigits~, throw a *TypeError* exception.</ins>
1. <ins class="block">If _intlObj_.[[MaximumFractionDigits]] is not equal to _intlObj_.[[MinimumFractionDigits]], throw a *RangeError* exception.</ins>
</emu-alg>
</emu-clause>

Expand Down Expand Up @@ -554,7 +556,7 @@
[[SignDisplay]] is one of the String values *"auto"*, *"always"*, *"never"*, <del>or</del> *"exceptZero"*, <ins>or *"negative"*</ins>, specifying <del>whether to show the sign on negative numbers only, positive and negative numbers including zero, neither positive nor negative numbers, or positive and negative numbers but not zero</del> <ins>when to include a sign (with non-*"auto"* options respectively corresponding with inclusion always, never, only for nonzero numbers, or only for nonzero negative numbers)</ins>.
In scientific notation, this slot affects the sign display of the mantissa but not the exponent.
</li>
<li><ins class="block">[[RoundingMode]] is one of the String values in the *Identifier* column of <emu-xref href="#table-intl-rounding-modes"></emu-xref>, specifying the rounding strategy for the number.</ins></li>
<li><ins class="block">[[RoundingMode]] is one of the String values in the Identifier column of <emu-xref href="#table-intl-rounding-modes"></emu-xref>, specifying which rounding mode to use.</ins></li>
<li><ins class="block">[[RoundingIncrement]] is an integer-valued Number that evenly divides 10, 100, 1000, or 10000 into tenths, fifths, quarters, or halves. It indicates the increment at which rounding should take place relative to the calculated rounding magnitude. For example, if [[MaximumFractionDigits]] is 2 and [[RoundingIncrement]] is 5, then the number is rounded to the nearest 0.05 ("nickel rounding").</ins></li>
<li><ins class="block">[[TrailingZeroDisplay]] is one of the String values *"auto"* or *"stripIfInteger"*, indicating whether to strip trailing zeros if the formatted number is an integer (i.e., has no nonzero fraction digit).</ins></li>
</ul>
Expand Down
26 changes: 14 additions & 12 deletions numberformat/proposed.emu
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,9 @@
1. Let _mxfdDefault_ be 0.
1. Else,
1. Let _mxfdDefault_ be 3.
1. Let _roundingIncrement_ be ? GetNumberOption(_options_, *"roundingIncrement"*, 1, 5000, 1).
1. If _roundingIncrement_ is not in « 1, 2, 5, 10, 20, 25, 50, 100, 200, 250, 500, 1000, 2000, 2500, 5000 », throw a *RangeError* exception.
1. If _roundingIncrement_ is not 1, set _mxfdDefault_ to _mnfdDefault_.
1. Let _notation_ be ? GetOption(_options_, *"notation"*, ~string~, &laquo; *"standard"*, *"scientific"*, *"engineering"*, *"compact"* &raquo;, *"standard"*).
1. Set _numberFormat_.[[Notation]] to _notation_.
1. Perform ? SetNumberFormatDigitOptions(_numberFormat_, _options_, _mnfdDefault_, _mxfdDefault_, _notation_).
1. If _roundingIncrement_ is not 1, then
1. If _numberFormat_.[[RoundingType]] is not ~fractionDigits~, throw a *TypeError* exception.
1. If _numberFormat_.[[MaximumFractionDigits]] is not equal to _numberFormat_.[[MinimumFractionDigits]], throw a *RangeError* exception.
1. Set _numberFormat_.[[RoundingIncrement]] to _roundingIncrement_.
1. Let _trailingZeroDisplay_ be ? GetOption(_options_, *"trailingZeroDisplay"*, ~string~, &laquo; *"auto"*, *"stripIfInteger"* &raquo;, *"auto"*).
1. Set _numberFormat_.[[TrailingZeroDisplay]] to _trailingZeroDisplay_.
1. Let _compactDisplay_ be ? GetOption(_options_, *"compactDisplay"*, ~string~, &laquo; *"short"*, *"long"* &raquo;, *"short"*).
1. Let _defaultUseGrouping_ be *"auto"*.
1. If _notation_ is *"compact"*, then
Expand All @@ -98,8 +89,6 @@
1. Set _numberFormat_.[[UseGrouping]] to _useGrouping_.
1. Let _signDisplay_ be ? GetOption(_options_, *"signDisplay"*, ~string~, &laquo; *"auto"*, *"never"*, *"always"*, *"exceptZero"*, *"negative"* &raquo;, *"auto"*).
1. Set _numberFormat_.[[SignDisplay]] to _signDisplay_.
1. Let _roundingMode_ be ? GetOption(_options_, *"roundingMode"*, ~string~, &laquo; *"ceil"*, *"floor"*, *"expand"*, *"trunc"*, *"halfCeil"*, *"halfFloor"*, *"halfExpand"*, *"halfTrunc"*, *"halfEven"* &raquo;, *"halfExpand"*).
1. Set _numberFormat_.[[RoundingMode]] to _roundingMode_.
1. Return _numberFormat_.
</emu-alg>
</emu-clause>
Expand All @@ -110,6 +99,7 @@
<p>
The abstract operation SetNumberFormatDigitOptions applies digit
options used for number formatting onto the intl object.
It is used by both Intl.NumberFormat and Intl.PluralRules.
</p>
<emu-alg>
1. Let _mnid_ be ? GetNumberOption(_options_, *"minimumIntegerDigits,"*, 1, 21, 1).
Expand All @@ -119,6 +109,15 @@
1. Let _mxsd_ be ? Get(_options_, *"maximumSignificantDigits"*).
1. Set _intlObj_.[[MinimumIntegerDigits]] to _mnid_.
1. Let _roundingPriority_ be ? GetOption(_options_, *"roundingPriority"*, ~string~, &laquo; *"auto"*, *"morePrecision"*, *"lessPrecision"* &raquo;, *"auto"*).
1. Let _roundingIncrement_ be ? GetNumberOption(_options_, *"roundingIncrement"*, 1, 5000, 1).
1. If _roundingIncrement_ is not in « 1, 2, 5, 10, 20, 25, 50, 100, 200, 250, 500, 1000, 2000, 2500, 5000 », throw a *RangeError* exception.
1. Let _roundingMode_ be ? GetOption(_options_, *"roundingMode"*, ~string~, &laquo; *"ceil"*, *"floor"*, *"expand"*, *"trunc"*, *"halfCeil"*, *"halfFloor"*, *"halfExpand"*, *"halfTrunc"*, *"halfEven"* &raquo;, *"halfExpand"*).
1. Let _trailingZeroDisplay_ be ? GetOption(_options_, *"trailingZeroDisplay"*, ~string~, &laquo; *"auto"*, *"stripIfInteger"* &raquo;, *"auto"*).
1. NOTE: All fields required by SetNumberFormatDigitOptions have now been read from _options_. The remainder of this AO interprets the options and may throw exceptions.
1. If _roundingIncrement_ is not 1, set _mxfdDefault_ to _mnfdDefault_.
1. Set _intlObj_.[[RoundingIncrement]] to _roundingIncrement_.
1. Set _intlObj_.[[RoundingMode]] to _roundingMode_.
1. Set _intlObj_.[[TrailingZeroDisplay]] to _trailingZeroDisplay_.
1. If _mnsd_ is not *undefined* or _mxsd_ is not *undefined*, then
1. Let _hasSd_ be *true*.
1. Else,
Expand Down Expand Up @@ -169,6 +168,9 @@
1. Set _intlObj_.[[MaximumFractionDigits]] to 0.
1. Set _intlObj_.[[MinimumSignificantDigits]] to 1.
1. Set _intlObj_.[[MaximumSignificantDigits]] to 2.
1. If _roundingIncrement_ is not 1, then
1. If _intlObj_.[[RoundingType]] is not ~fractionDigits~, throw a *TypeError* exception.
1. If _intlObj_.[[MaximumFractionDigits]] is not equal to _intlObj_.[[MinimumFractionDigits]], throw a *RangeError* exception.
</emu-alg>
</emu-clause>

Expand Down Expand Up @@ -540,7 +542,7 @@
[[SignDisplay]] is one of the String values *"auto"*, *"always"*, *"never"*, *"exceptZero"*, or *"negative"*, specifying when to include a sign (with non-*"auto"* options respectively corresponding with inclusion always, never, only for nonzero numbers, or only for nonzero negative numbers).
In scientific notation, this slot affects the sign display of the mantissa but not the exponent.
</li>
<li>[[RoundingMode]] is one of the String values in the *Identifier* column of <emu-xref href="#table-intl-rounding-modes"></emu-xref>, specifying the rounding strategy for the number.</li>
<li>[[RoundingMode]] is one of the String values in the Identifier column of <emu-xref href="#table-intl-rounding-modes"></emu-xref>, specifying which rounding mode to use.</li>
<li>[[RoundingIncrement]] is an integer-valued Number that evenly divides 10, 100, 1000, or 10000 into tenths, fifths, quarters, or halves. It indicates the increment at which rounding should take place relative to the calculated rounding magnitude. For example, if [[MaximumFractionDigits]] is 2 and [[RoundingIncrement]] is 5, then the number is rounded to the nearest 0.05 ("nickel rounding").</li>
<li>[[TrailingZeroDisplay]] is one of the String values *"auto"* or *"stripIfInteger"*, indicating whether to strip trailing zeros if the formatted number is an integer (i.e., has no nonzero fraction digit).</li>
</ul>
Expand Down
17 changes: 16 additions & 1 deletion pluralrules/diff.emu
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<emu-alg>
1. If NewTarget is *undefined*, throw a *TypeError* exception.
1. Let _pluralRules_ be ? OrdinaryCreateFromConstructor(NewTarget, *"%PluralRules.prototype%"*, &laquo; [[InitializedPluralRules]], [[Locale]], [[Type]], [[MinimumIntegerDigits]], [[MinimumFractionDigits]], [[MaximumFractionDigits]], [[MinimumSignificantDigits]], [[MaximumSignificantDigits]], [[RoundingType]] &raquo;).
1. Let _pluralRules_ be ? OrdinaryCreateFromConstructor(NewTarget, *"%PluralRules.prototype%"*, &laquo; [[InitializedPluralRules]], [[Locale]], [[Type]], [[MinimumIntegerDigits]], [[MinimumFractionDigits]], [[MaximumFractionDigits]], [[MinimumSignificantDigits]], [[MaximumSignificantDigits]], [[RoundingType]]<ins>, [[RoundingMode]], [[RoundingIncrement]], [[TrailingZeroDisplay]]</ins> &raquo;).
1. Return ? InitializePluralRules(_pluralRules_, _locales_, _options_).
</emu-alg>
</emu-clause>
Expand Down Expand Up @@ -227,6 +227,18 @@
<td>[[MaximumSignificantDigits]]</td>
<td>*"maximumSignificantDigits"*</td>
</tr>
<tr>
<td><ins>[[RoundingMode]]</ins></td>
<td><ins>*"roundingMode"*</ins></td>
</tr>
<tr>
<td><ins>[[RoundingIncrement]]</ins></td>
<td><ins>*"roundingIncrement"*</ins></td>
</tr>
<tr>
<td><ins>[[TrailingZeroDisplay]]</ins></td>
<td><ins>*"trailingZeroDisplay"*</ins></td>
</tr>
</table>
</emu-table>
</emu-clause>
Expand Down Expand Up @@ -254,6 +266,9 @@
<li>[[MinimumFractionDigits]] and [[MaximumFractionDigits]] are non-negative integer Number values indicating the minimum and maximum fraction digits to be used. Numbers will be rounded or padded with trailing zeroes if necessary.</li>
<li>[[MinimumSignificantDigits]] and [[MaximumSignificantDigits]] are positive integer Number values indicating the minimum and maximum fraction digits to be used. Either none or both of these properties are present; if they are, they override minimum and maximum integer and fraction digits.</li>
<li>[[RoundingType]] is one of the values ~fractionDigits~, <del>or</del> ~significantDigits~, <ins>~morePrecision~, or ~lessPrecision~</ins>, indicating which rounding strategy to use, as discussed in <emu-xref href="#sec-properties-of-intl-numberformat-instances"></emu-xref>.</li>
<li><ins class="block">[[RoundingMode]] is one of the String values in the Identifier column of <emu-xref href="#table-intl-rounding-modes"></emu-xref>, specifying which rounding mode to use.</ins></li>
<li><ins class="block">[[RoundingIncrement]] is an integer-valued Number that evenly divides 10, 100, 1000, or 10000 into tenths, fifths, quarters, or halves. It indicates the increment at which rounding should take place relative to the calculated rounding magnitude. For example, if [[MaximumFractionDigits]] is 2 and [[RoundingIncrement]] is 5, then the number is rounded to the nearest 0.05 ("nickel rounding").</ins></li>
<li><ins class="block">[[TrailingZeroDisplay]] is one of the String values *"auto"* or *"stripIfInteger"*, indicating whether to strip trailing zeros if the formatted number is an integer (i.e., has no nonzero fraction digit).</ins></li>
</ul>
</emu-clause>

Expand Down
17 changes: 16 additions & 1 deletion pluralrules/proposed.emu
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<emu-alg>
1. If NewTarget is *undefined*, throw a *TypeError* exception.
1. Let _pluralRules_ be ? OrdinaryCreateFromConstructor(NewTarget, *"%PluralRules.prototype%"*, &laquo; [[InitializedPluralRules]], [[Locale]], [[Type]], [[MinimumIntegerDigits]], [[MinimumFractionDigits]], [[MaximumFractionDigits]], [[MinimumSignificantDigits]], [[MaximumSignificantDigits]], [[RoundingType]] &raquo;).
1. Let _pluralRules_ be ? OrdinaryCreateFromConstructor(NewTarget, *"%PluralRules.prototype%"*, &laquo; [[InitializedPluralRules]], [[Locale]], [[Type]], [[MinimumIntegerDigits]], [[MinimumFractionDigits]], [[MaximumFractionDigits]], [[MinimumSignificantDigits]], [[MaximumSignificantDigits]], [[RoundingType]], [[RoundingMode]], [[RoundingIncrement]], [[TrailingZeroDisplay]] &raquo;).
1. Return ? InitializePluralRules(_pluralRules_, _locales_, _options_).
</emu-alg>
</emu-clause>
Expand Down Expand Up @@ -225,6 +225,18 @@
<td>[[MaximumSignificantDigits]]</td>
<td>*"maximumSignificantDigits"*</td>
</tr>
<tr>
<td>[[RoundingMode]]</td>
<td>*"roundingMode"*</td>
</tr>
<tr>
<td>[[RoundingIncrement]]</td>
<td>*"roundingIncrement"*</td>
</tr>
<tr>
<td>[[TrailingZeroDisplay]]</td>
<td>*"trailingZeroDisplay"*</td>
</tr>
</table>
</emu-table>
</emu-clause>
Expand Down Expand Up @@ -252,6 +264,9 @@
<li>[[MinimumFractionDigits]] and [[MaximumFractionDigits]] are non-negative integer Number values indicating the minimum and maximum fraction digits to be used. Numbers will be rounded or padded with trailing zeroes if necessary.</li>
<li>[[MinimumSignificantDigits]] and [[MaximumSignificantDigits]] are positive integer Number values indicating the minimum and maximum fraction digits to be used. Either none or both of these properties are present; if they are, they override minimum and maximum integer and fraction digits.</li>
<li>[[RoundingType]] is one of the values ~fractionDigits~, ~significantDigits~, ~morePrecision~, or ~lessPrecision~, indicating which rounding strategy to use, as discussed in <emu-xref href="#sec-properties-of-intl-numberformat-instances"></emu-xref>.</li>
<li>[[RoundingMode]] is one of the String values in the Identifier column of <emu-xref href="#table-intl-rounding-modes"></emu-xref>, specifying which rounding mode to use.</li>
<li>[[RoundingIncrement]] is an integer-valued Number that evenly divides 10, 100, 1000, or 10000 into tenths, fifths, quarters, or halves. It indicates the increment at which rounding should take place relative to the calculated rounding magnitude. For example, if [[MaximumFractionDigits]] is 2 and [[RoundingIncrement]] is 5, then the number is rounded to the nearest 0.05 ("nickel rounding").</li>
<li>[[TrailingZeroDisplay]] is one of the String values *"auto"* or *"stripIfInteger"*, indicating whether to strip trailing zeros if the formatted number is an integer (i.e., has no nonzero fraction digit).</li>
</ul>
</emu-clause>

Expand Down

0 comments on commit 29acfc6

Please sign in to comment.