Add GetAmountConvertToUnit definitions to both ECMA-262 and ECMA-402 - #110
Add GetAmountConvertToUnit definitions to both ECMA-262 and ECMA-402#110eemeli wants to merge 22 commits into
Conversation
|
|
This looks good. Thanks! |
gibson042
left a comment
There was a problem hiding this comment.
Thanks! I have many suggestions, but this is an excellent starting point.
gibson042
left a comment
There was a problem hiding this comment.
I'd like to name the Records that are passed between operations, but won't insist on it being part of this PR. The rest of this review is a large handful of non-blocking suggestions.
57854cf to
d3b7e95
Compare
Co-authored-by: Richard Gibson <richard.gibson@gmail.com>
Co-authored-by: Richard Gibson <richard.gibson@gmail.com>
Co-authored-by: Richard Gibson <richard.gibson@gmail.com>
Rename two leftover uses of _matchingElements_ to _matchingUnits_, declare GetPreferredUnits as returning a List of Records now that it can no longer throw, drop the ? at its call site accordingly, and add the missing closing asterisk in *"001"*.
fd51602 to
964a6d1
Compare
This reverts the ordering introduced in 5e2ef52. Rejecting incoherent options (no unit, no locale, no usage) should happen before looking up the source unit's category; otherwise a source unit without a category, such as a currency, masks the real problem with a RangeError when the options themselves are what is wrong. This also matches the expected eventual structure sketched in #95, where the source unit lookup comes after option validation.
| <emu-clause id="sup-amount-selecttargetunit" type="abstract operation"> | ||
| <h1> | ||
| SelectTargetUnit ( | ||
| _sourceValue_: a Number, |
There was a problem hiding this comment.
This input should probably be a mathematical value. https://github.com/unicode-org/cldr/blob/main/common/supplemental/units.xml contains <unitPreference regions="001" geq="0.1">kilometer</unitPreference>, and we're just lucky that the corresponding IEEE 754 binary64 value is slightly above 0.1 rather than slightly below it (otherwise it would incorrectly not be selected for 0.1𝔽).
| _sourceValue_: a Number, | |
| _sourceValue_: an extended mathematical value or *NaN*, |
(and likewise throughout this PR, including in spec.emu)
| 1. Let _locale_ be ? Get(_options_, *"locale"*). | ||
| 1. Let _usage_ be ? GetOption(_options_, *"usage"*, ~string~, ~empty~, *undefined*). |
There was a problem hiding this comment.
This will result in observably reading "locale" and "usage" after "fractionDigits", "roundingMode", "significantDigits", and "unit" (i.e., out of alphabetical order) if and only if the implementation includes ECMA-402. See #95 more a more complete writeup.
| 1. Let _requestedLocales_ be ? CanonicalizeLocaleList(_locale_). | ||
| 1. If _requestedLocales_ is an empty List, let _resolvedLocale_ be DefaultLocale(); else let _resolvedLocale_ be the first element of _requestedLocales_. |
There was a problem hiding this comment.
This should use actual locale negotiation rather than just picking the first element; again, see #95.
| 1. Let _unit_ be ~unset~. | ||
| 1. For each element _entry_ of _preferredUnits_, do | ||
| 1. Set _unit_ to _entry_.[[Unit]]. | ||
| 1. Let _convertedValue_ be ? ConvertUnitValue(_sourceValue_, _sourceUnit_, _unit_). |
There was a problem hiding this comment.
It's a bit awkward to specify O(n) ConvertUnitValue calls (n - 1 in SelectTargetUnit and the last in Amount.prototype.convertTo). I'm not objecting, but we should look for ways to improve that.
Co-authored-by: Richard Gibson <richard.gibson@gmail.com>
Co-authored-by: Richard Gibson <richard.gibson@gmail.com>
Co-authored-by: Richard Gibson <richard.gibson@gmail.com>
Co-authored-by: Richard Gibson <richard.gibson@gmail.com>
Co-authored-by: Richard Gibson <richard.gibson@gmail.com>
Fixes #109 by adding an AO to ECMA-262 that's superseded by a redefinition in ECMA-402.
The actual extraction of preferred units from the CLDREdit: now included.units.xmlis left as a TODO.The preferences depend on unit category, usage, region, and value thresholds. The categories we get from validity/unit.xml, the region is calculated from the first-choice locale, and the preferences themselves are in supplemental/units.xml.
As the data always includes
001("the world") as one of the regions for each supported category + usage combination, no locale fallback is ever done during.convertTo(), as all well-formed locales are supported.