LibJS: Implement the ECMA-402 PlainMonthDay.prototype.toLocaleString.js

This commit is contained in:
Timothy Flynn 2024-11-27 16:23:39 -05:00 committed by Andreas Kling
parent 697e68e68f
commit bca70584b9
Notes: github-actions[bot] 2024-11-29 08:53:27 +00:00
2 changed files with 15 additions and 8 deletions

View file

@ -5,6 +5,8 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/Intl/DateTimeFormat.h>
#include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
#include <LibJS/Runtime/Temporal/Calendar.h>
#include <LibJS/Runtime/Temporal/PlainDate.h>
#include <LibJS/Runtime/Temporal/PlainMonthDay.h>
@ -152,15 +154,23 @@ JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::to_string)
}
// 10.3.9 Temporal.PlainMonthDay.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.tolocalestring
// NOTE: This is the minimum toLocaleString implementation for engines without ECMA-402.
// 15.12.5.1 Temporal.PlainMonthDay.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sup-temporal.plainmonthday.prototype.tolocalestring
JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::to_locale_string)
{
auto& realm = *vm.current_realm();
auto locales = vm.argument(0);
auto options = vm.argument(1);
// 1. Let monthDay be the this value.
// 2. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]).
auto month_day = TRY(typed_this_object(vm));
// 3. Return TemporalMonthDayToString(monthDay, auto).
return PrimitiveString::create(vm, temporal_month_day_to_string(month_day, ShowCalendar::Auto));
// 3. Let dateFormat be ? CreateDateTimeFormat(%Intl.DateTimeFormat%, locales, options, DATE, DATE).
auto date_format = TRY(Intl::create_date_time_format(vm, realm.intrinsics().intl_date_time_format_constructor(), locales, options, Intl::OptionRequired::Date, Intl::OptionDefaults::Date));
// 4. Return ? FormatDateTime(dateFormat, monthDay).
return PrimitiveString::create(vm, TRY(Intl::format_date_time(vm, date_format, month_day)));
}
// 10.3.10 Temporal.PlainMonthDay.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.tolocalestring

View file

@ -6,11 +6,8 @@ describe("correct behavior", () => {
test("basic functionality", () => {
let plainMonthDay;
plainMonthDay = new Temporal.PlainMonthDay(7, 6);
expect(plainMonthDay.toLocaleString()).toBe("07-06");
plainMonthDay = new Temporal.PlainMonthDay(7, 6, "gregory", 2021);
expect(plainMonthDay.toLocaleString()).toBe("2021-07-06[u-ca=gregory]");
plainMonthDay = new Temporal.PlainMonthDay(7, 6, "gregory");
expect(plainMonthDay.toLocaleString()).toBe("7/6");
});
});