mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-31 21:29:06 +00:00
LibJS: Use Intl.DurationFormat for Temporal.Duration.p.toLocaleString
This is an normative change in the Temporal proposal. See:
ffb4fb5
This commit is contained in:
parent
8f51d1dd04
commit
080d32c7d0
Notes:
github-actions[bot]
2025-03-01 13:50:19 +00:00
Author: https://github.com/trflynn89
Commit: 080d32c7d0
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3734
2 changed files with 27 additions and 4 deletions
|
@ -5,6 +5,9 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <LibJS/Runtime/AbstractOperations.h>
|
||||||
|
#include <LibJS/Runtime/Intl/DurationFormat.h>
|
||||||
|
#include <LibJS/Runtime/Intl/DurationFormatConstructor.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>
|
||||||
#include <LibJS/Runtime/Temporal/DurationPrototype.h>
|
#include <LibJS/Runtime/Temporal/DurationPrototype.h>
|
||||||
|
@ -625,15 +628,35 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::to_json)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 7.3.24 Temporal.Duration.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.tolocalestring
|
// 7.3.24 Temporal.Duration.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.tolocalestring
|
||||||
// NOTE: This is the minimum toLocaleString implementation for engines without ECMA-402.
|
// 15.11.1.1 Temporal.Duration.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sup-temporal.duration.prototype.tolocalestring
|
||||||
JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::to_locale_string)
|
JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::to_locale_string)
|
||||||
{
|
{
|
||||||
|
auto& realm = *vm.current_realm();
|
||||||
|
|
||||||
|
auto locales = vm.argument(0);
|
||||||
|
auto options = vm.argument(1);
|
||||||
|
|
||||||
// 1. Let duration be the this value.
|
// 1. Let duration be the this value.
|
||||||
// 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
|
// 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
|
||||||
auto duration = TRY(typed_this_object(vm));
|
auto duration = TRY(typed_this_object(vm));
|
||||||
|
|
||||||
// 3. Return TemporalDurationToString(duration, AUTO).
|
// 3. Let formatter be ? Construct(%Intl.DurationFormat%, « locales, options »).
|
||||||
return PrimitiveString::create(vm, temporal_duration_to_string(duration, Auto {}));
|
auto& formatter = static_cast<Intl::DurationFormat&>(*TRY(construct(vm, realm.intrinsics().intl_duration_format_constructor(), locales, options)));
|
||||||
|
|
||||||
|
// 4. Let parts be PartitionDurationFormatPattern(formatter, duration).
|
||||||
|
auto parts = partition_duration_format_pattern(vm, formatter, duration);
|
||||||
|
|
||||||
|
// 5. Let result be the empty String.
|
||||||
|
StringBuilder result;
|
||||||
|
|
||||||
|
// 6. For each Record { [[Type]], [[Value]], [[Unit]] } part in parts, do
|
||||||
|
for (auto const& part : parts) {
|
||||||
|
// a. Set result to the string-concatenation of result and part.[[Value]].
|
||||||
|
result.append(part.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 7. Return result.
|
||||||
|
return PrimitiveString::create(vm, MUST(result.to_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
|
|
|
@ -5,7 +5,7 @@ describe("correct behavior", () => {
|
||||||
|
|
||||||
test("basic functionality", () => {
|
test("basic functionality", () => {
|
||||||
expect(new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).toLocaleString()).toBe(
|
expect(new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).toLocaleString()).toBe(
|
||||||
"P1Y2M3W4DT5H6M7.00800901S"
|
"1 yr, 2 mths, 3 wks, 4 days, 5 hr, 6 min, 7 sec, 8 ms, 9 μs, 10 ns"
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue