mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 09:07:35 +00:00
LibJS: Implement Temporal.Duration.prototype.abs()
This commit is contained in:
parent
7df47bf3fb
commit
86c6e68431
3 changed files with 69 additions and 0 deletions
|
@ -7,6 +7,7 @@
|
||||||
#include <LibJS/Runtime/GlobalObject.h>
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
#include <LibJS/Runtime/Temporal/Duration.h>
|
#include <LibJS/Runtime/Temporal/Duration.h>
|
||||||
#include <LibJS/Runtime/Temporal/DurationPrototype.h>
|
#include <LibJS/Runtime/Temporal/DurationPrototype.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
namespace JS::Temporal {
|
namespace JS::Temporal {
|
||||||
|
|
||||||
|
@ -41,6 +42,7 @@ void DurationPrototype::initialize(GlobalObject& global_object)
|
||||||
u8 attr = Attribute::Writable | Attribute::Configurable;
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||||
define_native_function(vm.names.with, with, 1, attr);
|
define_native_function(vm.names.with, with, 1, attr);
|
||||||
define_native_function(vm.names.negated, negated, 0, attr);
|
define_native_function(vm.names.negated, negated, 0, attr);
|
||||||
|
define_native_function(vm.names.abs, abs, 0, attr);
|
||||||
define_native_function(vm.names.valueOf, value_of, 0, attr);
|
define_native_function(vm.names.valueOf, value_of, 0, attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -311,6 +313,19 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::negated)
|
||||||
return create_temporal_duration(global_object, -duration->years(), -duration->months(), -duration->weeks(), -duration->days(), -duration->hours(), -duration->minutes(), -duration->seconds(), -duration->milliseconds(), -duration->microseconds(), -duration->nanoseconds());
|
return create_temporal_duration(global_object, -duration->years(), -duration->months(), -duration->weeks(), -duration->days(), -duration->hours(), -duration->minutes(), -duration->seconds(), -duration->milliseconds(), -duration->microseconds(), -duration->nanoseconds());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 7.3.17 Temporal.Duration.prototype.abs ( ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.abs
|
||||||
|
JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::abs)
|
||||||
|
{
|
||||||
|
// 1. Let duration be the this value.
|
||||||
|
// 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
|
||||||
|
auto* duration = typed_this(global_object);
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
// 3. Return ? CreateTemporalDuration(abs(duration.[[Years]]), abs(duration.[[Months]]), abs(duration.[[Weeks]]), abs(duration.[[Days]]), abs(duration.[[Hours]]), abs(duration.[[Minutes]]), abs(duration.[[Seconds]]), abs(duration.[[Milliseconds]]), abs(duration.[[Microseconds]]), abs(duration.[[Nanoseconds]])).
|
||||||
|
return create_temporal_duration(global_object, fabs(duration->years()), fabs(duration->months()), fabs(duration->weeks()), fabs(duration->days()), fabs(duration->hours()), fabs(duration->minutes()), fabs(duration->seconds()), fabs(duration->milliseconds()), fabs(duration->microseconds()), fabs(duration->nanoseconds()));
|
||||||
|
}
|
||||||
|
|
||||||
// 7.3.25 Temporal.Duration.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.valueof
|
// 7.3.25 Temporal.Duration.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.valueof
|
||||||
JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::value_of)
|
JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::value_of)
|
||||||
{
|
{
|
||||||
|
|
|
@ -33,6 +33,7 @@ private:
|
||||||
JS_DECLARE_NATIVE_FUNCTION(blank_getter);
|
JS_DECLARE_NATIVE_FUNCTION(blank_getter);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(with);
|
JS_DECLARE_NATIVE_FUNCTION(with);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(negated);
|
JS_DECLARE_NATIVE_FUNCTION(negated);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(abs);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(value_of);
|
JS_DECLARE_NATIVE_FUNCTION(value_of);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,53 @@
|
||||||
|
const DURATION_PROPERTIES = [
|
||||||
|
"years",
|
||||||
|
"months",
|
||||||
|
"weeks",
|
||||||
|
"days",
|
||||||
|
"hours",
|
||||||
|
"minutes",
|
||||||
|
"seconds",
|
||||||
|
"milliseconds",
|
||||||
|
"microseconds",
|
||||||
|
"nanoseconds",
|
||||||
|
];
|
||||||
|
|
||||||
|
describe("correct behavior", () => {
|
||||||
|
test("length is 0", () => {
|
||||||
|
expect(Temporal.Duration.prototype.abs).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("basic functionality", () => {
|
||||||
|
let absoluteDuration;
|
||||||
|
|
||||||
|
absoluteDuration = new Temporal.Duration(123).abs();
|
||||||
|
expect(absoluteDuration.years).toBe(123);
|
||||||
|
|
||||||
|
absoluteDuration = new Temporal.Duration(-123).abs();
|
||||||
|
expect(absoluteDuration.years).toBe(123);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("each property is made absolute", () => {
|
||||||
|
let values;
|
||||||
|
let duration;
|
||||||
|
|
||||||
|
values = Array(DURATION_PROPERTIES.length).fill(-1);
|
||||||
|
duration = new Temporal.Duration(...values).abs();
|
||||||
|
for (const property of DURATION_PROPERTIES) {
|
||||||
|
expect(duration[property]).toBe(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
values = Array(DURATION_PROPERTIES.length).fill(1);
|
||||||
|
duration = new Temporal.Duration(...values).abs();
|
||||||
|
for (const property of DURATION_PROPERTIES) {
|
||||||
|
expect(duration[property]).toBe(1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("errors", () => {
|
||||||
|
test("this value must be a Temporal.Duration object", () => {
|
||||||
|
expect(() => {
|
||||||
|
Temporal.Duration.prototype.abs.call("foo");
|
||||||
|
}).toThrowWithMessage(TypeError, "Not a Temporal.Duration");
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue