diff options
Diffstat (limited to 'js/src/builtin/intl/NumberFormat.js')
-rw-r--r-- | js/src/builtin/intl/NumberFormat.js | 122 |
1 files changed, 94 insertions, 28 deletions
diff --git a/js/src/builtin/intl/NumberFormat.js b/js/src/builtin/intl/NumberFormat.js index 261bff1dc6..85fac41e01 100644 --- a/js/src/builtin/intl/NumberFormat.js +++ b/js/src/builtin/intl/NumberFormat.js @@ -55,8 +55,14 @@ function resolveNumberFormatInternals(lazyNumberFormatData) { // Step 22.
internalProps.minimumIntegerDigits = lazyNumberFormatData.minimumIntegerDigits;
- internalProps.minimumFractionDigits = lazyNumberFormatData.minimumFractionDigits;
- internalProps.maximumFractionDigits = lazyNumberFormatData.maximumFractionDigits;
+
+ if ("minimumFractionDigits" in lazyNumberFormatData) {
+ // Note: Intl.NumberFormat.prototype.resolvedOptions() exposes the
+ // actual presence (versus undefined-ness) of these properties.
+ assert("maximumFractionDigits" in lazyNumberFormatData, "min/max frac digits mismatch");
+ internalProps.minimumFractionDigits = lazyNumberFormatData.minimumFractionDigits;
+ internalProps.maximumFractionDigits = lazyNumberFormatData.maximumFractionDigits;
+ }
if ("minimumSignificantDigits" in lazyNumberFormatData) {
// Note: Intl.NumberFormat.prototype.resolvedOptions() exposes the
@@ -122,34 +128,90 @@ function UnwrapNumberFormat(nf, methodName) { *
* Spec: ECMAScript Internationalization API Specification, 11.1.1.
*/
-function SetNumberFormatDigitOptions(lazyData, options, mnfdDefault) {
+function SetNumberFormatDigitOptions(lazyData, options, mnfdDefault, mxfdDefault) {
// We skip step 1 because we set the properties on a lazyData object.
// Steps 2-4.
assert(IsObject(options), "SetNumberFormatDigitOptions");
assert(typeof mnfdDefault === "number", "SetNumberFormatDigitOptions");
- // Steps 5-8.
- const mnid = GetNumberOption(options, "minimumIntegerDigits", 1, 21, 1);
- const mnfd = GetNumberOption(options, "minimumFractionDigits", 0, 20, mnfdDefault);
- const mxfd = GetNumberOption(options, "maximumFractionDigits", mnfd, 20);
+ assert(typeof mxfdDefault === "number", "SetNumberFormatDigitOptions");
+ assert(mnfdDefault <= mxfdDefault, "SetNumberFormatDigitOptions");
- // Steps 9-10.
+ // Steps 5-9.
+ const mnid = GetNumberOption(options, "minimumIntegerDigits", 1, 21, 1);
+ let mnfd = options.minimumFractionDigits;
+ let mxfd = options.maximumFractionDigits;
let mnsd = options.minimumSignificantDigits;
let mxsd = options.maximumSignificantDigits;
- // Steps 9-11.
+ // Step 10.
lazyData.minimumIntegerDigits = mnid;
- lazyData.minimumFractionDigits = mnfd;
- lazyData.maximumFractionDigits = mxfd;
- // Step 12.
+ // Step 11.
if (mnsd !== undefined || mxsd !== undefined) {
- mnsd = GetNumberOption(options, "minimumSignificantDigits", 1, 21, 1);
- mxsd = GetNumberOption(options, "maximumSignificantDigits", mnsd, 21, 21);
+ // Step 11.a (Omitted).
+
+ // Step 11.b.
+ mnsd = DefaultNumberOption(mnsd, 1, 21, 1);
+
+ // Step 11.c.
+ mxsd = DefaultNumberOption(mxsd, mnsd, 21, 21);
+
+ // Step 11.d.
lazyData.minimumSignificantDigits = mnsd;
+
+ // Step 11.e.
lazyData.maximumSignificantDigits = mxsd;
}
+
+ // Step 12.
+ else if (mnfd !== undefined || mxfd !== undefined) {
+ // Step 12.a (Omitted).
+
+ // Step 12.b.
+ mnfd = DefaultNumberOption(mnfd, 0, 20, undefined);
+
+ // Step 12.c.
+ mxfd = DefaultNumberOption(mxfd, 0, 20, undefined);
+
+ // Steps 12.d-e.
+ // Inlined DefaultNumberOption, only the fallback case applies here.
+ if (mnfd === undefined) {
+ assert(mxfd !== undefined, "mxfd isn't undefined when mnfd is undefined");
+ mnfd = std_Math_min(mnfdDefault, mxfd);
+ }
+
+ // Step 12.f.
+ // Inlined DefaultNumberOption, only the fallback case applies here.
+ else if (mxfd === undefined) {
+ mxfd = std_Math_max(mxfdDefault, mnfd);
+ }
+
+ // Step 12.g.
+ else if (mnfd > mxfd) {
+ ThrowRangeError(JSMSG_INVALID_DIGITS_VALUE, mxfd);
+ }
+
+ // Step 12.h.
+ lazyData.minimumFractionDigits = mnfd;
+
+ // Step 12.i.
+ lazyData.maximumFractionDigits = mxfd;
+ }
+
+ // Step 13 (TODO: Not yet implemented).
+
+ // Step 14.
+ else {
+ // Step 14.a (Omitted).
+
+ // Step 14.b.
+ lazyData.minimumFractionDigits = mnfdDefault;
+
+ // Step 14.c.
+ lazyData.maximumFractionDigits = mxfdDefault;
+ }
}
/**
@@ -291,19 +353,16 @@ function InitializeNumberFormat(numberFormat, thisValue, locales, options) { if (s === "currency")
lazyNumberFormatData.currencyDisplay = cd;
- // Steps 20-22.
- SetNumberFormatDigitOptions(lazyNumberFormatData, options, s === "currency" ? cDigits: 0);
-
- // Step 25.
- if (lazyNumberFormatData.maximumFractionDigits === undefined) {
- let mxfdDefault = s === "currency"
- ? cDigits
- : s === "percent"
- ? 0
- : 3;
- lazyNumberFormatData.maximumFractionDigits =
- std_Math_max(lazyNumberFormatData.minimumFractionDigits, mxfdDefault);
+ // Steps 22-25.
+ var mnfdDefault, mxfdDefault;
+ if (s === "currency") {
+ mnfdDefault = cDigits;
+ mxfdDefault = cDigits;
+ } else {
+ mnfdDefault = 0;
+ mxfdDefault = s === "percent" ? 0 : 3;
}
+ SetNumberFormatDigitOptions(lazyNumberFormatData, options, mnfdDefault, mxfdDefault);
// Steps 23.
var g = GetOption(options, "useGrouping", "boolean", undefined, true);
@@ -520,8 +579,6 @@ function Intl_NumberFormat_resolvedOptions() { numberingSystem: internals.numberingSystem,
style: internals.style,
minimumIntegerDigits: internals.minimumIntegerDigits,
- minimumFractionDigits: internals.minimumFractionDigits,
- maximumFractionDigits: internals.maximumFractionDigits,
useGrouping: internals.useGrouping
};
@@ -536,6 +593,15 @@ function Intl_NumberFormat_resolvedOptions() { _DefineDataProperty(result, "currencyDisplay", internals.currencyDisplay);
}
+ // Min/Max fraction digits are either both present or not present at all.
+ assert(hasOwn("minimumFractionDigits", internals) ===
+ hasOwn("maximumFractionDigits", internals),
+ "minimumFractionDigits is present iff maximumFractionDigits is present");
+ if (hasOwn("minimumFractionDigits", internals)) {
+ _DefineDataProperty(result, "minimumFractionDigits", internals.minimumFractionDigits);
+ _DefineDataProperty(result, "maximumFractionDigits", internals.maximumFractionDigits);
+ }
+
// Min/Max significant digits are either both present or not at all.
assert(hasOwn("minimumSignificantDigits", internals) ===
hasOwn("maximumSignificantDigits", internals),
|