LibJS: Implement Temporal.Duration.prototype.subtract

This commit is contained in:
Luke Wilde 2021-11-29 22:33:04 +00:00 committed by Linus Groh
parent ac8c3919cb
commit 2cea4ad508
Notes: sideshowbarker 2024-07-17 23:21:43 +09:00
3 changed files with 111 additions and 0 deletions

View file

@ -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)
{

View file

@ -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);

View file

@ -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"
);
});
});