summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp13
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.toLocaleString.js19
3 files changed, 33 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp
index b2d067a7ba..02c91a6963 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp
@@ -47,6 +47,7 @@ void DurationPrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.abs, abs, 0, attr);
define_native_function(vm.names.toString, to_string, 0, attr);
define_native_function(vm.names.toJSON, to_json, 0, attr);
+ define_native_function(vm.names.toLocaleString, to_locale_string, 0, attr);
define_native_function(vm.names.valueOf, value_of, 0, attr);
}
@@ -323,6 +324,18 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::to_json)
return js_string(vm, temporal_duration_to_string(duration->years(), duration->months(), duration->weeks(), duration->days(), duration->hours(), duration->minutes(), duration->seconds(), duration->milliseconds(), duration->microseconds(), duration->nanoseconds(), "auto"sv));
}
+// 7.3.24 Temporal.Duration.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.tolocalestring
+// NOTE: This is the minimum toLocaleString implementation for engines without ECMA-402.
+JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::to_locale_string)
+{
+ // 1. Let duration be the this value.
+ // 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
+ auto* duration = TRY(typed_this_object(global_object));
+
+ // 3. Return ! TemporalDurationToString(duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]], "auto").
+ return js_string(vm, temporal_duration_to_string(duration->years(), duration->months(), duration->weeks(), duration->days(), duration->hours(), duration->minutes(), duration->seconds(), duration->milliseconds(), duration->microseconds(), duration->nanoseconds(), "auto"sv));
+}
+
// 7.3.25 Temporal.Duration.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.valueof
JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::value_of)
{
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h
index f940c316a3..ef030254d5 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h
@@ -37,6 +37,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(abs);
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(to_json);
+ JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
JS_DECLARE_NATIVE_FUNCTION(value_of);
};
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.toLocaleString.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.toLocaleString.js
new file mode 100644
index 0000000000..189c3b6846
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.toLocaleString.js
@@ -0,0 +1,19 @@
+describe("correct behavior", () => {
+ test("length is 0", () => {
+ expect(Temporal.Duration.prototype.toLocaleString).toHaveLength(0);
+ });
+
+ test("basic functionality", () => {
+ expect(new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).toLocaleString()).toBe(
+ "P1Y2M3W4DT5H6M7.00800901S"
+ );
+ });
+});
+
+describe("errors", () => {
+ test("this value must be a Temporal.Duration object", () => {
+ expect(() => {
+ Temporal.Duration.prototype.toLocaleString.call("foo");
+ }).toThrowWithMessage(TypeError, "Not an object of type Temporal.Duration");
+ });
+});