mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 08:37:35 +00:00
LibJS: Implement Intl.DateTimeFormat.prototype.formatRangeToParts
This commit is contained in:
parent
04f8fb07e1
commit
53df13fed7
6 changed files with 569 additions and 0 deletions
|
@ -180,6 +180,7 @@ namespace JS {
|
|||
P(format) \
|
||||
P(formatMatcher) \
|
||||
P(formatRange) \
|
||||
P(formatRangeToParts) \
|
||||
P(formatToParts) \
|
||||
P(fractionalSecondDigits) \
|
||||
P(freeze) \
|
||||
|
|
|
@ -1404,6 +1404,45 @@ ThrowCompletionOr<String> format_date_time_range(GlobalObject& global_object, Da
|
|||
return result.build();
|
||||
}
|
||||
|
||||
// 11.1.13 FormatDateTimeRangeToParts ( dateTimeFormat, x, y ), https://tc39.es/ecma402/#sec-formatdatetimerangetoparts
|
||||
ThrowCompletionOr<Array*> format_date_time_range_to_parts(GlobalObject& global_object, DateTimeFormat& date_time_format, Value start, Value end)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
// 1. Let parts be ? PartitionDateTimeRangePattern(dateTimeFormat, x, y).
|
||||
auto parts = TRY(partition_date_time_range_pattern(global_object, date_time_format, start, end));
|
||||
|
||||
// 2. Let result be ArrayCreate(0).
|
||||
auto* result = MUST(Array::create(global_object, 0));
|
||||
|
||||
// 3. Let n be 0.
|
||||
size_t n = 0;
|
||||
|
||||
// 4. For each Record { [[Type]], [[Value]], [[Source]] } part in parts, do
|
||||
for (auto& part : parts) {
|
||||
// a. Let O be OrdinaryObjectCreate(%ObjectPrototype%).
|
||||
auto* object = Object::create(global_object, global_object.object_prototype());
|
||||
|
||||
// b. Perform ! CreateDataPropertyOrThrow(O, "type", part.[[Type]]).
|
||||
MUST(object->create_data_property_or_throw(vm.names.type, js_string(vm, part.type)));
|
||||
|
||||
// c. Perform ! CreateDataPropertyOrThrow(O, "value", part.[[Value]]).
|
||||
MUST(object->create_data_property_or_throw(vm.names.value, js_string(vm, move(part.value))));
|
||||
|
||||
// d. Perform ! CreateDataPropertyOrThrow(O, "source", part.[[Source]]).
|
||||
MUST(object->create_data_property_or_throw(vm.names.source, js_string(vm, part.source)));
|
||||
|
||||
// e. Perform ! CreateDataProperty(result, ! ToString(n), O).
|
||||
MUST(result->create_data_property_or_throw(n, object));
|
||||
|
||||
// f. Increment n by 1.
|
||||
++n;
|
||||
}
|
||||
|
||||
// 5. Return result.
|
||||
return result;
|
||||
}
|
||||
|
||||
// 11.1.14 ToLocalTime ( t, calendar, timeZone ), https://tc39.es/ecma402/#sec-tolocaltime
|
||||
ThrowCompletionOr<LocalTime> to_local_time(GlobalObject& global_object, double time, StringView calendar, [[maybe_unused]] StringView time_zone)
|
||||
{
|
||||
|
|
|
@ -205,6 +205,7 @@ ThrowCompletionOr<String> format_date_time(GlobalObject& global_object, DateTime
|
|||
ThrowCompletionOr<Array*> format_date_time_to_parts(GlobalObject& global_object, DateTimeFormat& date_time_format, Value time);
|
||||
ThrowCompletionOr<Vector<PatternPartitionWithSource>> partition_date_time_range_pattern(GlobalObject& global_object, DateTimeFormat& date_time_format, Value start, Value end);
|
||||
ThrowCompletionOr<String> format_date_time_range(GlobalObject& global_object, DateTimeFormat& date_time_format, Value start, Value end);
|
||||
ThrowCompletionOr<Array*> format_date_time_range_to_parts(GlobalObject& global_object, DateTimeFormat& date_time_format, Value start, Value end);
|
||||
ThrowCompletionOr<LocalTime> to_local_time(GlobalObject& global_object, double time, StringView calendar, StringView time_zone);
|
||||
|
||||
template<typename Callback>
|
||||
|
|
|
@ -33,6 +33,7 @@ void DateTimeFormatPrototype::initialize(GlobalObject& global_object)
|
|||
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||
define_native_function(vm.names.formatToParts, format_to_parts, 1, attr);
|
||||
define_native_function(vm.names.formatRange, format_range, 2, attr);
|
||||
define_native_function(vm.names.formatRangeToParts, format_range_to_parts, 2, attr);
|
||||
define_native_function(vm.names.resolvedOptions, resolved_options, 0, attr);
|
||||
}
|
||||
|
||||
|
@ -110,6 +111,32 @@ JS_DEFINE_NATIVE_FUNCTION(DateTimeFormatPrototype::format_range)
|
|||
return js_string(vm, move(formatted));
|
||||
}
|
||||
|
||||
// 11.4.6 Intl.DateTimeFormat.prototype.formatRangeToParts ( startDate, endDate ), https://tc39.es/ecma402/#sec-Intl.DateTimeFormat.prototype.formatRangeToParts
|
||||
JS_DEFINE_NATIVE_FUNCTION(DateTimeFormatPrototype::format_range_to_parts)
|
||||
{
|
||||
auto start_date = vm.argument(0);
|
||||
auto end_date = vm.argument(1);
|
||||
|
||||
// 1. Let dtf be this value.
|
||||
// 2. Perform ? RequireInternalSlot(dtf, [[InitializedDateTimeFormat]]).
|
||||
auto* date_time_format = TRY(typed_this_object(global_object));
|
||||
|
||||
// 3. If startDate is undefined or endDate is undefined, throw a TypeError exception.
|
||||
if (start_date.is_undefined())
|
||||
return vm.throw_completion<TypeError>(global_object, ErrorType::IsUndefined, "startDate"sv);
|
||||
if (end_date.is_undefined())
|
||||
return vm.throw_completion<TypeError>(global_object, ErrorType::IsUndefined, "endDate"sv);
|
||||
|
||||
// 4. Let x be ? ToNumber(startDate).
|
||||
start_date = TRY(start_date.to_number(global_object));
|
||||
|
||||
// 5. Let y be ? ToNumber(endDate).
|
||||
end_date = TRY(end_date.to_number(global_object));
|
||||
|
||||
// 6. Return ? FormatDateTimeRangeToParts(dtf, x, y).
|
||||
return TRY(format_date_time_range_to_parts(global_object, *date_time_format, start_date, end_date));
|
||||
}
|
||||
|
||||
// 11.4.7 Intl.DateTimeFormat.prototype.resolvedOptions ( ), https://tc39.es/ecma402/#sec-intl.datetimeformat.prototype.resolvedoptions
|
||||
JS_DEFINE_NATIVE_FUNCTION(DateTimeFormatPrototype::resolved_options)
|
||||
{
|
||||
|
|
|
@ -23,6 +23,7 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(format);
|
||||
JS_DECLARE_NATIVE_FUNCTION(format_to_parts);
|
||||
JS_DECLARE_NATIVE_FUNCTION(format_range);
|
||||
JS_DECLARE_NATIVE_FUNCTION(format_range_to_parts);
|
||||
JS_DECLARE_NATIVE_FUNCTION(resolved_options);
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue