diff options
author | Linus Groh <mail@linusgroh.de> | 2021-07-14 21:16:40 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-07-14 23:50:03 +0100 |
commit | 3ee169d8e776307c79f36522095b23b1af1e5513 (patch) | |
tree | e804865eae2ccc00aca686b42ed7aa64a9344ae7 /Userland/Libraries | |
parent | 83bbbbe567215b809d538cb65c529a77188a48cf (diff) | |
download | serenity-3ee169d8e776307c79f36522095b23b1af1e5513.zip |
LibJS: Implement Temporal.Calendar.prototype.toJSON()
Diffstat (limited to 'Userland/Libraries')
3 files changed, 26 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp index 7f16ead01b..c9328811ab 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp +++ b/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)); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h index aa48b08ada..229e63d749 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h +++ b/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); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.toJSON.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.toJSON.js new file mode 100644 index 0000000000..703283b897 --- /dev/null +++ b/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"); + }); +}); |