1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:17:34 +00:00

LibJS: Port Intl.RelativeTimeFormat to String

This commit is contained in:
Timothy Flynn 2023-01-27 15:48:21 -05:00 committed by Linus Groh
parent a824e1ac6a
commit 4c8f7d76c4
2 changed files with 6 additions and 6 deletions

View file

@ -12,6 +12,7 @@
#include <LibJS/Runtime/Intl/NumberFormatConstructor.h> #include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
#include <LibJS/Runtime/Intl/PluralRules.h> #include <LibJS/Runtime/Intl/PluralRules.h>
#include <LibJS/Runtime/Intl/RelativeTimeFormat.h> #include <LibJS/Runtime/Intl/RelativeTimeFormat.h>
#include <LibJS/Runtime/ThrowableStringBuilder.h>
namespace JS::Intl { namespace JS::Intl {
@ -222,22 +223,22 @@ ThrowCompletionOr<Vector<PatternPartitionWithUnit>> make_parts_list(VM& vm, Stri
} }
// 17.5.4 FormatRelativeTime ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-FormatRelativeTime // 17.5.4 FormatRelativeTime ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-FormatRelativeTime
ThrowCompletionOr<DeprecatedString> format_relative_time(VM& vm, RelativeTimeFormat& relative_time_format, double value, StringView unit) ThrowCompletionOr<String> format_relative_time(VM& vm, RelativeTimeFormat& relative_time_format, double value, StringView unit)
{ {
// 1. Let parts be ? PartitionRelativeTimePattern(relativeTimeFormat, value, unit). // 1. Let parts be ? PartitionRelativeTimePattern(relativeTimeFormat, value, unit).
auto parts = TRY(partition_relative_time_pattern(vm, relative_time_format, value, unit)); auto parts = TRY(partition_relative_time_pattern(vm, relative_time_format, value, unit));
// 2. Let result be an empty String. // 2. Let result be an empty String.
StringBuilder result; ThrowableStringBuilder result(vm);
// 3. For each Record { [[Type]], [[Value]], [[Unit]] } part in parts, do // 3. For each Record { [[Type]], [[Value]], [[Unit]] } part in parts, do
for (auto& part : parts) { for (auto& part : parts) {
// a. Set result to the string-concatenation of result and part.[[Value]]. // a. Set result to the string-concatenation of result and part.[[Value]].
result.append(move(part.value)); MUST_OR_THROW_OOM(result.append(part.value));
} }
// 4. Return result. // 4. Return result.
return result.to_deprecated_string(); return result.to_string();
} }
// 17.5.5 FormatRelativeTimeToParts ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-FormatRelativeTimeToParts // 17.5.5 FormatRelativeTimeToParts ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-FormatRelativeTimeToParts

View file

@ -7,7 +7,6 @@
#pragma once #pragma once
#include <AK/Array.h> #include <AK/Array.h>
#include <AK/DeprecatedString.h>
#include <AK/String.h> #include <AK/String.h>
#include <AK/StringView.h> #include <AK/StringView.h>
#include <LibJS/Runtime/Completion.h> #include <LibJS/Runtime/Completion.h>
@ -86,7 +85,7 @@ struct PatternPartitionWithUnit : public PatternPartition {
ThrowCompletionOr<::Locale::TimeUnit> singular_relative_time_unit(VM&, StringView unit); ThrowCompletionOr<::Locale::TimeUnit> singular_relative_time_unit(VM&, StringView unit);
ThrowCompletionOr<Vector<PatternPartitionWithUnit>> partition_relative_time_pattern(VM&, RelativeTimeFormat&, double value, StringView unit); ThrowCompletionOr<Vector<PatternPartitionWithUnit>> partition_relative_time_pattern(VM&, RelativeTimeFormat&, double value, StringView unit);
ThrowCompletionOr<Vector<PatternPartitionWithUnit>> make_parts_list(VM&, StringView pattern, StringView unit, Vector<PatternPartition> parts); ThrowCompletionOr<Vector<PatternPartitionWithUnit>> make_parts_list(VM&, StringView pattern, StringView unit, Vector<PatternPartition> parts);
ThrowCompletionOr<DeprecatedString> format_relative_time(VM&, RelativeTimeFormat&, double value, StringView unit); ThrowCompletionOr<String> format_relative_time(VM&, RelativeTimeFormat&, double value, StringView unit);
ThrowCompletionOr<Array*> format_relative_time_to_parts(VM&, RelativeTimeFormat&, double value, StringView unit); ThrowCompletionOr<Array*> format_relative_time_to_parts(VM&, RelativeTimeFormat&, double value, StringView unit);
} }