1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 02:37:36 +00:00

LibJS+LibUnicode: Convert Intl.ListFormat to use Unicode::Style

Remove ListFormat's own definition of the Style enum, which was further
duplicated by a generated ListPatternStyle enum with the same values.
This commit is contained in:
Timothy Flynn 2022-01-25 11:38:55 -05:00 committed by Linus Groh
parent e261132e8b
commit bced4e9324
5 changed files with 14 additions and 59 deletions

View file

@ -9,7 +9,6 @@
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Intl/ListFormat.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibUnicode/Locale.h>
namespace JS::Intl {
@ -46,33 +45,6 @@ StringView ListFormat::type_string() const
}
}
void ListFormat::set_style(StringView style)
{
if (style == "narrow"sv) {
m_style = Style::Narrow;
} else if (style == "short"sv) {
m_style = Style::Short;
} else if (style == "long"sv) {
m_style = Style::Long;
} else {
VERIFY_NOT_REACHED();
}
}
StringView ListFormat::style_string() const
{
switch (m_style) {
case Style::Narrow:
return "narrow"sv;
case Style::Short:
return "short"sv;
case Style::Long:
return "long"sv;
default:
VERIFY_NOT_REACHED();
}
}
// 13.1.1 DeconstructPattern ( pattern, placeables ), https://tc39.es/ecma402/#sec-deconstructpattern
Vector<PatternPartition> deconstruct_pattern(StringView pattern, Placeables placeables)
{
@ -123,7 +95,7 @@ Vector<PatternPartition> deconstruct_pattern(StringView pattern, Placeables plac
// 13.1.2 CreatePartsFromList ( listFormat, list ), https://tc39.es/ecma402/#sec-createpartsfromlist
Vector<PatternPartition> create_parts_from_list(ListFormat const& list_format, Vector<String> const& list)
{
auto list_patterns = Unicode::get_locale_list_patterns(list_format.locale(), list_format.type_string(), list_format.style_string());
auto list_patterns = Unicode::get_locale_list_patterns(list_format.locale(), list_format.type_string(), list_format.style());
if (!list_patterns.has_value())
return {};

View file

@ -13,6 +13,7 @@
#include <AK/Vector.h>
#include <LibJS/Runtime/Intl/AbstractOperations.h>
#include <LibJS/Runtime/Object.h>
#include <LibUnicode/Locale.h>
namespace JS::Intl {
@ -27,13 +28,6 @@ public:
Unit,
};
enum class Style {
Invalid,
Narrow,
Short,
Long,
};
ListFormat(Object& prototype);
virtual ~ListFormat() override = default;
@ -44,14 +38,14 @@ public:
void set_type(StringView type);
StringView type_string() const;
Style style() const { return m_style; }
void set_style(StringView style);
StringView style_string() const;
Unicode::Style style() const { return m_style; }
void set_style(StringView style) { m_style = Unicode::style_from_string(style); }
StringView style_string() const { return Unicode::style_to_string(m_style); }
private:
String m_locale; // [[Locale]]
Type m_type { Type::Invalid }; // [[Type]]
Style m_style { Style::Invalid }; // [[Style]]
String m_locale; // [[Locale]]
Type m_type { Type::Invalid }; // [[Type]]
Unicode::Style m_style { Unicode::Style::Long }; // [[Style]]
};
using Placeables = HashMap<StringView, Variant<PatternPartition, Vector<PatternPartition>>>;