ListFormat.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. namespace JS::Intl {
  15. class ListFormat final : public Object {
  16. JS_OBJECT(ListFormat, Object);
  17. public:
  18. enum class Type {
  19. Invalid,
  20. Conjunction,
  21. Disjunction,
  22. Unit,
  23. };
  24. enum class Style {
  25. Invalid,
  26. Narrow,
  27. Short,
  28. Long,
  29. };
  30. ListFormat(Object& prototype);
  31. virtual ~ListFormat() override = default;
  32. String const& locale() const { return m_locale; }
  33. void set_locale(String locale) { m_locale = move(locale); }
  34. Type type() const { return m_type; }
  35. void set_type(StringView type);
  36. StringView type_string() const;
  37. Style style() const { return m_style; }
  38. void set_style(StringView style);
  39. StringView style_string() const;
  40. private:
  41. String m_locale; // [[Locale]]
  42. Type m_type { Type::Invalid }; // [[Type]]
  43. Style m_style { Style::Invalid }; // [[Style]]
  44. };
  45. using Placeables = HashMap<StringView, Variant<PatternPartition, Vector<PatternPartition>>>;
  46. Vector<PatternPartition> deconstruct_pattern(StringView pattern, Placeables placeables);
  47. Vector<PatternPartition> create_parts_from_list(ListFormat const& list_format, Vector<String> const& list);
  48. String format_list(ListFormat const& list_format, Vector<String> const& list);
  49. Array* format_list_to_parts(GlobalObject& global_object, ListFormat const& list_format, Vector<String> const& list);
  50. Vector<String> string_list_from_iterable(GlobalObject& global_object, Value iterable);
  51. }