Skip to content

Commit 00a7349

Browse files
committed
[spec] Use fractionDigits/significantDigits in convertTo, allow Number values
Aligns Amount.prototype.convertTo with the README updates from #101: replaces the min/max fraction/significant digit options and roundingPriority with the constructor-style fractionDigits and significantDigits (mutually exclusive, throwing RangeError if both are set). When neither precision option is supplied — or the converted value is non-finite — the resulting Amount is now Number-backed rather than always String-backed.
1 parent fd3b200 commit 00a7349

1 file changed

Lines changed: 21 additions & 33 deletions

File tree

spec.emu

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -106,41 +106,29 @@ location: https://github.com/tc39/proposal-amount/
106106
<emu-clause id="sec-amount-getamountconverttooptions" type="abstract operation">
107107
<h1>GetAmountConvertToOptions (
108108
_opts_: an Object
109-
): either a normal completion containing a Record with fields [[MinimumFractionDigits]] (a non-negative integer or *undefined*), [[MaximumFractionDigits]] (a non-negative integer or *undefined*), [[RoundingMode]] (a <emu-xref href="#dfn-amount-rounding-mode">rounding mode</emu-xref>), [[RoundingPriority]] (a String), [[MinimumSignificantDigits]] (a positive integer or *undefined*), [[MaximumSignificantDigits]] (a positive integer or *undefined*), [[Locale]] (a String or *undefined*), [[Usage]] (a String or *undefined*), and [[Unit]] (a String or *undefined*) or a throw completion
109+
): either a normal completion containing a Record with fields [[FractionDigits]] (a non-negative integer or *undefined*), [[RoundingMode]] (a <emu-xref href="#dfn-amount-rounding-mode">rounding mode</emu-xref>), [[SignificantDigits]] (a positive integer or *undefined*), and [[Unit]] (a String or *undefined*) or a throw completion
110110
</h1>
111111
<dl class="header">
112112
<dt>description</dt>
113113
<dd>It validates the given _options_ (an ECMAScript object) for converting an Amount to another Amount and returns a Record with slots set to appropriate marthematical values (or *undefined*).</dd>
114114
</dl>
115115
<emu-alg>
116116
1. Let _opts_ be ? GetOptionsObject(_opts_).
117-
1. Let _minFractionDigits_ be ? GetOption(_opts_, *"minimumFractionDigits"*, ~number~, ~empty~, *undefined*).
118-
1. Let _maxFractionDigits_ be ? GetOption(_opts_, *"maximumFractionDigits"*, ~number~, ~empty~, *undefined*).
117+
1. Let _fractionDigits_ be ? GetOption(_opts_, *"fractionDigits"*, ~number~, ~empty~, *undefined*).
119118
1. Let _roundingMode_ be ? GetOption(_opts_, *"roundingMode"*, ~string~, « *"ceil"*, *"floor"*, *"expand"*, *"trunc"*, *"halfCeil"*, *"halfFloor"*, *"halfExpand"*, *"halfTrunc"*, *"halfEven"* », *"halfEven"*).
120-
1. Let _roundingPriority_ be ? GetOption(_opts_, *"roundingPriority"*, ~string~, ~empty~, *undefined*).
121-
1. Let _minSignificantDigits_ be ? GetOption(_opts_, *"minimumSignificantDigits"*, ~number~, ~empty~, *undefined*).
122-
1. Let _maxSignificantDigits_ be ? GetOption(_opts_, *"maximumSignificantDigits"*, ~number~, ~empty~, *undefined*).
119+
1. Let _significantDigits_ be ? GetOption(_opts_, *"significantDigits"*, ~number~, ~empty~, *undefined*).
123120
1. Let _unit_ be ? GetOption(_opts_, *"unit"*, ~string~, ~empty~, *undefined*).
124-
1. If _minFractionDigits_ is not *undefined*, then
125-
1. If _minFractionDigits_ is not an integral Number, throw a *RangeError* exception.
126-
1. Set _minFractionDigits_ to ℝ(_minFractionDigits_).
127-
1. If _minFractionDigits_ is not in the inclusive interval from 0 to 100, throw a *RangeError* exception.
128-
1. If _maxFractionDigits_ is not *undefined*, then
129-
1. If _maxFractionDigits_ is not an integral Number, throw a *RangeError* exception.
130-
1. Set _maxFractionDigits_ to ℝ(_maxFractionDigits_).
131-
1. If _maxFractionDigits_ is not in the inclusive interval from 0 to 100, throw a *RangeError* exception.
132-
1. If _minFractionDigits_ is not *undefined* and _maxFractionDigits_ is not *undefined* and _minFractionDigits_ > _maxFractionDigits_, throw a *RangeError* exception.
133-
1. If _minSignificantDigits_ is not *undefined*, then
134-
1. If _minSignificantDigits_ is not an integral Number, throw a *RangeError* exception.
135-
1. Set _minSignificantDigits_ to ℝ(_minSignificantDigits_).
136-
1. If _minSignificantDigits_ is not in the inclusive interval from 1 to 21, throw a *RangeError* exception.
137-
1. If _maxSignificantDigits_ is not *undefined*, then
138-
1. If _maxSignificantDigits_ is not an integral Number, throw a *RangeError* exception.
139-
1. Set _maxSignificantDigits_ to ℝ(_maxSignificantDigits_).
140-
1. If _maxSignificantDigits_ is not in the inclusive interval from 1 to 21, throw a *RangeError* exception.
141-
1. If _minSignificantDigits_ is not *undefined* and _maxSignificantDigits_ is not *undefined* and _minSignificantDigits_ > _maxSignificantDigits_, throw a *RangeError* exception.
121+
1. If _fractionDigits_ is not *undefined*, then
122+
1. If _significantDigits_ is not *undefined*, throw a *RangeError* exception.
123+
1. If _fractionDigits_ is not an integral Number, throw a *RangeError* exception.
124+
1. Set _fractionDigits_ to ℝ(_fractionDigits_).
125+
1. If _fractionDigits_ is not in the inclusive interval from 0 to 100, throw a *RangeError* exception.
126+
1. Else if _significantDigits_ is not *undefined*, then
127+
1. If _significantDigits_ is not an integral Number, throw a *RangeError* exception.
128+
1. Set _significantDigits_ to ℝ(_significantDigits_).
129+
1. If _significantDigits_ is not in the inclusive interval from 1 to 21, throw a *RangeError* exception.
142130
1. If _unit_ is the empty String, throw a *RangeError* exception.
143-
1. Return the Record { [[MinimumFractionDigits]]: _minFractionDigits_, [[MaximumFractionDigits]]: _maxFractionDigits_, [[MinimumSignificantDigits]]: _minSignificantDigits_, [[MaximumSignificantDigits]]: _maxSignificantDigits_, [[RoundingMode]]: _roundingMode_, [[RoundingPriority]]: _roundingPriority_, [[Unit]]: _unit_ }.
131+
1. Return the Record { [[FractionDigits]]: _fractionDigits_, [[RoundingMode]]: _roundingMode_, [[SignificantDigits]]: _significantDigits_, [[Unit]]: _unit_ }.
144132
</emu-alg>
145133
</emu-clause>
146134

@@ -389,11 +377,8 @@ location: https://github.com/tc39/proposal-amount/
389377
1. Let _targetUnit_ be _validatedOpts_.[[Unit]].
390378
1. If _targetUnit_ is *undefined*, throw a *TypeError* exception.
391379
1. Let _roundingMode_ be _validatedOpts_.[[RoundingMode]].
392-
1. Let _roundingPriority_ be _validatedOpts_.[[RoundingPriority]].
393-
1. Let _minFractionDigits_ be _validatedOpts_.[[MinimumFractionDigits]].
394-
1. Let _maxFractionDigits_ be _validatedOpts_.[[MaximumFractionDigits]].
395-
1. Let _minSignificantDigits_ be _validatedOpts_.[[MinimumSignificantDigits]].
396-
1. Let _maxSignificantDigits_ be _validatedOpts_.[[MaximumSignificantDigits]].
380+
1. Let _fractionDigits_ be _validatedOpts_.[[FractionDigits]].
381+
1. Let _significantDigits_ be _validatedOpts_.[[SignificantDigits]].
397382
1. Let _v_ be _O_.[[AmountValue]].
398383
1. If _v_ is a Number, then
399384
1. Let _sourceValue_ be _v_.
@@ -403,10 +388,13 @@ location: https://github.com/tc39/proposal-amount/
403388
1. Assert: _v_ is a String.
404389
1. Let _sourceValue_ be StringToNumber(_v_).
405390
1. Let _convertedValue_ be ? ConvertUnitValue(_sourceValue_, _sourceUnit_, _targetUnit_).
406-
1. Let _formatter_ be CreateFormatterObject(_roundingMode_, _minFractionDigits_, _maxFractionDigits_, _minSignificantDigits_, _maxSignificantDigits_, _roundingPriority_).
407-
1. Let _formatted_ be FormatNumericToString(_formatter_, ℝ(_convertedValue_), 0).
408391
1. Let _result_ be OrdinaryObjectCreate(%Amount.prototype%, « [[AmountValue]], [[Unit]] »).
409-
1. Set _result_.[[AmountValue]] to _formatted_.[[FormattedString]].
392+
1. If _convertedValue_ is finite and (_fractionDigits_ is not *undefined* or _significantDigits_ is not *undefined*), then
393+
1. Let _formatter_ be CreateFormatterObject(_roundingMode_, _fractionDigits_, _fractionDigits_, _significantDigits_, _significantDigits_, *undefined*).
394+
1. Let _formatted_ be FormatNumericToString(_formatter_, ℝ(_convertedValue_), 0).
395+
1. Set _result_.[[AmountValue]] to _formatted_.[[FormattedString]].
396+
1. Else,
397+
1. Set _result_.[[AmountValue]] to _convertedValue_.
410398
1. Set _result_.[[Unit]] to _targetUnit_.
411399
1. Return _result_.
412400
</emu-alg>

0 commit comments

Comments
 (0)