mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-08 09:09:43 +00:00
LibJS: Implement the ECMA-402 PlainDate.prototype.toLocaleString.js
This commit is contained in:
parent
964f41bb53
commit
697e68e68f
Notes:
github-actions[bot]
2024-11-29 08:53:34 +00:00
Author: https://github.com/trflynn89
Commit: 697e68e68f
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2615
2 changed files with 14 additions and 7 deletions
|
@ -6,6 +6,8 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <LibJS/Runtime/Intl/DateTimeFormat.h>
|
||||||
|
#include <LibJS/Runtime/Intl/DateTimeFormatConstructor.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>
|
||||||
#include <LibJS/Runtime/Temporal/Duration.h>
|
#include <LibJS/Runtime/Temporal/Duration.h>
|
||||||
|
@ -475,15 +477,23 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_string)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3.3.31 Temporal.PlainDate.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.tolocalestring
|
// 3.3.31 Temporal.PlainDate.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.tolocalestring
|
||||||
// NOTE: This is the minimum toLocaleString implementation for engines without ECMA-402.
|
// 15.12.3.1 Temporal.PlainDate.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sup-temporal.plaindate.prototype.tolocalestring
|
||||||
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_locale_string)
|
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_locale_string)
|
||||||
{
|
{
|
||||||
|
auto& realm = *vm.current_realm();
|
||||||
|
|
||||||
|
auto locales = vm.argument(0);
|
||||||
|
auto options = vm.argument(1);
|
||||||
|
|
||||||
// 1. Let temporalDate be the this value.
|
// 1. Let temporalDate be the this value.
|
||||||
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
|
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
|
||||||
auto temporal_date = TRY(typed_this_object(vm));
|
auto temporal_date = TRY(typed_this_object(vm));
|
||||||
|
|
||||||
// 3. Return TemporalDateToString(temporalDate, AUTO).
|
// 3. Let dateFormat be ? CreateDateTimeFormat(%Intl.DateTimeFormat%, locales, options, DATE, DATE).
|
||||||
return PrimitiveString::create(vm, temporal_date_to_string(temporal_date, ShowCalendar::Auto));
|
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, temporalDate).
|
||||||
|
return PrimitiveString::create(vm, TRY(Intl::format_date_time(vm, date_format, temporal_date)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3.3.32 Temporal.PlainDate.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.tojson
|
// 3.3.32 Temporal.PlainDate.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.tojson
|
||||||
|
|
|
@ -6,11 +6,8 @@ describe("correct behavior", () => {
|
||||||
test("basic functionality", () => {
|
test("basic functionality", () => {
|
||||||
let plainDate;
|
let plainDate;
|
||||||
|
|
||||||
plainDate = new Temporal.PlainDate(2021, 7, 6);
|
|
||||||
expect(plainDate.toLocaleString()).toBe("2021-07-06");
|
|
||||||
|
|
||||||
plainDate = new Temporal.PlainDate(2021, 7, 6, "gregory");
|
plainDate = new Temporal.PlainDate(2021, 7, 6, "gregory");
|
||||||
expect(plainDate.toLocaleString()).toBe("2021-07-06[u-ca=gregory]");
|
expect(plainDate.toLocaleString()).toBe("7/6/2021");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue