summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS
diff options
context:
space:
mode:
authorLuke Wilde <lukew@serenityos.org>2021-11-29 22:33:04 +0000
committerLinus Groh <mail@linusgroh.de>2021-11-29 22:56:35 +0000
commit2cea4ad50867d0b7275debad7fef2132716d9512 (patch)
tree39214e2f21b5fc3d7584be8bc17609ccb6ff6e8e /Userland/Libraries/LibJS
parentac8c3919cb964dc8e97ecb04fe8149c9fe1469f8 (diff)
downloadserenity-2cea4ad50867d0b7275debad7fef2132716d9512.zip
LibJS: Implement Temporal.Duration.prototype.subtract
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp24
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.subtract.js86
3 files changed, 111 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp
index 6f862b4dcc..8576d04083 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.negated, negated, 0, attr);
define_native_function(vm.names.abs, abs, 0, attr);
define_native_function(vm.names.add, add, 1, attr);
+ define_native_function(vm.names.subtract, subtract, 1, attr);
define_native_function(vm.names.round, round, 1, attr);
define_native_function(vm.names.total, total, 1, attr);
define_native_function(vm.names.toString, to_string, 0, attr);
@@ -313,6 +314,29 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::add)
return TRY(create_temporal_duration(global_object, result.years, result.months, result.weeks, result.days, result.hours, result.minutes, result.seconds, result.milliseconds, result.microseconds, result.nanoseconds));
}
+// 7.3.19 Temporal.Duration.prototype.subtract ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.subtract
+JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::subtract)
+{
+ // 1. Let duration be the this value.
+ // 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
+ auto* duration = TRY(typed_this_object(global_object));
+
+ // 3. Set other to ? ToLimitedTemporalDuration(other, « »).
+ auto other = TRY(to_limited_temporal_duration(global_object, vm.argument(0), {}));
+
+ // 4. Set options to ? GetOptionsObject(options).
+ auto* options = TRY(get_options_object(global_object, vm.argument(1)));
+
+ // 5. Let relativeTo be ? ToRelativeTemporalObject(options).
+ auto relative_to = TRY(to_relative_temporal_object(global_object, *options));
+
+ // 6. Let result be ? AddDuration(duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]], −other.[[Years]], −other.[[Months]], −other.[[Weeks]], −other.[[Days]], −other.[[Hours]], −other.[[Minutes]], −other.[[Seconds]], −other.[[Milliseconds]], −other.[[Microseconds]], −other.[[Nanoseconds]], relativeTo).
+ auto result = TRY(add_duration(global_object, duration->years(), duration->months(), duration->weeks(), duration->days(), duration->hours(), duration->minutes(), duration->seconds(), duration->milliseconds(), duration->microseconds(), duration->nanoseconds(), -other.years, -other.months, -other.weeks, -other.days, -other.hours, -other.minutes, -other.seconds, -other.milliseconds, -other.microseconds, -other.nanoseconds, relative_to));
+
+ // 7. Return ? CreateTemporalDuration(result.[[Years]], result.[[Months]], result.[[Weeks]], result.[[Days]], result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]]).
+ return TRY(create_temporal_duration(global_object, result.years, result.months, result.weeks, result.days, result.hours, result.minutes, result.seconds, result.milliseconds, result.microseconds, result.nanoseconds));
+}
+
// 7.3.20 Temporal.Duration.prototype.round ( roundTo ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.round
JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::round)
{
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h
index ece335210a..e29f06ee61 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(add);
+ JS_DECLARE_NATIVE_FUNCTION(subtract);
JS_DECLARE_NATIVE_FUNCTION(round);
JS_DECLARE_NATIVE_FUNCTION(total);
JS_DECLARE_NATIVE_FUNCTION(to_string);
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.subtract.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.subtract.js
new file mode 100644
index 0000000000..8b9f473fb4
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.subtract.js
@@ -0,0 +1,86 @@
+describe("correct behavior", () => {
+ test("length is 1", () => {
+ expect(Temporal.Duration.prototype.subtract).toHaveLength(1);
+ });
+
+ function checkCommonResults(durationResult) {
+ expect(durationResult.years).toBe(0);
+ expect(durationResult.months).toBe(0);
+ expect(durationResult.weeks).toBe(0);
+ expect(durationResult.days).toBe(1);
+ expect(durationResult.hours).toBe(1);
+ expect(durationResult.minutes).toBe(1);
+ expect(durationResult.seconds).toBe(1);
+ expect(durationResult.milliseconds).toBe(1);
+ expect(durationResult.microseconds).toBe(1);
+ expect(durationResult.nanoseconds).toBe(1);
+ }
+
+ test("basic functionality", () => {
+ const duration = new Temporal.Duration(0, 0, 0, 2, 2, 2, 2, 2, 2, 2);
+ const oneDuration = new Temporal.Duration(0, 0, 0, 1, 1, 1, 1, 1, 1, 1);
+ const durationResult = duration.subtract(oneDuration);
+
+ checkCommonResults(durationResult);
+ });
+
+ test("from duration-like", () => {
+ const duration = new Temporal.Duration(0, 0, 0, 2, 2, 2, 2, 2, 2, 2);
+ const oneDuration = {
+ days: 1,
+ hours: 1,
+ minutes: 1,
+ seconds: 1,
+ milliseconds: 1,
+ microseconds: 1,
+ nanoseconds: 1,
+ };
+ const durationResult = duration.subtract(oneDuration);
+
+ checkCommonResults(durationResult);
+ });
+
+ test("from string", () => {
+ const duration = new Temporal.Duration(0, 0, 0, 2, 2, 2, 2, 2, 2, 2);
+ const oneDuration = "P1DT1H1M1.001001001S";
+ const durationResult = duration.subtract(oneDuration);
+
+ checkCommonResults(durationResult);
+ });
+});
+
+describe("errors", () => {
+ test("this value must be a Temporal.Duration object", () => {
+ expect(() => {
+ Temporal.Duration.prototype.subtract.call("foo");
+ }).toThrowWithMessage(TypeError, "Not an object of type Temporal.Duration");
+ });
+
+ test("relativeTo is required when duration has calendar units", () => {
+ const yearDuration = new Temporal.Duration(1);
+ const monthDuration = new Temporal.Duration(0, 1);
+ const weekDuration = new Temporal.Duration(0, 0, 1);
+ const durationToSubtract = { seconds: 1 };
+
+ expect(() => {
+ yearDuration.subtract(durationToSubtract);
+ }).toThrowWithMessage(
+ RangeError,
+ "A starting point is required for balancing year, month or week"
+ );
+
+ expect(() => {
+ monthDuration.subtract(durationToSubtract);
+ }).toThrowWithMessage(
+ RangeError,
+ "A starting point is required for balancing year, month or week"
+ );
+
+ expect(() => {
+ weekDuration.subtract(durationToSubtract);
+ }).toThrowWithMessage(
+ RangeError,
+ "A starting point is required for balancing year, month or week"
+ );
+ });
+});