ListFormat.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2021-2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/String.h>
  8. #include <AK/StringView.h>
  9. #include <AK/Vector.h>
  10. #include <LibJS/Runtime/Intl/AbstractOperations.h>
  11. #include <LibJS/Runtime/Object.h>
  12. #include <LibLocale/ListFormat.h>
  13. #include <LibLocale/Locale.h>
  14. namespace JS::Intl {
  15. class ListFormat final : public Object {
  16. JS_OBJECT(ListFormat, Object);
  17. JS_DECLARE_ALLOCATOR(ListFormat);
  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. ::Locale::ListFormatType type() const { return m_type; }
  29. void set_type(StringView type) { m_type = ::Locale::list_format_type_from_string(type); }
  30. StringView type_string() const { return ::Locale::list_format_type_to_string(m_type); }
  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. ::Locale::ListFormatType m_type { ::Locale::ListFormatType::Conjunction }; // [[Type]]
  38. ::Locale::Style m_style { ::Locale::Style::Long }; // [[Style]]
  39. };
  40. Vector<::Locale::ListFormatPart> create_parts_from_list(ListFormat const&, Vector<String> const& list);
  41. String format_list(ListFormat const&, Vector<String> const& list);
  42. NonnullGCPtr<Array> format_list_to_parts(VM&, ListFormat const&, Vector<String> const& list);
  43. ThrowCompletionOr<Vector<String>> string_list_from_iterable(VM&, Value iterable);
  44. }