Explorar el Código

LibJS: Implement Temporal.TimeZone.prototype.toJSON()

Linus Groh hace 4 años
padre
commit
7f0fe352e2

+ 11 - 0
Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.cpp

@@ -24,6 +24,7 @@ void TimeZonePrototype::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.toJSON, to_json, 0, attr);
 
     // 11.4.2 Temporal.TimeZone.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.timezone.prototype-@@tostringtag
     define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "Temporal.TimeZone"), Attribute::Configurable);
@@ -55,4 +56,14 @@ JS_DEFINE_NATIVE_FUNCTION(TimeZonePrototype::to_string)
     return js_string(vm, time_zone->identifier());
 }
 
+// 11.4.12 Temporal.TimeZone.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.timezone.prototype.tojson
+JS_DEFINE_NATIVE_FUNCTION(TimeZonePrototype::to_json)
+{
+    // 1. Let timeZone be the this value.
+    auto time_zone = vm.this_value(global_object);
+
+    // 2. Return ? ToString(timeZone).
+    return js_string(vm, time_zone.to_string(global_object));
+}
+
 }

+ 1 - 0
Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.h

@@ -20,6 +20,7 @@ public:
 
 private:
     JS_DECLARE_NATIVE_FUNCTION(to_string);
+    JS_DECLARE_NATIVE_FUNCTION(to_json);
 };
 
 }

+ 14 - 0
Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.prototype.toJSON.js

@@ -0,0 +1,14 @@
+describe("correct behavior", () => {
+    test("length is 0", () => {
+        expect(Temporal.TimeZone.prototype.toJSON).toHaveLength(0);
+    });
+
+    test("basic functionality", () => {
+        const timeZone = new Temporal.TimeZone("UTC");
+        expect(timeZone.toJSON()).toBe("UTC");
+    });
+
+    test("works with any this value", () => {
+        expect(Temporal.TimeZone.prototype.toJSON.call("foo")).toBe("foo");
+    });
+});