Skip to content

Add GetAmountConvertToUnit definitions to both ECMA-262 and ECMA-402 - #110

Open
eemeli wants to merge 22 commits into
mainfrom
convert-options
Open

Add GetAmountConvertToUnit definitions to both ECMA-262 and ECMA-402#110
eemeli wants to merge 22 commits into
mainfrom
convert-options

Conversation

@eemeli

@eemeli eemeli commented May 16, 2026

Copy link
Copy Markdown
Member

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 CLDR units.xml is left as a TODO. Edit: now included.

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.

@github-actions

github-actions Bot commented May 16, 2026

Copy link
Copy Markdown
PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://tc39.github.io/proposal-amount/pr-preview/pr-110/

Built to branch gh-pages at 2026-08-19 15:16 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@eemeli
eemeli force-pushed the convert-options branch from 46f7cdf to 3c8bebf Compare May 17, 2026 19:45
@eemeli
eemeli requested a review from jessealama May 17, 2026 19:50
@jessealama

Copy link
Copy Markdown
Collaborator

This looks good. Thanks!

Comment thread spec.emu Outdated
Comment thread spec.emu

@gibson042 gibson042 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I have many suggestions, but this is an excellent starting point.

Comment thread spec.emu Outdated
Comment thread intl.emu Outdated
Comment thread intl.emu Outdated
Comment thread intl.emu Outdated
Comment thread intl.emu Outdated
Comment thread intl.emu Outdated
Comment thread intl.emu Outdated
Comment thread intl.emu Outdated
Comment thread intl.emu Outdated
Comment thread intl.emu
@eemeli
eemeli requested a review from gibson042 May 18, 2026 15:16

@gibson042 gibson042 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread spec.emu Outdated
Comment thread spec.emu Outdated
Comment thread spec.emu Outdated
Comment thread spec.emu Outdated
Comment thread intl.emu
Comment thread intl.emu Outdated
Comment thread intl.emu
@jessealama
jessealama force-pushed the convert-options branch 2 times, most recently from 57854cf to d3b7e95 Compare August 18, 2026 09:20
Options should be read in alphabetical order (see #95 and review
feedback on #110), and alphabetically "locale" precedes "usage".
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.
@jessealama
jessealama requested a review from gibson042 August 19, 2026 12:14

@gibson042 gibson042 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is still slightly too far off from #95.

Comment thread intl.emu
<emu-clause id="sup-amount-selecttargetunit" type="abstract operation">
<h1>
SelectTargetUnit (
_sourceValue_: a Number,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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𝔽).

Suggested change
_sourceValue_: a Number,
_sourceValue_: an extended mathematical value or *NaN*,

(and likewise throughout this PR, including in spec.emu)

Comment thread spec.emu Outdated
Comment thread spec.emu Outdated
Comment thread intl.emu
Comment on lines +51 to +52
1. Let _locale_ be ? Get(_options_, *"locale"*).
1. Let _usage_ be ? GetOption(_options_, *"usage"*, ~string~, ~empty~, *undefined*).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread intl.emu
Comment on lines +60 to +61
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_.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use actual locale negotiation rather than just picking the first element; again, see #95.

Comment thread intl.emu
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_).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread intl.emu Outdated
Comment thread intl.emu
Comment thread intl.emu Outdated
jessealama and others added 5 commits August 19, 2026 17:13
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add spec text for handling for locale and usage conversion options

3 participants