Skip to content

Commit 2cea4ad

Browse files
Lubrsilinusg
authored andcommitted
LibJS: Implement Temporal.Duration.prototype.subtract
1 parent ac8c391 commit 2cea4ad

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ void DurationPrototype::initialize(GlobalObject& global_object)
4747
define_native_function(vm.names.negated, negated, 0, attr);
4848
define_native_function(vm.names.abs, abs, 0, attr);
4949
define_native_function(vm.names.add, add, 1, attr);
50+
define_native_function(vm.names.subtract, subtract, 1, attr);
5051
define_native_function(vm.names.round, round, 1, attr);
5152
define_native_function(vm.names.total, total, 1, attr);
5253
define_native_function(vm.names.toString, to_string, 0, attr);
@@ -313,6 +314,29 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::add)
313314
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));
314315
}
315316

317+
// 7.3.19 Temporal.Duration.prototype.subtract ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.subtract
318+
JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::subtract)
319+
{
320+
// 1. Let duration be the this value.
321+
// 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
322+
auto* duration = TRY(typed_this_object(global_object));
323+
324+
// 3. Set other to ? ToLimitedTemporalDuration(other, « »).
325+
auto other = TRY(to_limited_temporal_duration(global_object, vm.argument(0), {}));
326+
327+
// 4. Set options to ? GetOptionsObject(options).
328+
auto* options = TRY(get_options_object(global_object, vm.argument(1)));
329+
330+
// 5. Let relativeTo be ? ToRelativeTemporalObject(options).
331+
auto relative_to = TRY(to_relative_temporal_object(global_object, *options));
332+
333+
// 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).
334+
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));
335+
336+
// 7. Return ? CreateTemporalDuration(result.[[Years]], result.[[Months]], result.[[Weeks]], result.[[Days]], result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]]).
337+
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));
338+
}
339+
316340
// 7.3.20 Temporal.Duration.prototype.round ( roundTo ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.round
317341
JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::round)
318342
{

Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class DurationPrototype final : public PrototypeObject<DurationPrototype, Durati
3636
JS_DECLARE_NATIVE_FUNCTION(negated);
3737
JS_DECLARE_NATIVE_FUNCTION(abs);
3838
JS_DECLARE_NATIVE_FUNCTION(add);
39+
JS_DECLARE_NATIVE_FUNCTION(subtract);
3940
JS_DECLARE_NATIVE_FUNCTION(round);
4041
JS_DECLARE_NATIVE_FUNCTION(total);
4142
JS_DECLARE_NATIVE_FUNCTION(to_string);
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
describe("correct behavior", () => {
2+
test("length is 1", () => {
3+
expect(Temporal.Duration.prototype.subtract).toHaveLength(1);
4+
});
5+
6+
function checkCommonResults(durationResult) {
7+
expect(durationResult.years).toBe(0);
8+
expect(durationResult.months).toBe(0);
9+
expect(durationResult.weeks).toBe(0);
10+
expect(durationResult.days).toBe(1);
11+
expect(durationResult.hours).toBe(1);
12+
expect(durationResult.minutes).toBe(1);
13+
expect(durationResult.seconds).toBe(1);
14+
expect(durationResult.milliseconds).toBe(1);
15+
expect(durationResult.microseconds).toBe(1);
16+
expect(durationResult.nanoseconds).toBe(1);
17+
}
18+
19+
test("basic functionality", () => {
20+
const duration = new Temporal.Duration(0, 0, 0, 2, 2, 2, 2, 2, 2, 2);
21+
const oneDuration = new Temporal.Duration(0, 0, 0, 1, 1, 1, 1, 1, 1, 1);
22+
const durationResult = duration.subtract(oneDuration);
23+
24+
checkCommonResults(durationResult);
25+
});
26+
27+
test("from duration-like", () => {
28+
const duration = new Temporal.Duration(0, 0, 0, 2, 2, 2, 2, 2, 2, 2);
29+
const oneDuration = {
30+
days: 1,
31+
hours: 1,
32+
minutes: 1,
33+
seconds: 1,
34+
milliseconds: 1,
35+
microseconds: 1,
36+
nanoseconds: 1,
37+
};
38+
const durationResult = duration.subtract(oneDuration);
39+
40+
checkCommonResults(durationResult);
41+
});
42+
43+
test("from string", () => {
44+
const duration = new Temporal.Duration(0, 0, 0, 2, 2, 2, 2, 2, 2, 2);
45+
const oneDuration = "P1DT1H1M1.001001001S";
46+
const durationResult = duration.subtract(oneDuration);
47+
48+
checkCommonResults(durationResult);
49+
});
50+
});
51+
52+
describe("errors", () => {
53+
test("this value must be a Temporal.Duration object", () => {
54+
expect(() => {
55+
Temporal.Duration.prototype.subtract.call("foo");
56+
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.Duration");
57+
});
58+
59+
test("relativeTo is required when duration has calendar units", () => {
60+
const yearDuration = new Temporal.Duration(1);
61+
const monthDuration = new Temporal.Duration(0, 1);
62+
const weekDuration = new Temporal.Duration(0, 0, 1);
63+
const durationToSubtract = { seconds: 1 };
64+
65+
expect(() => {
66+
yearDuration.subtract(durationToSubtract);
67+
}).toThrowWithMessage(
68+
RangeError,
69+
"A starting point is required for balancing year, month or week"
70+
);
71+
72+
expect(() => {
73+
monthDuration.subtract(durationToSubtract);
74+
}).toThrowWithMessage(
75+
RangeError,
76+
"A starting point is required for balancing year, month or week"
77+
);
78+
79+
expect(() => {
80+
weekDuration.subtract(durationToSubtract);
81+
}).toThrowWithMessage(
82+
RangeError,
83+
"A starting point is required for balancing year, month or week"
84+
);
85+
});
86+
});

0 commit comments

Comments
 (0)