From ac0292b18fceaae6c2938f44fe0ec66c535e7b86 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 27 Nov 2024 16:27:08 -0500 Subject: [PATCH] LibJS: Implement the ECMA-402 PlainYearMonth.prototype.toLocaleString.js --- .../Runtime/Temporal/PlainYearMonthPrototype.cpp | 16 +++++++++++++--- .../PlainYearMonth.prototype.toLocaleString.js | 7 ++----- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp b/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp index 7f818b1c0f4..a429198ae82 100644 --- a/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp +++ b/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp @@ -5,6 +5,8 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include +#include #include #include #include @@ -270,15 +272,23 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::to_string) } // 9.3.20 Temporal.PlainYearMonth.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.tolocalestring -// NOTE: This is the minimum toLocaleString implementation for engines without ECMA-402. +// 15.12.7.1 Temporal.PlainYearMonth.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sup-temporal.plainyearmonth.prototype.tolocalestring JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::to_locale_string) { + auto& realm = *vm.current_realm(); + + auto locales = vm.argument(0); + auto options = vm.argument(1); + // 1. Let yearMonth be the this value. // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). auto year_month = TRY(typed_this_object(vm)); - // 3. Return TemporalYearMonthToString(yearMonth, AUTO). - return PrimitiveString::create(vm, temporal_year_month_to_string(year_month, 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, yearMonth). + return PrimitiveString::create(vm, TRY(Intl::format_date_time(vm, date_format, year_month))); } // 9.3.21 Temporal.PlainYearMonth.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.tojson diff --git a/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.toLocaleString.js b/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.toLocaleString.js index 4fb4aa16bf4..942a24a5cec 100644 --- a/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.toLocaleString.js +++ b/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.toLocaleString.js @@ -6,11 +6,8 @@ describe("correct behavior", () => { test("basic functionality", () => { let plainYearMonth; - plainYearMonth = new Temporal.PlainYearMonth(2021, 7); - expect(plainYearMonth.toLocaleString()).toBe("2021-07"); - - plainYearMonth = new Temporal.PlainYearMonth(2021, 7, "gregory", 6); - expect(plainYearMonth.toLocaleString()).toBe("2021-07-06[u-ca=gregory]"); + plainYearMonth = new Temporal.PlainYearMonth(2021, 7, "gregory"); + expect(plainYearMonth.toLocaleString()).toBe("7/2021"); }); });