Skip to content

Add CreateFormatterObject AO#94

Open
jessealama wants to merge 4 commits intomainfrom
create-formatter-object
Open

Add CreateFormatterObject AO#94
jessealama wants to merge 4 commits intomainfrom
create-formatter-object

Conversation

@jessealama
Copy link
Copy Markdown
Collaborator

@jessealama jessealama commented Apr 1, 2026

Adds the CreateFormatterObject abstract operation, which builds the formatter object (with internal slots for rounding mode, digit limits, etc.) needed by FormatNumericToString. Replaces the TODO placeholders in the Amount constructor and convertTo with actual calls to this new AO. Spun off from #89 per reviewer feedback. Closes #93.

Replace TODO placeholders in the Amount constructor and convertTo with
actual CreateFormatterObject calls that build the formatter object
needed by FormatNumericToString.

Closes #93
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 1, 2026

PR Preview Action v1.8.1

QR code for preview link

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

Built to branch gh-pages at 2026-04-02 13:58 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

Copy link
Copy Markdown
Member

@gibson042 gibson042 left a comment

Choose a reason for hiding this comment

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

This is a change for the better, but is not currently addressing #89 (comment) .

Copy link
Copy Markdown
Member

@gibson042 gibson042 left a comment

Choose a reason for hiding this comment

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

This looks good after fixing the typing to correctly map integral Numbers to integers where required.

And for a potential followup, there's a common pattern here that has me wondering whether a new operation is warranted:

  1. If _x_ is not *undefined*, set _x_ to ? SnapToInteger(_x_, ~strict~, 1, 21).

where the second parameter is either ~strict~ to reject non-integral output from ToNumber or ~truncate-strict~ to reject non-finite output but truncate finite non-integers, and the optional third and fourth parameters respectively define inclusive lower and upper bounds.

Such an operation could replace Temporal ToIntegerIfIntegral(x) with SnapToInteger(x, ~strict~), Temporal ToIntegerWithTruncation(x) with SnapToInteger(x, ~truncate-strict~), and Temporal ToPositiveIntegerWithTruncation(x) with SnapToInteger(x, ~truncate-strict~, 1). It could potentially even be extended to consolidate almost all integer conversions by introducing a ~truncate-loose~ behavior (truncating finite non-integer output from ToNumber and mapping NaN to 0) for use in the definition of ToLength and ToIndex (respectively, 𝔽(the result of clamping SnapToInteger(argument, ~truncate-loose~) between 0 and 253 - 1) and SnapToInteger(argument, ~truncate-loose~, 0, 253 - 1)):

Operation NaN ±∞ Finite non-integer Bounds Behavior
ToIntegerOrInfinity → 0 preserve truncate n/a
ToIntegerIfIntegral (Temporal) RangeError RangeError RangeError STRICT
ToIntegerWithTruncation (Temporal) RangeError RangeError truncate TRUNCATE-STRICT
ToPositiveIntegerWithTruncation (Temporal) RangeError RangeError truncate RangeError if ≤ 0 TRUNCATE-STRICT
ToLength → 0 preserve truncate clamp to [0, 2**53 - 1] TRUNCATE-LOOSE
ToIndex → 0 preserve truncate RangeError if outside [0, 2**53 - 1] TRUNCATE-LOOSE
DefaultNumberOption (ECMA-402) RangeError RangeError preserve RangeError if outside [min, max]1 n/a1
GetAmountOptions RangeError RangeError RangeError RangeError if outside [min, max] STRICT
GetAmountConvertToOptions RangeError RangeError RangeError RangeError if outside [min, max] STRICT

Footnotes

  1. DefaultNumberOption currently checks output from ToNumber against specified bounds before truncation: https://github.com/tc39/ecma402/issues/691 2

Comment on lines 94 to +95
1. If _fractionDigits_ is not an integral Number, throw a *RangeError* exception.
1. If _fractionDigits_ &lt; *-0*<sub>𝔽</sub> or _fractionDigits_ > *100*<sub>𝔽</sub>, throw a *RangeError* exception.
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.

The operation header requires [[FractionDigits]] to be undefined or an integer, so let's convert it here and remove the awkward use of -0𝔽.

Suggested change
1. If _fractionDigits_ is not an integral Number, throw a *RangeError* exception.
1. If _fractionDigits_ &lt; *-0*<sub>𝔽</sub> or _fractionDigits_ > *100*<sub>𝔽</sub>, throw a *RangeError* exception.
1. If _fractionDigits_ is not an integral Number, throw a *RangeError* exception.
1. Set _fractionDigits_ to ℝ(_fractionDigits_).
1. If _fractionDigits_ is not in the inclusive interval from 0 to 100, throw a *RangeError* exception.

Comment on lines 97 to +98
1. If _significantDigits_ is not an integral Number, throw a *RangeError* exception.
1. If _significantDigits_ &lt; *1*<sub>𝔽</sub>, throw a *RangeError* exception.
1. If _significantDigits_ &lt; *1*<sub>𝔽</sub> or _significantDigits_ > *21*<sub>𝔽</sub>, throw a *RangeError* exception.
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.

Suggested change
1. If _significantDigits_ is not an integral Number, throw a *RangeError* exception.
1. If _significantDigits_ &lt; *1*<sub>𝔽</sub>, throw a *RangeError* exception.
1. If _significantDigits_ &lt; *1*<sub>𝔽</sub> or _significantDigits_ > *21*<sub>𝔽</sub>, throw a *RangeError* exception.
1. If _significantDigits_ is not an integral Number, throw a *RangeError* exception.
1. Set _significantDigits_ to ℝ(_significantDigits_).
1. If _significantDigits_ is not in the inclusive interval from 1 to 21, throw a *RangeError* exception.

Comment on lines +122 to +125
1. If _minFractionDigits_ is not *undefined*, then
1. If _minFractionDigits_ is not an integral Number, or _minFractionDigits_ &lt; *-0*<sub>𝔽</sub>, or _minFractionDigits_ > *100*<sub>𝔽</sub>, throw a *RangeError* exception.
1. If _maxFractionDigits_ is not *undefined*, then
1. If _maxFractionDigits_ is not an integral Number, or _maxFractionDigits_ &lt; *-0*<sub>𝔽</sub>, or _maxFractionDigits_ > *100*<sub>𝔽</sub>, throw a *RangeError* exception.
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.

Suggested change
1. If _minFractionDigits_ is not *undefined*, then
1. If _minFractionDigits_ is not an integral Number, or _minFractionDigits_ &lt; *-0*<sub>𝔽</sub>, or _minFractionDigits_ > *100*<sub>𝔽</sub>, throw a *RangeError* exception.
1. If _maxFractionDigits_ is not *undefined*, then
1. If _maxFractionDigits_ is not an integral Number, or _maxFractionDigits_ &lt; *-0*<sub>𝔽</sub>, or _maxFractionDigits_ > *100*<sub>𝔽</sub>, throw a *RangeError* exception.
1. If _minFractionDigits_ is not *undefined*, then
1. If _minFractionDigits_ is not an integral Number, throw a *RangeError* exception.
1. Set _minFractionDigits_ to ℝ(_minFractionDigits_).
1. If _minFractionDigits_ is not in the inclusive interval from 0 to 100, throw a *RangeError* exception.
1. If _maxFractionDigits_ is not *undefined*, then
1. If _maxFractionDigits_ is not an integral Number, throw a *RangeError* exception.
1. Set _maxFractionDigits_ to ℝ(_maxFractionDigits_).
1. If _maxFractionDigits_ is not in the inclusive interval from 0 to 100, throw a *RangeError* exception.

Comment on lines +127 to +130
1. If _minSignificantDigits_ is not *undefined*, then
1. If _minSignificantDigits_ is not an integral Number, or _minSignificantDigits_ &lt; *1*<sub>𝔽</sub>, or _minSignificantDigits_ > *21*<sub>𝔽</sub>, throw a *RangeError* exception.
1. If _maxSignificantDigits_ is not *undefined*, then
1. If _maxSignificantDigits_ is not an integral Number, or _maxSignificantDigits_ &lt; *1*<sub>𝔽</sub>, or _maxSignificantDigits_ > *21*<sub>𝔽</sub>, throw a *RangeError* exception.
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.

Suggested change
1. If _minSignificantDigits_ is not *undefined*, then
1. If _minSignificantDigits_ is not an integral Number, or _minSignificantDigits_ &lt; *1*<sub>𝔽</sub>, or _minSignificantDigits_ > *21*<sub>𝔽</sub>, throw a *RangeError* exception.
1. If _maxSignificantDigits_ is not *undefined*, then
1. If _maxSignificantDigits_ is not an integral Number, or _maxSignificantDigits_ &lt; *1*<sub>𝔽</sub>, or _maxSignificantDigits_ > *21*<sub>𝔽</sub>, throw a *RangeError* exception.
1. If _minSignificantDigits_ is not *undefined*, then
1. If _minSignificantDigits_ is not an integral Number, throw a *RangeError* exception.
1. Set _minSignificantDigits_ to ℝ(_minSignificantDigits_).
1. If _minSignificantDigits_ is not in the inclusive interval from 1 to 21, throw a *RangeError* exception.
1. If _maxSignificantDigits_ is not *undefined*, then
1. If _maxSignificantDigits_ is not an integral Number, throw a *RangeError* exception.
1. Set _maxSignificantDigits_ to ℝ(_maxSignificantDigits_).
1. If _maxSignificantDigits_ is not in the inclusive interval from 1 to 21, throw a *RangeError* exception.

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.

Defining this and adding the CreateFormatterObject AO should be done as a separate change.

2 participants