summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-08-19 00:23:48 +0100
committerLinus Groh <mail@linusgroh.de>2021-08-19 00:23:48 +0100
commit73d888e9e6a6102ec9057fd926706c6f050d4272 (patch)
tree1a32b78469d506b1ecd193bb27748601d1e20629 /Userland/Libraries/LibJS
parent402f04c2fc3de9dcb289130711b3760892e91666 (diff)
downloadserenity-73d888e9e6a6102ec9057fd926706c6f050d4272.zip
LibJS: Implement Temporal.PlainDate.prototype.toLocaleString()
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp18
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.toLocaleString.js23
3 files changed, 42 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp
index e0e448502d..607e4352a3 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp
@@ -52,6 +52,7 @@ void PlainDatePrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.withCalendar, with_calendar, 1, attr);
define_native_function(vm.names.equals, equals, 1, attr);
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);
}
@@ -425,6 +426,23 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_string)
return js_string(vm, *string);
}
+// 3.3.29 Temporal.PlainDate.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.tolocalestring
+JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_locale_string)
+{
+ // 1. Let temporalDate be the this value.
+ // 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
+ auto* temporal_date = typed_this(global_object);
+ if (vm.exception())
+ return {};
+
+ // 3. Return ? TemporalDateToString(temporalDate, "auto").
+ auto string = temporal_date_to_string(global_object, *temporal_date, "auto"sv);
+ if (vm.exception())
+ return {};
+
+ return js_string(vm, *string);
+}
+
// 3.3.31 Temporal.PlainDate.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.valueof
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::value_of)
{
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h
index 6a217700d7..a75534258c 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h
@@ -38,6 +38,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(with_calendar);
JS_DECLARE_NATIVE_FUNCTION(equals);
JS_DECLARE_NATIVE_FUNCTION(to_string);
+ JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
JS_DECLARE_NATIVE_FUNCTION(value_of);
};
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.toLocaleString.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.toLocaleString.js
new file mode 100644
index 0000000000..527e711de2
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.toLocaleString.js
@@ -0,0 +1,23 @@
+describe("correct behavior", () => {
+ test("length is 0", () => {
+ expect(Temporal.PlainDate.prototype.toLocaleString).toHaveLength(0);
+ });
+
+ test("basic functionality", () => {
+ let plainDate;
+
+ plainDate = new Temporal.PlainDate(2021, 7, 6);
+ expect(plainDate.toLocaleString()).toBe("2021-07-06");
+
+ plainDate = new Temporal.PlainDate(2021, 7, 6, { toString: () => "foo" });
+ expect(plainDate.toLocaleString()).toBe("2021-07-06[u-ca=foo]");
+ });
+});
+
+describe("errors", () => {
+ test("this value must be a Temporal.PlainDate object", () => {
+ expect(() => {
+ Temporal.PlainDate.prototype.toLocaleString.call("foo");
+ }).toThrowWithMessage(TypeError, "Not a Temporal.PlainDate");
+ });
+});