Issue #2350 - Part 0: Add a helper function DefaultNumberOption

This is the abstract operation that converts an option value to a number
value, checking whether it is in the allowed range, and filling in a
fallback value if necessary.
This commit is contained in:
Moonchild 2023-10-19 10:17:11 +02:00 committed by roytam1
commit 735bb78c28

View file

@ -647,6 +647,52 @@ function GetOption(options, property, type, values, fallback) {
return fallback;
}
/**
* The abstract operation DefaultNumberOption converts value to a Number value,
* checks whether it is in the allowed range, and fills in a fallback value if
* necessary.
*
* Spec: ECMAScript Internationalization API Specification, 9.2.11.
*/
function DefaultNumberOption(value, minimum, maximum, fallback) {
assert(
typeof minimum === "number" && (minimum | 0) === minimum,
"DefaultNumberOption"
);
assert(
typeof maximum === "number" && (maximum | 0) === maximum,
"DefaultNumberOption"
);
assert(
fallback === undefined ||
(typeof fallback === "number" && (fallback | 0) === fallback),
"DefaultNumberOption"
);
assert(
fallback === undefined || (minimum <= fallback && fallback <= maximum),
"DefaultNumberOption"
);
// Step 1.
if (value === undefined) {
return fallback;
}
// Step 2.
value = ToNumber(value);
// Step 3.
if (Number_isNaN(value) || value < minimum || value > maximum) {
ThrowRangeError(JSMSG_INVALID_DIGITS_VALUE, value);
}
// Step 4.
// Apply bitwise-or to convert -0 to +0 per ES2017, 5.2 and to ensure the
// result is an int32 value.
return std_Math_floor(value) | 0;
}
/**
* Extracts a property value from the provided options object, converts it to a
* Number value, checks whether it is in the allowed range, and fills in a