mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 07:27:45 +00:00
LibJS: Implement Temporal.PlainDate.prototype.subtract()
This commit is contained in:
parent
bcd96c80f3
commit
654380c2c2
3 changed files with 41 additions and 0 deletions
|
@ -54,6 +54,7 @@ void PlainDatePrototype::initialize(GlobalObject& global_object)
|
|||
define_native_function(vm.names.toPlainMonthDay, to_plain_month_day, 0, attr);
|
||||
define_native_function(vm.names.getISOFields, get_iso_fields, 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.withCalendar, with_calendar, 1, attr);
|
||||
define_native_function(vm.names.equals, equals, 1, attr);
|
||||
define_native_function(vm.names.toPlainDateTime, to_plain_date_time, 0, attr);
|
||||
|
@ -353,6 +354,26 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::add)
|
|||
return TRY(calendar_date_add(global_object, temporal_date->calendar(), *temporal_date, *duration, options));
|
||||
}
|
||||
|
||||
// 3.3.20 Temporal.PlainDate.prototype.subtract ( temporalDurationLike [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.subtract
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::subtract)
|
||||
{
|
||||
// 1. Let temporalDate be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
|
||||
auto* temporal_date = TRY(typed_this_object(global_object));
|
||||
|
||||
// 3. Let duration be ? ToTemporalDuration(temporalDurationLike).
|
||||
auto* duration = TRY(to_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 negatedDuration be ! CreateNegatedTemporalDuration(duration).
|
||||
auto* negated_duration = create_negated_temporal_duration(global_object, *duration);
|
||||
|
||||
// 6. Return ? CalendarDateAdd(temporalDate.[[Calendar]], temporalDate, negatedDuration, options).
|
||||
return TRY(calendar_date_add(global_object, temporal_date->calendar(), *temporal_date, *negated_duration, options));
|
||||
}
|
||||
|
||||
// 3.3.22 Temporal.PlainDate.prototype.withCalendar ( calendar ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.withcalendar
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::with_calendar)
|
||||
{
|
||||
|
|
|
@ -39,6 +39,7 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(to_plain_month_day);
|
||||
JS_DECLARE_NATIVE_FUNCTION(get_iso_fields);
|
||||
JS_DECLARE_NATIVE_FUNCTION(add);
|
||||
JS_DECLARE_NATIVE_FUNCTION(subtract);
|
||||
JS_DECLARE_NATIVE_FUNCTION(with_calendar);
|
||||
JS_DECLARE_NATIVE_FUNCTION(equals);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_plain_date_time);
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Temporal.PlainDate.prototype.subtract).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const plainDate = new Temporal.PlainDate(2021, 7, 6);
|
||||
const result = plainDate.subtract(new Temporal.Duration(51, 6, 0, 5));
|
||||
expect(result.equals(new Temporal.PlainDate(1970, 1, 1))).toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.PlainDate object", () => {
|
||||
expect(() => {
|
||||
Temporal.PlainDate.prototype.subtract.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainDate");
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue