diff options
author | Linus Groh <mail@linusgroh.de> | 2021-11-07 01:46:15 +0000 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-11-07 15:31:28 +0100 |
commit | 90fa356b93bb43fc69042ffb7fc1e4f1e6b9666c (patch) | |
tree | 5fef6bfa368a1cd4b859bae6910e2ade523a3d3e | |
parent | b2548393d2bfed2ecb11bc8f8b598806e37fd4ff (diff) | |
download | serenity-90fa356b93bb43fc69042ffb7fc1e4f1e6b9666c.zip |
LibJS: Implement Temporal.Duration.prototype.toJSON()
3 files changed, 32 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp index 292ca8afab..b2d067a7ba 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp @@ -46,6 +46,7 @@ void DurationPrototype::initialize(GlobalObject& global_object) define_native_function(vm.names.negated, negated, 0, attr); 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.valueOf, value_of, 0, attr); } @@ -311,6 +312,17 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::to_string) return js_string(vm, temporal_duration_to_string(result.years, result.months, result.weeks, result.days, result.hours, result.minutes, result.seconds, result.milliseconds, result.microseconds, result.nanoseconds, precision.precision)); } +// 7.3.23 Temporal.Duration.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.tojson +JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::to_json) +{ + // 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 0444605ae6..f940c316a3 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h @@ -36,6 +36,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(negated); JS_DECLARE_NATIVE_FUNCTION(abs); JS_DECLARE_NATIVE_FUNCTION(to_string); + JS_DECLARE_NATIVE_FUNCTION(to_json); JS_DECLARE_NATIVE_FUNCTION(value_of); }; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.toJSON.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.toJSON.js new file mode 100644 index 0000000000..d324e957d2 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.toJSON.js @@ -0,0 +1,19 @@ +describe("correct behavior", () => { + test("length is 0", () => { + expect(Temporal.Duration.prototype.toJSON).toHaveLength(0); + }); + + test("basic functionality", () => { + expect(new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).toJSON()).toBe( + "P1Y2M3W4DT5H6M7.00800901S" + ); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.Duration object", () => { + expect(() => { + Temporal.Duration.prototype.toJSON.call("foo"); + }).toThrowWithMessage(TypeError, "Not an object of type Temporal.Duration"); + }); +}); |