ListFormat.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
  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 <LibUnicode/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. ListFormat(Object& prototype);
  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. Unicode::Style style() const { return m_style; }
  33. void set_style(StringView style) { m_style = Unicode::style_from_string(style); }
  34. StringView style_string() const { return Unicode::style_to_string(m_style); }
  35. private:
  36. String m_locale; // [[Locale]]
  37. Type m_type { Type::Invalid }; // [[Type]]
  38. Unicode::Style m_style { Unicode::Style::Long }; // [[Style]]
  39. };
  40. using Placeables = HashMap<StringView, Variant<PatternPartition, Vector<PatternPartition>>>;
  41. Vector<PatternPartition> deconstruct_pattern(StringView pattern, Placeables placeables);
  42. Vector<PatternPartition> create_parts_from_list(ListFormat const& list_format, Vector<String> const& list);
  43. String format_list(ListFormat const& list_format, Vector<String> const& list);
  44. Array* format_list_to_parts(GlobalObject& global_object, ListFormat const& list_format, Vector<String> const& list);
  45. ThrowCompletionOr<Vector<String>> string_list_from_iterable(GlobalObject& global_object, Value iterable);
  46. }