mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-20 19:45:12 +00:00
LibJS: Implement the ECMA-402 PlainMonthDay.prototype.toLocaleString.js
This commit is contained in:
parent
697e68e68f
commit
bca70584b9
Notes:
github-actions[bot]
2024-11-29 08:53:27 +00:00
Author: https://github.com/trflynn89 Commit: https://github.com/LadybirdBrowser/ladybird/commit/bca70584b92 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2615
2 changed files with 15 additions and 8 deletions
|
@ -5,6 +5,8 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibJS/Runtime/Intl/DateTimeFormat.h>
|
||||
#include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
|
||||
#include <LibJS/Runtime/Temporal/Calendar.h>
|
||||
#include <LibJS/Runtime/Temporal/PlainDate.h>
|
||||
#include <LibJS/Runtime/Temporal/PlainMonthDay.h>
|
||||
|
@ -152,15 +154,23 @@ JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::to_string)
|
|||
}
|
||||
|
||||
// 10.3.9 Temporal.PlainMonthDay.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.tolocalestring
|
||||
// NOTE: This is the minimum toLocaleString implementation for engines without ECMA-402.
|
||||
// 15.12.5.1 Temporal.PlainMonthDay.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sup-temporal.plainmonthday.prototype.tolocalestring
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::to_locale_string)
|
||||
{
|
||||
auto& realm = *vm.current_realm();
|
||||
|
||||
auto locales = vm.argument(0);
|
||||
auto options = vm.argument(1);
|
||||
|
||||
// 1. Let monthDay be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]).
|
||||
auto month_day = TRY(typed_this_object(vm));
|
||||
|
||||
// 3. Return TemporalMonthDayToString(monthDay, auto).
|
||||
return PrimitiveString::create(vm, temporal_month_day_to_string(month_day, ShowCalendar::Auto));
|
||||
// 3. Let dateFormat be ? CreateDateTimeFormat(%Intl.DateTimeFormat%, locales, options, DATE, DATE).
|
||||
auto date_format = TRY(Intl::create_date_time_format(vm, realm.intrinsics().intl_date_time_format_constructor(), locales, options, Intl::OptionRequired::Date, Intl::OptionDefaults::Date));
|
||||
|
||||
// 4. Return ? FormatDateTime(dateFormat, monthDay).
|
||||
return PrimitiveString::create(vm, TRY(Intl::format_date_time(vm, date_format, month_day)));
|
||||
}
|
||||
|
||||
// 10.3.10 Temporal.PlainMonthDay.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.tolocalestring
|
||||
|
|
|
@ -6,11 +6,8 @@ describe("correct behavior", () => {
|
|||
test("basic functionality", () => {
|
||||
let plainMonthDay;
|
||||
|
||||
plainMonthDay = new Temporal.PlainMonthDay(7, 6);
|
||||
expect(plainMonthDay.toLocaleString()).toBe("07-06");
|
||||
|
||||
plainMonthDay = new Temporal.PlainMonthDay(7, 6, "gregory", 2021);
|
||||
expect(plainMonthDay.toLocaleString()).toBe("2021-07-06[u-ca=gregory]");
|
||||
plainMonthDay = new Temporal.PlainMonthDay(7, 6, "gregory");
|
||||
expect(plainMonthDay.toLocaleString()).toBe("7/6");
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue