ListFormat.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. JS_DECLARE_ALLOCATOR(ListFormat);
  19. public:
  20. enum class Type {
  21. Invalid,
  22. Conjunction,
  23. Disjunction,
  24. Unit,
  25. };
  26. virtual ~ListFormat() override = default;
  27. String const& locale() const { return m_locale; }
  28. void set_locale(String locale) { m_locale = move(locale); }
  29. Type type() const { return m_type; }
  30. void set_type(StringView type);
  31. StringView type_string() const;
  32. ::Locale::Style style() const { return m_style; }
  33. void set_style(StringView style) { m_style = ::Locale::style_from_string(style); }
  34. StringView style_string() const { return ::Locale::style_to_string(m_style); }
  35. private:
  36. explicit ListFormat(Object& prototype);
  37. String m_locale; // [[Locale]]
  38. Type m_type { Type::Invalid }; // [[Type]]
  39. ::Locale::Style m_style { ::Locale::Style::Long }; // [[Style]]
  40. };
  41. using Placeables = HashMap<StringView, Variant<PatternPartition, Vector<PatternPartition>>>;
  42. Vector<PatternPartition> deconstruct_pattern(StringView pattern, Placeables);
  43. Vector<PatternPartition> create_parts_from_list(ListFormat const&, Vector<String> const& list);
  44. String format_list(ListFormat const&, Vector<String> const& list);
  45. NonnullGCPtr<Array> format_list_to_parts(VM&, ListFormat const&, Vector<String> const& list);
  46. ThrowCompletionOr<Vector<String>> string_list_from_iterable(VM&, Value iterable);
  47. }