From 5904c6bf189354e5a858e170ed1cecaf9e3e63f0 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Thu, 19 Aug 2021 08:42:49 +0100 Subject: LibJS: Implement Temporal.PlainMonthDay.prototype.toLocaleString() --- .../Runtime/Temporal/PlainMonthDayPrototype.cpp | 19 ++++++++++++++++++ .../Runtime/Temporal/PlainMonthDayPrototype.h | 1 + .../PlainMonthDay.prototype.toLocaleString.js | 23 ++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.toLocaleString.js (limited to 'Userland') diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp index fae46668fe..4ac2774571 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp @@ -34,6 +34,7 @@ void PlainMonthDayPrototype::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); } @@ -123,6 +124,24 @@ JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::to_string) return js_string(vm, *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. +JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::to_locale_string) +{ + // 1. Let monthDay be the this value. + // 2. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]). + auto* month_day = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Return ? TemporalMonthDayToString(monthDay, "auto"). + auto string = temporal_month_day_to_string(global_object, *month_day, "auto"sv); + if (vm.exception()) + return {}; + + return js_string(vm, *string); +} + // 10.3.11 Temporal.PlainMonthDay.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.valueof JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::value_of) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h index 698bd297b7..e62aaf3f37 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h @@ -23,6 +23,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(month_code_getter); JS_DECLARE_NATIVE_FUNCTION(day_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/PlainMonthDay/PlainMonthDay.prototype.toLocaleString.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.toLocaleString.js new file mode 100644 index 0000000000..388c4269f1 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.toLocaleString.js @@ -0,0 +1,23 @@ +describe("correct behavior", () => { + test("length is 0", () => { + expect(Temporal.PlainMonthDay.prototype.toLocaleString).toHaveLength(0); + }); + + test("basic functionality", () => { + let plainMonthDay; + + plainMonthDay = new Temporal.PlainMonthDay(7, 6); + expect(plainMonthDay.toLocaleString()).toBe("07-06"); + + plainMonthDay = new Temporal.PlainMonthDay(7, 6, { toString: () => "foo" }, 2021); + expect(plainMonthDay.toLocaleString()).toBe("2021-07-06[u-ca=foo]"); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.PlainMonthDay object", () => { + expect(() => { + Temporal.PlainMonthDay.prototype.toLocaleString.call("foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.PlainMonthDay"); + }); +}); -- cgit v1.2.3