diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp index 7f49778135..7718a85d8e 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp @@ -428,6 +428,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_string) } // 3.3.29 Temporal.PlainDate.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.tolocalestring +// NOTE: This is the minimum toLocaleString implementation for engines without ECMA-402. JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_locale_string) { // 1. Let temporalDate be the this value. diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp index f045797438..7404d9a522 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp @@ -39,6 +39,7 @@ void PlainYearMonthPrototype::initialize(GlobalObject& global_object) u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.toString, to_string, 0, attr); + define_native_function(vm.names.toLocaleString, to_locale_string, 0, attr); define_native_function(vm.names.valueOf, value_of, 0, attr); define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr); } @@ -208,6 +209,24 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::to_string) return js_string(vm, *string); } +// 9.3.18 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. +JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::to_locale_string) +{ + // 1. Let yearMonth be the this value. + // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). + auto* year_month = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Return ? TemporalYearMonthToString(yearMonth, "auto"). + auto string = temporal_year_month_to_string(global_object, *year_month, "auto"sv); + if (vm.exception()) + return {}; + + return js_string(vm, *string); +} + // 9.3.20 Temporal.PlainYearMonth.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.valueof JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::value_of) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h index 97509e8a86..feb9130be3 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h @@ -28,6 +28,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter); JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter); JS_DECLARE_NATIVE_FUNCTION(to_string); + JS_DECLARE_NATIVE_FUNCTION(to_locale_string); JS_DECLARE_NATIVE_FUNCTION(value_of); JS_DECLARE_NATIVE_FUNCTION(get_iso_fields); }; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.toLocaleString.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.toLocaleString.js new file mode 100644 index 0000000000..ec4879c6e1 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.toLocaleString.js @@ -0,0 +1,23 @@ +describe("correct behavior", () => { + test("length is 0", () => { + expect(Temporal.PlainYearMonth.prototype.toLocaleString).toHaveLength(0); + }); + + test("basic functionality", () => { + let plainYearMonth; + + plainYearMonth = new Temporal.PlainYearMonth(2021, 7); + expect(plainYearMonth.toLocaleString()).toBe("2021-07"); + + plainYearMonth = new Temporal.PlainYearMonth(2021, 7, { toString: () => "foo" }, 6); + expect(plainYearMonth.toLocaleString()).toBe("2021-07-06[u-ca=foo]"); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.PlainYearMonth object", () => { + expect(() => { + Temporal.PlainYearMonth.prototype.toLocaleString.call("foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.PlainYearMonth"); + }); +});