浏览代码

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

Linus Groh 4 年之前
父节点
当前提交
3ee169d8e7

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

@@ -27,6 +27,7 @@ void CalendarPrototype::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);
 }
 
 static Calendar* typed_this(GlobalObject& global_object)
@@ -55,4 +56,14 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::to_string)
     return js_string(vm, calendar->identifier());
 }
 
+// 12.4.24 Temporal.Calendar.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.tojson
+JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::to_json)
+{
+    // 1. Let calendar be the this value.
+    auto calendar = vm.this_value(global_object);
+
+    // 2. Return ? ToString(calendar).
+    return js_string(vm, calendar.to_string(global_object));
+}
+
 }

+ 1 - 0
Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.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/Calendar/Calendar.prototype.toJSON.js

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