mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 06:27:45 +00:00
LibJS: Implement Temporal.PlainYearMonth.compare
This commit is contained in:
parent
092ec0cecf
commit
90ada407db
3 changed files with 35 additions and 0 deletions
|
@ -1,9 +1,11 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
|
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
|
||||||
|
* Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/TypeCasts.h>
|
||||||
#include <LibJS/Runtime/GlobalObject.h>
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
|
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
|
||||||
#include <LibJS/Runtime/Temporal/Calendar.h>
|
#include <LibJS/Runtime/Temporal/Calendar.h>
|
||||||
|
@ -31,6 +33,7 @@ void PlainYearMonthConstructor::initialize(GlobalObject& global_object)
|
||||||
|
|
||||||
u8 attr = Attribute::Writable | Attribute::Configurable;
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||||
define_native_function(vm.names.from, from, 1, attr);
|
define_native_function(vm.names.from, from, 1, attr);
|
||||||
|
define_native_function(vm.names.compare, compare, 2, attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 9.1.1 Temporal.PlainYearMonth ( isoYear, isoMonth [ , calendarLike [ , referenceISODay ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth
|
// 9.1.1 Temporal.PlainYearMonth ( isoYear, isoMonth [ , calendarLike [ , referenceISODay ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth
|
||||||
|
@ -119,4 +122,21 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthConstructor::from)
|
||||||
return to_temporal_year_month(global_object, item, options);
|
return to_temporal_year_month(global_object, item, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 9.2.3 Temporal.PlainYearMonth.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.compare
|
||||||
|
JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthConstructor::compare)
|
||||||
|
{
|
||||||
|
// 1. Set one to ? ToTemporalYearMonth(one).
|
||||||
|
auto* one = to_temporal_year_month(global_object, vm.argument(0));
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
// 2. Set two to ? ToTemporalYearMonth(one).
|
||||||
|
auto* two = to_temporal_year_month(global_object, vm.argument(1));
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
// 3. Return 𝔽(! CompareISODate(one.[[ISOYear]], one.[[ISOMonth]], one.[[ISODay]], two.[[ISOYear]], two.[[ISOMonth]], two.[[ISODay]])).
|
||||||
|
return Value(compare_iso_date(one->iso_year(), one->iso_month(), one->iso_day(), two->iso_year(), two->iso_month(), two->iso_day()));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@ private:
|
||||||
virtual bool has_constructor() const override { return true; }
|
virtual bool has_constructor() const override { return true; }
|
||||||
|
|
||||||
JS_DECLARE_NATIVE_FUNCTION(from);
|
JS_DECLARE_NATIVE_FUNCTION(from);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(compare);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
describe("correct behavior", () => {
|
||||||
|
test("length is 2", () => {
|
||||||
|
expect(Temporal.PlainYearMonth.compare).toHaveLength(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("basic functionality", () => {
|
||||||
|
const plainYearMonth1 = new Temporal.PlainYearMonth(2021, 8);
|
||||||
|
expect(Temporal.PlainYearMonth.compare(plainYearMonth1, plainYearMonth1)).toBe(0);
|
||||||
|
const plainYearMonth2 = new Temporal.PlainYearMonth(2021, 9);
|
||||||
|
expect(Temporal.PlainYearMonth.compare(plainYearMonth2, plainYearMonth2)).toBe(0);
|
||||||
|
expect(Temporal.PlainYearMonth.compare(plainYearMonth1, plainYearMonth2)).toBe(-1);
|
||||||
|
expect(Temporal.PlainYearMonth.compare(plainYearMonth2, plainYearMonth1)).toBe(1);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue