ListFormat.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. ::Locale::ListFormat const& formatter() const { return *m_formatter; }
  35. void set_formatter(NonnullOwnPtr<::Locale::ListFormat> formatter) { m_formatter = move(formatter); }
  36. private:
  37. explicit ListFormat(Object& prototype);
  38. String m_locale; // [[Locale]]
  39. ::Locale::ListFormatType m_type { ::Locale::ListFormatType::Conjunction }; // [[Type]]
  40. ::Locale::Style m_style { ::Locale::Style::Long }; // [[Style]]
  41. // Non-standard. Stores the ICU list formatter for the Intl object's formatting options.
  42. OwnPtr<::Locale::ListFormat> m_formatter;
  43. };
  44. Vector<::Locale::ListFormat::Partition> create_parts_from_list(ListFormat const&, ReadonlySpan<String> list);
  45. String format_list(ListFormat const&, ReadonlySpan<String> list);
  46. NonnullGCPtr<Array> format_list_to_parts(VM&, ListFormat const&, ReadonlySpan<String> list);
  47. ThrowCompletionOr<Vector<String>> string_list_from_iterable(VM&, Value iterable);
  48. }