mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 09:30:24 +00:00
LibJS: Refactor the Intl.NumberFormat GetStringOrBooleanOption AO
This is an editorial change in the Intl.NumberFormat V3 spec. See: https://github.com/tc39/proposal-intl-numberformat-v3/commit/654bfad
This commit is contained in:
parent
be347f67dc
commit
a283daaab7
Notes:
sideshowbarker
2024-07-17 21:11:12 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/a283daaab7 Pull-request: https://github.com/SerenityOS/serenity/pull/17251 Reviewed-by: https://github.com/linusg ✅
3 changed files with 34 additions and 29 deletions
|
@ -608,8 +608,8 @@ ThrowCompletionOr<Object*> coerce_options_to_object(VM& vm, Value options)
|
|||
|
||||
// NOTE: 9.2.13 GetOption has been removed and is being pulled in from ECMA-262 in the Temporal proposal.
|
||||
|
||||
// 1.2.12 GetStringOrBooleanOption ( options, property, values, trueValue, falsyValue, fallback ), https://tc39.es/proposal-intl-numberformat-v3/out/negotiation/proposed.html#sec-getstringorbooleanoption
|
||||
ThrowCompletionOr<StringOrBoolean> get_string_or_boolean_option(VM& vm, Object const& options, PropertyKey const& property, Span<StringView const> values, StringOrBoolean true_value, StringOrBoolean falsy_value, StringOrBoolean fallback)
|
||||
// 1.2.14 GetBooleanOrStringNumberFormatOption ( options, property, stringValues, fallback ), https://tc39.es/proposal-intl-numberformat-v3/out/negotiation/proposed.html#sec-getbooleanorstringnumberformatoption
|
||||
ThrowCompletionOr<StringOrBoolean> get_boolean_or_string_number_format_option(VM& vm, Object const& options, PropertyKey const& property, Span<StringView const> string_values, StringOrBoolean fallback)
|
||||
{
|
||||
// 1. Let value be ? Get(options, property).
|
||||
auto value = TRY(options.get(property));
|
||||
|
@ -618,31 +618,23 @@ ThrowCompletionOr<StringOrBoolean> get_string_or_boolean_option(VM& vm, Object c
|
|||
if (value.is_undefined())
|
||||
return fallback;
|
||||
|
||||
// 3. If value is true, return trueValue.
|
||||
// 3. If value is true, return true.
|
||||
if (value.is_boolean() && value.as_bool())
|
||||
return true_value;
|
||||
return StringOrBoolean { true };
|
||||
|
||||
// 4. Let valueBoolean be ToBoolean(value).
|
||||
auto value_boolean = value.to_boolean();
|
||||
// 4. If ToBoolean(value) is false, return false.
|
||||
if (!value.to_boolean())
|
||||
return StringOrBoolean { false };
|
||||
|
||||
// 5. If valueBoolean is false, return falsyValue.
|
||||
if (!value_boolean)
|
||||
return falsy_value;
|
||||
|
||||
// 6. Let value be ? ToString(value).
|
||||
// 5. Let value be ? ToString(value).
|
||||
auto value_string = TRY(value.to_string(vm));
|
||||
|
||||
// 7. NOTE: For historical reasons, the strings "true" and "false" are treated the same as the boolean value true.
|
||||
// 8. If value is "true" or "false", return fallback.
|
||||
if (value_string.is_one_of("true"sv, "false"sv))
|
||||
return fallback;
|
||||
|
||||
// 9. If values does not contain an element equal to value, throw a RangeError exception.
|
||||
auto it = find(values.begin(), values.end(), value_string.bytes_as_string_view());
|
||||
if (it == values.end())
|
||||
// 6. If stringValues does not contain value, throw a RangeError exception.
|
||||
auto it = find(string_values.begin(), string_values.end(), value_string.bytes_as_string_view());
|
||||
if (it == string_values.end())
|
||||
return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, value_string, property.as_string());
|
||||
|
||||
// 10. Return value.
|
||||
// 7. Return value.
|
||||
return StringOrBoolean { *it };
|
||||
}
|
||||
|
||||
|
|
|
@ -89,15 +89,15 @@ ThrowCompletionOr<String> insert_unicode_extension_and_canonicalize(VM&, ::Local
|
|||
ThrowCompletionOr<LocaleResult> resolve_locale(VM&, Vector<String> const& requested_locales, LocaleOptions const& options, Span<StringView const> relevant_extension_keys);
|
||||
ThrowCompletionOr<Array*> supported_locales(VM&, Vector<String> const& requested_locales, Value options);
|
||||
ThrowCompletionOr<Object*> coerce_options_to_object(VM&, Value options);
|
||||
ThrowCompletionOr<StringOrBoolean> get_string_or_boolean_option(VM&, Object const& options, PropertyKey const& property, Span<StringView const> values, StringOrBoolean true_value, StringOrBoolean falsy_value, StringOrBoolean fallback);
|
||||
ThrowCompletionOr<StringOrBoolean> get_boolean_or_string_number_format_option(VM& vm, Object const& options, PropertyKey const& property, Span<StringView const> string_values, StringOrBoolean fallback);
|
||||
ThrowCompletionOr<Optional<int>> default_number_option(VM&, Value value, int minimum, int maximum, Optional<int> fallback);
|
||||
ThrowCompletionOr<Optional<int>> get_number_option(VM&, Object const& options, PropertyKey const& property, int minimum, int maximum, Optional<int> fallback);
|
||||
ThrowCompletionOr<Vector<PatternPartition>> partition_pattern(VM&, StringView pattern);
|
||||
|
||||
template<size_t Size>
|
||||
ThrowCompletionOr<StringOrBoolean> get_string_or_boolean_option(VM& vm, Object const& options, PropertyKey const& property, StringView const (&values)[Size], StringOrBoolean true_value, StringOrBoolean falsy_value, StringOrBoolean fallback)
|
||||
ThrowCompletionOr<StringOrBoolean> get_boolean_or_string_number_format_option(VM& vm, Object const& options, PropertyKey const& property, StringView const (&string_values)[Size], StringOrBoolean fallback)
|
||||
{
|
||||
return get_string_or_boolean_option(vm, options, property, Span<StringView const> { values }, move(true_value), move(falsy_value), move(fallback));
|
||||
return get_boolean_or_string_number_format_option(vm, options, property, Span<StringView const> { string_values }, move(fallback));
|
||||
}
|
||||
|
||||
// NOTE: ECMA-402's GetOption is being removed in favor of a shared ECMA-262 GetOption in the Temporal proposal.
|
||||
|
|
|
@ -185,19 +185,32 @@ ThrowCompletionOr<NumberFormat*> initialize_number_format(VM& vm, NumberFormat&
|
|||
default_use_grouping = "min2"sv;
|
||||
}
|
||||
|
||||
// 24. Let useGrouping be ? GetStringOrBooleanOption(options, "useGrouping", « "min2", "auto", "always" », "always", false, defaultUseGrouping).
|
||||
auto use_grouping = TRY(get_string_or_boolean_option(vm, *options, vm.names.useGrouping, { "min2"sv, "auto"sv, "always"sv }, "always"sv, false, default_use_grouping));
|
||||
// 24. NOTE: For historical reasons, the strings "true" and "false" are accepted and replaced with the default value.
|
||||
// 25. Let useGrouping be ? GetBooleanOrStringNumberFormatOption(options, "useGrouping", « "min2", "auto", "always", "true", "false" », defaultUseGrouping).
|
||||
auto use_grouping = TRY(get_boolean_or_string_number_format_option(vm, *options, vm.names.useGrouping, { "min2"sv, "auto"sv, "always"sv, "true"sv, "false"sv }, default_use_grouping));
|
||||
|
||||
// 25. Set numberFormat.[[UseGrouping]] to useGrouping.
|
||||
// 26. If useGrouping is "true" or useGrouping is "false", set useGrouping to defaultUseGrouping.
|
||||
if (auto const* use_grouping_string = use_grouping.get_pointer<StringView>()) {
|
||||
if (use_grouping_string->is_one_of("true"sv, "false"sv))
|
||||
use_grouping = default_use_grouping;
|
||||
}
|
||||
|
||||
// 27. If useGrouping is true, set useGrouping to "always".
|
||||
if (auto const* use_grouping_boolean = use_grouping.get_pointer<bool>()) {
|
||||
if (*use_grouping_boolean)
|
||||
use_grouping = "always"sv;
|
||||
}
|
||||
|
||||
// 28. Set numberFormat.[[UseGrouping]] to useGrouping.
|
||||
number_format.set_use_grouping(use_grouping);
|
||||
|
||||
// 26. Let signDisplay be ? GetOption(options, "signDisplay", string, « "auto", "never", "always", "exceptZero, "negative" », "auto").
|
||||
// 29. Let signDisplay be ? GetOption(options, "signDisplay", string, « "auto", "never", "always", "exceptZero, "negative" », "auto").
|
||||
auto sign_display = TRY(get_option(vm, *options, vm.names.signDisplay, OptionType::String, { "auto"sv, "never"sv, "always"sv, "exceptZero"sv, "negative"sv }, "auto"sv));
|
||||
|
||||
// 27. Set numberFormat.[[SignDisplay]] to signDisplay.
|
||||
// 30. Set numberFormat.[[SignDisplay]] to signDisplay.
|
||||
number_format.set_sign_display(TRY(sign_display.as_string().utf8_string_view()));
|
||||
|
||||
// 28. Return numberFormat.
|
||||
// 31. Return numberFormat.
|
||||
return &number_format;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue