LibJS: Port Intl.DurationFormat to String

This commit is contained in:
Timothy Flynn 2023-01-27 15:57:43 -05:00 committed by Linus Groh
parent 4c8f7d76c4
commit 8dc4e05ecf
Notes: sideshowbarker 2024-07-17 05:00:08 +09:00
4 changed files with 14 additions and 14 deletions

View file

@ -268,10 +268,10 @@ bool is_valid_duration_record(Temporal::DurationRecord const& record)
}
// 1.1.6 GetDurationUnitOptions ( unit, options, baseStyle, stylesList, digitalBase, prevStyle ), https://tc39.es/proposal-intl-duration-format/#sec-getdurationunitoptions
ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(VM& vm, DeprecatedString const& unit, Object const& options, StringView base_style, Span<StringView const> styles_list, StringView digital_base, StringView previous_style)
ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(VM& vm, String const& unit, Object const& options, StringView base_style, Span<StringView const> styles_list, StringView digital_base, StringView previous_style)
{
// 1. Let style be ? GetOption(options, unit, string, stylesList, undefined).
auto style_value = TRY(get_option(vm, options, unit, OptionType::String, styles_list, Empty {}));
auto style_value = TRY(get_option(vm, options, unit.to_deprecated_string(), OptionType::String, styles_list, Empty {}));
// 2. Let displayDefault be "always".
auto display_default = "always"sv;
@ -312,10 +312,10 @@ ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(VM& vm, Depreca
}
// 4. Let displayField be the string-concatenation of unit and "Display".
auto display_field = DeprecatedString::formatted("{}Display", unit);
auto display_field = TRY_OR_THROW_OOM(vm, String::formatted("{}Display", unit));
// 5. Let display be ? GetOption(options, displayField, string, « "auto", "always" », displayDefault).
auto display = TRY(get_option(vm, options, display_field, OptionType::String, { "auto"sv, "always"sv }, display_default));
auto display = TRY(get_option(vm, options, display_field.to_deprecated_string(), OptionType::String, { "auto"sv, "always"sv }, display_default));
// 6. If prevStyle is "numeric" or "2-digit", then
if (previous_style == "numeric"sv || previous_style == "2-digit"sv) {
@ -332,7 +332,7 @@ ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(VM& vm, Depreca
}
// 7. Return the Record { [[Style]]: style, [[Display]]: display }.
return DurationUnitOptions { .style = move(style), .display = TRY(display.as_string().deprecated_string()) };
return DurationUnitOptions { .style = TRY_OR_THROW_OOM(vm, String::from_utf8(style)), .display = TRY(display.as_string().utf8_string()) };
}
// 1.1.7 PartitionDurationFormatPattern ( durationFormat, duration ), https://tc39.es/proposal-intl-duration-format/#sec-partitiondurationformatpattern

View file

@ -8,7 +8,6 @@
#pragma once
#include <AK/Array.h>
#include <AK/DeprecatedString.h>
#include <AK/String.h>
#include <LibJS/Runtime/Intl/AbstractOperations.h>
#include <LibJS/Runtime/Object.h>
@ -218,14 +217,14 @@ static constexpr AK::Array<DurationInstanceComponent, 10> duration_instances_com
};
struct DurationUnitOptions {
DeprecatedString style;
DeprecatedString display;
String style;
String display;
};
ThrowCompletionOr<Temporal::DurationRecord> to_duration_record(VM&, Value input);
i8 duration_record_sign(Temporal::DurationRecord const&);
bool is_valid_duration_record(Temporal::DurationRecord const&);
ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(VM&, DeprecatedString const& unit, Object const& options, StringView base_style, Span<StringView const> styles_list, StringView digital_base, StringView previous_style);
ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(VM&, String const& unit, Object const& options, StringView base_style, Span<StringView const> styles_list, StringView digital_base, StringView previous_style);
ThrowCompletionOr<Vector<PatternPartition>> partition_duration_format_pattern(VM&, DurationFormat const&, Temporal::DurationRecord const& duration);
}

View file

@ -99,7 +99,7 @@ ThrowCompletionOr<NonnullGCPtr<Object>> DurationFormatConstructor::construct(Fun
duration_format->set_data_locale(move(result.data_locale));
// 16. Let prevStyle be the empty String.
auto previous_style = DeprecatedString::empty();
String previous_style {};
// 17. For each row of Table 1, except the header row, in table order, do
for (auto const& duration_instances_component : duration_instances_components) {
@ -110,7 +110,7 @@ ThrowCompletionOr<NonnullGCPtr<Object>> DurationFormatConstructor::construct(Fun
auto display_slot = duration_instances_component.set_display_slot;
// c. Let unit be the Unit value.
auto unit = duration_instances_component.unit;
auto unit = TRY_OR_THROW_OOM(vm, String::from_utf8(duration_instances_component.unit));
// d. Let valueList be the Values value.
auto value_list = duration_instances_component.values;

View file

@ -8,6 +8,7 @@
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Intl/DurationFormatPrototype.h>
#include <LibJS/Runtime/ThrowableStringBuilder.h>
namespace JS::Intl {
@ -46,16 +47,16 @@ JS_DEFINE_NATIVE_FUNCTION(DurationFormatPrototype::format)
auto parts = MUST_OR_THROW_OOM(partition_duration_format_pattern(vm, *duration_format, record));
// 5. Let result be a new empty String.
StringBuilder result;
ThrowableStringBuilder result(vm);
// 6. For each Record { [[Type]], [[Value]] } part in parts, do
for (auto const& part : parts) {
// a. Set result to the string-concatenation of result and part.[[Value]].
result.append(part.value);
MUST_OR_THROW_OOM(result.append(part.value));
}
// 7. Return result.
return PrimitiveString::create(vm, result.to_deprecated_string());
return PrimitiveString::create(vm, MUST_OR_THROW_OOM(result.to_string()));
}
// 1.4.4 Intl.DurationFormat.prototype.formatToParts ( duration ), https://tc39.es/proposal-intl-duration-format/#sec-Intl.DurationFormat.prototype.formatToParts