RelativeTimeFormat.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2022, Tim Flynn <trflynn89@pm.me>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Array.h>
  8. #include <AK/String.h>
  9. #include <AK/StringView.h>
  10. #include <LibJS/Runtime/Completion.h>
  11. #include <LibJS/Runtime/Object.h>
  12. #include <LibUnicode/Locale.h>
  13. namespace JS::Intl {
  14. class RelativeTimeFormat final : public Object {
  15. JS_OBJECT(RelativeTimeFormat, Object);
  16. public:
  17. enum class Numeric {
  18. Always,
  19. Auto,
  20. };
  21. static constexpr auto relevant_extension_keys()
  22. {
  23. // 17.3.3 Internal slots, https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat-internal-slots
  24. // The value of the [[RelevantExtensionKeys]] internal slot is « "nu" ».
  25. return AK::Array { "nu"sv };
  26. }
  27. RelativeTimeFormat(Object& prototype);
  28. virtual ~RelativeTimeFormat() override = default;
  29. String const& locale() const { return m_locale; }
  30. void set_locale(String locale) { m_locale = move(locale); }
  31. String const& data_locale() const { return m_data_locale; }
  32. void set_data_locale(String data_locale) { m_data_locale = move(data_locale); }
  33. String const& numbering_system() const { return m_numbering_system; }
  34. void set_numbering_system(String numbering_system) { m_numbering_system = move(numbering_system); }
  35. Unicode::Style style() const { return m_style; }
  36. void set_style(StringView style) { m_style = Unicode::style_from_string(style); }
  37. StringView style_string() const { return Unicode::style_to_string(m_style); }
  38. Numeric numeric() const { return m_numeric; }
  39. void set_numeric(StringView numeric);
  40. StringView numeric_string() const;
  41. NumberFormat& number_format() const { return *m_number_format; }
  42. void set_number_format(NumberFormat* number_format) { m_number_format = number_format; }
  43. private:
  44. virtual void visit_edges(Cell::Visitor&) override;
  45. String m_locale; // [[Locale]]
  46. String m_data_locale; // [[DataLocale]]
  47. String m_numbering_system; // [[NumberingSystem]]
  48. Unicode::Style m_style { Unicode::Style::Long }; // [[Style]]
  49. Numeric m_numeric { Numeric::Always }; // [[Numeric]]
  50. NumberFormat* m_number_format { nullptr }; // [[NumberFormat]]
  51. };
  52. ThrowCompletionOr<RelativeTimeFormat*> initialize_relative_time_format(GlobalObject& global_object, RelativeTimeFormat& relative_time_format, Value locales_value, Value options_value);
  53. }