From 6ad00088c0d1c492bacd0582e1c486b631e8292b Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sun, 28 Nov 2021 18:05:32 -0500 Subject: [PATCH] js: Implement pretty-printing of Intl.DateTimeFormat --- Userland/Utilities/js.cpp | 49 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/Userland/Utilities/js.cpp b/Userland/Utilities/js.cpp index 26daa17e75..fa2ea3c4c9 100644 --- a/Userland/Utilities/js.cpp +++ b/Userland/Utilities/js.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -708,6 +709,52 @@ static void print_intl_number_format(JS::Object const& object, HashTable& seen_objects) +{ + auto& date_time_format = static_cast(object); + print_type("Intl.DateTimeFormat"); + js_out("\n locale: "); + print_value(js_string(object.vm(), date_time_format.locale()), seen_objects); + js_out("\n pattern: "); + print_value(js_string(object.vm(), date_time_format.pattern()), seen_objects); + js_out("\n calendar: "); + print_value(js_string(object.vm(), date_time_format.calendar()), seen_objects); + js_out("\n numberingSystem: "); + print_value(js_string(object.vm(), date_time_format.numbering_system()), seen_objects); + if (date_time_format.has_hour_cycle()) { + js_out("\n hourCycle: "); + print_value(js_string(object.vm(), date_time_format.hour_cycle_string()), seen_objects); + } + js_out("\n timeZone: "); + print_value(js_string(object.vm(), date_time_format.time_zone()), seen_objects); + if (date_time_format.has_date_style()) { + js_out("\n dateStyle: "); + print_value(js_string(object.vm(), date_time_format.date_style_string()), seen_objects); + } + if (date_time_format.has_time_style()) { + js_out("\n timeStyle: "); + print_value(js_string(object.vm(), date_time_format.time_style_string()), seen_objects); + } + + JS::Intl::for_each_calendar_field(date_time_format.global_object(), date_time_format, [&](auto& option, auto const& property, auto const&) -> JS::ThrowCompletionOr { + using ValueType = typename RemoveReference::ValueType; + + if (!option.has_value()) + return {}; + + js_out("\n {}: ", property); + + if constexpr (IsIntegral) { + print_value(JS::Value(*option), seen_objects); + } else { + auto name = Unicode::calendar_pattern_style_to_string(*option); + print_value(js_string(object.vm(), name), seen_objects); + } + + return {}; + }); +} + static void print_primitive_wrapper_object(FlyString const& name, JS::Object const& object, HashTable& seen_objects) { // BooleanObject, NumberObject, StringObject @@ -802,6 +849,8 @@ static void print_value(JS::Value value, HashTable& seen_objects) return print_intl_list_format(object, seen_objects); if (is(object)) return print_intl_number_format(object, seen_objects); + if (is(object)) + return print_intl_date_time_format(object, seen_objects); return print_object(object, seen_objects); }