ListFormat.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/HashMap.h>
  8. #include <AK/String.h>
  9. #include <AK/StringView.h>
  10. #include <AK/Variant.h>
  11. #include <AK/Vector.h>
  12. #include <LibJS/Runtime/Intl/AbstractOperations.h>
  13. #include <LibJS/Runtime/Object.h>
  14. #include <LibLocale/Locale.h>
  15. namespace JS::Intl {
  16. class ListFormat final : public Object {
  17. JS_OBJECT(ListFormat, Object);
  18. public:
  19. enum class Type {
  20. Invalid,
  21. Conjunction,
  22. Disjunction,
  23. Unit,
  24. };
  25. virtual ~ListFormat() override = default;
  26. String const& locale() const { return m_locale; }
  27. void set_locale(String locale) { m_locale = move(locale); }
  28. Type type() const { return m_type; }
  29. void set_type(StringView type);
  30. StringView type_string() const;
  31. ::Locale::Style style() const { return m_style; }
  32. void set_style(StringView style) { m_style = ::Locale::style_from_string(style); }
  33. StringView style_string() const { return ::Locale::style_to_string(m_style); }
  34. private:
  35. explicit ListFormat(Object& prototype);
  36. String m_locale; // [[Locale]]
  37. Type m_type { Type::Invalid }; // [[Type]]
  38. ::Locale::Style m_style { ::Locale::Style::Long }; // [[Style]]
  39. };
  40. using Placeables = HashMap<StringView, Variant<PatternPartition, Vector<PatternPartition>>>;
  41. Vector<PatternPartition> deconstruct_pattern(StringView pattern, Placeables);
  42. Vector<PatternPartition> create_parts_from_list(ListFormat const&, Vector<String> const& list);
  43. String format_list(ListFormat const&, Vector<String> const& list);
  44. NonnullGCPtr<Array> format_list_to_parts(VM&, ListFormat const&, Vector<String> const& list);
  45. ThrowCompletionOr<Vector<String>> string_list_from_iterable(VM&, Value iterable);
  46. }